script_callbacks.py 16.0 KB
Newer Older
1
import inspect
2
import os
3
from collections import namedtuple
A
a666 已提交
4
from typing import Optional, Any
5

M
Maiko Tan 已提交
6 7
from fastapi import FastAPI
from gradio import Blocks
8

9
from modules import errors, timer
A
AUTOMATIC 已提交
10

11

12
def report_exception(c, job):
13
    errors.report(f"Error executing callback {job} for {c.script}", exc_info=True)
14 15


16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
class ImageSaveParams:
    def __init__(self, image, p, filename, pnginfo):
        self.image = image
        """the PIL image itself"""

        self.p = p
        """p object with processing parameters; either StableDiffusionProcessing or an object with same fields"""

        self.filename = filename
        """name of file that the image would be saved to"""

        self.pnginfo = pnginfo
        """dictionary with parameters for image's PNG info data; infotext will have the key 'parameters'"""


C
catboxanon 已提交
31
class ExtraNoiseParams:
32
    def __init__(self, noise, x, xi):
C
catboxanon 已提交
33 34 35 36
        self.noise = noise
        """Random noise generated by the seed"""

        self.x = x
37 38 39 40
        """Latent representation of the image"""

        self.xi = xi
        """Noisy latent representation of the image"""
C
catboxanon 已提交
41 42


43
class CFGDenoiserParams:
44
    def __init__(self, x, image_cond, sigma, sampling_step, total_sampling_steps, text_cond, text_uncond):
45 46
        self.x = x
        """Latent image representation in the process of being denoised"""
47

48 49
        self.image_cond = image_cond
        """Conditioning image"""
50

51 52
        self.sigma = sigma
        """Current sigma noise step value"""
53

D
DepFA 已提交
54
        self.sampling_step = sampling_step
55
        """Current Sampling step number"""
56

D
DepFA 已提交
57
        self.total_sampling_steps = total_sampling_steps
58
        """Total number of sampling steps planned"""
59

60 61
        self.text_cond = text_cond
        """ Encoder hidden states of text conditioning from prompt"""
62

63 64
        self.text_uncond = text_uncond
        """ Encoder hidden states of text conditioning from negative prompt"""
D
DepFA 已提交
65 66


O
opparco 已提交
67
class CFGDenoisedParams:
C
catboxanon 已提交
68 69 70 71 72 73 74 75 76 77 78
    def __init__(self, x, sampling_step, total_sampling_steps, inner_model):
        self.x = x
        """Latent image representation in the process of being denoised"""

        self.sampling_step = sampling_step
        """Current Sampling step number"""

        self.total_sampling_steps = total_sampling_steps
        """Total number of sampling steps planned"""

        self.inner_model = inner_model
S
Sakura-Luna 已提交
79
        """Inner model reference used for denoising"""
C
catboxanon 已提交
80 81 82


class AfterCFGCallbackParams:
O
opparco 已提交
83 84 85 86 87 88 89 90 91 92 93
    def __init__(self, x, sampling_step, total_sampling_steps):
        self.x = x
        """Latent image representation in the process of being denoised"""

        self.sampling_step = sampling_step
        """Current Sampling step number"""

        self.total_sampling_steps = total_sampling_steps
        """Total number of sampling steps planned"""


94 95 96 97 98
class UiTrainTabParams:
    def __init__(self, txt2img_preview_params):
        self.txt2img_preview_params = txt2img_preview_params


A
AlUlkesh 已提交
99
class ImageGridLoopParams:
A
AUTOMATIC 已提交
100 101 102 103
    def __init__(self, imgs, cols, rows):
        self.imgs = imgs
        self.cols = cols
        self.rows = rows
A
AlUlkesh 已提交
104 105


106
ScriptCallback = namedtuple("ScriptCallback", ["script", "callback"])
D
DepFA 已提交
107
callback_map = dict(
108 109 110
    callbacks_app_started=[],
    callbacks_model_loaded=[],
    callbacks_ui_tabs=[],
111
    callbacks_ui_train_tabs=[],
112 113 114
    callbacks_ui_settings=[],
    callbacks_before_image_saved=[],
    callbacks_image_saved=[],
C
catboxanon 已提交
115
    callbacks_extra_noise=[],
116
    callbacks_cfg_denoiser=[],
O
opparco 已提交
117
    callbacks_cfg_denoised=[],
C
catboxanon 已提交
118
    callbacks_cfg_after_cfg=[],
119 120
    callbacks_before_component=[],
    callbacks_after_component=[],
A
AUTOMATIC 已提交
121
    callbacks_image_grid=[],
122
    callbacks_infotext_pasted=[],
123
    callbacks_script_unloaded=[],
A
AUTOMATIC 已提交
124
    callbacks_before_ui=[],
125
    callbacks_on_reload=[],
126
    callbacks_list_optimizers=[],
A
AUTOMATIC 已提交
127
    callbacks_list_unets=[],
128
)
129

130

131
def clear_callbacks():
D
DepFA 已提交
132
    for callback_list in callback_map.values():
133
        callback_list.clear()
134

A
AUTOMATIC 已提交
135

136
def app_started_callback(demo: Optional[Blocks], app: FastAPI):
D
DepFA 已提交
137
    for c in callback_map['callbacks_app_started']:
M
Maiko Tan 已提交
138 139
        try:
            c.callback(demo, app)
140
            timer.startup_timer.record(os.path.basename(c.script))
M
Maiko Tan 已提交
141 142 143 144
        except Exception:
            report_exception(c, 'app_started_callback')


A
AUTOMATIC 已提交
145
def app_reload_callback():
146 147 148 149 150 151
    for c in callback_map['callbacks_on_reload']:
        try:
            c.callback()
        except Exception:
            report_exception(c, 'callbacks_on_reload')

M
Maiko Tan 已提交
152

153
def model_loaded_callback(sd_model):
D
DepFA 已提交
154
    for c in callback_map['callbacks_model_loaded']:
155 156 157 158
        try:
            c.callback(sd_model)
        except Exception:
            report_exception(c, 'model_loaded_callback')
159 160 161 162


def ui_tabs_callback():
    res = []
163

D
DepFA 已提交
164
    for c in callback_map['callbacks_ui_tabs']:
165 166 167 168
        try:
            res += c.callback() or []
        except Exception:
            report_exception(c, 'ui_tabs_callback')
169 170 171 172

    return res


173 174 175 176 177 178 179 180
def ui_train_tabs_callback(params: UiTrainTabParams):
    for c in callback_map['callbacks_ui_train_tabs']:
        try:
            c.callback(params)
        except Exception:
            report_exception(c, 'callbacks_ui_train_tabs')


181
def ui_settings_callback():
D
DepFA 已提交
182
    for c in callback_map['callbacks_ui_settings']:
183 184 185 186 187 188
        try:
            c.callback()
        except Exception:
            report_exception(c, 'ui_settings_callback')


189
def before_image_saved_callback(params: ImageSaveParams):
D
DepFA 已提交
190
    for c in callback_map['callbacks_before_image_saved']:
A
AUTOMATIC 已提交
191
        try:
192 193 194 195 196 197
            c.callback(params)
        except Exception:
            report_exception(c, 'before_image_saved_callback')


def image_saved_callback(params: ImageSaveParams):
D
DepFA 已提交
198
    for c in callback_map['callbacks_image_saved']:
199 200
        try:
            c.callback(params)
A
AUTOMATIC 已提交
201 202 203 204
        except Exception:
            report_exception(c, 'image_saved_callback')


C
catboxanon 已提交
205 206 207 208 209 210 211 212
def extra_noise_callback(params: ExtraNoiseParams):
    for c in callback_map['callbacks_extra_noise']:
        try:
            c.callback(params)
        except Exception:
            report_exception(c, 'callbacks_extra_noise')


213
def cfg_denoiser_callback(params: CFGDenoiserParams):
D
DepFA 已提交
214
    for c in callback_map['callbacks_cfg_denoiser']:
D
DepFA 已提交
215 216 217 218 219 220
        try:
            c.callback(params)
        except Exception:
            report_exception(c, 'cfg_denoiser_callback')


O
opparco 已提交
221 222 223 224 225 226 227 228
def cfg_denoised_callback(params: CFGDenoisedParams):
    for c in callback_map['callbacks_cfg_denoised']:
        try:
            c.callback(params)
        except Exception:
            report_exception(c, 'cfg_denoised_callback')


C
catboxanon 已提交
229 230 231 232 233 234 235 236
def cfg_after_cfg_callback(params: AfterCFGCallbackParams):
    for c in callback_map['callbacks_cfg_after_cfg']:
        try:
            c.callback(params)
        except Exception:
            report_exception(c, 'cfg_after_cfg_callback')


237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
def before_component_callback(component, **kwargs):
    for c in callback_map['callbacks_before_component']:
        try:
            c.callback(component, **kwargs)
        except Exception:
            report_exception(c, 'before_component_callback')


def after_component_callback(component, **kwargs):
    for c in callback_map['callbacks_after_component']:
        try:
            c.callback(component, **kwargs)
        except Exception:
            report_exception(c, 'after_component_callback')

A
AUTOMATIC 已提交
252 253 254

def image_grid_callback(params: ImageGridLoopParams):
    for c in callback_map['callbacks_image_grid']:
A
AlUlkesh 已提交
255
        try:
A
AUTOMATIC 已提交
256
            c.callback(params)
A
AlUlkesh 已提交
257
        except Exception:
A
AUTOMATIC 已提交
258 259
            report_exception(c, 'image_grid')

260

A
a666 已提交
261
def infotext_pasted_callback(infotext: str, params: dict[str, Any]):
262 263 264 265 266 267 268
    for c in callback_map['callbacks_infotext_pasted']:
        try:
            c.callback(infotext, params)
        except Exception:
            report_exception(c, 'infotext_pasted')


269 270 271 272 273 274 275 276
def script_unloaded_callback():
    for c in reversed(callback_map['callbacks_script_unloaded']):
        try:
            c.callback()
        except Exception:
            report_exception(c, 'script_unloaded')


A
AUTOMATIC 已提交
277 278 279 280 281 282 283 284
def before_ui_callback():
    for c in reversed(callback_map['callbacks_before_ui']):
        try:
            c.callback()
        except Exception:
            report_exception(c, 'before_ui')


285 286 287 288 289 290 291 292 293 294 295 296
def list_optimizers_callback():
    res = []

    for c in callback_map['callbacks_list_optimizers']:
        try:
            c.callback(res)
        except Exception:
            report_exception(c, 'list_optimizers')

    return res


A
AUTOMATIC 已提交
297 298 299 300 301 302 303 304 305 306 307 308
def list_unets_callback():
    res = []

    for c in callback_map['callbacks_list_unets']:
        try:
            c.callback(res)
        except Exception:
            report_exception(c, 'list_unets')

    return res


309 310
def add_callback(callbacks, fun):
    stack = [x for x in inspect.stack() if x.filename != __file__]
311
    filename = stack[0].filename if stack else 'unknown file'
312 313

    callbacks.append(ScriptCallback(filename, fun))
314

315

316 317
def remove_current_script_callbacks():
    stack = [x for x in inspect.stack() if x.filename != __file__]
318
    filename = stack[0].filename if stack else 'unknown file'
319 320
    if filename == 'unknown file':
        return
D
DepFA 已提交
321
    for callback_list in callback_map.values():
322 323 324 325 326
        for callback_to_remove in [cb for cb in callback_list if cb.script == filename]:
            callback_list.remove(callback_to_remove)


def remove_callbacks_for_function(callback_func):
D
DepFA 已提交
327
    for callback_list in callback_map.values():
328 329 330
        for callback_to_remove in [cb for cb in callback_list if cb.callback == callback_func]:
            callback_list.remove(callback_to_remove)

331

M
Maiko Tan 已提交
332
def on_app_started(callback):
M
Maiko Sinkyaet Tan 已提交
333 334
    """register a function to be called when the webui started, the gradio `Block` component and
    fastapi `FastAPI` object are passed as the arguments"""
D
DepFA 已提交
335
    add_callback(callback_map['callbacks_app_started'], callback)
M
Maiko Tan 已提交
336 337


338 339 340
def on_before_reload(callback):
    """register a function to be called just before the server reloads."""
    add_callback(callback_map['callbacks_on_reload'], callback)
R
rucadi 已提交
341

A
AUTOMATIC 已提交
342

343 344
def on_model_loaded(callback):
    """register a function to be called when the stable diffusion model is created; the model is
345
    passed as an argument; this function is also called when the script is reloaded. """
D
DepFA 已提交
346
    add_callback(callback_map['callbacks_model_loaded'], callback)
347 348 349 350 351 352 353 354 355 356 357 358


def on_ui_tabs(callback):
    """register a function to be called when the UI is creating new tabs.
    The function must either return a None, which means no new tabs to be added, or a list, where
    each element is a tuple:
        (gradio_component, title, elem_id)

    gradio_component is a gradio component to be used for contents of the tab (usually gr.Blocks)
    title is tab text displayed to user in the UI
    elem_id is HTML id for the tab
    """
D
DepFA 已提交
359
    add_callback(callback_map['callbacks_ui_tabs'], callback)
360

361

362 363 364 365 366 367 368
def on_ui_train_tabs(callback):
    """register a function to be called when the UI is creating new tabs for the train tab.
    Create your new tabs with gr.Tab.
    """
    add_callback(callback_map['callbacks_ui_train_tabs'], callback)


369
def on_ui_settings(callback):
A
typo  
AUTOMATIC 已提交
370
    """register a function to be called before UI settings are populated; add your settings
371
    by using shared.opts.add_option(shared.OptionInfo(...)) """
D
DepFA 已提交
372
    add_callback(callback_map['callbacks_ui_settings'], callback)
T
Trung Ngo 已提交
373 374


375 376 377 378 379
def on_before_image_saved(callback):
    """register a function to be called before an image is saved to a file.
    The callback is called with one argument:
        - params: ImageSaveParams - parameters the image is to be saved with. You can change fields in this object.
    """
D
DepFA 已提交
380
    add_callback(callback_map['callbacks_before_image_saved'], callback)
381 382


383
def on_image_saved(callback):
384 385 386
    """register a function to be called after an image is saved to a file.
    The callback is called with one argument:
        - params: ImageSaveParams - parameters the image was saved with. Changing fields in this object does nothing.
A
AUTOMATIC 已提交
387
    """
D
DepFA 已提交
388
    add_callback(callback_map['callbacks_image_saved'], callback)
D
DepFA 已提交
389 390


C
catboxanon 已提交
391 392 393 394 395 396 397 398
def on_extra_noise(callback):
    """register a function to be called before adding extra noise in img2img or hires fix;
    The callback is called with one argument:
        - params: ExtraNoiseParams - contains noise determined by seed and latent representation of image
    """
    add_callback(callback_map['callbacks_extra_noise'], callback)


D
DepFA 已提交
399 400 401
def on_cfg_denoiser(callback):
    """register a function to be called in the kdiffussion cfg_denoiser method after building the inner model inputs.
    The callback is called with one argument:
402
        - params: CFGDenoiserParams - parameters to be passed to the inner model and sampling state details.
D
DepFA 已提交
403
    """
D
DepFA 已提交
404
    add_callback(callback_map['callbacks_cfg_denoiser'], callback)
405 406


O
opparco 已提交
407 408 409 410 411 412 413 414
def on_cfg_denoised(callback):
    """register a function to be called in the kdiffussion cfg_denoiser method after building the inner model inputs.
    The callback is called with one argument:
        - params: CFGDenoisedParams - parameters to be passed to the inner model and sampling state details.
    """
    add_callback(callback_map['callbacks_cfg_denoised'], callback)


C
catboxanon 已提交
415
def on_cfg_after_cfg(callback):
S
Sakura-Luna 已提交
416
    """register a function to be called in the kdiffussion cfg_denoiser method after cfg calculations are completed.
C
catboxanon 已提交
417
    The callback is called with one argument:
S
Sakura-Luna 已提交
418
        - params: AfterCFGCallbackParams - parameters to be passed to the script for post-processing after cfg calculation.
C
catboxanon 已提交
419 420 421 422
    """
    add_callback(callback_map['callbacks_cfg_after_cfg'], callback)


423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
def on_before_component(callback):
    """register a function to be called before a component is created.
    The callback is called with arguments:
        - component - gradio component that is about to be created.
        - **kwargs - args to gradio.components.IOComponent.__init__ function

    Use elem_id/label fields of kwargs to figure out which component it is.
    This can be useful to inject your own components somewhere in the middle of vanilla UI.
    """
    add_callback(callback_map['callbacks_before_component'], callback)


def on_after_component(callback):
    """register a function to be called after a component is created. See on_before_component for more."""
    add_callback(callback_map['callbacks_after_component'], callback)
A
AlUlkesh 已提交
438 439


A
AUTOMATIC 已提交
440 441
def on_image_grid(callback):
    """register a function to be called before making an image grid.
A
AlUlkesh 已提交
442
    The callback is called with one argument:
A
AUTOMATIC 已提交
443
       - params: ImageGridLoopParams - parameters to be used for grid creation. Can be modified.
A
AlUlkesh 已提交
444
    """
A
AUTOMATIC 已提交
445
    add_callback(callback_map['callbacks_image_grid'], callback)
446 447


448 449 450 451
def on_infotext_pasted(callback):
    """register a function to be called before applying an infotext.
    The callback is called with two arguments:
       - infotext: str - raw infotext.
A
a666 已提交
452
       - result: dict[str, any] - parsed infotext parameters.
453 454 455 456
    """
    add_callback(callback_map['callbacks_infotext_pasted'], callback)


457 458 459 460 461
def on_script_unloaded(callback):
    """register a function to be called before the script is unloaded. Any hooks/hijacks/monkeying about that
    the script did should be reverted here"""

    add_callback(callback_map['callbacks_script_unloaded'], callback)
A
AUTOMATIC 已提交
462 463 464 465 466 467


def on_before_ui(callback):
    """register a function to be called before the UI is created."""

    add_callback(callback_map['callbacks_before_ui'], callback)
468 469 470 471 472 473 474 475


def on_list_optimizers(callback):
    """register a function to be called when UI is making a list of cross attention optimization options.
    The function will be called with one argument, a list, and shall add objects of type modules.sd_hijack_optimizations.SdOptimization
    to it."""

    add_callback(callback_map['callbacks_list_optimizers'], callback)
A
AUTOMATIC 已提交
476 477 478 479 480 481 482


def on_list_unets(callback):
    """register a function to be called when UI is making a list of alternative options for unet.
    The function will be called with one argument, a list, and shall add objects of type modules.sd_unet.SdUnetOption to it."""

    add_callback(callback_map['callbacks_list_unets'], callback)