mirror of
https://github.com/AUTOMATIC1111/stable-diffusion-webui.git
synced 2025-12-06 02:30:30 -08:00
add support for specifying callback order in metadata
This commit is contained in:
parent
7e5e67330b
commit
2f55d669a2
4 changed files with 84 additions and 25 deletions
|
|
@ -8,7 +8,7 @@ from typing import Optional, Any
|
|||
from fastapi import FastAPI
|
||||
from gradio import Blocks
|
||||
|
||||
from modules import errors, timer, extensions, shared
|
||||
from modules import errors, timer, extensions, shared, util
|
||||
|
||||
|
||||
def report_exception(c, job):
|
||||
|
|
@ -149,6 +149,38 @@ def add_callback(callbacks, fun, *, name=None, category='unknown', filename=None
|
|||
|
||||
def sort_callbacks(category, unordered_callbacks, *, enable_user_sort=True):
|
||||
callbacks = unordered_callbacks.copy()
|
||||
callback_lookup = {x.name: x for x in callbacks}
|
||||
dependencies = {}
|
||||
|
||||
order_instructions = {}
|
||||
for extension in extensions.extensions:
|
||||
for order_instruction in extension.metadata.list_callback_order_instructions():
|
||||
if order_instruction.name in callback_lookup:
|
||||
if order_instruction.name not in order_instructions:
|
||||
order_instructions[order_instruction.name] = []
|
||||
|
||||
order_instructions[order_instruction.name].append(order_instruction)
|
||||
|
||||
if order_instructions:
|
||||
for callback in callbacks:
|
||||
dependencies[callback.name] = []
|
||||
|
||||
for callback in callbacks:
|
||||
for order_instruction in order_instructions.get(callback.name, []):
|
||||
for after in order_instruction.after:
|
||||
if after not in callback_lookup:
|
||||
continue
|
||||
|
||||
dependencies[callback.name].append(after)
|
||||
|
||||
for before in order_instruction.before:
|
||||
if before not in callback_lookup:
|
||||
continue
|
||||
|
||||
dependencies[before].append(callback.name)
|
||||
|
||||
sorted_names = util.topological_sort(dependencies)
|
||||
callbacks = [callback_lookup[x] for x in sorted_names]
|
||||
|
||||
if enable_user_sort:
|
||||
for name in reversed(getattr(shared.opts, 'prioritized_callbacks_' + category, [])):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue