Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add app started callback #3982

Merged
merged 2 commits into from
Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions modules/script_callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from collections import namedtuple
import inspect

from fastapi import FastAPI
from gradio import Blocks

def report_exception(c, job):
print(f"Error executing callback {job} for {c.script}", file=sys.stderr)
Expand All @@ -25,6 +27,7 @@ def __init__(self, image, p, filename, pnginfo):


ScriptCallback = namedtuple("ScriptCallback", ["script", "callback"])
callbacks_app_started = []
callbacks_model_loaded = []
callbacks_ui_tabs = []
callbacks_ui_settings = []
Expand All @@ -40,6 +43,14 @@ def clear_callbacks():
callbacks_image_saved.clear()


def app_started_callback(demo: Blocks, app: FastAPI):
for c in callbacks_app_started:
try:
c.callback(demo, app)
except Exception:
report_exception(c, 'app_started_callback')


def model_loaded_callback(sd_model):
for c in callbacks_model_loaded:
try:
Expand Down Expand Up @@ -91,6 +102,12 @@ def add_callback(callbacks, fun):
callbacks.append(ScriptCallback(filename, fun))


def on_app_started(callback):
"""register a function to be called when the webui started, the gradio `Block` component and
fastapi `FastAPI` object are passed as the arguments"""
add_callback(callbacks_app_started, callback)


def on_model_loaded(callback):
"""register a function to be called when the stable diffusion model is created; the model is
passed as an argument"""
Expand Down
3 changes: 3 additions & 0 deletions webui.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import modules.sd_models
import modules.shared as shared
import modules.txt2img
import modules.script_callbacks

import modules.ui
from modules import devices
Expand Down Expand Up @@ -135,6 +136,8 @@ def webui():
if (launch_api):
create_api(app)

modules.script_callbacks.app_started_callback(demo, app)

wait_on_server(demo)

sd_samplers.set_samplers()
Expand Down