script_callbacks.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. from __future__ import annotations
  2. import dataclasses
  3. import inspect
  4. import os
  5. from typing import Optional, Any
  6. from fastapi import FastAPI
  7. from gradio import Blocks
  8. from modules import errors, timer, extensions, shared
  9. def report_exception(c, job):
  10. errors.report(f"Error executing callback {job} for {c.script}", exc_info=True)
  11. class ImageSaveParams:
  12. def __init__(self, image, p, filename, pnginfo):
  13. self.image = image
  14. """the PIL image itself"""
  15. self.p = p
  16. """p object with processing parameters; either StableDiffusionProcessing or an object with same fields"""
  17. self.filename = filename
  18. """name of file that the image would be saved to"""
  19. self.pnginfo = pnginfo
  20. """dictionary with parameters for image's PNG info data; infotext will have the key 'parameters'"""
  21. class ExtraNoiseParams:
  22. def __init__(self, noise, x, xi):
  23. self.noise = noise
  24. """Random noise generated by the seed"""
  25. self.x = x
  26. """Latent representation of the image"""
  27. self.xi = xi
  28. """Noisy latent representation of the image"""
  29. class CFGDenoiserParams:
  30. def __init__(self, x, image_cond, sigma, sampling_step, total_sampling_steps, text_cond, text_uncond, denoiser=None):
  31. self.x = x
  32. """Latent image representation in the process of being denoised"""
  33. self.image_cond = image_cond
  34. """Conditioning image"""
  35. self.sigma = sigma
  36. """Current sigma noise step value"""
  37. self.sampling_step = sampling_step
  38. """Current Sampling step number"""
  39. self.total_sampling_steps = total_sampling_steps
  40. """Total number of sampling steps planned"""
  41. self.text_cond = text_cond
  42. """ Encoder hidden states of text conditioning from prompt"""
  43. self.text_uncond = text_uncond
  44. """ Encoder hidden states of text conditioning from negative prompt"""
  45. self.denoiser = denoiser
  46. """Current CFGDenoiser object with processing parameters"""
  47. class CFGDenoisedParams:
  48. def __init__(self, x, sampling_step, total_sampling_steps, inner_model):
  49. self.x = x
  50. """Latent image representation in the process of being denoised"""
  51. self.sampling_step = sampling_step
  52. """Current Sampling step number"""
  53. self.total_sampling_steps = total_sampling_steps
  54. """Total number of sampling steps planned"""
  55. self.inner_model = inner_model
  56. """Inner model reference used for denoising"""
  57. class AfterCFGCallbackParams:
  58. def __init__(self, x, sampling_step, total_sampling_steps):
  59. self.x = x
  60. """Latent image representation in the process of being denoised"""
  61. self.sampling_step = sampling_step
  62. """Current Sampling step number"""
  63. self.total_sampling_steps = total_sampling_steps
  64. """Total number of sampling steps planned"""
  65. class UiTrainTabParams:
  66. def __init__(self, txt2img_preview_params):
  67. self.txt2img_preview_params = txt2img_preview_params
  68. class ImageGridLoopParams:
  69. def __init__(self, imgs, cols, rows):
  70. self.imgs = imgs
  71. self.cols = cols
  72. self.rows = rows
  73. @dataclasses.dataclass
  74. class BeforeTokenCounterParams:
  75. prompt: str
  76. steps: int
  77. styles: list
  78. is_positive: bool = True
  79. @dataclasses.dataclass
  80. class ScriptCallback:
  81. script: str
  82. callback: ''
  83. name: str = None
  84. def add_callback(callbacks, fun, *, name=None, category='unknown', filename=None):
  85. if filename is None:
  86. stack = [x for x in inspect.stack() if x.filename != __file__]
  87. filename = stack[0].filename if stack else 'unknown file'
  88. extension = extensions.find_extension(filename)
  89. extension_name = extension.canonical_name if extension else 'base'
  90. callback_name = f"{extension_name}/{os.path.basename(filename)}/{category}"
  91. if name is not None:
  92. callback_name += f'/{name}'
  93. unique_callback_name = callback_name
  94. for index in range(1000):
  95. existing = any(x.name == unique_callback_name for x in callbacks)
  96. if not existing:
  97. break
  98. unique_callback_name = f'{callback_name}-{index+1}'
  99. callbacks.append(ScriptCallback(filename, fun, unique_callback_name))
  100. def sort_callbacks(category, unordered_callbacks, *, enable_user_sort=True):
  101. callbacks = unordered_callbacks.copy()
  102. if enable_user_sort:
  103. for name in reversed(getattr(shared.opts, 'prioritized_callbacks_' + category, [])):
  104. index = next((i for i, callback in enumerate(callbacks) if callback.name == name), None)
  105. if index is not None:
  106. callbacks.insert(0, callbacks.pop(index))
  107. return callbacks
  108. def ordered_callbacks(category, unordered_callbacks=None, *, enable_user_sort=True):
  109. if unordered_callbacks is None:
  110. unordered_callbacks = callback_map.get('callbacks_' + category, [])
  111. if not enable_user_sort:
  112. return sort_callbacks(category, unordered_callbacks, enable_user_sort=False)
  113. callbacks = ordered_callbacks_map.get(category)
  114. if callbacks is not None and len(callbacks) == len(unordered_callbacks):
  115. return callbacks
  116. callbacks = sort_callbacks(category, unordered_callbacks)
  117. ordered_callbacks_map[category] = callbacks
  118. return callbacks
  119. def enumerate_callbacks():
  120. for category, callbacks in callback_map.items():
  121. if category.startswith('callbacks_'):
  122. category = category[10:]
  123. yield category, callbacks
  124. callback_map = dict(
  125. callbacks_app_started=[],
  126. callbacks_model_loaded=[],
  127. callbacks_ui_tabs=[],
  128. callbacks_ui_train_tabs=[],
  129. callbacks_ui_settings=[],
  130. callbacks_before_image_saved=[],
  131. callbacks_image_saved=[],
  132. callbacks_extra_noise=[],
  133. callbacks_cfg_denoiser=[],
  134. callbacks_cfg_denoised=[],
  135. callbacks_cfg_after_cfg=[],
  136. callbacks_before_component=[],
  137. callbacks_after_component=[],
  138. callbacks_image_grid=[],
  139. callbacks_infotext_pasted=[],
  140. callbacks_script_unloaded=[],
  141. callbacks_before_ui=[],
  142. callbacks_on_reload=[],
  143. callbacks_list_optimizers=[],
  144. callbacks_list_unets=[],
  145. callbacks_before_token_counter=[],
  146. )
  147. ordered_callbacks_map = {}
  148. def clear_callbacks():
  149. for callback_list in callback_map.values():
  150. callback_list.clear()
  151. ordered_callbacks_map.clear()
  152. def app_started_callback(demo: Optional[Blocks], app: FastAPI):
  153. for c in ordered_callbacks('app_started'):
  154. try:
  155. c.callback(demo, app)
  156. timer.startup_timer.record(os.path.basename(c.script))
  157. except Exception:
  158. report_exception(c, 'app_started_callback')
  159. def app_reload_callback():
  160. for c in ordered_callbacks('on_reload'):
  161. try:
  162. c.callback()
  163. except Exception:
  164. report_exception(c, 'callbacks_on_reload')
  165. def model_loaded_callback(sd_model):
  166. for c in ordered_callbacks('model_loaded'):
  167. try:
  168. c.callback(sd_model)
  169. except Exception:
  170. report_exception(c, 'model_loaded_callback')
  171. def ui_tabs_callback():
  172. res = []
  173. for c in ordered_callbacks('ui_tabs'):
  174. try:
  175. res += c.callback() or []
  176. except Exception:
  177. report_exception(c, 'ui_tabs_callback')
  178. return res
  179. def ui_train_tabs_callback(params: UiTrainTabParams):
  180. for c in ordered_callbacks('ui_train_tabs'):
  181. try:
  182. c.callback(params)
  183. except Exception:
  184. report_exception(c, 'callbacks_ui_train_tabs')
  185. def ui_settings_callback():
  186. for c in ordered_callbacks('ui_settings'):
  187. try:
  188. c.callback()
  189. except Exception:
  190. report_exception(c, 'ui_settings_callback')
  191. def before_image_saved_callback(params: ImageSaveParams):
  192. for c in ordered_callbacks('before_image_saved'):
  193. try:
  194. c.callback(params)
  195. except Exception:
  196. report_exception(c, 'before_image_saved_callback')
  197. def image_saved_callback(params: ImageSaveParams):
  198. for c in ordered_callbacks('image_saved'):
  199. try:
  200. c.callback(params)
  201. except Exception:
  202. report_exception(c, 'image_saved_callback')
  203. def extra_noise_callback(params: ExtraNoiseParams):
  204. for c in ordered_callbacks('extra_noise'):
  205. try:
  206. c.callback(params)
  207. except Exception:
  208. report_exception(c, 'callbacks_extra_noise')
  209. def cfg_denoiser_callback(params: CFGDenoiserParams):
  210. for c in ordered_callbacks('cfg_denoiser'):
  211. try:
  212. c.callback(params)
  213. except Exception:
  214. report_exception(c, 'cfg_denoiser_callback')
  215. def cfg_denoised_callback(params: CFGDenoisedParams):
  216. for c in ordered_callbacks('cfg_denoised'):
  217. try:
  218. c.callback(params)
  219. except Exception:
  220. report_exception(c, 'cfg_denoised_callback')
  221. def cfg_after_cfg_callback(params: AfterCFGCallbackParams):
  222. for c in ordered_callbacks('cfg_after_cfg'):
  223. try:
  224. c.callback(params)
  225. except Exception:
  226. report_exception(c, 'cfg_after_cfg_callback')
  227. def before_component_callback(component, **kwargs):
  228. for c in ordered_callbacks('before_component'):
  229. try:
  230. c.callback(component, **kwargs)
  231. except Exception:
  232. report_exception(c, 'before_component_callback')
  233. def after_component_callback(component, **kwargs):
  234. for c in ordered_callbacks('after_component'):
  235. try:
  236. c.callback(component, **kwargs)
  237. except Exception:
  238. report_exception(c, 'after_component_callback')
  239. def image_grid_callback(params: ImageGridLoopParams):
  240. for c in ordered_callbacks('image_grid'):
  241. try:
  242. c.callback(params)
  243. except Exception:
  244. report_exception(c, 'image_grid')
  245. def infotext_pasted_callback(infotext: str, params: dict[str, Any]):
  246. for c in ordered_callbacks('infotext_pasted'):
  247. try:
  248. c.callback(infotext, params)
  249. except Exception:
  250. report_exception(c, 'infotext_pasted')
  251. def script_unloaded_callback():
  252. for c in reversed(ordered_callbacks('script_unloaded')):
  253. try:
  254. c.callback()
  255. except Exception:
  256. report_exception(c, 'script_unloaded')
  257. def before_ui_callback():
  258. for c in reversed(ordered_callbacks('before_ui')):
  259. try:
  260. c.callback()
  261. except Exception:
  262. report_exception(c, 'before_ui')
  263. def list_optimizers_callback():
  264. res = []
  265. for c in ordered_callbacks('list_optimizers'):
  266. try:
  267. c.callback(res)
  268. except Exception:
  269. report_exception(c, 'list_optimizers')
  270. return res
  271. def list_unets_callback():
  272. res = []
  273. for c in ordered_callbacks('list_unets'):
  274. try:
  275. c.callback(res)
  276. except Exception:
  277. report_exception(c, 'list_unets')
  278. return res
  279. def before_token_counter_callback(params: BeforeTokenCounterParams):
  280. for c in ordered_callbacks('before_token_counter'):
  281. try:
  282. c.callback(params)
  283. except Exception:
  284. report_exception(c, 'before_token_counter')
  285. def remove_current_script_callbacks():
  286. stack = [x for x in inspect.stack() if x.filename != __file__]
  287. filename = stack[0].filename if stack else 'unknown file'
  288. if filename == 'unknown file':
  289. return
  290. for callback_list in callback_map.values():
  291. for callback_to_remove in [cb for cb in callback_list if cb.script == filename]:
  292. callback_list.remove(callback_to_remove)
  293. def remove_callbacks_for_function(callback_func):
  294. for callback_list in callback_map.values():
  295. for callback_to_remove in [cb for cb in callback_list if cb.callback == callback_func]:
  296. callback_list.remove(callback_to_remove)
  297. def on_app_started(callback, *, name=None):
  298. """register a function to be called when the webui started, the gradio `Block` component and
  299. fastapi `FastAPI` object are passed as the arguments"""
  300. add_callback(callback_map['callbacks_app_started'], callback, name=name, category='app_started')
  301. def on_before_reload(callback, *, name=None):
  302. """register a function to be called just before the server reloads."""
  303. add_callback(callback_map['callbacks_on_reload'], callback, name=name, category='on_reload')
  304. def on_model_loaded(callback, *, name=None):
  305. """register a function to be called when the stable diffusion model is created; the model is
  306. passed as an argument; this function is also called when the script is reloaded. """
  307. add_callback(callback_map['callbacks_model_loaded'], callback, name=name, category='model_loaded')
  308. def on_ui_tabs(callback, *, name=None):
  309. """register a function to be called when the UI is creating new tabs.
  310. The function must either return a None, which means no new tabs to be added, or a list, where
  311. each element is a tuple:
  312. (gradio_component, title, elem_id)
  313. gradio_component is a gradio component to be used for contents of the tab (usually gr.Blocks)
  314. title is tab text displayed to user in the UI
  315. elem_id is HTML id for the tab
  316. """
  317. add_callback(callback_map['callbacks_ui_tabs'], callback, name=name, category='ui_tabs')
  318. def on_ui_train_tabs(callback, *, name=None):
  319. """register a function to be called when the UI is creating new tabs for the train tab.
  320. Create your new tabs with gr.Tab.
  321. """
  322. add_callback(callback_map['callbacks_ui_train_tabs'], callback, name=name, category='ui_train_tabs')
  323. def on_ui_settings(callback, *, name=None):
  324. """register a function to be called before UI settings are populated; add your settings
  325. by using shared.opts.add_option(shared.OptionInfo(...)) """
  326. add_callback(callback_map['callbacks_ui_settings'], callback, name=name, category='ui_settings')
  327. def on_before_image_saved(callback, *, name=None):
  328. """register a function to be called before an image is saved to a file.
  329. The callback is called with one argument:
  330. - params: ImageSaveParams - parameters the image is to be saved with. You can change fields in this object.
  331. """
  332. add_callback(callback_map['callbacks_before_image_saved'], callback, name=name, category='before_image_saved')
  333. def on_image_saved(callback, *, name=None):
  334. """register a function to be called after an image is saved to a file.
  335. The callback is called with one argument:
  336. - params: ImageSaveParams - parameters the image was saved with. Changing fields in this object does nothing.
  337. """
  338. add_callback(callback_map['callbacks_image_saved'], callback, name=name, category='image_saved')
  339. def on_extra_noise(callback, *, name=None):
  340. """register a function to be called before adding extra noise in img2img or hires fix;
  341. The callback is called with one argument:
  342. - params: ExtraNoiseParams - contains noise determined by seed and latent representation of image
  343. """
  344. add_callback(callback_map['callbacks_extra_noise'], callback, name=name, category='extra_noise')
  345. def on_cfg_denoiser(callback, *, name=None):
  346. """register a function to be called in the kdiffussion cfg_denoiser method after building the inner model inputs.
  347. The callback is called with one argument:
  348. - params: CFGDenoiserParams - parameters to be passed to the inner model and sampling state details.
  349. """
  350. add_callback(callback_map['callbacks_cfg_denoiser'], callback, name=name, category='cfg_denoiser')
  351. def on_cfg_denoised(callback, *, name=None):
  352. """register a function to be called in the kdiffussion cfg_denoiser method after building the inner model inputs.
  353. The callback is called with one argument:
  354. - params: CFGDenoisedParams - parameters to be passed to the inner model and sampling state details.
  355. """
  356. add_callback(callback_map['callbacks_cfg_denoised'], callback, name=name, category='cfg_denoised')
  357. def on_cfg_after_cfg(callback, *, name=None):
  358. """register a function to be called in the kdiffussion cfg_denoiser method after cfg calculations are completed.
  359. The callback is called with one argument:
  360. - params: AfterCFGCallbackParams - parameters to be passed to the script for post-processing after cfg calculation.
  361. """
  362. add_callback(callback_map['callbacks_cfg_after_cfg'], callback, name=name, category='cfg_after_cfg')
  363. def on_before_component(callback, *, name=None):
  364. """register a function to be called before a component is created.
  365. The callback is called with arguments:
  366. - component - gradio component that is about to be created.
  367. - **kwargs - args to gradio.components.IOComponent.__init__ function
  368. Use elem_id/label fields of kwargs to figure out which component it is.
  369. This can be useful to inject your own components somewhere in the middle of vanilla UI.
  370. """
  371. add_callback(callback_map['callbacks_before_component'], callback, name=name, category='before_component')
  372. def on_after_component(callback, *, name=None):
  373. """register a function to be called after a component is created. See on_before_component for more."""
  374. add_callback(callback_map['callbacks_after_component'], callback, name=name, category='after_component')
  375. def on_image_grid(callback, *, name=None):
  376. """register a function to be called before making an image grid.
  377. The callback is called with one argument:
  378. - params: ImageGridLoopParams - parameters to be used for grid creation. Can be modified.
  379. """
  380. add_callback(callback_map['callbacks_image_grid'], callback, name=name, category='image_grid')
  381. def on_infotext_pasted(callback, *, name=None):
  382. """register a function to be called before applying an infotext.
  383. The callback is called with two arguments:
  384. - infotext: str - raw infotext.
  385. - result: dict[str, any] - parsed infotext parameters.
  386. """
  387. add_callback(callback_map['callbacks_infotext_pasted'], callback, name=name, category='infotext_pasted')
  388. def on_script_unloaded(callback, *, name=None):
  389. """register a function to be called before the script is unloaded. Any hooks/hijacks/monkeying about that
  390. the script did should be reverted here"""
  391. add_callback(callback_map['callbacks_script_unloaded'], callback, name=name, category='script_unloaded')
  392. def on_before_ui(callback, *, name=None):
  393. """register a function to be called before the UI is created."""
  394. add_callback(callback_map['callbacks_before_ui'], callback, name=name, category='before_ui')
  395. def on_list_optimizers(callback, *, name=None):
  396. """register a function to be called when UI is making a list of cross attention optimization options.
  397. The function will be called with one argument, a list, and shall add objects of type modules.sd_hijack_optimizations.SdOptimization
  398. to it."""
  399. add_callback(callback_map['callbacks_list_optimizers'], callback, name=name, category='list_optimizers')
  400. def on_list_unets(callback, *, name=None):
  401. """register a function to be called when UI is making a list of alternative options for unet.
  402. The function will be called with one argument, a list, and shall add objects of type modules.sd_unet.SdUnetOption to it."""
  403. add_callback(callback_map['callbacks_list_unets'], callback, name=name, category='list_unets')
  404. def on_before_token_counter(callback, *, name=None):
  405. """register a function to be called when UI is counting tokens for a prompt.
  406. The function will be called with one argument of type BeforeTokenCounterParams, and should modify its fields if necessary."""
  407. add_callback(callback_map['callbacks_before_token_counter'], callback, name=name, category='before_token_counter')