Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
S
Stable Diffusion Webui
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
novelai-storage
Stable Diffusion Webui
Commits
8eb0a972
Commit
8eb0a972
authored
Nov 04, 2022
by
AUTOMATIC1111
Committed by
GitHub
Nov 04, 2022
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #4179 from AUTOMATIC1111/callback-structure
Convert callbacks into a private map, add utility functions
parents
17087e30
5844ef8a
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
41 additions
and
28 deletions
+41
-28
modules/script_callbacks.py
modules/script_callbacks.py
+41
-28
No files found.
modules/script_callbacks.py
View file @
8eb0a972
...
@@ -46,25 +46,23 @@ class CFGDenoiserParams:
...
@@ -46,25 +46,23 @@ class CFGDenoiserParams:
ScriptCallback
=
namedtuple
(
"ScriptCallback"
,
[
"script"
,
"callback"
])
ScriptCallback
=
namedtuple
(
"ScriptCallback"
,
[
"script"
,
"callback"
])
callbacks_app_started
=
[]
callback_map
=
dict
(
callbacks_model_loaded
=
[]
callbacks_app_started
=
[],
callbacks_ui_tabs
=
[]
callbacks_model_loaded
=
[],
callbacks_ui_settings
=
[]
callbacks_ui_tabs
=
[],
callbacks_before_image_saved
=
[]
callbacks_ui_settings
=
[],
callbacks_image_saved
=
[]
callbacks_before_image_saved
=
[],
callbacks_cfg_denoiser
=
[]
callbacks_image_saved
=
[],
callbacks_cfg_denoiser
=
[]
)
def
clear_callbacks
():
def
clear_callbacks
():
callbacks_model_loaded
.
clear
()
for
callback_list
in
callback_map
.
values
():
callbacks_ui_tabs
.
clear
()
callback_list
.
clear
()
callbacks_ui_settings
.
clear
()
callbacks_before_image_saved
.
clear
()
callbacks_image_saved
.
clear
()
callbacks_cfg_denoiser
.
clear
()
def
app_started_callback
(
demo
:
Optional
[
Blocks
],
app
:
FastAPI
):
def
app_started_callback
(
demo
:
Optional
[
Blocks
],
app
:
FastAPI
):
for
c
in
callback
s_app_started
:
for
c
in
callback
_map
[
'callbacks_app_started'
]
:
try
:
try
:
c
.
callback
(
demo
,
app
)
c
.
callback
(
demo
,
app
)
except
Exception
:
except
Exception
:
...
@@ -72,7 +70,7 @@ def app_started_callback(demo: Optional[Blocks], app: FastAPI):
...
@@ -72,7 +70,7 @@ def app_started_callback(demo: Optional[Blocks], app: FastAPI):
def
model_loaded_callback
(
sd_model
):
def
model_loaded_callback
(
sd_model
):
for
c
in
callback
s_model_loaded
:
for
c
in
callback
_map
[
'callbacks_model_loaded'
]
:
try
:
try
:
c
.
callback
(
sd_model
)
c
.
callback
(
sd_model
)
except
Exception
:
except
Exception
:
...
@@ -82,7 +80,7 @@ def model_loaded_callback(sd_model):
...
@@ -82,7 +80,7 @@ def model_loaded_callback(sd_model):
def
ui_tabs_callback
():
def
ui_tabs_callback
():
res
=
[]
res
=
[]
for
c
in
callback
s_ui_tabs
:
for
c
in
callback
_map
[
'callbacks_ui_tabs'
]
:
try
:
try
:
res
+=
c
.
callback
()
or
[]
res
+=
c
.
callback
()
or
[]
except
Exception
:
except
Exception
:
...
@@ -92,7 +90,7 @@ def ui_tabs_callback():
...
@@ -92,7 +90,7 @@ def ui_tabs_callback():
def
ui_settings_callback
():
def
ui_settings_callback
():
for
c
in
callback
s_ui_settings
:
for
c
in
callback
_map
[
'callbacks_ui_settings'
]
:
try
:
try
:
c
.
callback
()
c
.
callback
()
except
Exception
:
except
Exception
:
...
@@ -100,7 +98,7 @@ def ui_settings_callback():
...
@@ -100,7 +98,7 @@ def ui_settings_callback():
def
before_image_saved_callback
(
params
:
ImageSaveParams
):
def
before_image_saved_callback
(
params
:
ImageSaveParams
):
for
c
in
callback
s_before_image_saved
:
for
c
in
callback
_map
[
'callbacks_before_image_saved'
]
:
try
:
try
:
c
.
callback
(
params
)
c
.
callback
(
params
)
except
Exception
:
except
Exception
:
...
@@ -108,7 +106,7 @@ def before_image_saved_callback(params: ImageSaveParams):
...
@@ -108,7 +106,7 @@ def before_image_saved_callback(params: ImageSaveParams):
def
image_saved_callback
(
params
:
ImageSaveParams
):
def
image_saved_callback
(
params
:
ImageSaveParams
):
for
c
in
callback
s_image_saved
:
for
c
in
callback
_map
[
'callbacks_image_saved'
]
:
try
:
try
:
c
.
callback
(
params
)
c
.
callback
(
params
)
except
Exception
:
except
Exception
:
...
@@ -116,7 +114,7 @@ def image_saved_callback(params: ImageSaveParams):
...
@@ -116,7 +114,7 @@ def image_saved_callback(params: ImageSaveParams):
def
cfg_denoiser_callback
(
params
:
CFGDenoiserParams
):
def
cfg_denoiser_callback
(
params
:
CFGDenoiserParams
):
for
c
in
callback
s_cfg_denoiser
:
for
c
in
callback
_map
[
'callbacks_cfg_denoiser'
]
:
try
:
try
:
c
.
callback
(
params
)
c
.
callback
(
params
)
except
Exception
:
except
Exception
:
...
@@ -129,17 +127,33 @@ def add_callback(callbacks, fun):
...
@@ -129,17 +127,33 @@ def add_callback(callbacks, fun):
callbacks
.
append
(
ScriptCallback
(
filename
,
fun
))
callbacks
.
append
(
ScriptCallback
(
filename
,
fun
))
def
remove_current_script_callbacks
():
stack
=
[
x
for
x
in
inspect
.
stack
()
if
x
.
filename
!=
__file__
]
filename
=
stack
[
0
]
.
filename
if
len
(
stack
)
>
0
else
'unknown file'
if
filename
==
'unknown file'
:
return
for
callback_list
in
callback_map
.
values
():
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
):
for
callback_list
in
callback_map
.
values
():
for
callback_to_remove
in
[
cb
for
cb
in
callback_list
if
cb
.
callback
==
callback_func
]:
callback_list
.
remove
(
callback_to_remove
)
def
on_app_started
(
callback
):
def
on_app_started
(
callback
):
"""register a function to be called when the webui started, the gradio `Block` component and
"""register a function to be called when the webui started, the gradio `Block` component and
fastapi `FastAPI` object are passed as the arguments"""
fastapi `FastAPI` object are passed as the arguments"""
add_callback
(
callback
s_app_started
,
callback
)
add_callback
(
callback
_map
[
'callbacks_app_started'
]
,
callback
)
def
on_model_loaded
(
callback
):
def
on_model_loaded
(
callback
):
"""register a function to be called when the stable diffusion model is created; the model is
"""register a function to be called when the stable diffusion model is created; the model is
passed as an argument"""
passed as an argument"""
add_callback
(
callback
s_model_loaded
,
callback
)
add_callback
(
callback
_map
[
'callbacks_model_loaded'
]
,
callback
)
def
on_ui_tabs
(
callback
):
def
on_ui_tabs
(
callback
):
...
@@ -152,13 +166,13 @@ def on_ui_tabs(callback):
...
@@ -152,13 +166,13 @@ def on_ui_tabs(callback):
title is tab text displayed to user in the UI
title is tab text displayed to user in the UI
elem_id is HTML id for the tab
elem_id is HTML id for the tab
"""
"""
add_callback
(
callback
s_ui_tabs
,
callback
)
add_callback
(
callback
_map
[
'callbacks_ui_tabs'
]
,
callback
)
def
on_ui_settings
(
callback
):
def
on_ui_settings
(
callback
):
"""register a function to be called before UI settings are populated; add your settings
"""register a function to be called before UI settings are populated; add your settings
by using shared.opts.add_option(shared.OptionInfo(...)) """
by using shared.opts.add_option(shared.OptionInfo(...)) """
add_callback
(
callback
s_ui_settings
,
callback
)
add_callback
(
callback
_map
[
'callbacks_ui_settings'
]
,
callback
)
def
on_before_image_saved
(
callback
):
def
on_before_image_saved
(
callback
):
...
@@ -166,7 +180,7 @@ def on_before_image_saved(callback):
...
@@ -166,7 +180,7 @@ def on_before_image_saved(callback):
The callback is called with one argument:
The callback is called with one argument:
- params: ImageSaveParams - parameters the image is to be saved with. You can change fields in this object.
- params: ImageSaveParams - parameters the image is to be saved with. You can change fields in this object.
"""
"""
add_callback
(
callback
s_before_image_saved
,
callback
)
add_callback
(
callback
_map
[
'callbacks_before_image_saved'
]
,
callback
)
def
on_image_saved
(
callback
):
def
on_image_saved
(
callback
):
...
@@ -174,7 +188,7 @@ def on_image_saved(callback):
...
@@ -174,7 +188,7 @@ def on_image_saved(callback):
The callback is called with one argument:
The callback is called with one argument:
- params: ImageSaveParams - parameters the image was saved with. Changing fields in this object does nothing.
- params: ImageSaveParams - parameters the image was saved with. Changing fields in this object does nothing.
"""
"""
add_callback
(
callback
s_image_saved
,
callback
)
add_callback
(
callback
_map
[
'callbacks_image_saved'
]
,
callback
)
def
on_cfg_denoiser
(
callback
):
def
on_cfg_denoiser
(
callback
):
...
@@ -182,5 +196,4 @@ def on_cfg_denoiser(callback):
...
@@ -182,5 +196,4 @@ def on_cfg_denoiser(callback):
The callback is called with one argument:
The callback is called with one argument:
- params: CFGDenoiserParams - parameters to be passed to the inner model and sampling state details.
- params: CFGDenoiserParams - parameters to be passed to the inner model and sampling state details.
"""
"""
add_callback
(
callbacks_cfg_denoiser
,
callback
)
add_callback
(
callback_map
[
'callbacks_cfg_denoiser'
],
callback
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment