2022-09-03 03:08:45 -06:00
|
|
|
import os
|
2023-01-05 02:08:11 -07:00
|
|
|
import re
|
2022-09-03 03:08:45 -06:00
|
|
|
import sys
|
2023-06-16 13:17:17 -06:00
|
|
|
import inspect
|
2022-10-22 03:23:45 -06:00
|
|
|
from collections import namedtuple
|
2023-08-12 08:46:13 -06:00
|
|
|
from dataclasses import dataclass
|
2022-09-03 03:08:45 -06:00
|
|
|
|
|
|
|
import gradio as gr
|
|
|
|
|
2023-05-31 23:42:50 -06:00
|
|
|
from modules import shared, paths, script_callbacks, extensions, script_loading, scripts_postprocessing, errors, timer
|
2022-10-22 03:23:45 -06:00
|
|
|
|
|
|
|
AlwaysVisible = object()
|
|
|
|
|
2022-09-03 08:21:15 -06:00
|
|
|
|
2023-01-26 13:29:27 -07:00
|
|
|
class PostprocessImageArgs:
|
|
|
|
def __init__(self, image):
|
|
|
|
self.image = image
|
|
|
|
|
|
|
|
|
2023-07-24 11:52:24 -06:00
|
|
|
class PostprocessBatchListArgs:
|
|
|
|
def __init__(self, images):
|
|
|
|
self.images = images
|
|
|
|
|
|
|
|
|
2023-08-12 08:46:13 -06:00
|
|
|
@dataclass
|
|
|
|
class OnComponent:
|
|
|
|
component: gr.blocks.Block
|
|
|
|
|
|
|
|
|
2022-09-03 03:08:45 -06:00
|
|
|
class Script:
|
2023-05-17 13:43:24 -06:00
|
|
|
name = None
|
|
|
|
"""script's internal name derived from title"""
|
|
|
|
|
2023-05-31 13:40:09 -06:00
|
|
|
section = None
|
|
|
|
"""name of UI section that the script's controls will be placed into"""
|
|
|
|
|
2022-09-03 03:08:45 -06:00
|
|
|
filename = None
|
2022-09-03 08:21:15 -06:00
|
|
|
args_from = None
|
|
|
|
args_to = None
|
2022-10-22 03:23:45 -06:00
|
|
|
alwayson = False
|
|
|
|
|
2022-11-19 09:10:17 -07:00
|
|
|
is_txt2img = False
|
|
|
|
is_img2img = False
|
2023-08-12 08:46:13 -06:00
|
|
|
tabname = None
|
2022-11-19 09:10:17 -07:00
|
|
|
|
2022-11-03 23:38:11 -06:00
|
|
|
group = None
|
2023-08-12 03:39:59 -06:00
|
|
|
"""A gr.Group component that has all script's UI inside it."""
|
|
|
|
|
|
|
|
create_group = True
|
|
|
|
"""If False, for alwayson scripts, a group component will not be created."""
|
2022-11-03 23:38:11 -06:00
|
|
|
|
2022-10-22 03:23:45 -06:00
|
|
|
infotext_fields = None
|
|
|
|
"""if set in ui(), this is a list of pairs of gradio component + text; the text will be used when
|
|
|
|
parsing infotext to set the value for the component; see ui.py's txt2img_paste_fields for an example
|
|
|
|
"""
|
2022-09-03 03:08:45 -06:00
|
|
|
|
2023-02-14 04:55:42 -07:00
|
|
|
paste_field_names = None
|
|
|
|
"""if set in ui(), this is a list of names of infotext fields; the fields will be sent through the
|
|
|
|
various "Send to <X>" buttons when clicked
|
|
|
|
"""
|
|
|
|
|
2023-05-17 13:43:24 -06:00
|
|
|
api_info = None
|
|
|
|
"""Generated value of type modules.api.models.ScriptInfo with information about the script for API"""
|
|
|
|
|
2023-08-12 09:36:30 -06:00
|
|
|
on_before_component_elem_id = None
|
2023-08-12 08:46:13 -06:00
|
|
|
"""list of callbacks to be called before a component with an elem_id is created"""
|
|
|
|
|
2023-08-12 09:36:30 -06:00
|
|
|
on_after_component_elem_id = None
|
2023-08-12 08:46:13 -06:00
|
|
|
"""list of callbacks to be called after a component with an elem_id is created"""
|
|
|
|
|
2023-08-14 01:43:18 -06:00
|
|
|
setup_for_ui_only = False
|
|
|
|
"""If true, the script setup will only be run in Gradio UI, not in API"""
|
|
|
|
|
2022-09-03 03:08:45 -06:00
|
|
|
def title(self):
|
2022-10-22 03:23:45 -06:00
|
|
|
"""this function should return the title of the script. This is what will be displayed in the dropdown menu."""
|
|
|
|
|
2022-09-03 03:08:45 -06:00
|
|
|
raise NotImplementedError()
|
|
|
|
|
2022-09-03 08:21:15 -06:00
|
|
|
def ui(self, is_img2img):
|
2022-10-22 03:23:45 -06:00
|
|
|
"""this function should create gradio UI elements. See https://gradio.app/docs/#components
|
|
|
|
The return value should be an array of all components that are used in processing.
|
2022-12-14 19:01:32 -07:00
|
|
|
Values of those returned components will be passed to run() and process() functions.
|
2022-10-22 03:23:45 -06:00
|
|
|
"""
|
|
|
|
|
2022-09-03 08:21:15 -06:00
|
|
|
pass
|
|
|
|
|
2022-09-03 16:29:43 -06:00
|
|
|
def show(self, is_img2img):
|
2022-10-22 03:23:45 -06:00
|
|
|
"""
|
|
|
|
is_img2img is True if this function is called for the img2img interface, and Fasle otherwise
|
|
|
|
|
|
|
|
This function should return:
|
|
|
|
- False if the script should not be shown in UI at all
|
2022-12-14 19:01:32 -07:00
|
|
|
- True if the script should be shown in UI if it's selected in the scripts dropdown
|
2022-10-22 03:23:45 -06:00
|
|
|
- script.AlwaysVisible if the script should be shown in UI at all times
|
|
|
|
"""
|
|
|
|
|
2022-09-03 16:29:43 -06:00
|
|
|
return True
|
|
|
|
|
2022-10-22 03:23:45 -06:00
|
|
|
def run(self, p, *args):
|
|
|
|
"""
|
|
|
|
This function is called if the script has been selected in the script dropdown.
|
|
|
|
It must do all processing and return the Processed object with results, same as
|
|
|
|
one returned by processing.process_images.
|
|
|
|
|
|
|
|
Usually the processing is done by calling the processing.process_images function.
|
|
|
|
|
|
|
|
args contains all values returned by components from ui()
|
|
|
|
"""
|
|
|
|
|
2023-01-26 13:29:27 -07:00
|
|
|
pass
|
2022-09-03 08:21:15 -06:00
|
|
|
|
2023-08-13 08:31:10 -06:00
|
|
|
def setup(self, p, *args):
|
|
|
|
"""For AlwaysVisible scripts, this function is called when the processing object is set up, before any processing starts.
|
|
|
|
args contains all values returned by components from ui().
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2023-05-31 13:40:09 -06:00
|
|
|
def before_process(self, p, *args):
|
|
|
|
"""
|
2023-08-13 08:31:10 -06:00
|
|
|
This function is called very early during processing begins for AlwaysVisible scripts.
|
2023-05-31 13:40:09 -06:00
|
|
|
You can modify the processing object (p) here, inject hooks, etc.
|
|
|
|
args contains all values returned by components from ui()
|
|
|
|
"""
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
2022-10-22 03:23:45 -06:00
|
|
|
def process(self, p, *args):
|
|
|
|
"""
|
|
|
|
This function is called before processing begins for AlwaysVisible scripts.
|
2022-10-29 13:20:02 -06:00
|
|
|
You can modify the processing object (p) here, inject hooks, etc.
|
|
|
|
args contains all values returned by components from ui()
|
|
|
|
"""
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
2023-02-22 02:52:53 -07:00
|
|
|
def before_process_batch(self, p, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Called before extra networks are parsed from the prompt, so you can add
|
|
|
|
new extra network keywords to the prompt with this callback.
|
|
|
|
|
|
|
|
**kwargs will have those items:
|
|
|
|
- batch_number - index of current batch, from 0 to number of batches-1
|
|
|
|
- prompts - list of prompts for current batch; you can change contents of this list but changing the number of entries will likely break things
|
|
|
|
- seeds - list of seeds for current batch
|
|
|
|
- subseeds - list of subseeds for current batch
|
|
|
|
"""
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
2023-06-19 00:37:20 -06:00
|
|
|
def after_extra_networks_activate(self, p, *args, **kwargs):
|
|
|
|
"""
|
2023-07-21 07:56:41 -06:00
|
|
|
Called after extra networks activation, before conds calculation
|
2023-06-19 00:37:20 -06:00
|
|
|
allow modification of the network after extra networks activation been applied
|
|
|
|
won't be call if p.disable_extra_networks
|
|
|
|
|
|
|
|
**kwargs will have those items:
|
|
|
|
- batch_number - index of current batch, from 0 to number of batches-1
|
|
|
|
- prompts - list of prompts for current batch; you can change contents of this list but changing the number of entries will likely break things
|
|
|
|
- seeds - list of seeds for current batch
|
|
|
|
- subseeds - list of subseeds for current batch
|
|
|
|
- extra_network_data - list of ExtraNetworkParams for current stage
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
2022-11-04 02:21:40 -06:00
|
|
|
def process_batch(self, p, *args, **kwargs):
|
2022-11-02 10:05:01 -06:00
|
|
|
"""
|
2022-11-04 02:21:40 -06:00
|
|
|
Same as process(), but called for every batch.
|
|
|
|
|
|
|
|
**kwargs will have those items:
|
|
|
|
- batch_number - index of current batch, from 0 to number of batches-1
|
|
|
|
- prompts - list of prompts for current batch; you can change contents of this list but changing the number of entries will likely break things
|
|
|
|
- seeds - list of seeds for current batch
|
|
|
|
- subseeds - list of subseeds for current batch
|
2022-11-02 10:05:01 -06:00
|
|
|
"""
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
2022-12-10 04:54:02 -07:00
|
|
|
def postprocess_batch(self, p, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Same as process_batch(), but called for every batch after it has been generated.
|
|
|
|
|
|
|
|
**kwargs will have same items as process_batch, and also:
|
|
|
|
- batch_number - index of current batch, from 0 to number of batches-1
|
|
|
|
- images - torch tensor with all generated images, with values ranging from 0 to 1;
|
|
|
|
"""
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
2023-07-24 11:52:24 -06:00
|
|
|
def postprocess_batch_list(self, p, pp: PostprocessBatchListArgs, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Same as postprocess_batch(), but receives batch images as a list of 3D tensors instead of a 4D tensor.
|
|
|
|
This is useful when you want to update the entire batch instead of individual images.
|
|
|
|
|
|
|
|
You can modify the postprocessing object (pp) to update the images in the batch, remove images, add images, etc.
|
|
|
|
If the number of images is different from the batch size when returning,
|
|
|
|
then the script has the responsibility to also update the following attributes in the processing object (p):
|
|
|
|
- p.prompts
|
|
|
|
- p.negative_prompts
|
|
|
|
- p.seeds
|
|
|
|
- p.subseeds
|
|
|
|
|
|
|
|
**kwargs will have same items as process_batch, and also:
|
|
|
|
- batch_number - index of current batch, from 0 to number of batches-1
|
|
|
|
"""
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
2023-01-26 13:29:27 -07:00
|
|
|
def postprocess_image(self, p, pp: PostprocessImageArgs, *args):
|
|
|
|
"""
|
|
|
|
Called for every image after it has been generated.
|
|
|
|
"""
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
2022-10-29 13:20:02 -06:00
|
|
|
def postprocess(self, p, processed, *args):
|
|
|
|
"""
|
|
|
|
This function is called after processing ends for AlwaysVisible scripts.
|
|
|
|
args contains all values returned by components from ui()
|
2022-10-22 03:23:45 -06:00
|
|
|
"""
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
2022-11-19 09:10:17 -07:00
|
|
|
def before_component(self, component, **kwargs):
|
|
|
|
"""
|
|
|
|
Called before a component is created.
|
|
|
|
Use elem_id/label fields of kwargs to figure out which component it is.
|
|
|
|
This can be useful to inject your own components somewhere in the middle of vanilla UI.
|
|
|
|
You can return created components in the ui() function to add them to the list of arguments for your processing functions
|
|
|
|
"""
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
def after_component(self, component, **kwargs):
|
|
|
|
"""
|
|
|
|
Called after a component is created. Same as above.
|
|
|
|
"""
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
2023-08-12 08:46:13 -06:00
|
|
|
def on_before_component(self, callback, *, elem_id):
|
|
|
|
"""
|
|
|
|
Calls callback before a component is created. The callback function is called with a single argument of type OnComponent.
|
|
|
|
|
2023-08-13 23:49:39 -06:00
|
|
|
May be called in show() or ui() - but it may be too late in latter as some components may already be created.
|
|
|
|
|
2023-08-12 08:46:13 -06:00
|
|
|
This function is an alternative to before_component in that it also cllows to run before a component is created, but
|
|
|
|
it doesn't require to be called for every created component - just for the one you need.
|
|
|
|
"""
|
2023-08-12 09:36:30 -06:00
|
|
|
if self.on_before_component_elem_id is None:
|
|
|
|
self.on_before_component_elem_id = []
|
2023-08-12 08:46:13 -06:00
|
|
|
|
|
|
|
self.on_before_component_elem_id.append((elem_id, callback))
|
|
|
|
|
|
|
|
def on_after_component(self, callback, *, elem_id):
|
|
|
|
"""
|
|
|
|
Calls callback after a component is created. The callback function is called with a single argument of type OnComponent.
|
|
|
|
"""
|
2023-08-12 09:36:30 -06:00
|
|
|
if self.on_after_component_elem_id is None:
|
|
|
|
self.on_after_component_elem_id = []
|
2023-08-12 08:46:13 -06:00
|
|
|
|
|
|
|
self.on_after_component_elem_id.append((elem_id, callback))
|
|
|
|
|
2022-09-03 08:21:15 -06:00
|
|
|
def describe(self):
|
2022-10-22 03:23:45 -06:00
|
|
|
"""unused"""
|
2022-09-03 08:21:15 -06:00
|
|
|
return ""
|
|
|
|
|
2023-01-05 02:08:11 -07:00
|
|
|
def elem_id(self, item_id):
|
|
|
|
"""helper function to generate id for a HTML element, constructs final id out of script name, tab and user-supplied item_id"""
|
|
|
|
|
|
|
|
need_tabname = self.show(True) == self.show(False)
|
2023-05-09 13:17:58 -06:00
|
|
|
tabkind = 'img2img' if self.is_img2img else 'txt2txt'
|
|
|
|
tabname = f"{tabkind}_" if need_tabname else ""
|
2023-01-05 02:08:11 -07:00
|
|
|
title = re.sub(r'[^a-z_0-9]', '', re.sub(r'\s', '_', self.title().lower()))
|
|
|
|
|
|
|
|
return f'script_{tabname}{title}_{item_id}'
|
|
|
|
|
2023-07-08 05:44:02 -06:00
|
|
|
def before_hr(self, p, *args):
|
2023-06-28 03:37:08 -06:00
|
|
|
"""
|
|
|
|
This function is called before hires fix start.
|
|
|
|
"""
|
|
|
|
pass
|
2022-09-03 03:08:45 -06:00
|
|
|
|
2023-08-12 03:39:59 -06:00
|
|
|
|
2023-08-14 01:43:18 -06:00
|
|
|
class ScriptBuiltinUI(Script):
|
|
|
|
setup_for_ui_only = True
|
2023-08-12 08:46:13 -06:00
|
|
|
|
|
|
|
def elem_id(self, item_id):
|
|
|
|
"""helper function to generate id for a HTML element, constructs final id out of tab and user-supplied item_id"""
|
|
|
|
|
|
|
|
need_tabname = self.show(True) == self.show(False)
|
|
|
|
tabname = ('img2img' if self.is_img2img else 'txt2txt') + "_" if need_tabname else ""
|
|
|
|
|
|
|
|
return f'{tabname}{item_id}'
|
|
|
|
|
|
|
|
|
2022-10-22 03:23:45 -06:00
|
|
|
current_basedir = paths.script_path
|
|
|
|
|
|
|
|
|
|
|
|
def basedir():
|
|
|
|
"""returns the base directory for the current script. For scripts in the main scripts directory,
|
|
|
|
this is the main directory (where webui.py resides), and for scripts in extensions directory
|
|
|
|
(ie extensions/aesthetic/script/aesthetic.py), this is extension's directory (extensions/aesthetic)
|
|
|
|
"""
|
|
|
|
return current_basedir
|
|
|
|
|
|
|
|
|
|
|
|
ScriptFile = namedtuple("ScriptFile", ["basedir", "filename", "path"])
|
2023-01-22 23:24:43 -07:00
|
|
|
|
|
|
|
scripts_data = []
|
|
|
|
postprocessing_scripts_data = []
|
2023-01-10 21:30:09 -07:00
|
|
|
ScriptClassData = namedtuple("ScriptClassData", ["script_class", "path", "basedir", "module"])
|
2022-10-22 03:23:45 -06:00
|
|
|
|
|
|
|
|
2023-08-12 03:39:59 -06:00
|
|
|
def list_scripts(scriptdirname, extension, *, include_extensions=True):
|
2022-10-22 03:23:45 -06:00
|
|
|
scripts_list = []
|
|
|
|
|
|
|
|
basedir = os.path.join(paths.script_path, scriptdirname)
|
|
|
|
if os.path.exists(basedir):
|
|
|
|
for filename in sorted(os.listdir(basedir)):
|
|
|
|
scripts_list.append(ScriptFile(paths.script_path, filename, os.path.join(basedir, filename)))
|
|
|
|
|
2023-08-12 03:39:59 -06:00
|
|
|
if include_extensions:
|
|
|
|
for ext in extensions.active():
|
|
|
|
scripts_list += ext.list_files(scriptdirname, extension)
|
2022-09-03 03:08:45 -06:00
|
|
|
|
2022-10-22 03:23:45 -06:00
|
|
|
scripts_list = [x for x in scripts_list if os.path.splitext(x.path)[1].lower() == extension and os.path.isfile(x.path)]
|
2022-09-03 10:09:14 -06:00
|
|
|
|
2022-10-22 03:23:45 -06:00
|
|
|
return scripts_list
|
2022-09-03 03:08:45 -06:00
|
|
|
|
2022-10-16 22:56:08 -06:00
|
|
|
|
2022-10-22 04:34:49 -06:00
|
|
|
def list_files_with_name(filename):
|
|
|
|
res = []
|
|
|
|
|
2022-10-31 08:36:45 -06:00
|
|
|
dirs = [paths.script_path] + [ext.path for ext in extensions.active()]
|
2022-10-22 04:34:49 -06:00
|
|
|
|
|
|
|
for dirpath in dirs:
|
|
|
|
if not os.path.isdir(dirpath):
|
|
|
|
continue
|
|
|
|
|
|
|
|
path = os.path.join(dirpath, filename)
|
2022-11-09 19:34:03 -07:00
|
|
|
if os.path.isfile(path):
|
2022-10-22 04:34:49 -06:00
|
|
|
res.append(path)
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
2022-10-22 03:23:45 -06:00
|
|
|
def load_scripts():
|
|
|
|
global current_basedir
|
|
|
|
scripts_data.clear()
|
2023-01-22 23:24:43 -07:00
|
|
|
postprocessing_scripts_data.clear()
|
2022-10-22 03:23:45 -06:00
|
|
|
script_callbacks.clear_callbacks()
|
|
|
|
|
2023-08-12 03:39:59 -06:00
|
|
|
scripts_list = list_scripts("scripts", ".py") + list_scripts("modules/processing_scripts", ".py", include_extensions=False)
|
2022-10-22 03:23:45 -06:00
|
|
|
|
|
|
|
syspath = sys.path
|
2022-09-03 03:08:45 -06:00
|
|
|
|
2023-01-22 23:24:43 -07:00
|
|
|
def register_scripts_from_module(module):
|
2023-05-10 02:37:18 -06:00
|
|
|
for script_class in module.__dict__.values():
|
2023-06-16 13:17:17 -06:00
|
|
|
if not inspect.isclass(script_class):
|
2023-01-22 23:24:43 -07:00
|
|
|
continue
|
|
|
|
|
|
|
|
if issubclass(script_class, Script):
|
|
|
|
scripts_data.append(ScriptClassData(script_class, scriptfile.path, scriptfile.basedir, module))
|
|
|
|
elif issubclass(script_class, scripts_postprocessing.ScriptPostprocessing):
|
|
|
|
postprocessing_scripts_data.append(ScriptClassData(script_class, scriptfile.path, scriptfile.basedir, module))
|
|
|
|
|
2023-03-22 03:24:11 -06:00
|
|
|
def orderby(basedir):
|
|
|
|
# 1st webui, 2nd extensions-builtin, 3rd extensions
|
|
|
|
priority = {os.path.join(paths.script_path, "extensions-builtin"):1, paths.script_path:0}
|
|
|
|
for key in priority:
|
|
|
|
if basedir.startswith(key):
|
|
|
|
return priority[key]
|
|
|
|
return 9999
|
|
|
|
|
2023-03-22 10:28:09 -06:00
|
|
|
for scriptfile in sorted(scripts_list, key=lambda x: [orderby(x.basedir), x]):
|
2022-09-03 10:59:11 -06:00
|
|
|
try:
|
2022-10-22 03:23:45 -06:00
|
|
|
if scriptfile.basedir != paths.script_path:
|
|
|
|
sys.path = [scriptfile.basedir] + sys.path
|
|
|
|
current_basedir = scriptfile.basedir
|
|
|
|
|
2023-01-22 23:24:43 -07:00
|
|
|
script_module = script_loading.load_module(scriptfile.path)
|
|
|
|
register_scripts_from_module(script_module)
|
2022-09-03 10:59:11 -06:00
|
|
|
|
|
|
|
except Exception:
|
2023-05-31 10:56:37 -06:00
|
|
|
errors.report(f"Error loading script: {scriptfile.filename}", exc_info=True)
|
2022-09-03 03:08:45 -06:00
|
|
|
|
2022-10-22 03:23:45 -06:00
|
|
|
finally:
|
|
|
|
sys.path = syspath
|
|
|
|
current_basedir = paths.script_path
|
2023-05-20 15:41:41 -06:00
|
|
|
timer.startup_timer.record(scriptfile.filename)
|
2022-10-22 03:23:45 -06:00
|
|
|
|
2023-05-19 06:49:36 -06:00
|
|
|
global scripts_txt2img, scripts_img2img, scripts_postproc
|
|
|
|
|
|
|
|
scripts_txt2img = ScriptRunner()
|
|
|
|
scripts_img2img = ScriptRunner()
|
|
|
|
scripts_postproc = scripts_postprocessing.ScriptPostprocessingRunner()
|
|
|
|
|
2022-09-03 03:08:45 -06:00
|
|
|
|
|
|
|
def wrap_call(func, filename, funcname, *args, default=None, **kwargs):
|
|
|
|
try:
|
2023-05-28 23:54:13 -06:00
|
|
|
return func(*args, **kwargs)
|
2022-09-03 03:08:45 -06:00
|
|
|
except Exception:
|
2023-05-31 10:56:37 -06:00
|
|
|
errors.report(f"Error calling: {filename}/{funcname}", exc_info=True)
|
2022-09-03 03:08:45 -06:00
|
|
|
|
|
|
|
return default
|
|
|
|
|
|
|
|
|
2022-09-03 16:29:43 -06:00
|
|
|
class ScriptRunner:
|
|
|
|
def __init__(self):
|
|
|
|
self.scripts = []
|
2022-10-22 03:23:45 -06:00
|
|
|
self.selectable_scripts = []
|
|
|
|
self.alwayson_scripts = []
|
2022-10-18 02:29:42 -06:00
|
|
|
self.titles = []
|
2023-08-12 08:46:13 -06:00
|
|
|
self.title_map = {}
|
2022-10-22 03:23:45 -06:00
|
|
|
self.infotext_fields = []
|
2023-02-14 04:55:42 -07:00
|
|
|
self.paste_field_names = []
|
2023-05-31 13:40:09 -06:00
|
|
|
self.inputs = [None]
|
2022-09-03 16:29:43 -06:00
|
|
|
|
2023-08-12 08:46:13 -06:00
|
|
|
self.on_before_component_elem_id = {}
|
|
|
|
"""dict of callbacks to be called before an element is created; key=elem_id, value=list of callbacks"""
|
|
|
|
|
|
|
|
self.on_after_component_elem_id = {}
|
|
|
|
"""dict of callbacks to be called after an element is created; key=elem_id, value=list of callbacks"""
|
|
|
|
|
2022-11-19 09:10:17 -07:00
|
|
|
def initialize_scripts(self, is_img2img):
|
2023-01-26 13:29:27 -07:00
|
|
|
from modules import scripts_auto_postprocessing
|
|
|
|
|
2022-11-19 09:10:17 -07:00
|
|
|
self.scripts.clear()
|
|
|
|
self.alwayson_scripts.clear()
|
|
|
|
self.selectable_scripts.clear()
|
|
|
|
|
2023-01-26 13:29:27 -07:00
|
|
|
auto_processing_scripts = scripts_auto_postprocessing.create_auto_preprocessing_script_data()
|
|
|
|
|
2023-05-10 02:37:18 -06:00
|
|
|
for script_data in auto_processing_scripts + scripts_data:
|
|
|
|
script = script_data.script_class()
|
|
|
|
script.filename = script_data.path
|
2022-11-19 09:10:17 -07:00
|
|
|
script.is_txt2img = not is_img2img
|
|
|
|
script.is_img2img = is_img2img
|
2023-08-12 08:46:13 -06:00
|
|
|
script.tabname = "img2img" if is_img2img else "txt2img"
|
2022-09-03 16:29:43 -06:00
|
|
|
|
2022-11-19 09:10:17 -07:00
|
|
|
visibility = script.show(script.is_img2img)
|
2022-09-03 16:29:43 -06:00
|
|
|
|
2022-10-22 03:23:45 -06:00
|
|
|
if visibility == AlwaysVisible:
|
|
|
|
self.scripts.append(script)
|
|
|
|
self.alwayson_scripts.append(script)
|
|
|
|
script.alwayson = True
|
2022-09-03 16:29:43 -06:00
|
|
|
|
2022-10-22 03:23:45 -06:00
|
|
|
elif visibility:
|
|
|
|
self.scripts.append(script)
|
|
|
|
self.selectable_scripts.append(script)
|
2022-09-03 16:29:43 -06:00
|
|
|
|
2023-08-13 23:49:39 -06:00
|
|
|
self.apply_on_before_component_callbacks()
|
|
|
|
|
|
|
|
def apply_on_before_component_callbacks(self):
|
|
|
|
for script in self.scripts:
|
|
|
|
on_before = script.on_before_component_elem_id or []
|
|
|
|
on_after = script.on_after_component_elem_id or []
|
|
|
|
|
|
|
|
for elem_id, callback in on_before:
|
|
|
|
if elem_id not in self.on_before_component_elem_id:
|
|
|
|
self.on_before_component_elem_id[elem_id] = []
|
|
|
|
|
|
|
|
self.on_before_component_elem_id[elem_id].append((callback, script))
|
|
|
|
|
|
|
|
for elem_id, callback in on_after:
|
|
|
|
if elem_id not in self.on_after_component_elem_id:
|
|
|
|
self.on_after_component_elem_id[elem_id] = []
|
|
|
|
|
|
|
|
self.on_after_component_elem_id[elem_id].append((callback, script))
|
|
|
|
|
|
|
|
on_before.clear()
|
|
|
|
on_after.clear()
|
|
|
|
|
2023-05-31 13:40:09 -06:00
|
|
|
def create_script_ui(self, script):
|
2023-05-17 13:43:24 -06:00
|
|
|
import modules.api.models as api_models
|
|
|
|
|
2023-05-31 13:40:09 -06:00
|
|
|
script.args_from = len(self.inputs)
|
|
|
|
script.args_to = len(self.inputs)
|
2022-10-22 03:23:45 -06:00
|
|
|
|
2023-05-31 13:40:09 -06:00
|
|
|
controls = wrap_call(script.ui, script.filename, "ui", script.is_img2img)
|
2022-09-03 16:29:43 -06:00
|
|
|
|
2023-05-31 13:40:09 -06:00
|
|
|
if controls is None:
|
|
|
|
return
|
2022-09-03 16:29:43 -06:00
|
|
|
|
2023-05-31 13:40:09 -06:00
|
|
|
script.name = wrap_call(script.title, script.filename, "title", default=script.filename).lower()
|
|
|
|
api_args = []
|
2022-09-03 16:29:43 -06:00
|
|
|
|
2023-05-31 13:40:09 -06:00
|
|
|
for control in controls:
|
|
|
|
control.custom_script_source = os.path.basename(script.filename)
|
2022-09-03 08:21:15 -06:00
|
|
|
|
2023-05-31 13:40:09 -06:00
|
|
|
arg_info = api_models.ScriptArg(label=control.label or "")
|
2023-05-17 13:43:24 -06:00
|
|
|
|
2023-05-31 13:40:09 -06:00
|
|
|
for field in ("value", "minimum", "maximum", "step", "choices"):
|
|
|
|
v = getattr(control, field, None)
|
|
|
|
if v is not None:
|
|
|
|
setattr(arg_info, field, v)
|
2022-10-22 03:23:45 -06:00
|
|
|
|
2023-05-31 13:40:09 -06:00
|
|
|
api_args.append(arg_info)
|
2023-05-17 13:43:24 -06:00
|
|
|
|
2023-05-31 13:40:09 -06:00
|
|
|
script.api_info = api_models.ScriptInfo(
|
|
|
|
name=script.name,
|
|
|
|
is_img2img=script.is_img2img,
|
|
|
|
is_alwayson=script.alwayson,
|
|
|
|
args=api_args,
|
|
|
|
)
|
2023-05-17 13:43:24 -06:00
|
|
|
|
2023-05-31 13:40:09 -06:00
|
|
|
if script.infotext_fields is not None:
|
|
|
|
self.infotext_fields += script.infotext_fields
|
2023-05-17 13:43:24 -06:00
|
|
|
|
2023-05-31 13:40:09 -06:00
|
|
|
if script.paste_field_names is not None:
|
|
|
|
self.paste_field_names += script.paste_field_names
|
2023-05-17 13:43:24 -06:00
|
|
|
|
2023-05-31 13:40:09 -06:00
|
|
|
self.inputs += controls
|
|
|
|
script.args_to = len(self.inputs)
|
2022-09-03 08:21:15 -06:00
|
|
|
|
2023-05-31 13:40:09 -06:00
|
|
|
def setup_ui_for_section(self, section, scriptlist=None):
|
|
|
|
if scriptlist is None:
|
|
|
|
scriptlist = self.alwayson_scripts
|
2023-02-14 04:55:42 -07:00
|
|
|
|
2023-05-31 13:40:09 -06:00
|
|
|
for script in scriptlist:
|
|
|
|
if script.alwayson and script.section != section:
|
|
|
|
continue
|
2022-09-03 08:21:15 -06:00
|
|
|
|
2023-08-12 03:39:59 -06:00
|
|
|
if script.create_group:
|
|
|
|
with gr.Group(visible=script.alwayson) as group:
|
|
|
|
self.create_script_ui(script)
|
2022-10-22 03:23:45 -06:00
|
|
|
|
2023-08-12 03:39:59 -06:00
|
|
|
script.group = group
|
|
|
|
else:
|
|
|
|
self.create_script_ui(script)
|
2022-11-03 23:38:11 -06:00
|
|
|
|
2023-05-31 13:40:09 -06:00
|
|
|
def prepare_ui(self):
|
|
|
|
self.inputs = [None]
|
2022-10-22 03:23:45 -06:00
|
|
|
|
2023-05-31 13:40:09 -06:00
|
|
|
def setup_ui(self):
|
2023-08-12 08:46:13 -06:00
|
|
|
all_titles = [wrap_call(script.title, script.filename, "title") or script.filename for script in self.scripts]
|
|
|
|
self.title_map = {title.lower(): script for title, script in zip(all_titles, self.scripts)}
|
2023-05-31 13:40:09 -06:00
|
|
|
self.titles = [wrap_call(script.title, script.filename, "title") or f"{script.filename} [error]" for script in self.selectable_scripts]
|
2022-11-03 23:38:11 -06:00
|
|
|
|
2023-05-31 13:40:09 -06:00
|
|
|
self.setup_ui_for_section(None)
|
|
|
|
|
|
|
|
dropdown = gr.Dropdown(label="Script", elem_id="script_list", choices=["None"] + self.titles, value="None", type="index")
|
|
|
|
self.inputs[0] = dropdown
|
|
|
|
|
|
|
|
self.setup_ui_for_section(None, self.selectable_scripts)
|
2022-10-22 03:23:45 -06:00
|
|
|
|
2022-09-03 16:29:43 -06:00
|
|
|
def select_script(script_index):
|
2022-11-03 23:38:11 -06:00
|
|
|
selected_script = self.selectable_scripts[script_index - 1] if script_index>0 else None
|
2022-09-03 08:21:15 -06:00
|
|
|
|
2022-11-03 23:38:11 -06:00
|
|
|
return [gr.update(visible=selected_script == s) for s in self.selectable_scripts]
|
2022-09-03 08:21:15 -06:00
|
|
|
|
2022-10-18 02:29:42 -06:00
|
|
|
def init_field(title):
|
2022-11-03 23:38:11 -06:00
|
|
|
"""called when an initial value is set from ui-config.json to show script's UI components"""
|
|
|
|
|
2022-10-18 03:52:27 -06:00
|
|
|
if title == 'None':
|
2022-10-18 02:29:42 -06:00
|
|
|
return
|
2022-11-03 23:38:11 -06:00
|
|
|
|
2022-10-18 02:29:42 -06:00
|
|
|
script_index = self.titles.index(title)
|
2022-11-03 23:38:11 -06:00
|
|
|
self.selectable_scripts[script_index].group.visible = True
|
2022-10-18 02:29:42 -06:00
|
|
|
|
|
|
|
dropdown.init_field = init_field
|
2022-11-03 23:38:11 -06:00
|
|
|
|
2022-09-03 16:29:43 -06:00
|
|
|
dropdown.change(
|
|
|
|
fn=select_script,
|
|
|
|
inputs=[dropdown],
|
2022-11-03 23:38:11 -06:00
|
|
|
outputs=[script.group for script in self.selectable_scripts]
|
2022-09-03 16:29:43 -06:00
|
|
|
)
|
2022-09-03 08:21:15 -06:00
|
|
|
|
2023-01-25 22:18:41 -07:00
|
|
|
self.script_load_ctr = 0
|
2023-05-31 13:40:09 -06:00
|
|
|
|
2023-01-25 22:18:41 -07:00
|
|
|
def onload_script_visibility(params):
|
|
|
|
title = params.get('Script', None)
|
|
|
|
if title:
|
|
|
|
title_index = self.titles.index(title)
|
|
|
|
visibility = title_index == self.script_load_ctr
|
|
|
|
self.script_load_ctr = (self.script_load_ctr + 1) % len(self.titles)
|
|
|
|
return gr.update(visible=visibility)
|
|
|
|
else:
|
|
|
|
return gr.update(visible=False)
|
|
|
|
|
2023-05-31 13:40:09 -06:00
|
|
|
self.infotext_fields.append((dropdown, lambda x: gr.update(value=x.get('Script', 'None'))))
|
|
|
|
self.infotext_fields.extend([(script.group, onload_script_visibility) for script in self.selectable_scripts])
|
2023-01-25 22:18:41 -07:00
|
|
|
|
2023-08-13 23:49:39 -06:00
|
|
|
self.apply_on_before_component_callbacks()
|
2023-08-12 08:46:13 -06:00
|
|
|
|
2023-05-31 13:40:09 -06:00
|
|
|
return self.inputs
|
2022-09-03 08:21:15 -06:00
|
|
|
|
2023-01-26 13:29:27 -07:00
|
|
|
def run(self, p, *args):
|
2022-09-03 16:29:43 -06:00
|
|
|
script_index = args[0]
|
2022-09-03 08:21:15 -06:00
|
|
|
|
2022-09-03 16:29:43 -06:00
|
|
|
if script_index == 0:
|
|
|
|
return None
|
2022-09-03 08:21:15 -06:00
|
|
|
|
2022-10-22 03:23:45 -06:00
|
|
|
script = self.selectable_scripts[script_index-1]
|
2022-09-03 08:21:15 -06:00
|
|
|
|
2022-09-03 16:29:43 -06:00
|
|
|
if script is None:
|
|
|
|
return None
|
2022-09-03 08:21:15 -06:00
|
|
|
|
2022-09-03 16:29:43 -06:00
|
|
|
script_args = args[script.args_from:script.args_to]
|
|
|
|
processed = script.run(p, *script_args)
|
2022-09-03 08:21:15 -06:00
|
|
|
|
2022-09-08 07:37:13 -06:00
|
|
|
shared.total_tqdm.clear()
|
|
|
|
|
2022-09-03 16:29:43 -06:00
|
|
|
return processed
|
2022-09-03 08:21:15 -06:00
|
|
|
|
2023-05-31 13:40:09 -06:00
|
|
|
def before_process(self, p):
|
|
|
|
for script in self.alwayson_scripts:
|
|
|
|
try:
|
|
|
|
script_args = p.script_args[script.args_from:script.args_to]
|
|
|
|
script.before_process(p, *script_args)
|
|
|
|
except Exception:
|
|
|
|
errors.report(f"Error running before_process: {script.filename}", exc_info=True)
|
|
|
|
|
2022-10-29 13:20:02 -06:00
|
|
|
def process(self, p):
|
2022-10-22 03:23:45 -06:00
|
|
|
for script in self.alwayson_scripts:
|
|
|
|
try:
|
|
|
|
script_args = p.script_args[script.args_from:script.args_to]
|
|
|
|
script.process(p, *script_args)
|
|
|
|
except Exception:
|
2023-05-31 10:56:37 -06:00
|
|
|
errors.report(f"Error running process: {script.filename}", exc_info=True)
|
2022-10-29 13:20:02 -06:00
|
|
|
|
2023-02-22 02:52:53 -07:00
|
|
|
def before_process_batch(self, p, **kwargs):
|
|
|
|
for script in self.alwayson_scripts:
|
|
|
|
try:
|
|
|
|
script_args = p.script_args[script.args_from:script.args_to]
|
|
|
|
script.before_process_batch(p, *script_args, **kwargs)
|
|
|
|
except Exception:
|
2023-05-31 10:56:37 -06:00
|
|
|
errors.report(f"Error running before_process_batch: {script.filename}", exc_info=True)
|
2023-02-22 02:52:53 -07:00
|
|
|
|
2023-06-19 00:37:20 -06:00
|
|
|
def after_extra_networks_activate(self, p, **kwargs):
|
|
|
|
for script in self.alwayson_scripts:
|
|
|
|
try:
|
|
|
|
script_args = p.script_args[script.args_from:script.args_to]
|
|
|
|
script.after_extra_networks_activate(p, *script_args, **kwargs)
|
|
|
|
except Exception:
|
|
|
|
errors.report(f"Error running after_extra_networks_activate: {script.filename}", exc_info=True)
|
|
|
|
|
2022-11-04 02:21:40 -06:00
|
|
|
def process_batch(self, p, **kwargs):
|
2022-11-02 10:05:01 -06:00
|
|
|
for script in self.alwayson_scripts:
|
|
|
|
try:
|
|
|
|
script_args = p.script_args[script.args_from:script.args_to]
|
2022-11-04 02:21:40 -06:00
|
|
|
script.process_batch(p, *script_args, **kwargs)
|
2022-11-02 10:05:01 -06:00
|
|
|
except Exception:
|
2023-05-31 10:56:37 -06:00
|
|
|
errors.report(f"Error running process_batch: {script.filename}", exc_info=True)
|
2022-11-02 10:05:01 -06:00
|
|
|
|
2022-10-29 13:20:02 -06:00
|
|
|
def postprocess(self, p, processed):
|
|
|
|
for script in self.alwayson_scripts:
|
|
|
|
try:
|
|
|
|
script_args = p.script_args[script.args_from:script.args_to]
|
|
|
|
script.postprocess(p, processed, *script_args)
|
|
|
|
except Exception:
|
2023-05-31 10:56:37 -06:00
|
|
|
errors.report(f"Error running postprocess: {script.filename}", exc_info=True)
|
2022-10-22 03:23:45 -06:00
|
|
|
|
2022-12-10 04:54:02 -07:00
|
|
|
def postprocess_batch(self, p, images, **kwargs):
|
|
|
|
for script in self.alwayson_scripts:
|
|
|
|
try:
|
|
|
|
script_args = p.script_args[script.args_from:script.args_to]
|
|
|
|
script.postprocess_batch(p, *script_args, images=images, **kwargs)
|
|
|
|
except Exception:
|
2023-05-31 10:56:37 -06:00
|
|
|
errors.report(f"Error running postprocess_batch: {script.filename}", exc_info=True)
|
2022-12-10 04:54:02 -07:00
|
|
|
|
2023-07-24 11:52:24 -06:00
|
|
|
def postprocess_batch_list(self, p, pp: PostprocessBatchListArgs, **kwargs):
|
|
|
|
for script in self.alwayson_scripts:
|
|
|
|
try:
|
|
|
|
script_args = p.script_args[script.args_from:script.args_to]
|
|
|
|
script.postprocess_batch_list(p, pp, *script_args, **kwargs)
|
|
|
|
except Exception:
|
|
|
|
errors.report(f"Error running postprocess_batch_list: {script.filename}", exc_info=True)
|
|
|
|
|
2023-01-26 13:29:27 -07:00
|
|
|
def postprocess_image(self, p, pp: PostprocessImageArgs):
|
|
|
|
for script in self.alwayson_scripts:
|
|
|
|
try:
|
|
|
|
script_args = p.script_args[script.args_from:script.args_to]
|
|
|
|
script.postprocess_image(p, pp, *script_args)
|
|
|
|
except Exception:
|
2023-05-31 10:56:37 -06:00
|
|
|
errors.report(f"Error running postprocess_image: {script.filename}", exc_info=True)
|
2023-01-26 13:29:27 -07:00
|
|
|
|
2022-11-19 09:10:17 -07:00
|
|
|
def before_component(self, component, **kwargs):
|
2023-08-12 09:36:30 -06:00
|
|
|
for callback, script in self.on_before_component_elem_id.get(kwargs.get("elem_id"), []):
|
|
|
|
try:
|
|
|
|
callback(OnComponent(component=component))
|
|
|
|
except Exception:
|
|
|
|
errors.report(f"Error running on_before_component: {script.filename}", exc_info=True)
|
2023-08-12 08:46:13 -06:00
|
|
|
|
2022-11-19 09:10:17 -07:00
|
|
|
for script in self.scripts:
|
|
|
|
try:
|
|
|
|
script.before_component(component, **kwargs)
|
|
|
|
except Exception:
|
2023-05-31 10:56:37 -06:00
|
|
|
errors.report(f"Error running before_component: {script.filename}", exc_info=True)
|
2022-11-19 09:10:17 -07:00
|
|
|
|
|
|
|
def after_component(self, component, **kwargs):
|
2023-08-12 09:36:30 -06:00
|
|
|
for callback, script in self.on_after_component_elem_id.get(component.elem_id, []):
|
|
|
|
try:
|
|
|
|
callback(OnComponent(component=component))
|
|
|
|
except Exception:
|
|
|
|
errors.report(f"Error running on_after_component: {script.filename}", exc_info=True)
|
2023-08-12 08:46:13 -06:00
|
|
|
|
2022-11-19 09:10:17 -07:00
|
|
|
for script in self.scripts:
|
|
|
|
try:
|
|
|
|
script.after_component(component, **kwargs)
|
|
|
|
except Exception:
|
2023-05-31 10:56:37 -06:00
|
|
|
errors.report(f"Error running after_component: {script.filename}", exc_info=True)
|
2022-11-19 09:10:17 -07:00
|
|
|
|
2023-08-12 08:46:13 -06:00
|
|
|
def script(self, title):
|
|
|
|
return self.title_map.get(title.lower())
|
|
|
|
|
2022-10-22 04:34:49 -06:00
|
|
|
def reload_sources(self, cache):
|
2022-10-02 12:26:06 -06:00
|
|
|
for si, script in list(enumerate(self.scripts)):
|
2022-11-12 00:56:06 -07:00
|
|
|
args_from = script.args_from
|
|
|
|
args_to = script.args_to
|
|
|
|
filename = script.filename
|
|
|
|
|
|
|
|
module = cache.get(filename, None)
|
|
|
|
if module is None:
|
|
|
|
module = script_loading.load_module(script.filename)
|
|
|
|
cache[filename] = module
|
|
|
|
|
2023-05-10 02:37:18 -06:00
|
|
|
for script_class in module.__dict__.values():
|
2022-11-12 00:56:06 -07:00
|
|
|
if type(script_class) == type and issubclass(script_class, Script):
|
|
|
|
self.scripts[si] = script_class()
|
|
|
|
self.scripts[si].filename = filename
|
|
|
|
self.scripts[si].args_from = args_from
|
|
|
|
self.scripts[si].args_to = args_to
|
2022-09-03 08:21:15 -06:00
|
|
|
|
2023-06-28 03:37:08 -06:00
|
|
|
def before_hr(self, p):
|
|
|
|
for script in self.alwayson_scripts:
|
|
|
|
try:
|
|
|
|
script_args = p.script_args[script.args_from:script.args_to]
|
|
|
|
script.before_hr(p, *script_args)
|
|
|
|
except Exception:
|
|
|
|
errors.report(f"Error running before_hr: {script.filename}", exc_info=True)
|
|
|
|
|
2023-08-14 01:43:18 -06:00
|
|
|
def setup_scrips(self, p, *, is_ui=True):
|
2023-08-13 08:31:10 -06:00
|
|
|
for script in self.alwayson_scripts:
|
2023-08-14 01:43:18 -06:00
|
|
|
if not is_ui and script.setup_for_ui_only:
|
|
|
|
continue
|
|
|
|
|
2023-08-13 08:31:10 -06:00
|
|
|
try:
|
|
|
|
script_args = p.script_args[script.args_from:script.args_to]
|
|
|
|
script.setup(p, *script_args)
|
|
|
|
except Exception:
|
|
|
|
errors.report(f"Error running setup: {script.filename}", exc_info=True)
|
|
|
|
|
2023-06-28 03:37:08 -06:00
|
|
|
|
2023-05-19 06:49:36 -06:00
|
|
|
scripts_txt2img: ScriptRunner = None
|
|
|
|
scripts_img2img: ScriptRunner = None
|
|
|
|
scripts_postproc: scripts_postprocessing.ScriptPostprocessingRunner = None
|
2022-11-19 09:10:17 -07:00
|
|
|
scripts_current: ScriptRunner = None
|
2022-10-01 11:30:53 -06:00
|
|
|
|
2022-10-22 03:23:45 -06:00
|
|
|
|
2022-10-01 18:18:42 -06:00
|
|
|
def reload_script_body_only():
|
2022-10-22 04:34:49 -06:00
|
|
|
cache = {}
|
|
|
|
scripts_txt2img.reload_sources(cache)
|
|
|
|
scripts_img2img.reload_sources(cache)
|
2022-10-02 12:26:06 -06:00
|
|
|
|
2022-10-01 18:18:42 -06:00
|
|
|
|
2023-05-19 06:49:36 -06:00
|
|
|
reload_scripts = load_scripts # compatibility alias
|