Skip to content

Commit 21fba39

Browse files
authored
Add callbacks and param objects
1 parent 17a2076 commit 21fba39

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

modules/script_callbacks.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,22 @@ def __init__(self, image, p, filename, pnginfo):
2424
"""dictionary with parameters for image's PNG info data; infotext will have the key 'parameters'"""
2525

2626

27+
class CGFDenoiserParams:
28+
def __init__(self, x_in, image_cond_in, sigma_in, sampling_step, total_sampling_steps):
29+
self.x_in = x_in
30+
self.image_cond_in = image_cond_in
31+
self.sigma_in = sigma_in
32+
self.sampling_step = sampling_step
33+
self.total_sampling_steps = total_sampling_steps
34+
35+
2736
ScriptCallback = namedtuple("ScriptCallback", ["script", "callback"])
2837
callbacks_model_loaded = []
2938
callbacks_ui_tabs = []
3039
callbacks_ui_settings = []
3140
callbacks_before_image_saved = []
3241
callbacks_image_saved = []
42+
callbacks_cfg_denoiser = []
3343

3444

3545
def clear_callbacks():
@@ -84,6 +94,14 @@ def image_saved_callback(params: ImageSaveParams):
8494
report_exception(c, 'image_saved_callback')
8595

8696

97+
def cfg_denoiser_callback(params: CGFDenoiserParams):
98+
for c in callbacks_cfg_denoiser:
99+
try:
100+
c.callback(params)
101+
except Exception:
102+
report_exception(c, 'cfg_denoiser_callback')
103+
104+
87105
def add_callback(callbacks, fun):
88106
stack = [x for x in inspect.stack() if x.filename != __file__]
89107
filename = stack[0].filename if len(stack) > 0 else 'unknown file'
@@ -130,3 +148,12 @@ def on_image_saved(callback):
130148
- params: ImageSaveParams - parameters the image was saved with. Changing fields in this object does nothing.
131149
"""
132150
add_callback(callbacks_image_saved, callback)
151+
152+
153+
def on_cfg_denoiser(callback):
154+
"""register a function to be called in the kdiffussion cfg_denoiser method after building the inner model inputs.
155+
The callback is called with one argument:
156+
- params: CGFDenoiserParams - parameters to be passed to the inner model and sampling state details.
157+
"""
158+
add_callback(callbacks_cfg_denoiser, callback)
159+

0 commit comments

Comments
 (0)