@@ -26,13 +26,32 @@ def __init__(self, image, p, filename, pnginfo):
26
26
"""dictionary with parameters for image's PNG info data; infotext will have the key 'parameters'"""
27
27
28
28
29
+ class CFGDenoiserParams :
30
+ def __init__ (self , x , image_cond , sigma , sampling_step , total_sampling_steps ):
31
+ self .x = x
32
+ """Latent image representation in the process of being denoised"""
33
+
34
+ self .image_cond = image_cond
35
+ """Conditioning image"""
36
+
37
+ self .sigma = sigma
38
+ """Current sigma noise step value"""
39
+
40
+ self .sampling_step = sampling_step
41
+ """Current Sampling step number"""
42
+
43
+ self .total_sampling_steps = total_sampling_steps
44
+ """Total number of sampling steps planned"""
45
+
46
+
29
47
ScriptCallback = namedtuple ("ScriptCallback" , ["script" , "callback" ])
30
48
callbacks_app_started = []
31
49
callbacks_model_loaded = []
32
50
callbacks_ui_tabs = []
33
51
callbacks_ui_settings = []
34
52
callbacks_before_image_saved = []
35
53
callbacks_image_saved = []
54
+ callbacks_cfg_denoiser = []
36
55
37
56
38
57
def clear_callbacks ():
@@ -41,7 +60,7 @@ def clear_callbacks():
41
60
callbacks_ui_settings .clear ()
42
61
callbacks_before_image_saved .clear ()
43
62
callbacks_image_saved .clear ()
44
-
63
+ callbacks_cfg_denoiser . clear ()
45
64
46
65
def app_started_callback (demo : Blocks , app : FastAPI ):
47
66
for c in callbacks_app_started :
@@ -95,6 +114,14 @@ def image_saved_callback(params: ImageSaveParams):
95
114
report_exception (c , 'image_saved_callback' )
96
115
97
116
117
+ def cfg_denoiser_callback (params : CFGDenoiserParams ):
118
+ for c in callbacks_cfg_denoiser :
119
+ try :
120
+ c .callback (params )
121
+ except Exception :
122
+ report_exception (c , 'cfg_denoiser_callback' )
123
+
124
+
98
125
def add_callback (callbacks , fun ):
99
126
stack = [x for x in inspect .stack () if x .filename != __file__ ]
100
127
filename = stack [0 ].filename if len (stack ) > 0 else 'unknown file'
@@ -147,3 +174,12 @@ def on_image_saved(callback):
147
174
- params: ImageSaveParams - parameters the image was saved with. Changing fields in this object does nothing.
148
175
"""
149
176
add_callback (callbacks_image_saved , callback )
177
+
178
+
179
+ def on_cfg_denoiser (callback ):
180
+ """register a function to be called in the kdiffussion cfg_denoiser method after building the inner model inputs.
181
+ The callback is called with one argument:
182
+ - params: CFGDenoiserParams - parameters to be passed to the inner model and sampling state details.
183
+ """
184
+ add_callback (callbacks_cfg_denoiser , callback )
185
+
0 commit comments