scripts.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. import os
  2. import re
  3. import sys
  4. import traceback
  5. from collections import namedtuple
  6. import gradio as gr
  7. from modules.processing import StableDiffusionProcessing
  8. from modules import shared, paths, script_callbacks, extensions, script_loading
  9. AlwaysVisible = object()
  10. class Script:
  11. filename = None
  12. args_from = None
  13. args_to = None
  14. alwayson = False
  15. is_txt2img = False
  16. is_img2img = False
  17. """A gr.Group component that has all script's UI inside it"""
  18. group = None
  19. infotext_fields = None
  20. """if set in ui(), this is a list of pairs of gradio component + text; the text will be used when
  21. parsing infotext to set the value for the component; see ui.py's txt2img_paste_fields for an example
  22. """
  23. def title(self):
  24. """this function should return the title of the script. This is what will be displayed in the dropdown menu."""
  25. raise NotImplementedError()
  26. def ui(self, is_img2img):
  27. """this function should create gradio UI elements. See https://gradio.app/docs/#components
  28. The return value should be an array of all components that are used in processing.
  29. Values of those returned components will be passed to run() and process() functions.
  30. """
  31. pass
  32. def show(self, is_img2img):
  33. """
  34. is_img2img is True if this function is called for the img2img interface, and Fasle otherwise
  35. This function should return:
  36. - False if the script should not be shown in UI at all
  37. - True if the script should be shown in UI if it's selected in the scripts dropdown
  38. - script.AlwaysVisible if the script should be shown in UI at all times
  39. """
  40. return True
  41. def run(self, p, *args):
  42. """
  43. This function is called if the script has been selected in the script dropdown.
  44. It must do all processing and return the Processed object with results, same as
  45. one returned by processing.process_images.
  46. Usually the processing is done by calling the processing.process_images function.
  47. args contains all values returned by components from ui()
  48. """
  49. raise NotImplementedError()
  50. def process(self, p, *args):
  51. """
  52. This function is called before processing begins for AlwaysVisible scripts.
  53. You can modify the processing object (p) here, inject hooks, etc.
  54. args contains all values returned by components from ui()
  55. """
  56. pass
  57. def process_batch(self, p, *args, **kwargs):
  58. """
  59. Same as process(), but called for every batch.
  60. **kwargs will have those items:
  61. - batch_number - index of current batch, from 0 to number of batches-1
  62. - prompts - list of prompts for current batch; you can change contents of this list but changing the number of entries will likely break things
  63. - seeds - list of seeds for current batch
  64. - subseeds - list of subseeds for current batch
  65. """
  66. pass
  67. def postprocess_batch(self, p, *args, **kwargs):
  68. """
  69. Same as process_batch(), but called for every batch after it has been generated.
  70. **kwargs will have same items as process_batch, and also:
  71. - batch_number - index of current batch, from 0 to number of batches-1
  72. - images - torch tensor with all generated images, with values ranging from 0 to 1;
  73. """
  74. pass
  75. def postprocess(self, p, processed, *args):
  76. """
  77. This function is called after processing ends for AlwaysVisible scripts.
  78. args contains all values returned by components from ui()
  79. """
  80. pass
  81. def before_component(self, component, **kwargs):
  82. """
  83. Called before a component is created.
  84. Use elem_id/label fields of kwargs to figure out which component it is.
  85. This can be useful to inject your own components somewhere in the middle of vanilla UI.
  86. You can return created components in the ui() function to add them to the list of arguments for your processing functions
  87. """
  88. pass
  89. def after_component(self, component, **kwargs):
  90. """
  91. Called after a component is created. Same as above.
  92. """
  93. pass
  94. def describe(self):
  95. """unused"""
  96. return ""
  97. def elem_id(self, item_id):
  98. """helper function to generate id for a HTML element, constructs final id out of script name, tab and user-supplied item_id"""
  99. need_tabname = self.show(True) == self.show(False)
  100. tabname = ('img2img' if self.is_img2img else 'txt2txt') + "_" if need_tabname else ""
  101. title = re.sub(r'[^a-z_0-9]', '', re.sub(r'\s', '_', self.title().lower()))
  102. return f'script_{tabname}{title}_{item_id}'
  103. current_basedir = paths.script_path
  104. def basedir():
  105. """returns the base directory for the current script. For scripts in the main scripts directory,
  106. this is the main directory (where webui.py resides), and for scripts in extensions directory
  107. (ie extensions/aesthetic/script/aesthetic.py), this is extension's directory (extensions/aesthetic)
  108. """
  109. return current_basedir
  110. scripts_data = []
  111. ScriptFile = namedtuple("ScriptFile", ["basedir", "filename", "path"])
  112. ScriptClassData = namedtuple("ScriptClassData", ["script_class", "path", "basedir"])
  113. def list_scripts(scriptdirname, extension):
  114. scripts_list = []
  115. basedir = os.path.join(paths.script_path, scriptdirname)
  116. if os.path.exists(basedir):
  117. for filename in sorted(os.listdir(basedir)):
  118. scripts_list.append(ScriptFile(paths.script_path, filename, os.path.join(basedir, filename)))
  119. for ext in extensions.active():
  120. scripts_list += ext.list_files(scriptdirname, extension)
  121. scripts_list = [x for x in scripts_list if os.path.splitext(x.path)[1].lower() == extension and os.path.isfile(x.path)]
  122. return scripts_list
  123. def list_files_with_name(filename):
  124. res = []
  125. dirs = [paths.script_path] + [ext.path for ext in extensions.active()]
  126. for dirpath in dirs:
  127. if not os.path.isdir(dirpath):
  128. continue
  129. path = os.path.join(dirpath, filename)
  130. if os.path.isfile(path):
  131. res.append(path)
  132. return res
  133. def load_scripts():
  134. global current_basedir
  135. scripts_data.clear()
  136. script_callbacks.clear_callbacks()
  137. scripts_list = list_scripts("scripts", ".py")
  138. syspath = sys.path
  139. for scriptfile in sorted(scripts_list):
  140. try:
  141. if scriptfile.basedir != paths.script_path:
  142. sys.path = [scriptfile.basedir] + sys.path
  143. current_basedir = scriptfile.basedir
  144. module = script_loading.load_module(scriptfile.path)
  145. for key, script_class in module.__dict__.items():
  146. if type(script_class) == type and issubclass(script_class, Script):
  147. scripts_data.append(ScriptClassData(script_class, scriptfile.path, scriptfile.basedir))
  148. except Exception:
  149. print(f"Error loading script: {scriptfile.filename}", file=sys.stderr)
  150. print(traceback.format_exc(), file=sys.stderr)
  151. finally:
  152. sys.path = syspath
  153. current_basedir = paths.script_path
  154. def wrap_call(func, filename, funcname, *args, default=None, **kwargs):
  155. try:
  156. res = func(*args, **kwargs)
  157. return res
  158. except Exception:
  159. print(f"Error calling: {filename}/{funcname}", file=sys.stderr)
  160. print(traceback.format_exc(), file=sys.stderr)
  161. return default
  162. class ScriptRunner:
  163. def __init__(self):
  164. self.scripts = []
  165. self.selectable_scripts = []
  166. self.alwayson_scripts = []
  167. self.titles = []
  168. self.infotext_fields = []
  169. def initialize_scripts(self, is_img2img):
  170. self.scripts.clear()
  171. self.alwayson_scripts.clear()
  172. self.selectable_scripts.clear()
  173. for script_class, path, basedir in scripts_data:
  174. script = script_class()
  175. script.filename = path
  176. script.is_txt2img = not is_img2img
  177. script.is_img2img = is_img2img
  178. visibility = script.show(script.is_img2img)
  179. if visibility == AlwaysVisible:
  180. self.scripts.append(script)
  181. self.alwayson_scripts.append(script)
  182. script.alwayson = True
  183. elif visibility:
  184. self.scripts.append(script)
  185. self.selectable_scripts.append(script)
  186. def setup_ui(self):
  187. self.titles = [wrap_call(script.title, script.filename, "title") or f"{script.filename} [error]" for script in self.selectable_scripts]
  188. inputs = [None]
  189. inputs_alwayson = [True]
  190. def create_script_ui(script, inputs, inputs_alwayson):
  191. script.args_from = len(inputs)
  192. script.args_to = len(inputs)
  193. controls = wrap_call(script.ui, script.filename, "ui", script.is_img2img)
  194. if controls is None:
  195. return
  196. for control in controls:
  197. control.custom_script_source = os.path.basename(script.filename)
  198. if script.infotext_fields is not None:
  199. self.infotext_fields += script.infotext_fields
  200. inputs += controls
  201. inputs_alwayson += [script.alwayson for _ in controls]
  202. script.args_to = len(inputs)
  203. for script in self.alwayson_scripts:
  204. with gr.Group() as group:
  205. create_script_ui(script, inputs, inputs_alwayson)
  206. script.group = group
  207. dropdown = gr.Dropdown(label="Script", elem_id="script_list", choices=["None"] + self.titles, value="None", type="index")
  208. dropdown.save_to_config = True
  209. inputs[0] = dropdown
  210. for script in self.selectable_scripts:
  211. with gr.Group(visible=False) as group:
  212. create_script_ui(script, inputs, inputs_alwayson)
  213. script.group = group
  214. def select_script(script_index):
  215. selected_script = self.selectable_scripts[script_index - 1] if script_index>0 else None
  216. return [gr.update(visible=selected_script == s) for s in self.selectable_scripts]
  217. def init_field(title):
  218. """called when an initial value is set from ui-config.json to show script's UI components"""
  219. if title == 'None':
  220. return
  221. script_index = self.titles.index(title)
  222. self.selectable_scripts[script_index].group.visible = True
  223. dropdown.init_field = init_field
  224. dropdown.change(
  225. fn=select_script,
  226. inputs=[dropdown],
  227. outputs=[script.group for script in self.selectable_scripts]
  228. )
  229. return inputs
  230. def run(self, p: StableDiffusionProcessing, *args):
  231. script_index = args[0]
  232. if script_index == 0:
  233. return None
  234. script = self.selectable_scripts[script_index-1]
  235. if script is None:
  236. return None
  237. script_args = args[script.args_from:script.args_to]
  238. processed = script.run(p, *script_args)
  239. shared.total_tqdm.clear()
  240. return processed
  241. def process(self, p):
  242. for script in self.alwayson_scripts:
  243. try:
  244. script_args = p.script_args[script.args_from:script.args_to]
  245. script.process(p, *script_args)
  246. except Exception:
  247. print(f"Error running process: {script.filename}", file=sys.stderr)
  248. print(traceback.format_exc(), file=sys.stderr)
  249. def process_batch(self, p, **kwargs):
  250. for script in self.alwayson_scripts:
  251. try:
  252. script_args = p.script_args[script.args_from:script.args_to]
  253. script.process_batch(p, *script_args, **kwargs)
  254. except Exception:
  255. print(f"Error running process_batch: {script.filename}", file=sys.stderr)
  256. print(traceback.format_exc(), file=sys.stderr)
  257. def postprocess(self, p, processed):
  258. for script in self.alwayson_scripts:
  259. try:
  260. script_args = p.script_args[script.args_from:script.args_to]
  261. script.postprocess(p, processed, *script_args)
  262. except Exception:
  263. print(f"Error running postprocess: {script.filename}", file=sys.stderr)
  264. print(traceback.format_exc(), file=sys.stderr)
  265. def postprocess_batch(self, p, images, **kwargs):
  266. for script in self.alwayson_scripts:
  267. try:
  268. script_args = p.script_args[script.args_from:script.args_to]
  269. script.postprocess_batch(p, *script_args, images=images, **kwargs)
  270. except Exception:
  271. print(f"Error running postprocess_batch: {script.filename}", file=sys.stderr)
  272. print(traceback.format_exc(), file=sys.stderr)
  273. def before_component(self, component, **kwargs):
  274. for script in self.scripts:
  275. try:
  276. script.before_component(component, **kwargs)
  277. except Exception:
  278. print(f"Error running before_component: {script.filename}", file=sys.stderr)
  279. print(traceback.format_exc(), file=sys.stderr)
  280. def after_component(self, component, **kwargs):
  281. for script in self.scripts:
  282. try:
  283. script.after_component(component, **kwargs)
  284. except Exception:
  285. print(f"Error running after_component: {script.filename}", file=sys.stderr)
  286. print(traceback.format_exc(), file=sys.stderr)
  287. def reload_sources(self, cache):
  288. for si, script in list(enumerate(self.scripts)):
  289. args_from = script.args_from
  290. args_to = script.args_to
  291. filename = script.filename
  292. module = cache.get(filename, None)
  293. if module is None:
  294. module = script_loading.load_module(script.filename)
  295. cache[filename] = module
  296. for key, script_class in module.__dict__.items():
  297. if type(script_class) == type and issubclass(script_class, Script):
  298. self.scripts[si] = script_class()
  299. self.scripts[si].filename = filename
  300. self.scripts[si].args_from = args_from
  301. self.scripts[si].args_to = args_to
  302. scripts_txt2img = ScriptRunner()
  303. scripts_img2img = ScriptRunner()
  304. scripts_current: ScriptRunner = None
  305. def reload_script_body_only():
  306. cache = {}
  307. scripts_txt2img.reload_sources(cache)
  308. scripts_img2img.reload_sources(cache)
  309. def reload_scripts():
  310. global scripts_txt2img, scripts_img2img
  311. load_scripts()
  312. scripts_txt2img = ScriptRunner()
  313. scripts_img2img = ScriptRunner()
  314. def IOComponent_init(self, *args, **kwargs):
  315. if scripts_current is not None:
  316. scripts_current.before_component(self, **kwargs)
  317. script_callbacks.before_component_callback(self, **kwargs)
  318. res = original_IOComponent_init(self, *args, **kwargs)
  319. script_callbacks.after_component_callback(self, **kwargs)
  320. if scripts_current is not None:
  321. scripts_current.after_component(self, **kwargs)
  322. return res
  323. original_IOComponent_init = gr.components.IOComponent.__init__
  324. gr.components.IOComponent.__init__ = IOComponent_init