scripts.py 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  1. import os
  2. import re
  3. import sys
  4. import inspect
  5. from collections import namedtuple
  6. from dataclasses import dataclass
  7. import gradio as gr
  8. from modules import shared, paths, script_callbacks, extensions, script_loading, scripts_postprocessing, errors, timer, util
  9. topological_sort = util.topological_sort
  10. AlwaysVisible = object()
  11. class MaskBlendArgs:
  12. def __init__(self, current_latent, nmask, init_latent, mask, blended_latent, denoiser=None, sigma=None):
  13. self.current_latent = current_latent
  14. self.nmask = nmask
  15. self.init_latent = init_latent
  16. self.mask = mask
  17. self.blended_latent = blended_latent
  18. self.denoiser = denoiser
  19. self.is_final_blend = denoiser is None
  20. self.sigma = sigma
  21. class PostSampleArgs:
  22. def __init__(self, samples):
  23. self.samples = samples
  24. class PostprocessImageArgs:
  25. def __init__(self, image):
  26. self.image = image
  27. class PostProcessMaskOverlayArgs:
  28. def __init__(self, index, mask_for_overlay, overlay_image):
  29. self.index = index
  30. self.mask_for_overlay = mask_for_overlay
  31. self.overlay_image = overlay_image
  32. class PostprocessBatchListArgs:
  33. def __init__(self, images):
  34. self.images = images
  35. @dataclass
  36. class OnComponent:
  37. component: gr.blocks.Block
  38. class Script:
  39. name = None
  40. """script's internal name derived from title"""
  41. section = None
  42. """name of UI section that the script's controls will be placed into"""
  43. filename = None
  44. args_from = None
  45. args_to = None
  46. alwayson = False
  47. is_txt2img = False
  48. is_img2img = False
  49. tabname = None
  50. group = None
  51. """A gr.Group component that has all script's UI inside it."""
  52. create_group = True
  53. """If False, for alwayson scripts, a group component will not be created."""
  54. infotext_fields = None
  55. """if set in ui(), this is a list of pairs of gradio component + text; the text will be used when
  56. parsing infotext to set the value for the component; see ui.py's txt2img_paste_fields for an example
  57. """
  58. paste_field_names = None
  59. """if set in ui(), this is a list of names of infotext fields; the fields will be sent through the
  60. various "Send to <X>" buttons when clicked
  61. """
  62. api_info = None
  63. """Generated value of type modules.api.models.ScriptInfo with information about the script for API"""
  64. on_before_component_elem_id = None
  65. """list of callbacks to be called before a component with an elem_id is created"""
  66. on_after_component_elem_id = None
  67. """list of callbacks to be called after a component with an elem_id is created"""
  68. setup_for_ui_only = False
  69. """If true, the script setup will only be run in Gradio UI, not in API"""
  70. controls = None
  71. """A list of controls returned by the ui()."""
  72. def title(self):
  73. """this function should return the title of the script. This is what will be displayed in the dropdown menu."""
  74. raise NotImplementedError()
  75. def ui(self, is_img2img):
  76. """this function should create gradio UI elements. See https://gradio.app/docs/#components
  77. The return value should be an array of all components that are used in processing.
  78. Values of those returned components will be passed to run() and process() functions.
  79. """
  80. pass
  81. def show(self, is_img2img):
  82. """
  83. is_img2img is True if this function is called for the img2img interface, and False otherwise
  84. This function should return:
  85. - False if the script should not be shown in UI at all
  86. - True if the script should be shown in UI if it's selected in the scripts dropdown
  87. - script.AlwaysVisible if the script should be shown in UI at all times
  88. """
  89. return True
  90. def run(self, p, *args):
  91. """
  92. This function is called if the script has been selected in the script dropdown.
  93. It must do all processing and return the Processed object with results, same as
  94. one returned by processing.process_images.
  95. Usually the processing is done by calling the processing.process_images function.
  96. args contains all values returned by components from ui()
  97. """
  98. pass
  99. def setup(self, p, *args):
  100. """For AlwaysVisible scripts, this function is called when the processing object is set up, before any processing starts.
  101. args contains all values returned by components from ui().
  102. """
  103. pass
  104. def before_process(self, p, *args):
  105. """
  106. This function is called very early during processing begins for AlwaysVisible scripts.
  107. You can modify the processing object (p) here, inject hooks, etc.
  108. args contains all values returned by components from ui()
  109. """
  110. pass
  111. def process(self, p, *args):
  112. """
  113. This function is called before processing begins for AlwaysVisible scripts.
  114. You can modify the processing object (p) here, inject hooks, etc.
  115. args contains all values returned by components from ui()
  116. """
  117. pass
  118. def before_process_batch(self, p, *args, **kwargs):
  119. """
  120. Called before extra networks are parsed from the prompt, so you can add
  121. new extra network keywords to the prompt with this callback.
  122. **kwargs will have those items:
  123. - batch_number - index of current batch, from 0 to number of batches-1
  124. - prompts - list of prompts for current batch; you can change contents of this list but changing the number of entries will likely break things
  125. - seeds - list of seeds for current batch
  126. - subseeds - list of subseeds for current batch
  127. """
  128. pass
  129. def after_extra_networks_activate(self, p, *args, **kwargs):
  130. """
  131. Called after extra networks activation, before conds calculation
  132. allow modification of the network after extra networks activation been applied
  133. won't be call if p.disable_extra_networks
  134. **kwargs will have those items:
  135. - batch_number - index of current batch, from 0 to number of batches-1
  136. - prompts - list of prompts for current batch; you can change contents of this list but changing the number of entries will likely break things
  137. - seeds - list of seeds for current batch
  138. - subseeds - list of subseeds for current batch
  139. - extra_network_data - list of ExtraNetworkParams for current stage
  140. """
  141. pass
  142. def process_before_every_sampling(self, p, *args, **kwargs):
  143. """
  144. Similar to process(), called before every sampling.
  145. If you use high-res fix, this will be called two times.
  146. """
  147. pass
  148. def process_batch(self, p, *args, **kwargs):
  149. """
  150. Same as process(), but called for every batch.
  151. **kwargs will have those items:
  152. - batch_number - index of current batch, from 0 to number of batches-1
  153. - prompts - list of prompts for current batch; you can change contents of this list but changing the number of entries will likely break things
  154. - seeds - list of seeds for current batch
  155. - subseeds - list of subseeds for current batch
  156. """
  157. pass
  158. def postprocess_batch(self, p, *args, **kwargs):
  159. """
  160. Same as process_batch(), but called for every batch after it has been generated.
  161. **kwargs will have same items as process_batch, and also:
  162. - batch_number - index of current batch, from 0 to number of batches-1
  163. - images - torch tensor with all generated images, with values ranging from 0 to 1;
  164. """
  165. pass
  166. def postprocess_batch_list(self, p, pp: PostprocessBatchListArgs, *args, **kwargs):
  167. """
  168. Same as postprocess_batch(), but receives batch images as a list of 3D tensors instead of a 4D tensor.
  169. This is useful when you want to update the entire batch instead of individual images.
  170. You can modify the postprocessing object (pp) to update the images in the batch, remove images, add images, etc.
  171. If the number of images is different from the batch size when returning,
  172. then the script has the responsibility to also update the following attributes in the processing object (p):
  173. - p.prompts
  174. - p.negative_prompts
  175. - p.seeds
  176. - p.subseeds
  177. **kwargs will have same items as process_batch, and also:
  178. - batch_number - index of current batch, from 0 to number of batches-1
  179. """
  180. pass
  181. def on_mask_blend(self, p, mba: MaskBlendArgs, *args):
  182. """
  183. Called in inpainting mode when the original content is blended with the inpainted content.
  184. This is called at every step in the denoising process and once at the end.
  185. If is_final_blend is true, this is called for the final blending stage.
  186. Otherwise, denoiser and sigma are defined and may be used to inform the procedure.
  187. """
  188. pass
  189. def post_sample(self, p, ps: PostSampleArgs, *args):
  190. """
  191. Called after the samples have been generated,
  192. but before they have been decoded by the VAE, if applicable.
  193. Check getattr(samples, 'already_decoded', False) to test if the images are decoded.
  194. """
  195. pass
  196. def postprocess_image(self, p, pp: PostprocessImageArgs, *args):
  197. """
  198. Called for every image after it has been generated.
  199. """
  200. pass
  201. def postprocess_maskoverlay(self, p, ppmo: PostProcessMaskOverlayArgs, *args):
  202. """
  203. Called for every image after it has been generated.
  204. """
  205. pass
  206. def postprocess_image_after_composite(self, p, pp: PostprocessImageArgs, *args):
  207. """
  208. Called for every image after it has been generated.
  209. Same as postprocess_image but after inpaint_full_res composite
  210. So that it operates on the full image instead of the inpaint_full_res crop region.
  211. """
  212. pass
  213. def postprocess(self, p, processed, *args):
  214. """
  215. This function is called after processing ends for AlwaysVisible scripts.
  216. args contains all values returned by components from ui()
  217. """
  218. pass
  219. def before_component(self, component, **kwargs):
  220. """
  221. Called before a component is created.
  222. Use elem_id/label fields of kwargs to figure out which component it is.
  223. This can be useful to inject your own components somewhere in the middle of vanilla UI.
  224. You can return created components in the ui() function to add them to the list of arguments for your processing functions
  225. """
  226. pass
  227. def after_component(self, component, **kwargs):
  228. """
  229. Called after a component is created. Same as above.
  230. """
  231. pass
  232. def on_before_component(self, callback, *, elem_id):
  233. """
  234. Calls callback before a component is created. The callback function is called with a single argument of type OnComponent.
  235. May be called in show() or ui() - but it may be too late in latter as some components may already be created.
  236. This function is an alternative to before_component in that it also cllows to run before a component is created, but
  237. it doesn't require to be called for every created component - just for the one you need.
  238. """
  239. if self.on_before_component_elem_id is None:
  240. self.on_before_component_elem_id = []
  241. self.on_before_component_elem_id.append((elem_id, callback))
  242. def on_after_component(self, callback, *, elem_id):
  243. """
  244. Calls callback after a component is created. The callback function is called with a single argument of type OnComponent.
  245. """
  246. if self.on_after_component_elem_id is None:
  247. self.on_after_component_elem_id = []
  248. self.on_after_component_elem_id.append((elem_id, callback))
  249. def describe(self):
  250. """unused"""
  251. return ""
  252. def elem_id(self, item_id):
  253. """helper function to generate id for a HTML element, constructs final id out of script name, tab and user-supplied item_id"""
  254. need_tabname = self.show(True) == self.show(False)
  255. tabkind = 'img2img' if self.is_img2img else 'txt2img'
  256. tabname = f"{tabkind}_" if need_tabname else ""
  257. title = re.sub(r'[^a-z_0-9]', '', re.sub(r'\s', '_', self.title().lower()))
  258. return f'script_{tabname}{title}_{item_id}'
  259. def before_hr(self, p, *args):
  260. """
  261. This function is called before hires fix start.
  262. """
  263. pass
  264. class ScriptBuiltinUI(Script):
  265. setup_for_ui_only = True
  266. def elem_id(self, item_id):
  267. """helper function to generate id for a HTML element, constructs final id out of tab and user-supplied item_id"""
  268. need_tabname = self.show(True) == self.show(False)
  269. tabname = ('img2img' if self.is_img2img else 'txt2img') + "_" if need_tabname else ""
  270. return f'{tabname}{item_id}'
  271. def show(self, is_img2img):
  272. return AlwaysVisible
  273. current_basedir = paths.script_path
  274. def basedir():
  275. """returns the base directory for the current script. For scripts in the main scripts directory,
  276. this is the main directory (where webui.py resides), and for scripts in extensions directory
  277. (ie extensions/aesthetic/script/aesthetic.py), this is extension's directory (extensions/aesthetic)
  278. """
  279. return current_basedir
  280. ScriptFile = namedtuple("ScriptFile", ["basedir", "filename", "path"])
  281. scripts_data = []
  282. postprocessing_scripts_data = []
  283. ScriptClassData = namedtuple("ScriptClassData", ["script_class", "path", "basedir", "module"])
  284. @dataclass
  285. class ScriptWithDependencies:
  286. script_canonical_name: str
  287. file: ScriptFile
  288. requires: list
  289. load_before: list
  290. load_after: list
  291. def list_scripts(scriptdirname, extension, *, include_extensions=True):
  292. scripts = {}
  293. loaded_extensions = {ext.canonical_name: ext for ext in extensions.active()}
  294. loaded_extensions_scripts = {ext.canonical_name: [] for ext in extensions.active()}
  295. # build script dependency map
  296. root_script_basedir = os.path.join(paths.script_path, scriptdirname)
  297. if os.path.exists(root_script_basedir):
  298. for filename in sorted(os.listdir(root_script_basedir)):
  299. if not os.path.isfile(os.path.join(root_script_basedir, filename)):
  300. continue
  301. if os.path.splitext(filename)[1].lower() != extension:
  302. continue
  303. script_file = ScriptFile(paths.script_path, filename, os.path.join(root_script_basedir, filename))
  304. scripts[filename] = ScriptWithDependencies(filename, script_file, [], [], [])
  305. if include_extensions:
  306. for ext in extensions.active():
  307. extension_scripts_list = ext.list_files(scriptdirname, extension)
  308. for extension_script in extension_scripts_list:
  309. if not os.path.isfile(extension_script.path):
  310. continue
  311. script_canonical_name = ("builtin/" if ext.is_builtin else "") + ext.canonical_name + "/" + extension_script.filename
  312. relative_path = scriptdirname + "/" + extension_script.filename
  313. script = ScriptWithDependencies(
  314. script_canonical_name=script_canonical_name,
  315. file=extension_script,
  316. requires=ext.metadata.get_script_requirements("Requires", relative_path, scriptdirname),
  317. load_before=ext.metadata.get_script_requirements("Before", relative_path, scriptdirname),
  318. load_after=ext.metadata.get_script_requirements("After", relative_path, scriptdirname),
  319. )
  320. scripts[script_canonical_name] = script
  321. loaded_extensions_scripts[ext.canonical_name].append(script)
  322. for script_canonical_name, script in scripts.items():
  323. # load before requires inverse dependency
  324. # in this case, append the script name into the load_after list of the specified script
  325. for load_before in script.load_before:
  326. # if this requires an individual script to be loaded before
  327. other_script = scripts.get(load_before)
  328. if other_script:
  329. other_script.load_after.append(script_canonical_name)
  330. # if this requires an extension
  331. other_extension_scripts = loaded_extensions_scripts.get(load_before)
  332. if other_extension_scripts:
  333. for other_script in other_extension_scripts:
  334. other_script.load_after.append(script_canonical_name)
  335. # if After mentions an extension, remove it and instead add all of its scripts
  336. for load_after in list(script.load_after):
  337. if load_after not in scripts and load_after in loaded_extensions_scripts:
  338. script.load_after.remove(load_after)
  339. for other_script in loaded_extensions_scripts.get(load_after, []):
  340. script.load_after.append(other_script.script_canonical_name)
  341. dependencies = {}
  342. for script_canonical_name, script in scripts.items():
  343. for required_script in script.requires:
  344. if required_script not in scripts and required_script not in loaded_extensions:
  345. errors.report(f'Script "{script_canonical_name}" requires "{required_script}" to be loaded, but it is not.', exc_info=False)
  346. dependencies[script_canonical_name] = script.load_after
  347. ordered_scripts = topological_sort(dependencies)
  348. scripts_list = [scripts[script_canonical_name].file for script_canonical_name in ordered_scripts]
  349. return scripts_list
  350. def list_files_with_name(filename):
  351. res = []
  352. dirs = [paths.script_path] + [ext.path for ext in extensions.active()]
  353. for dirpath in dirs:
  354. if not os.path.isdir(dirpath):
  355. continue
  356. path = os.path.join(dirpath, filename)
  357. if os.path.isfile(path):
  358. res.append(path)
  359. return res
  360. def load_scripts():
  361. global current_basedir
  362. scripts_data.clear()
  363. postprocessing_scripts_data.clear()
  364. script_callbacks.clear_callbacks()
  365. scripts_list = list_scripts("scripts", ".py") + list_scripts("modules/processing_scripts", ".py", include_extensions=False)
  366. syspath = sys.path
  367. def register_scripts_from_module(module):
  368. for script_class in module.__dict__.values():
  369. if not inspect.isclass(script_class):
  370. continue
  371. if issubclass(script_class, Script):
  372. scripts_data.append(ScriptClassData(script_class, scriptfile.path, scriptfile.basedir, module))
  373. elif issubclass(script_class, scripts_postprocessing.ScriptPostprocessing):
  374. postprocessing_scripts_data.append(ScriptClassData(script_class, scriptfile.path, scriptfile.basedir, module))
  375. # here the scripts_list is already ordered
  376. # processing_script is not considered though
  377. for scriptfile in scripts_list:
  378. try:
  379. if scriptfile.basedir != paths.script_path:
  380. sys.path = [scriptfile.basedir] + sys.path
  381. current_basedir = scriptfile.basedir
  382. script_module = script_loading.load_module(scriptfile.path)
  383. register_scripts_from_module(script_module)
  384. except Exception:
  385. errors.report(f"Error loading script: {scriptfile.filename}", exc_info=True)
  386. finally:
  387. sys.path = syspath
  388. current_basedir = paths.script_path
  389. timer.startup_timer.record(scriptfile.filename)
  390. global scripts_txt2img, scripts_img2img, scripts_postproc
  391. scripts_txt2img = ScriptRunner()
  392. scripts_img2img = ScriptRunner()
  393. scripts_postproc = scripts_postprocessing.ScriptPostprocessingRunner()
  394. def wrap_call(func, filename, funcname, *args, default=None, **kwargs):
  395. try:
  396. return func(*args, **kwargs)
  397. except Exception:
  398. errors.report(f"Error calling: {filename}/{funcname}", exc_info=True)
  399. return default
  400. class ScriptRunner:
  401. def __init__(self):
  402. self.scripts = []
  403. self.selectable_scripts = []
  404. self.alwayson_scripts = []
  405. self.titles = []
  406. self.title_map = {}
  407. self.infotext_fields = []
  408. self.paste_field_names = []
  409. self.inputs = [None]
  410. self.callback_map = {}
  411. self.callback_names = [
  412. 'before_process',
  413. 'process',
  414. 'before_process_batch',
  415. 'after_extra_networks_activate',
  416. 'process_batch',
  417. 'postprocess',
  418. 'postprocess_batch',
  419. 'postprocess_batch_list',
  420. 'post_sample',
  421. 'on_mask_blend',
  422. 'postprocess_image',
  423. 'postprocess_maskoverlay',
  424. 'postprocess_image_after_composite',
  425. 'before_component',
  426. 'after_component',
  427. ]
  428. self.on_before_component_elem_id = {}
  429. """dict of callbacks to be called before an element is created; key=elem_id, value=list of callbacks"""
  430. self.on_after_component_elem_id = {}
  431. """dict of callbacks to be called after an element is created; key=elem_id, value=list of callbacks"""
  432. def initialize_scripts(self, is_img2img):
  433. from modules import scripts_auto_postprocessing
  434. self.scripts.clear()
  435. self.alwayson_scripts.clear()
  436. self.selectable_scripts.clear()
  437. auto_processing_scripts = scripts_auto_postprocessing.create_auto_preprocessing_script_data()
  438. for script_data in auto_processing_scripts + scripts_data:
  439. try:
  440. script = script_data.script_class()
  441. except Exception:
  442. errors.report(f"Error # failed to initialize Script {script_data.module}: ", exc_info=True)
  443. continue
  444. script.filename = script_data.path
  445. script.is_txt2img = not is_img2img
  446. script.is_img2img = is_img2img
  447. script.tabname = "img2img" if is_img2img else "txt2img"
  448. visibility = script.show(script.is_img2img)
  449. if visibility == AlwaysVisible:
  450. self.scripts.append(script)
  451. self.alwayson_scripts.append(script)
  452. script.alwayson = True
  453. elif visibility:
  454. self.scripts.append(script)
  455. self.selectable_scripts.append(script)
  456. self.callback_map.clear()
  457. self.apply_on_before_component_callbacks()
  458. def apply_on_before_component_callbacks(self):
  459. for script in self.scripts:
  460. on_before = script.on_before_component_elem_id or []
  461. on_after = script.on_after_component_elem_id or []
  462. for elem_id, callback in on_before:
  463. if elem_id not in self.on_before_component_elem_id:
  464. self.on_before_component_elem_id[elem_id] = []
  465. self.on_before_component_elem_id[elem_id].append((callback, script))
  466. for elem_id, callback in on_after:
  467. if elem_id not in self.on_after_component_elem_id:
  468. self.on_after_component_elem_id[elem_id] = []
  469. self.on_after_component_elem_id[elem_id].append((callback, script))
  470. on_before.clear()
  471. on_after.clear()
  472. def create_script_ui(self, script):
  473. script.args_from = len(self.inputs)
  474. script.args_to = len(self.inputs)
  475. try:
  476. self.create_script_ui_inner(script)
  477. except Exception:
  478. errors.report(f"Error creating UI for {script.name}: ", exc_info=True)
  479. def create_script_ui_inner(self, script):
  480. import modules.api.models as api_models
  481. controls = wrap_call(script.ui, script.filename, "ui", script.is_img2img)
  482. script.controls = controls
  483. if controls is None:
  484. return
  485. script.name = wrap_call(script.title, script.filename, "title", default=script.filename).lower()
  486. api_args = []
  487. for control in controls:
  488. control.custom_script_source = os.path.basename(script.filename)
  489. arg_info = api_models.ScriptArg(label=control.label or "")
  490. for field in ("value", "minimum", "maximum", "step"):
  491. v = getattr(control, field, None)
  492. if v is not None:
  493. setattr(arg_info, field, v)
  494. choices = getattr(control, 'choices', None) # as of gradio 3.41, some items in choices are strings, and some are tuples where the first elem is the string
  495. if choices is not None:
  496. arg_info.choices = [x[0] if isinstance(x, tuple) else x for x in choices]
  497. api_args.append(arg_info)
  498. script.api_info = api_models.ScriptInfo(
  499. name=script.name,
  500. is_img2img=script.is_img2img,
  501. is_alwayson=script.alwayson,
  502. args=api_args,
  503. )
  504. if script.infotext_fields is not None:
  505. self.infotext_fields += script.infotext_fields
  506. if script.paste_field_names is not None:
  507. self.paste_field_names += script.paste_field_names
  508. self.inputs += controls
  509. script.args_to = len(self.inputs)
  510. def setup_ui_for_section(self, section, scriptlist=None):
  511. if scriptlist is None:
  512. scriptlist = self.alwayson_scripts
  513. for script in scriptlist:
  514. if script.alwayson and script.section != section:
  515. continue
  516. if script.create_group:
  517. with gr.Group(visible=script.alwayson) as group:
  518. self.create_script_ui(script)
  519. script.group = group
  520. else:
  521. self.create_script_ui(script)
  522. def prepare_ui(self):
  523. self.inputs = [None]
  524. def setup_ui(self):
  525. all_titles = [wrap_call(script.title, script.filename, "title") or script.filename for script in self.scripts]
  526. self.title_map = {title.lower(): script for title, script in zip(all_titles, self.scripts)}
  527. self.titles = [wrap_call(script.title, script.filename, "title") or f"{script.filename} [error]" for script in self.selectable_scripts]
  528. self.setup_ui_for_section(None)
  529. dropdown = gr.Dropdown(label="Script", elem_id="script_list", choices=["None"] + self.titles, value="None", type="index")
  530. self.inputs[0] = dropdown
  531. self.setup_ui_for_section(None, self.selectable_scripts)
  532. def select_script(script_index):
  533. if script_index is None:
  534. script_index = 0
  535. selected_script = self.selectable_scripts[script_index - 1] if script_index>0 else None
  536. return [gr.update(visible=selected_script == s) for s in self.selectable_scripts]
  537. def init_field(title):
  538. """called when an initial value is set from ui-config.json to show script's UI components"""
  539. if title == 'None':
  540. return
  541. script_index = self.titles.index(title)
  542. self.selectable_scripts[script_index].group.visible = True
  543. dropdown.init_field = init_field
  544. dropdown.change(
  545. fn=select_script,
  546. inputs=[dropdown],
  547. outputs=[script.group for script in self.selectable_scripts]
  548. )
  549. self.script_load_ctr = 0
  550. def onload_script_visibility(params):
  551. title = params.get('Script', None)
  552. if title:
  553. try:
  554. title_index = self.titles.index(title)
  555. visibility = title_index == self.script_load_ctr
  556. self.script_load_ctr = (self.script_load_ctr + 1) % len(self.titles)
  557. return gr.update(visible=visibility)
  558. except ValueError:
  559. params['Script'] = None
  560. massage = f'Cannot find Script: "{title}"'
  561. print(massage)
  562. gr.Warning(massage)
  563. return gr.update(visible=False)
  564. self.infotext_fields.append((dropdown, lambda x: gr.update(value=x.get('Script', 'None'))))
  565. self.infotext_fields.extend([(script.group, onload_script_visibility) for script in self.selectable_scripts])
  566. self.apply_on_before_component_callbacks()
  567. return self.inputs
  568. def run(self, p, *args):
  569. script_index = args[0]
  570. if script_index == 0 or script_index is None:
  571. return None
  572. script = self.selectable_scripts[script_index-1]
  573. if script is None:
  574. return None
  575. script_args = args[script.args_from:script.args_to]
  576. processed = script.run(p, *script_args)
  577. shared.total_tqdm.clear()
  578. return processed
  579. def list_scripts_for_method(self, method_name):
  580. if method_name in ('before_component', 'after_component'):
  581. return self.scripts
  582. else:
  583. return self.alwayson_scripts
  584. def create_ordered_callbacks_list(self, method_name, *, enable_user_sort=True):
  585. script_list = self.list_scripts_for_method(method_name)
  586. category = f'script_{method_name}'
  587. callbacks = []
  588. for script in script_list:
  589. if getattr(script.__class__, method_name, None) == getattr(Script, method_name, None):
  590. continue
  591. script_callbacks.add_callback(callbacks, script, category=category, name=script.__class__.__name__, filename=script.filename)
  592. return script_callbacks.sort_callbacks(category, callbacks, enable_user_sort=enable_user_sort)
  593. def ordered_callbacks(self, method_name, *, enable_user_sort=True):
  594. script_list = self.list_scripts_for_method(method_name)
  595. category = f'script_{method_name}'
  596. scrpts_len, callbacks = self.callback_map.get(category, (-1, None))
  597. if callbacks is None or scrpts_len != len(script_list):
  598. callbacks = self.create_ordered_callbacks_list(method_name, enable_user_sort=enable_user_sort)
  599. self.callback_map[category] = len(script_list), callbacks
  600. return callbacks
  601. def ordered_scripts(self, method_name):
  602. return [x.callback for x in self.ordered_callbacks(method_name)]
  603. def before_process(self, p):
  604. for script in self.ordered_scripts('before_process'):
  605. try:
  606. script_args = p.script_args[script.args_from:script.args_to]
  607. script.before_process(p, *script_args)
  608. except Exception:
  609. errors.report(f"Error running before_process: {script.filename}", exc_info=True)
  610. def process(self, p):
  611. for script in self.ordered_scripts('process'):
  612. try:
  613. script_args = p.script_args[script.args_from:script.args_to]
  614. script.process(p, *script_args)
  615. except Exception:
  616. errors.report(f"Error running process: {script.filename}", exc_info=True)
  617. def process_before_every_sampling(self, p, **kwargs):
  618. for script in self.ordered_scripts('process_before_every_sampling'):
  619. try:
  620. script_args = p.script_args[script.args_from:script.args_to]
  621. script.process_before_every_sampling(p, *script_args, **kwargs)
  622. except Exception:
  623. errors.report(f"Error running process_before_every_sampling: {script.filename}", exc_info=True)
  624. def before_process_batch(self, p, **kwargs):
  625. for script in self.ordered_scripts('before_process_batch'):
  626. try:
  627. script_args = p.script_args[script.args_from:script.args_to]
  628. script.before_process_batch(p, *script_args, **kwargs)
  629. except Exception:
  630. errors.report(f"Error running before_process_batch: {script.filename}", exc_info=True)
  631. def after_extra_networks_activate(self, p, **kwargs):
  632. for script in self.ordered_scripts('after_extra_networks_activate'):
  633. try:
  634. script_args = p.script_args[script.args_from:script.args_to]
  635. script.after_extra_networks_activate(p, *script_args, **kwargs)
  636. except Exception:
  637. errors.report(f"Error running after_extra_networks_activate: {script.filename}", exc_info=True)
  638. def process_batch(self, p, **kwargs):
  639. for script in self.ordered_scripts('process_batch'):
  640. try:
  641. script_args = p.script_args[script.args_from:script.args_to]
  642. script.process_batch(p, *script_args, **kwargs)
  643. except Exception:
  644. errors.report(f"Error running process_batch: {script.filename}", exc_info=True)
  645. def postprocess(self, p, processed):
  646. for script in self.ordered_scripts('postprocess'):
  647. try:
  648. script_args = p.script_args[script.args_from:script.args_to]
  649. script.postprocess(p, processed, *script_args)
  650. except Exception:
  651. errors.report(f"Error running postprocess: {script.filename}", exc_info=True)
  652. def postprocess_batch(self, p, images, **kwargs):
  653. for script in self.ordered_scripts('postprocess_batch'):
  654. try:
  655. script_args = p.script_args[script.args_from:script.args_to]
  656. script.postprocess_batch(p, *script_args, images=images, **kwargs)
  657. except Exception:
  658. errors.report(f"Error running postprocess_batch: {script.filename}", exc_info=True)
  659. def postprocess_batch_list(self, p, pp: PostprocessBatchListArgs, **kwargs):
  660. for script in self.ordered_scripts('postprocess_batch_list'):
  661. try:
  662. script_args = p.script_args[script.args_from:script.args_to]
  663. script.postprocess_batch_list(p, pp, *script_args, **kwargs)
  664. except Exception:
  665. errors.report(f"Error running postprocess_batch_list: {script.filename}", exc_info=True)
  666. def post_sample(self, p, ps: PostSampleArgs):
  667. for script in self.ordered_scripts('post_sample'):
  668. try:
  669. script_args = p.script_args[script.args_from:script.args_to]
  670. script.post_sample(p, ps, *script_args)
  671. except Exception:
  672. errors.report(f"Error running post_sample: {script.filename}", exc_info=True)
  673. def on_mask_blend(self, p, mba: MaskBlendArgs):
  674. for script in self.ordered_scripts('on_mask_blend'):
  675. try:
  676. script_args = p.script_args[script.args_from:script.args_to]
  677. script.on_mask_blend(p, mba, *script_args)
  678. except Exception:
  679. errors.report(f"Error running post_sample: {script.filename}", exc_info=True)
  680. def postprocess_image(self, p, pp: PostprocessImageArgs):
  681. for script in self.ordered_scripts('postprocess_image'):
  682. try:
  683. script_args = p.script_args[script.args_from:script.args_to]
  684. script.postprocess_image(p, pp, *script_args)
  685. except Exception:
  686. errors.report(f"Error running postprocess_image: {script.filename}", exc_info=True)
  687. def postprocess_maskoverlay(self, p, ppmo: PostProcessMaskOverlayArgs):
  688. for script in self.ordered_scripts('postprocess_maskoverlay'):
  689. try:
  690. script_args = p.script_args[script.args_from:script.args_to]
  691. script.postprocess_maskoverlay(p, ppmo, *script_args)
  692. except Exception:
  693. errors.report(f"Error running postprocess_image: {script.filename}", exc_info=True)
  694. def postprocess_image_after_composite(self, p, pp: PostprocessImageArgs):
  695. for script in self.ordered_scripts('postprocess_image_after_composite'):
  696. try:
  697. script_args = p.script_args[script.args_from:script.args_to]
  698. script.postprocess_image_after_composite(p, pp, *script_args)
  699. except Exception:
  700. errors.report(f"Error running postprocess_image_after_composite: {script.filename}", exc_info=True)
  701. def before_component(self, component, **kwargs):
  702. for callback, script in self.on_before_component_elem_id.get(kwargs.get("elem_id"), []):
  703. try:
  704. callback(OnComponent(component=component))
  705. except Exception:
  706. errors.report(f"Error running on_before_component: {script.filename}", exc_info=True)
  707. for script in self.ordered_scripts('before_component'):
  708. try:
  709. script.before_component(component, **kwargs)
  710. except Exception:
  711. errors.report(f"Error running before_component: {script.filename}", exc_info=True)
  712. def after_component(self, component, **kwargs):
  713. for callback, script in self.on_after_component_elem_id.get(component.elem_id, []):
  714. try:
  715. callback(OnComponent(component=component))
  716. except Exception:
  717. errors.report(f"Error running on_after_component: {script.filename}", exc_info=True)
  718. for script in self.ordered_scripts('after_component'):
  719. try:
  720. script.after_component(component, **kwargs)
  721. except Exception:
  722. errors.report(f"Error running after_component: {script.filename}", exc_info=True)
  723. def script(self, title):
  724. return self.title_map.get(title.lower())
  725. def reload_sources(self, cache):
  726. for si, script in list(enumerate(self.scripts)):
  727. args_from = script.args_from
  728. args_to = script.args_to
  729. filename = script.filename
  730. module = cache.get(filename, None)
  731. if module is None:
  732. module = script_loading.load_module(script.filename)
  733. cache[filename] = module
  734. for script_class in module.__dict__.values():
  735. if type(script_class) == type and issubclass(script_class, Script):
  736. self.scripts[si] = script_class()
  737. self.scripts[si].filename = filename
  738. self.scripts[si].args_from = args_from
  739. self.scripts[si].args_to = args_to
  740. def before_hr(self, p):
  741. for script in self.ordered_scripts('before_hr'):
  742. try:
  743. script_args = p.script_args[script.args_from:script.args_to]
  744. script.before_hr(p, *script_args)
  745. except Exception:
  746. errors.report(f"Error running before_hr: {script.filename}", exc_info=True)
  747. def setup_scrips(self, p, *, is_ui=True):
  748. for script in self.ordered_scripts('setup'):
  749. if not is_ui and script.setup_for_ui_only:
  750. continue
  751. try:
  752. script_args = p.script_args[script.args_from:script.args_to]
  753. script.setup(p, *script_args)
  754. except Exception:
  755. errors.report(f"Error running setup: {script.filename}", exc_info=True)
  756. def set_named_arg(self, args, script_name, arg_elem_id, value, fuzzy=False):
  757. """Locate an arg of a specific script in script_args and set its value
  758. Args:
  759. args: all script args of process p, p.script_args
  760. script_name: the name target script name to
  761. arg_elem_id: the elem_id of the target arg
  762. value: the value to set
  763. fuzzy: if True, arg_elem_id can be a substring of the control.elem_id else exact match
  764. Returns:
  765. Updated script args
  766. when script_name in not found or arg_elem_id is not found in script controls, raise RuntimeError
  767. """
  768. script = next((x for x in self.scripts if x.name == script_name), None)
  769. if script is None:
  770. raise RuntimeError(f"script {script_name} not found")
  771. for i, control in enumerate(script.controls):
  772. if arg_elem_id in control.elem_id if fuzzy else arg_elem_id == control.elem_id:
  773. index = script.args_from + i
  774. if isinstance(args, tuple):
  775. return args[:index] + (value,) + args[index + 1:]
  776. elif isinstance(args, list):
  777. args[index] = value
  778. return args
  779. else:
  780. raise RuntimeError(f"args is not a list or tuple, but {type(args)}")
  781. raise RuntimeError(f"arg_elem_id {arg_elem_id} not found in script {script_name}")
  782. scripts_txt2img: ScriptRunner = None
  783. scripts_img2img: ScriptRunner = None
  784. scripts_postproc: scripts_postprocessing.ScriptPostprocessingRunner = None
  785. scripts_current: ScriptRunner = None
  786. def reload_script_body_only():
  787. cache = {}
  788. scripts_txt2img.reload_sources(cache)
  789. scripts_img2img.reload_sources(cache)
  790. reload_scripts = load_scripts # compatibility alias