• stable-diffusion-webui之extension


    https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Developing-extensionsicon-default.png?t=N7T8https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Developing-extensionshttps://github.com/udon-universe/stable-diffusion-webui-extension-templatesicon-default.png?t=N7T8https://github.com/udon-universe/stable-diffusion-webui-extension-templateDeveloping custom scripts · AUTOMATIC1111/stable-diffusion-webui Wiki · GitHubStable Diffusion web UI. Contribute to AUTOMATIC1111/stable-diffusion-webui development by creating an account on GitHub.icon-default.png?t=N7T8https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Developing-custom-scripts主要是对scripts的原理进行分析。

    extension中:

    1.install.py一定会被执行。环境安装都在launch.py中,在launch.py/run_extension_installer=launch_utils.run_extension_installer中执行。

    1. import launch
    2. if not launch.is_installed("aitextgen"):
    3. launch.run_pip("install aitextgen==0.6.0", "requirements for MagicPrompt")

    2.extension中一定要包括scripts目录下脚本,extension目录已被包含在sys.path中。

    Script类在modules/scripts.py中,

    1. import modules.scripts as scripts
    2. import gradio as gr
    3. import os
    4. from modules import images
    5. from modules.processing import process_images, Processed
    6. from modules.processing import Processed
    7. from modules.shared import opts, cmd_opts, state
    8. class Script(scripts.Script):
    9. # The title of the script. This is what will be displayed in the dropdown menu.
    10.     def title(self):
    11.         return "Flip/Rotate Output"
    12. # Determines when the script should be shown in the dropdown menu via the
    13. # returned value. As an example:
    14. # is_img2img is True if the current tab is img2img, and False if it is txt2img.
    15. # Thus, return is_img2img to only show the script on the img2img tab.
    16. # 在文生图、图生图页面是否出现
    17.     def show(self, is_img2img):
    18.         return is_img2img
    19. # How the script's is displayed in the UI. See https://gradio.app/docs/#components
    20. # for the different UI components you can use and how to create them.
    21. # Most UI components can return a value, such as a boolean for a checkbox.
    22. # The returned values are passed to the run method as parameters.
    23. # 具体展示的页面,这块也可以用on_ui_tabs()实现
    24.     def ui(self, is_img2img):
    25.         angle = gr.Slider(minimum=0.0, maximum=360.0, step=1, value=0,
    26.         label="Angle")
    27.         hflip = gr.Checkbox(False, label="Horizontal flip")
    28.         vflip = gr.Checkbox(False, label="Vertical flip")
    29.         overwrite = gr.Checkbox(False, label="Overwrite existing files")
    30.         return [angle, hflip, vflip, overwrite]
    31. # This is where the additional processing is implemented. The parameters include
    32. # self, the model object "p" (a StableDiffusionProcessing class, see
    33. # processing.py), and the parameters returned by the ui method.
    34. # Custom functions can be defined here, and additional libraries can be imported
    35. # to be used in processing. The return value should be a Processed object, which is
    36. # what is returned by the process_images method.
    37. # 一般process函数,主函数
    38. def run(self, p, angle, hflip, vflip, overwrite):
    39. # function which takes an image from the Processed object,
    40. # and the angle and two booleans indicating horizontal and
    41. # vertical flips from the UI, then returns the
    42. # image rotated and flipped accordingly
    43. def rotate_and_flip(im, angle, hflip, vflip):
    44. from PIL import Image
    45. raf = im
    46. if angle != 0:
    47. raf = raf.rotate(angle, expand=True)
    48. if hflip:
    49. raf = raf.transpose(Image.FLIP_LEFT_RIGHT)
    50. if vflip:
    51. raf = raf.transpose(Image.FLIP_TOP_BOTTOM)
    52. return raf
    53. # If overwrite is false, append the rotation information to the filename
    54. # using the "basename" parameter and save it in the same directory.
    55. # If overwrite is true, stop the model from saving its outputs and
    56. # save the rotated and flipped images instead.
    57. basename = ""
    58. if(not overwrite):
    59. if angle != 0:
    60. basename += "rotated_" + str(angle)
    61. if hflip:
    62. basename += "_hflip"
    63. if vflip:
    64. basename += "_vflip"
    65. else:
    66. p.do_not_save_samples = True
    67. proc = process_images(p)
    68. # rotate and flip each image in the processed images
    69. # use the save_images method from images.py to save
    70. # them.
    71. for i in range(len(proc.images)):
    72. proc.images[i] = rotate_and_flip(proc.images[i], angle, hflip, vflip)
    73. images.save_image(proc.images[i], p.outpath_samples, basename,
    74. proc.seed + i, proc.prompt, opts.samples_format, info= proc.info, p=p)
    75. return proc

    3.extension中的JavaScript和css都将添加到页面。

    4.extension中的localizations也会添加,名称相同会替换。

    5.extension中的preload.py中的参数,在解析命令行参数之前加载该文件。一般含有preload函数。

    一般放置到extensions目录下。

    下面是一个例子,我把代码都列出来方便参考:

    template.py

    1. import modules.scripts as scripts
    2. import gradio as gr
    3. import os
    4. from modules import images, script_callbacks
    5. from modules.processing import process_images, Processed
    6. from modules.processing import Processed
    7. from modules.shared import opts, cmd_opts, state
    8. class ExtensionTemplateScript(scripts.Script):
    9. # Extension title in menu UI
    10. def title(self):
    11. return "Extension Template"
    12. # Decide to show menu in txt2img or img2img
    13. # - in "txt2img" -> is_img2img is `False`
    14. # - in "img2img" -> is_img2img is `True`
    15. #
    16. # below code always show extension menu
    17. def show(self, is_img2img):
    18. return scripts.AlwaysVisible
    19. # Setup menu ui detail
    20. def ui(self, is_img2img):
    21. with gr.Accordion('Extension Template', open=False):
    22. with gr.Row():
    23. angle = gr.Slider(
    24. minimum=0.0,
    25. maximum=360.0,
    26. step=1,
    27. value=0,
    28. label="Angle"
    29. )
    30. checkbox = gr.Checkbox(
    31. False,
    32. label="Checkbox"
    33. )
    34. # TODO: add more UI components (cf. https://gradio.app/docs/#components)
    35. return [angle, checkbox]
    36. # Extension main process
    37. # Type: (StableDiffusionProcessing, List) -> (Processed)
    38. # args is [StableDiffusionProcessing, UI1, UI2, ...]
    39. def run(self, p, angle, checkbox):
    40. # TODO: get UI info through UI object angle, checkbox
    41. proc = process_images(p)
    42. # TODO: add image edit process via Processed object proc
    43. return proc

    template_on_settings.py

    1. import modules.scripts as scripts
    2. import gradio as gr
    3. import os
    4. from modules import shared
    5. from modules import script_callbacks
    6. def on_ui_settings():
    7. section = ('template', "Template")
    8. shared.opts.add_option(
    9. "option1",
    10. shared.OptionInfo(
    11. False,
    12. "option1 description",
    13. gr.Checkbox,
    14. {"interactive": True},
    15. section=section)
    16. )
    17. script_callbacks.on_ui_settings(on_ui_settings)

    template_on_tab.py

    1. import modules.scripts as scripts
    2. import gradio as gr
    3. import os
    4. from modules import script_callbacks
    5. def on_ui_tabs():
    6. with gr.Blocks(analytics_enabled=False) as ui_component:
    7. with gr.Row():
    8. angle = gr.Slider(
    9. minimum=0.0,
    10. maximum=360.0,
    11. step=1,
    12. value=0,
    13. label="Angle"
    14. )
    15. checkbox = gr.Checkbox(
    16. False,
    17. label="Checkbox"
    18. )
    19. # TODO: add more UI components (cf. https://gradio.app/docs/#components)
    20. return [(ui_component, "Extension Template", "extension_template_tab")]
    21. script_callbacks.on_ui_tabs(on_ui_tabs)

  • 相关阅读:
    【算法基础】基础算法(一)--(快速排序、归并排序、二分)
    PMP考生必读,7月30日考试防疫要求都在这里
    2、Elasticsearch 基础功能
    TensorFlow自定义训练函数
    Linux 命令(190)—— skill 命令
    华为数通方向HCIP-DataCom H12-831题库(单选题:301-310)
    [Spring MVC 8]高并发实战小Demo
    Modbus动态链接库供多语言使用 | Go
    JUC并发编程第二篇,对Future的改进,CompletableFuture核心使用
    Ubuntu 22.04 无法使用网易云音乐
  • 原文地址:https://blog.csdn.net/u012193416/article/details/134506238