script_callbacks.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. import inspect
  2. import os
  3. from collections import namedtuple
  4. from typing import Optional, Dict, Any
  5. from fastapi import FastAPI
  6. from gradio import Blocks
  7. from modules import errors, timer
  8. def report_exception(c, job):
  9. errors.report(f"Error executing callback {job} for {c.script}", exc_info=True)
  10. class ImageSaveParams:
  11. def __init__(self, image, p, filename, pnginfo):
  12. self.image = image
  13. """the PIL image itself"""
  14. self.p = p
  15. """p object with processing parameters; either StableDiffusionProcessing or an object with same fields"""
  16. self.filename = filename
  17. """name of file that the image would be saved to"""
  18. self.pnginfo = pnginfo
  19. """dictionary with parameters for image's PNG info data; infotext will have the key 'parameters'"""
  20. class CFGDenoiserParams:
  21. def __init__(self, x, image_cond, sigma, sampling_step, total_sampling_steps, text_cond, text_uncond):
  22. self.x = x
  23. """Latent image representation in the process of being denoised"""
  24. self.image_cond = image_cond
  25. """Conditioning image"""
  26. self.sigma = sigma
  27. """Current sigma noise step value"""
  28. self.sampling_step = sampling_step
  29. """Current Sampling step number"""
  30. self.total_sampling_steps = total_sampling_steps
  31. """Total number of sampling steps planned"""
  32. self.text_cond = text_cond
  33. """ Encoder hidden states of text conditioning from prompt"""
  34. self.text_uncond = text_uncond
  35. """ Encoder hidden states of text conditioning from negative prompt"""
  36. class CFGDenoisedParams:
  37. def __init__(self, x, sampling_step, total_sampling_steps, inner_model):
  38. self.x = x
  39. """Latent image representation in the process of being denoised"""
  40. self.sampling_step = sampling_step
  41. """Current Sampling step number"""
  42. self.total_sampling_steps = total_sampling_steps
  43. """Total number of sampling steps planned"""
  44. self.inner_model = inner_model
  45. """Inner model reference used for denoising"""
  46. class AfterCFGCallbackParams:
  47. def __init__(self, x, sampling_step, total_sampling_steps):
  48. self.x = x
  49. """Latent image representation in the process of being denoised"""
  50. self.sampling_step = sampling_step
  51. """Current Sampling step number"""
  52. self.total_sampling_steps = total_sampling_steps
  53. """Total number of sampling steps planned"""
  54. class UiTrainTabParams:
  55. def __init__(self, txt2img_preview_params):
  56. self.txt2img_preview_params = txt2img_preview_params
  57. class ImageGridLoopParams:
  58. def __init__(self, imgs, cols, rows):
  59. self.imgs = imgs
  60. self.cols = cols
  61. self.rows = rows
  62. ScriptCallback = namedtuple("ScriptCallback", ["script", "callback"])
  63. callback_map = dict(
  64. callbacks_app_started=[],
  65. callbacks_model_loaded=[],
  66. callbacks_ui_tabs=[],
  67. callbacks_ui_train_tabs=[],
  68. callbacks_ui_settings=[],
  69. callbacks_before_image_saved=[],
  70. callbacks_image_saved=[],
  71. callbacks_cfg_denoiser=[],
  72. callbacks_cfg_denoised=[],
  73. callbacks_cfg_after_cfg=[],
  74. callbacks_before_component=[],
  75. callbacks_after_component=[],
  76. callbacks_image_grid=[],
  77. callbacks_infotext_pasted=[],
  78. callbacks_script_unloaded=[],
  79. callbacks_before_ui=[],
  80. callbacks_on_reload=[],
  81. callbacks_list_optimizers=[],
  82. callbacks_list_unets=[],
  83. )
  84. def clear_callbacks():
  85. for callback_list in callback_map.values():
  86. callback_list.clear()
  87. def app_started_callback(demo: Optional[Blocks], app: FastAPI):
  88. for c in callback_map['callbacks_app_started']:
  89. try:
  90. c.callback(demo, app)
  91. timer.startup_timer.record(os.path.basename(c.script))
  92. except Exception:
  93. report_exception(c, 'app_started_callback')
  94. def app_reload_callback():
  95. for c in callback_map['callbacks_on_reload']:
  96. try:
  97. c.callback()
  98. except Exception:
  99. report_exception(c, 'callbacks_on_reload')
  100. def model_loaded_callback(sd_model):
  101. for c in callback_map['callbacks_model_loaded']:
  102. try:
  103. c.callback(sd_model)
  104. except Exception:
  105. report_exception(c, 'model_loaded_callback')
  106. def ui_tabs_callback():
  107. res = []
  108. for c in callback_map['callbacks_ui_tabs']:
  109. try:
  110. res += c.callback() or []
  111. except Exception:
  112. report_exception(c, 'ui_tabs_callback')
  113. return res
  114. def ui_train_tabs_callback(params: UiTrainTabParams):
  115. for c in callback_map['callbacks_ui_train_tabs']:
  116. try:
  117. c.callback(params)
  118. except Exception:
  119. report_exception(c, 'callbacks_ui_train_tabs')
  120. def ui_settings_callback():
  121. for c in callback_map['callbacks_ui_settings']:
  122. try:
  123. c.callback()
  124. except Exception:
  125. report_exception(c, 'ui_settings_callback')
  126. def before_image_saved_callback(params: ImageSaveParams):
  127. for c in callback_map['callbacks_before_image_saved']:
  128. try:
  129. c.callback(params)
  130. except Exception:
  131. report_exception(c, 'before_image_saved_callback')
  132. def image_saved_callback(params: ImageSaveParams):
  133. for c in callback_map['callbacks_image_saved']:
  134. try:
  135. c.callback(params)
  136. except Exception:
  137. report_exception(c, 'image_saved_callback')
  138. def cfg_denoiser_callback(params: CFGDenoiserParams):
  139. for c in callback_map['callbacks_cfg_denoiser']:
  140. try:
  141. c.callback(params)
  142. except Exception:
  143. report_exception(c, 'cfg_denoiser_callback')
  144. def cfg_denoised_callback(params: CFGDenoisedParams):
  145. for c in callback_map['callbacks_cfg_denoised']:
  146. try:
  147. c.callback(params)
  148. except Exception:
  149. report_exception(c, 'cfg_denoised_callback')
  150. def cfg_after_cfg_callback(params: AfterCFGCallbackParams):
  151. for c in callback_map['callbacks_cfg_after_cfg']:
  152. try:
  153. c.callback(params)
  154. except Exception:
  155. report_exception(c, 'cfg_after_cfg_callback')
  156. def before_component_callback(component, **kwargs):
  157. for c in callback_map['callbacks_before_component']:
  158. try:
  159. c.callback(component, **kwargs)
  160. except Exception:
  161. report_exception(c, 'before_component_callback')
  162. def after_component_callback(component, **kwargs):
  163. for c in callback_map['callbacks_after_component']:
  164. try:
  165. c.callback(component, **kwargs)
  166. except Exception:
  167. report_exception(c, 'after_component_callback')
  168. def image_grid_callback(params: ImageGridLoopParams):
  169. for c in callback_map['callbacks_image_grid']:
  170. try:
  171. c.callback(params)
  172. except Exception:
  173. report_exception(c, 'image_grid')
  174. def infotext_pasted_callback(infotext: str, params: Dict[str, Any]):
  175. for c in callback_map['callbacks_infotext_pasted']:
  176. try:
  177. c.callback(infotext, params)
  178. except Exception:
  179. report_exception(c, 'infotext_pasted')
  180. def script_unloaded_callback():
  181. for c in reversed(callback_map['callbacks_script_unloaded']):
  182. try:
  183. c.callback()
  184. except Exception:
  185. report_exception(c, 'script_unloaded')
  186. def before_ui_callback():
  187. for c in reversed(callback_map['callbacks_before_ui']):
  188. try:
  189. c.callback()
  190. except Exception:
  191. report_exception(c, 'before_ui')
  192. def list_optimizers_callback():
  193. res = []
  194. for c in callback_map['callbacks_list_optimizers']:
  195. try:
  196. c.callback(res)
  197. except Exception:
  198. report_exception(c, 'list_optimizers')
  199. return res
  200. def list_unets_callback():
  201. res = []
  202. for c in callback_map['callbacks_list_unets']:
  203. try:
  204. c.callback(res)
  205. except Exception:
  206. report_exception(c, 'list_unets')
  207. return res
  208. def add_callback(callbacks, fun):
  209. stack = [x for x in inspect.stack() if x.filename != __file__]
  210. filename = stack[0].filename if stack else 'unknown file'
  211. callbacks.append(ScriptCallback(filename, fun))
  212. def remove_current_script_callbacks():
  213. stack = [x for x in inspect.stack() if x.filename != __file__]
  214. filename = stack[0].filename if stack else 'unknown file'
  215. if filename == 'unknown file':
  216. return
  217. for callback_list in callback_map.values():
  218. for callback_to_remove in [cb for cb in callback_list if cb.script == filename]:
  219. callback_list.remove(callback_to_remove)
  220. def remove_callbacks_for_function(callback_func):
  221. for callback_list in callback_map.values():
  222. for callback_to_remove in [cb for cb in callback_list if cb.callback == callback_func]:
  223. callback_list.remove(callback_to_remove)
  224. def on_app_started(callback):
  225. """register a function to be called when the webui started, the gradio `Block` component and
  226. fastapi `FastAPI` object are passed as the arguments"""
  227. add_callback(callback_map['callbacks_app_started'], callback)
  228. def on_before_reload(callback):
  229. """register a function to be called just before the server reloads."""
  230. add_callback(callback_map['callbacks_on_reload'], callback)
  231. def on_model_loaded(callback):
  232. """register a function to be called when the stable diffusion model is created; the model is
  233. passed as an argument; this function is also called when the script is reloaded. """
  234. add_callback(callback_map['callbacks_model_loaded'], callback)
  235. def on_ui_tabs(callback):
  236. """register a function to be called when the UI is creating new tabs.
  237. The function must either return a None, which means no new tabs to be added, or a list, where
  238. each element is a tuple:
  239. (gradio_component, title, elem_id)
  240. gradio_component is a gradio component to be used for contents of the tab (usually gr.Blocks)
  241. title is tab text displayed to user in the UI
  242. elem_id is HTML id for the tab
  243. """
  244. add_callback(callback_map['callbacks_ui_tabs'], callback)
  245. def on_ui_train_tabs(callback):
  246. """register a function to be called when the UI is creating new tabs for the train tab.
  247. Create your new tabs with gr.Tab.
  248. """
  249. add_callback(callback_map['callbacks_ui_train_tabs'], callback)
  250. def on_ui_settings(callback):
  251. """register a function to be called before UI settings are populated; add your settings
  252. by using shared.opts.add_option(shared.OptionInfo(...)) """
  253. add_callback(callback_map['callbacks_ui_settings'], callback)
  254. def on_before_image_saved(callback):
  255. """register a function to be called before an image is saved to a file.
  256. The callback is called with one argument:
  257. - params: ImageSaveParams - parameters the image is to be saved with. You can change fields in this object.
  258. """
  259. add_callback(callback_map['callbacks_before_image_saved'], callback)
  260. def on_image_saved(callback):
  261. """register a function to be called after an image is saved to a file.
  262. The callback is called with one argument:
  263. - params: ImageSaveParams - parameters the image was saved with. Changing fields in this object does nothing.
  264. """
  265. add_callback(callback_map['callbacks_image_saved'], callback)
  266. def on_cfg_denoiser(callback):
  267. """register a function to be called in the kdiffussion cfg_denoiser method after building the inner model inputs.
  268. The callback is called with one argument:
  269. - params: CFGDenoiserParams - parameters to be passed to the inner model and sampling state details.
  270. """
  271. add_callback(callback_map['callbacks_cfg_denoiser'], callback)
  272. def on_cfg_denoised(callback):
  273. """register a function to be called in the kdiffussion cfg_denoiser method after building the inner model inputs.
  274. The callback is called with one argument:
  275. - params: CFGDenoisedParams - parameters to be passed to the inner model and sampling state details.
  276. """
  277. add_callback(callback_map['callbacks_cfg_denoised'], callback)
  278. def on_cfg_after_cfg(callback):
  279. """register a function to be called in the kdiffussion cfg_denoiser method after cfg calculations are completed.
  280. The callback is called with one argument:
  281. - params: AfterCFGCallbackParams - parameters to be passed to the script for post-processing after cfg calculation.
  282. """
  283. add_callback(callback_map['callbacks_cfg_after_cfg'], callback)
  284. def on_before_component(callback):
  285. """register a function to be called before a component is created.
  286. The callback is called with arguments:
  287. - component - gradio component that is about to be created.
  288. - **kwargs - args to gradio.components.IOComponent.__init__ function
  289. Use elem_id/label fields of kwargs to figure out which component it is.
  290. This can be useful to inject your own components somewhere in the middle of vanilla UI.
  291. """
  292. add_callback(callback_map['callbacks_before_component'], callback)
  293. def on_after_component(callback):
  294. """register a function to be called after a component is created. See on_before_component for more."""
  295. add_callback(callback_map['callbacks_after_component'], callback)
  296. def on_image_grid(callback):
  297. """register a function to be called before making an image grid.
  298. The callback is called with one argument:
  299. - params: ImageGridLoopParams - parameters to be used for grid creation. Can be modified.
  300. """
  301. add_callback(callback_map['callbacks_image_grid'], callback)
  302. def on_infotext_pasted(callback):
  303. """register a function to be called before applying an infotext.
  304. The callback is called with two arguments:
  305. - infotext: str - raw infotext.
  306. - result: Dict[str, any] - parsed infotext parameters.
  307. """
  308. add_callback(callback_map['callbacks_infotext_pasted'], callback)
  309. def on_script_unloaded(callback):
  310. """register a function to be called before the script is unloaded. Any hooks/hijacks/monkeying about that
  311. the script did should be reverted here"""
  312. add_callback(callback_map['callbacks_script_unloaded'], callback)
  313. def on_before_ui(callback):
  314. """register a function to be called before the UI is created."""
  315. add_callback(callback_map['callbacks_before_ui'], callback)
  316. def on_list_optimizers(callback):
  317. """register a function to be called when UI is making a list of cross attention optimization options.
  318. The function will be called with one argument, a list, and shall add objects of type modules.sd_hijack_optimizations.SdOptimization
  319. to it."""
  320. add_callback(callback_map['callbacks_list_optimizers'], callback)
  321. def on_list_unets(callback):
  322. """register a function to be called when UI is making a list of alternative options for unet.
  323. The function will be called with one argument, a list, and shall add objects of type modules.sd_unet.SdUnetOption to it."""
  324. add_callback(callback_map['callbacks_list_unets'], callback)