3
3
from collections import namedtuple
4
4
import inspect
5
5
6
+ from fastapi import FastAPI
7
+ from gradio import Blocks
6
8
7
9
def report_exception (c , job ):
8
10
print (f"Error executing callback { job } for { c .script } " , file = sys .stderr )
@@ -25,6 +27,7 @@ def __init__(self, image, p, filename, pnginfo):
25
27
26
28
27
29
ScriptCallback = namedtuple ("ScriptCallback" , ["script" , "callback" ])
30
+ callbacks_app_started = []
28
31
callbacks_model_loaded = []
29
32
callbacks_ui_tabs = []
30
33
callbacks_ui_settings = []
@@ -40,6 +43,14 @@ def clear_callbacks():
40
43
callbacks_image_saved .clear ()
41
44
42
45
46
+ def app_started_callback (demo : Blocks , app : FastAPI ):
47
+ for c in callbacks_app_started :
48
+ try :
49
+ c .callback (demo , app )
50
+ except Exception :
51
+ report_exception (c , 'app_started_callback' )
52
+
53
+
43
54
def model_loaded_callback (sd_model ):
44
55
for c in callbacks_model_loaded :
45
56
try :
@@ -91,6 +102,12 @@ def add_callback(callbacks, fun):
91
102
callbacks .append (ScriptCallback (filename , fun ))
92
103
93
104
105
+ def on_app_started (callback ):
106
+ """register a function to be called when the webui started, the gradio `Block` component and
107
+ fastapi `FastAPI` object are passed as the arguments"""
108
+ add_callback (callbacks_app_started , callback )
109
+
110
+
94
111
def on_model_loaded (callback ):
95
112
"""register a function to be called when the stable diffusion model is created; the model is
96
113
passed as an argument"""
0 commit comments