scripts.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. import os
  2. import re
  3. import sys
  4. from collections import namedtuple
  5. import gradio as gr
  6. from modules import shared, paths, script_callbacks, extensions, script_loading, scripts_postprocessing, errors, timer
  7. AlwaysVisible = object()
  8. class PostprocessImageArgs:
  9. def __init__(self, image):
  10. self.image = image
  11. class Script:
  12. name = None
  13. """script's internal name derived from title"""
  14. section = None
  15. """name of UI section that the script's controls will be placed into"""
  16. filename = None
  17. args_from = None
  18. args_to = None
  19. alwayson = False
  20. is_txt2img = False
  21. is_img2img = False
  22. group = None
  23. """A gr.Group component that has all script's UI inside it"""
  24. infotext_fields = None
  25. """if set in ui(), this is a list of pairs of gradio component + text; the text will be used when
  26. parsing infotext to set the value for the component; see ui.py's txt2img_paste_fields for an example
  27. """
  28. paste_field_names = None
  29. """if set in ui(), this is a list of names of infotext fields; the fields will be sent through the
  30. various "Send to <X>" buttons when clicked
  31. """
  32. api_info = None
  33. """Generated value of type modules.api.models.ScriptInfo with information about the script for API"""
  34. def title(self):
  35. """this function should return the title of the script. This is what will be displayed in the dropdown menu."""
  36. raise NotImplementedError()
  37. def ui(self, is_img2img):
  38. """this function should create gradio UI elements. See https://gradio.app/docs/#components
  39. The return value should be an array of all components that are used in processing.
  40. Values of those returned components will be passed to run() and process() functions.
  41. """
  42. pass
  43. def show(self, is_img2img):
  44. """
  45. is_img2img is True if this function is called for the img2img interface, and Fasle otherwise
  46. This function should return:
  47. - False if the script should not be shown in UI at all
  48. - True if the script should be shown in UI if it's selected in the scripts dropdown
  49. - script.AlwaysVisible if the script should be shown in UI at all times
  50. """
  51. return True
  52. def run(self, p, *args):
  53. """
  54. This function is called if the script has been selected in the script dropdown.
  55. It must do all processing and return the Processed object with results, same as
  56. one returned by processing.process_images.
  57. Usually the processing is done by calling the processing.process_images function.
  58. args contains all values returned by components from ui()
  59. """
  60. pass
  61. def before_process(self, p, *args):
  62. """
  63. This function is called very early before processing begins for AlwaysVisible scripts.
  64. You can modify the processing object (p) here, inject hooks, etc.
  65. args contains all values returned by components from ui()
  66. """
  67. pass
  68. def process(self, p, *args):
  69. """
  70. This function is called before processing begins for AlwaysVisible scripts.
  71. You can modify the processing object (p) here, inject hooks, etc.
  72. args contains all values returned by components from ui()
  73. """
  74. pass
  75. def before_process_batch(self, p, *args, **kwargs):
  76. """
  77. Called before extra networks are parsed from the prompt, so you can add
  78. new extra network keywords to the prompt with this callback.
  79. **kwargs will have those items:
  80. - batch_number - index of current batch, from 0 to number of batches-1
  81. - prompts - list of prompts for current batch; you can change contents of this list but changing the number of entries will likely break things
  82. - seeds - list of seeds for current batch
  83. - subseeds - list of subseeds for current batch
  84. """
  85. pass
  86. def process_batch(self, p, *args, **kwargs):
  87. """
  88. Same as process(), but called for every batch.
  89. **kwargs will have those items:
  90. - batch_number - index of current batch, from 0 to number of batches-1
  91. - prompts - list of prompts for current batch; you can change contents of this list but changing the number of entries will likely break things
  92. - seeds - list of seeds for current batch
  93. - subseeds - list of subseeds for current batch
  94. """
  95. pass
  96. def postprocess_batch(self, p, *args, **kwargs):
  97. """
  98. Same as process_batch(), but called for every batch after it has been generated.
  99. **kwargs will have same items as process_batch, and also:
  100. - batch_number - index of current batch, from 0 to number of batches-1
  101. - images - torch tensor with all generated images, with values ranging from 0 to 1;
  102. """
  103. pass
  104. def postprocess_image(self, p, pp: PostprocessImageArgs, *args):
  105. """
  106. Called for every image after it has been generated.
  107. """
  108. pass
  109. def postprocess(self, p, processed, *args):
  110. """
  111. This function is called after processing ends for AlwaysVisible scripts.
  112. args contains all values returned by components from ui()
  113. """
  114. pass
  115. def before_component(self, component, **kwargs):
  116. """
  117. Called before a component is created.
  118. Use elem_id/label fields of kwargs to figure out which component it is.
  119. This can be useful to inject your own components somewhere in the middle of vanilla UI.
  120. You can return created components in the ui() function to add them to the list of arguments for your processing functions
  121. """
  122. pass
  123. def after_component(self, component, **kwargs):
  124. """
  125. Called after a component is created. Same as above.
  126. """
  127. pass
  128. def describe(self):
  129. """unused"""
  130. return ""
  131. def elem_id(self, item_id):
  132. """helper function to generate id for a HTML element, constructs final id out of script name, tab and user-supplied item_id"""
  133. need_tabname = self.show(True) == self.show(False)
  134. tabkind = 'img2img' if self.is_img2img else 'txt2txt'
  135. tabname = f"{tabkind}_" if need_tabname else ""
  136. title = re.sub(r'[^a-z_0-9]', '', re.sub(r'\s', '_', self.title().lower()))
  137. return f'script_{tabname}{title}_{item_id}'
  138. current_basedir = paths.script_path
  139. def basedir():
  140. """returns the base directory for the current script. For scripts in the main scripts directory,
  141. this is the main directory (where webui.py resides), and for scripts in extensions directory
  142. (ie extensions/aesthetic/script/aesthetic.py), this is extension's directory (extensions/aesthetic)
  143. """
  144. return current_basedir
  145. ScriptFile = namedtuple("ScriptFile", ["basedir", "filename", "path"])
  146. scripts_data = []
  147. postprocessing_scripts_data = []
  148. ScriptClassData = namedtuple("ScriptClassData", ["script_class", "path", "basedir", "module"])
  149. def list_scripts(scriptdirname, extension):
  150. scripts_list = []
  151. basedir = os.path.join(paths.script_path, scriptdirname)
  152. if os.path.exists(basedir):
  153. for filename in sorted(os.listdir(basedir)):
  154. scripts_list.append(ScriptFile(paths.script_path, filename, os.path.join(basedir, filename)))
  155. for ext in extensions.active():
  156. scripts_list += ext.list_files(scriptdirname, extension)
  157. scripts_list = [x for x in scripts_list if os.path.splitext(x.path)[1].lower() == extension and os.path.isfile(x.path)]
  158. return scripts_list
  159. def list_files_with_name(filename):
  160. res = []
  161. dirs = [paths.script_path] + [ext.path for ext in extensions.active()]
  162. for dirpath in dirs:
  163. if not os.path.isdir(dirpath):
  164. continue
  165. path = os.path.join(dirpath, filename)
  166. if os.path.isfile(path):
  167. res.append(path)
  168. return res
  169. def load_scripts():
  170. global current_basedir
  171. scripts_data.clear()
  172. postprocessing_scripts_data.clear()
  173. script_callbacks.clear_callbacks()
  174. scripts_list = list_scripts("scripts", ".py")
  175. syspath = sys.path
  176. def register_scripts_from_module(module):
  177. for script_class in module.__dict__.values():
  178. if type(script_class) != type:
  179. continue
  180. if issubclass(script_class, Script):
  181. scripts_data.append(ScriptClassData(script_class, scriptfile.path, scriptfile.basedir, module))
  182. elif issubclass(script_class, scripts_postprocessing.ScriptPostprocessing):
  183. postprocessing_scripts_data.append(ScriptClassData(script_class, scriptfile.path, scriptfile.basedir, module))
  184. def orderby(basedir):
  185. # 1st webui, 2nd extensions-builtin, 3rd extensions
  186. priority = {os.path.join(paths.script_path, "extensions-builtin"):1, paths.script_path:0}
  187. for key in priority:
  188. if basedir.startswith(key):
  189. return priority[key]
  190. return 9999
  191. for scriptfile in sorted(scripts_list, key=lambda x: [orderby(x.basedir), x]):
  192. try:
  193. if scriptfile.basedir != paths.script_path:
  194. sys.path = [scriptfile.basedir] + sys.path
  195. current_basedir = scriptfile.basedir
  196. script_module = script_loading.load_module(scriptfile.path)
  197. register_scripts_from_module(script_module)
  198. except Exception:
  199. errors.report(f"Error loading script: {scriptfile.filename}", exc_info=True)
  200. finally:
  201. sys.path = syspath
  202. current_basedir = paths.script_path
  203. timer.startup_timer.record(scriptfile.filename)
  204. global scripts_txt2img, scripts_img2img, scripts_postproc
  205. scripts_txt2img = ScriptRunner()
  206. scripts_img2img = ScriptRunner()
  207. scripts_postproc = scripts_postprocessing.ScriptPostprocessingRunner()
  208. def wrap_call(func, filename, funcname, *args, default=None, **kwargs):
  209. try:
  210. return func(*args, **kwargs)
  211. except Exception:
  212. errors.report(f"Error calling: {filename}/{funcname}", exc_info=True)
  213. return default
  214. class ScriptRunner:
  215. def __init__(self):
  216. self.scripts = []
  217. self.selectable_scripts = []
  218. self.alwayson_scripts = []
  219. self.titles = []
  220. self.infotext_fields = []
  221. self.paste_field_names = []
  222. self.inputs = [None]
  223. def initialize_scripts(self, is_img2img):
  224. from modules import scripts_auto_postprocessing
  225. self.scripts.clear()
  226. self.alwayson_scripts.clear()
  227. self.selectable_scripts.clear()
  228. auto_processing_scripts = scripts_auto_postprocessing.create_auto_preprocessing_script_data()
  229. for script_data in auto_processing_scripts + scripts_data:
  230. script = script_data.script_class()
  231. script.filename = script_data.path
  232. script.is_txt2img = not is_img2img
  233. script.is_img2img = is_img2img
  234. visibility = script.show(script.is_img2img)
  235. if visibility == AlwaysVisible:
  236. self.scripts.append(script)
  237. self.alwayson_scripts.append(script)
  238. script.alwayson = True
  239. elif visibility:
  240. self.scripts.append(script)
  241. self.selectable_scripts.append(script)
  242. def create_script_ui(self, script):
  243. import modules.api.models as api_models
  244. script.args_from = len(self.inputs)
  245. script.args_to = len(self.inputs)
  246. controls = wrap_call(script.ui, script.filename, "ui", script.is_img2img)
  247. if controls is None:
  248. return
  249. script.name = wrap_call(script.title, script.filename, "title", default=script.filename).lower()
  250. api_args = []
  251. for control in controls:
  252. control.custom_script_source = os.path.basename(script.filename)
  253. arg_info = api_models.ScriptArg(label=control.label or "")
  254. for field in ("value", "minimum", "maximum", "step", "choices"):
  255. v = getattr(control, field, None)
  256. if v is not None:
  257. setattr(arg_info, field, v)
  258. api_args.append(arg_info)
  259. script.api_info = api_models.ScriptInfo(
  260. name=script.name,
  261. is_img2img=script.is_img2img,
  262. is_alwayson=script.alwayson,
  263. args=api_args,
  264. )
  265. if script.infotext_fields is not None:
  266. self.infotext_fields += script.infotext_fields
  267. if script.paste_field_names is not None:
  268. self.paste_field_names += script.paste_field_names
  269. self.inputs += controls
  270. script.args_to = len(self.inputs)
  271. def setup_ui_for_section(self, section, scriptlist=None):
  272. if scriptlist is None:
  273. scriptlist = self.alwayson_scripts
  274. for script in scriptlist:
  275. if script.alwayson and script.section != section:
  276. continue
  277. with gr.Group(visible=script.alwayson) as group:
  278. self.create_script_ui(script)
  279. script.group = group
  280. def prepare_ui(self):
  281. self.inputs = [None]
  282. def setup_ui(self):
  283. self.titles = [wrap_call(script.title, script.filename, "title") or f"{script.filename} [error]" for script in self.selectable_scripts]
  284. self.setup_ui_for_section(None)
  285. dropdown = gr.Dropdown(label="Script", elem_id="script_list", choices=["None"] + self.titles, value="None", type="index")
  286. self.inputs[0] = dropdown
  287. self.setup_ui_for_section(None, self.selectable_scripts)
  288. def select_script(script_index):
  289. selected_script = self.selectable_scripts[script_index - 1] if script_index>0 else None
  290. return [gr.update(visible=selected_script == s) for s in self.selectable_scripts]
  291. def init_field(title):
  292. """called when an initial value is set from ui-config.json to show script's UI components"""
  293. if title == 'None':
  294. return
  295. script_index = self.titles.index(title)
  296. self.selectable_scripts[script_index].group.visible = True
  297. dropdown.init_field = init_field
  298. dropdown.change(
  299. fn=select_script,
  300. inputs=[dropdown],
  301. outputs=[script.group for script in self.selectable_scripts]
  302. )
  303. self.script_load_ctr = 0
  304. def onload_script_visibility(params):
  305. title = params.get('Script', None)
  306. if title:
  307. title_index = self.titles.index(title)
  308. visibility = title_index == self.script_load_ctr
  309. self.script_load_ctr = (self.script_load_ctr + 1) % len(self.titles)
  310. return gr.update(visible=visibility)
  311. else:
  312. return gr.update(visible=False)
  313. self.infotext_fields.append((dropdown, lambda x: gr.update(value=x.get('Script', 'None'))))
  314. self.infotext_fields.extend([(script.group, onload_script_visibility) for script in self.selectable_scripts])
  315. return self.inputs
  316. def run(self, p, *args):
  317. script_index = args[0]
  318. if script_index == 0:
  319. return None
  320. script = self.selectable_scripts[script_index-1]
  321. if script is None:
  322. return None
  323. script_args = args[script.args_from:script.args_to]
  324. processed = script.run(p, *script_args)
  325. shared.total_tqdm.clear()
  326. return processed
  327. def before_process(self, p):
  328. for script in self.alwayson_scripts:
  329. try:
  330. script_args = p.script_args[script.args_from:script.args_to]
  331. script.before_process(p, *script_args)
  332. except Exception:
  333. errors.report(f"Error running before_process: {script.filename}", exc_info=True)
  334. def process(self, p):
  335. for script in self.alwayson_scripts:
  336. try:
  337. script_args = p.script_args[script.args_from:script.args_to]
  338. script.process(p, *script_args)
  339. except Exception:
  340. errors.report(f"Error running process: {script.filename}", exc_info=True)
  341. def before_process_batch(self, p, **kwargs):
  342. for script in self.alwayson_scripts:
  343. try:
  344. script_args = p.script_args[script.args_from:script.args_to]
  345. script.before_process_batch(p, *script_args, **kwargs)
  346. except Exception:
  347. errors.report(f"Error running before_process_batch: {script.filename}", exc_info=True)
  348. def process_batch(self, p, **kwargs):
  349. for script in self.alwayson_scripts:
  350. try:
  351. script_args = p.script_args[script.args_from:script.args_to]
  352. script.process_batch(p, *script_args, **kwargs)
  353. except Exception:
  354. errors.report(f"Error running process_batch: {script.filename}", exc_info=True)
  355. def postprocess(self, p, processed):
  356. for script in self.alwayson_scripts:
  357. try:
  358. script_args = p.script_args[script.args_from:script.args_to]
  359. script.postprocess(p, processed, *script_args)
  360. except Exception:
  361. errors.report(f"Error running postprocess: {script.filename}", exc_info=True)
  362. def postprocess_batch(self, p, images, **kwargs):
  363. for script in self.alwayson_scripts:
  364. try:
  365. script_args = p.script_args[script.args_from:script.args_to]
  366. script.postprocess_batch(p, *script_args, images=images, **kwargs)
  367. except Exception:
  368. errors.report(f"Error running postprocess_batch: {script.filename}", exc_info=True)
  369. def postprocess_image(self, p, pp: PostprocessImageArgs):
  370. for script in self.alwayson_scripts:
  371. try:
  372. script_args = p.script_args[script.args_from:script.args_to]
  373. script.postprocess_image(p, pp, *script_args)
  374. except Exception:
  375. errors.report(f"Error running postprocess_image: {script.filename}", exc_info=True)
  376. def before_component(self, component, **kwargs):
  377. for script in self.scripts:
  378. try:
  379. script.before_component(component, **kwargs)
  380. except Exception:
  381. errors.report(f"Error running before_component: {script.filename}", exc_info=True)
  382. def after_component(self, component, **kwargs):
  383. for script in self.scripts:
  384. try:
  385. script.after_component(component, **kwargs)
  386. except Exception:
  387. errors.report(f"Error running after_component: {script.filename}", exc_info=True)
  388. def reload_sources(self, cache):
  389. for si, script in list(enumerate(self.scripts)):
  390. args_from = script.args_from
  391. args_to = script.args_to
  392. filename = script.filename
  393. module = cache.get(filename, None)
  394. if module is None:
  395. module = script_loading.load_module(script.filename)
  396. cache[filename] = module
  397. for script_class in module.__dict__.values():
  398. if type(script_class) == type and issubclass(script_class, Script):
  399. self.scripts[si] = script_class()
  400. self.scripts[si].filename = filename
  401. self.scripts[si].args_from = args_from
  402. self.scripts[si].args_to = args_to
  403. scripts_txt2img: ScriptRunner = None
  404. scripts_img2img: ScriptRunner = None
  405. scripts_postproc: scripts_postprocessing.ScriptPostprocessingRunner = None
  406. scripts_current: ScriptRunner = None
  407. def reload_script_body_only():
  408. cache = {}
  409. scripts_txt2img.reload_sources(cache)
  410. scripts_img2img.reload_sources(cache)
  411. reload_scripts = load_scripts # compatibility alias
  412. def add_classes_to_gradio_component(comp):
  413. """
  414. this adds gradio-* to the component for css styling (ie gradio-button to gr.Button), as well as some others
  415. """
  416. comp.elem_classes = [f"gradio-{comp.get_block_name()}", *(comp.elem_classes or [])]
  417. if getattr(comp, 'multiselect', False):
  418. comp.elem_classes.append('multiselect')
  419. def IOComponent_init(self, *args, **kwargs):
  420. if scripts_current is not None:
  421. scripts_current.before_component(self, **kwargs)
  422. script_callbacks.before_component_callback(self, **kwargs)
  423. res = original_IOComponent_init(self, *args, **kwargs)
  424. add_classes_to_gradio_component(self)
  425. script_callbacks.after_component_callback(self, **kwargs)
  426. if scripts_current is not None:
  427. scripts_current.after_component(self, **kwargs)
  428. return res
  429. original_IOComponent_init = gr.components.IOComponent.__init__
  430. gr.components.IOComponent.__init__ = IOComponent_init
  431. def BlockContext_init(self, *args, **kwargs):
  432. res = original_BlockContext_init(self, *args, **kwargs)
  433. add_classes_to_gradio_component(self)
  434. return res
  435. original_BlockContext_init = gr.blocks.BlockContext.__init__
  436. gr.blocks.BlockContext.__init__ = BlockContext_init