2022-09-03 12:08:45 +03:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import traceback
|
2022-10-22 12:23:45 +03:00
|
|
|
from collections import namedtuple
|
2022-09-03 12:08:45 +03:00
|
|
|
|
2022-09-03 17:21:15 +03:00
|
|
|
import modules.ui as ui
|
2022-09-03 12:08:45 +03:00
|
|
|
import gradio as gr
|
|
|
|
|
2022-09-03 17:21:15 +03:00
|
|
|
from modules.processing import StableDiffusionProcessing
|
2022-10-22 12:23:45 +03:00
|
|
|
from modules import shared, paths, script_callbacks
|
|
|
|
|
|
|
|
AlwaysVisible = object()
|
|
|
|
|
2022-09-03 17:21:15 +03:00
|
|
|
|
2022-09-03 12:08:45 +03:00
|
|
|
class Script:
|
|
|
|
filename = None
|
2022-09-03 17:21:15 +03:00
|
|
|
args_from = None
|
|
|
|
args_to = None
|
2022-10-22 12:23:45 +03:00
|
|
|
alwayson = False
|
|
|
|
|
|
|
|
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 12:08:45 +03:00
|
|
|
|
|
|
|
def title(self):
|
2022-10-22 12:23:45 +03:00
|
|
|
"""this function should return the title of the script. This is what will be displayed in the dropdown menu."""
|
|
|
|
|
2022-09-03 12:08:45 +03:00
|
|
|
raise NotImplementedError()
|
|
|
|
|
2022-09-03 17:21:15 +03:00
|
|
|
def ui(self, is_img2img):
|
2022-10-22 12:23:45 +03: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.
|
|
|
|
Values of those returned componenbts will be passed to run() and process() functions.
|
|
|
|
"""
|
|
|
|
|
2022-09-03 17:21:15 +03:00
|
|
|
pass
|
|
|
|
|
2022-09-04 01:29:43 +03:00
|
|
|
def show(self, is_img2img):
|
2022-10-22 12:23:45 +03: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
|
|
|
|
- True if the script should be shown in UI if it's scelected in the scripts drowpdown
|
|
|
|
- script.AlwaysVisible if the script should be shown in UI at all times
|
|
|
|
"""
|
|
|
|
|
2022-09-04 01:29:43 +03:00
|
|
|
return True
|
|
|
|
|
2022-10-22 12:23:45 +03: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()
|
|
|
|
"""
|
|
|
|
|
2022-09-03 17:21:15 +03:00
|
|
|
raise NotImplementedError()
|
|
|
|
|
2022-10-22 12:23:45 +03:00
|
|
|
def process(self, p, *args):
|
|
|
|
"""
|
|
|
|
This function is called before processing begins for AlwaysVisible scripts.
|
|
|
|
scripts. You can modify the processing object (p) here, inject hooks, etc.
|
|
|
|
"""
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
2022-09-03 17:21:15 +03:00
|
|
|
def describe(self):
|
2022-10-22 12:23:45 +03:00
|
|
|
"""unused"""
|
2022-09-03 17:21:15 +03:00
|
|
|
return ""
|
|
|
|
|
2022-09-03 12:08:45 +03:00
|
|
|
|
2022-10-22 12:23:45 +03: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
|
|
|
|
|
|
|
|
|
2022-09-04 01:29:43 +03:00
|
|
|
scripts_data = []
|
2022-10-22 12:23:45 +03:00
|
|
|
ScriptFile = namedtuple("ScriptFile", ["basedir", "filename", "path"])
|
|
|
|
ScriptClassData = namedtuple("ScriptClassData", ["script_class", "path", "basedir"])
|
|
|
|
|
|
|
|
|
|
|
|
def list_scripts(scriptdirname, extension):
|
|
|
|
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)))
|
|
|
|
|
|
|
|
extdir = os.path.join(paths.script_path, "extensions")
|
|
|
|
if os.path.exists(extdir):
|
|
|
|
for dirname in sorted(os.listdir(extdir)):
|
|
|
|
dirpath = os.path.join(extdir, dirname)
|
2022-10-22 13:34:49 +03:00
|
|
|
scriptdirpath = os.path.join(dirpath, scriptdirname)
|
|
|
|
|
|
|
|
if not os.path.isdir(scriptdirpath):
|
2022-10-22 12:23:45 +03:00
|
|
|
continue
|
2022-09-03 12:08:45 +03:00
|
|
|
|
2022-10-22 13:34:49 +03:00
|
|
|
for filename in sorted(os.listdir(scriptdirpath)):
|
|
|
|
scripts_list.append(ScriptFile(dirpath, filename, os.path.join(scriptdirpath, filename)))
|
2022-09-03 12:08:45 +03:00
|
|
|
|
2022-10-22 12:23:45 +03: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 19:09:14 +03:00
|
|
|
|
2022-10-22 12:23:45 +03:00
|
|
|
return scripts_list
|
2022-09-03 12:08:45 +03:00
|
|
|
|
2022-10-17 07:56:08 +03:00
|
|
|
|
2022-10-22 13:34:49 +03:00
|
|
|
def list_files_with_name(filename):
|
|
|
|
res = []
|
|
|
|
|
|
|
|
dirs = [paths.script_path]
|
|
|
|
|
|
|
|
extdir = os.path.join(paths.script_path, "extensions")
|
|
|
|
if os.path.exists(extdir):
|
|
|
|
dirs += [os.path.join(extdir, d) for d in sorted(os.listdir(extdir))]
|
|
|
|
|
|
|
|
for dirpath in dirs:
|
|
|
|
if not os.path.isdir(dirpath):
|
|
|
|
continue
|
|
|
|
|
|
|
|
path = os.path.join(dirpath, filename)
|
|
|
|
if os.path.isfile(filename):
|
|
|
|
res.append(path)
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
2022-10-22 12:23:45 +03:00
|
|
|
def load_scripts():
|
|
|
|
global current_basedir
|
|
|
|
scripts_data.clear()
|
|
|
|
script_callbacks.clear_callbacks()
|
|
|
|
|
|
|
|
scripts_list = list_scripts("scripts", ".py")
|
|
|
|
|
|
|
|
syspath = sys.path
|
2022-09-03 12:08:45 +03:00
|
|
|
|
2022-10-22 12:23:45 +03:00
|
|
|
for scriptfile in sorted(scripts_list):
|
2022-09-03 19:59:11 +03:00
|
|
|
try:
|
2022-10-22 12:23:45 +03:00
|
|
|
if scriptfile.basedir != paths.script_path:
|
|
|
|
sys.path = [scriptfile.basedir] + sys.path
|
|
|
|
current_basedir = scriptfile.basedir
|
|
|
|
|
|
|
|
with open(scriptfile.path, "r", encoding="utf8") as file:
|
2022-09-14 13:20:24 +03:00
|
|
|
text = file.read()
|
|
|
|
|
2022-09-03 19:59:11 +03:00
|
|
|
from types import ModuleType
|
2022-10-22 12:23:45 +03:00
|
|
|
compiled = compile(text, scriptfile.path, 'exec')
|
|
|
|
module = ModuleType(scriptfile.filename)
|
2022-09-03 19:59:11 +03:00
|
|
|
exec(compiled, module.__dict__)
|
|
|
|
|
|
|
|
for key, script_class in module.__dict__.items():
|
|
|
|
if type(script_class) == type and issubclass(script_class, Script):
|
2022-10-22 12:23:45 +03:00
|
|
|
scripts_data.append(ScriptClassData(script_class, scriptfile.path, scriptfile.basedir))
|
2022-09-03 19:59:11 +03:00
|
|
|
|
|
|
|
except Exception:
|
2022-10-22 12:23:45 +03:00
|
|
|
print(f"Error loading script: {scriptfile.filename}", file=sys.stderr)
|
2022-09-03 19:59:11 +03:00
|
|
|
print(traceback.format_exc(), file=sys.stderr)
|
2022-09-03 12:08:45 +03:00
|
|
|
|
2022-10-22 12:23:45 +03:00
|
|
|
finally:
|
|
|
|
sys.path = syspath
|
|
|
|
current_basedir = paths.script_path
|
|
|
|
|
2022-09-03 12:08:45 +03:00
|
|
|
|
|
|
|
def wrap_call(func, filename, funcname, *args, default=None, **kwargs):
|
|
|
|
try:
|
2022-09-03 17:21:15 +03:00
|
|
|
res = func(*args, **kwargs)
|
2022-09-03 12:08:45 +03:00
|
|
|
return res
|
|
|
|
except Exception:
|
2022-09-03 17:21:15 +03:00
|
|
|
print(f"Error calling: {filename}/{funcname}", file=sys.stderr)
|
2022-09-03 12:08:45 +03:00
|
|
|
print(traceback.format_exc(), file=sys.stderr)
|
|
|
|
|
|
|
|
return default
|
|
|
|
|
|
|
|
|
2022-09-04 01:29:43 +03:00
|
|
|
class ScriptRunner:
|
|
|
|
def __init__(self):
|
|
|
|
self.scripts = []
|
2022-10-22 12:23:45 +03:00
|
|
|
self.selectable_scripts = []
|
|
|
|
self.alwayson_scripts = []
|
2022-10-18 11:29:42 +03:00
|
|
|
self.titles = []
|
2022-10-22 12:23:45 +03:00
|
|
|
self.infotext_fields = []
|
2022-09-04 01:29:43 +03:00
|
|
|
|
|
|
|
def setup_ui(self, is_img2img):
|
2022-10-22 12:23:45 +03:00
|
|
|
for script_class, path, basedir in scripts_data:
|
2022-09-04 01:29:43 +03:00
|
|
|
script = script_class()
|
|
|
|
script.filename = path
|
|
|
|
|
2022-10-22 12:23:45 +03:00
|
|
|
visibility = script.show(is_img2img)
|
2022-09-04 01:29:43 +03:00
|
|
|
|
2022-10-22 12:23:45 +03:00
|
|
|
if visibility == AlwaysVisible:
|
|
|
|
self.scripts.append(script)
|
|
|
|
self.alwayson_scripts.append(script)
|
|
|
|
script.alwayson = True
|
2022-09-04 01:29:43 +03:00
|
|
|
|
2022-10-22 12:23:45 +03:00
|
|
|
elif visibility:
|
|
|
|
self.scripts.append(script)
|
|
|
|
self.selectable_scripts.append(script)
|
2022-09-04 01:29:43 +03:00
|
|
|
|
2022-10-22 12:23:45 +03:00
|
|
|
self.titles = [wrap_call(script.title, script.filename, "title") or f"{script.filename} [error]" for script in self.selectable_scripts]
|
|
|
|
|
|
|
|
inputs = [None]
|
|
|
|
inputs_alwayson = [True]
|
2022-09-04 01:29:43 +03:00
|
|
|
|
2022-10-22 12:23:45 +03:00
|
|
|
def create_script_ui(script, inputs, inputs_alwayson):
|
2022-09-04 01:29:43 +03:00
|
|
|
script.args_from = len(inputs)
|
2022-09-15 19:43:48 +03:00
|
|
|
script.args_to = len(inputs)
|
2022-09-04 01:29:43 +03:00
|
|
|
|
|
|
|
controls = wrap_call(script.ui, script.filename, "ui", is_img2img)
|
|
|
|
|
|
|
|
if controls is None:
|
2022-10-22 12:23:45 +03:00
|
|
|
return
|
2022-09-03 17:21:15 +03:00
|
|
|
|
2022-09-04 01:29:43 +03:00
|
|
|
for control in controls:
|
2022-09-25 08:55:33 +03:00
|
|
|
control.custom_script_source = os.path.basename(script.filename)
|
2022-10-22 12:23:45 +03:00
|
|
|
if not script.alwayson:
|
|
|
|
control.visible = False
|
|
|
|
|
|
|
|
if script.infotext_fields is not None:
|
|
|
|
self.infotext_fields += script.infotext_fields
|
2022-09-03 17:21:15 +03:00
|
|
|
|
2022-09-04 01:29:43 +03:00
|
|
|
inputs += controls
|
2022-10-22 12:23:45 +03:00
|
|
|
inputs_alwayson += [script.alwayson for _ in controls]
|
2022-09-04 01:29:43 +03:00
|
|
|
script.args_to = len(inputs)
|
2022-09-03 17:21:15 +03:00
|
|
|
|
2022-10-22 12:23:45 +03:00
|
|
|
for script in self.alwayson_scripts:
|
|
|
|
with gr.Group():
|
|
|
|
create_script_ui(script, inputs, inputs_alwayson)
|
|
|
|
|
|
|
|
dropdown = gr.Dropdown(label="Script", choices=["None"] + self.titles, value="None", type="index")
|
|
|
|
dropdown.save_to_config = True
|
|
|
|
inputs[0] = dropdown
|
|
|
|
|
|
|
|
for script in self.selectable_scripts:
|
|
|
|
create_script_ui(script, inputs, inputs_alwayson)
|
|
|
|
|
2022-09-04 01:29:43 +03:00
|
|
|
def select_script(script_index):
|
2022-10-22 12:23:45 +03:00
|
|
|
if 0 < script_index <= len(self.selectable_scripts):
|
|
|
|
script = self.selectable_scripts[script_index-1]
|
2022-09-04 01:29:43 +03:00
|
|
|
args_from = script.args_from
|
|
|
|
args_to = script.args_to
|
|
|
|
else:
|
|
|
|
args_from = 0
|
|
|
|
args_to = 0
|
2022-09-03 17:21:15 +03:00
|
|
|
|
2022-10-22 12:23:45 +03:00
|
|
|
return [ui.gr_show(True if i == 0 else args_from <= i < args_to or is_alwayson) for i, is_alwayson in enumerate(inputs_alwayson)]
|
2022-09-03 17:21:15 +03:00
|
|
|
|
2022-10-18 11:29:42 +03:00
|
|
|
def init_field(title):
|
2022-10-18 12:52:27 +03:00
|
|
|
if title == 'None':
|
2022-10-18 11:29:42 +03:00
|
|
|
return
|
|
|
|
script_index = self.titles.index(title)
|
2022-10-22 12:23:45 +03:00
|
|
|
script = self.selectable_scripts[script_index]
|
2022-10-18 11:29:42 +03:00
|
|
|
for i in range(script.args_from, script.args_to):
|
|
|
|
inputs[i].visible = True
|
|
|
|
|
|
|
|
dropdown.init_field = init_field
|
2022-09-04 01:29:43 +03:00
|
|
|
dropdown.change(
|
|
|
|
fn=select_script,
|
|
|
|
inputs=[dropdown],
|
|
|
|
outputs=inputs
|
|
|
|
)
|
2022-09-03 17:21:15 +03:00
|
|
|
|
2022-09-04 01:29:43 +03:00
|
|
|
return inputs
|
2022-09-03 17:21:15 +03:00
|
|
|
|
2022-09-04 01:29:43 +03:00
|
|
|
def run(self, p: StableDiffusionProcessing, *args):
|
|
|
|
script_index = args[0]
|
2022-09-03 17:21:15 +03:00
|
|
|
|
2022-09-04 01:29:43 +03:00
|
|
|
if script_index == 0:
|
|
|
|
return None
|
2022-09-03 17:21:15 +03:00
|
|
|
|
2022-10-22 12:23:45 +03:00
|
|
|
script = self.selectable_scripts[script_index-1]
|
2022-09-03 17:21:15 +03:00
|
|
|
|
2022-09-04 01:29:43 +03:00
|
|
|
if script is None:
|
|
|
|
return None
|
2022-09-03 17:21:15 +03:00
|
|
|
|
2022-09-04 01:29:43 +03:00
|
|
|
script_args = args[script.args_from:script.args_to]
|
|
|
|
processed = script.run(p, *script_args)
|
2022-09-03 17:21:15 +03:00
|
|
|
|
2022-09-08 16:37:13 +03:00
|
|
|
shared.total_tqdm.clear()
|
|
|
|
|
2022-09-04 01:29:43 +03:00
|
|
|
return processed
|
2022-09-03 17:21:15 +03:00
|
|
|
|
2022-10-22 12:23:45 +03:00
|
|
|
def run_alwayson_scripts(self, p):
|
|
|
|
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:
|
|
|
|
print(f"Error running alwayson script: {script.filename}", file=sys.stderr)
|
|
|
|
print(traceback.format_exc(), file=sys.stderr)
|
|
|
|
|
2022-10-22 13:34:49 +03:00
|
|
|
def reload_sources(self, cache):
|
2022-10-02 21:26:06 +03:00
|
|
|
for si, script in list(enumerate(self.scripts)):
|
|
|
|
with open(script.filename, "r", encoding="utf8") as file:
|
|
|
|
args_from = script.args_from
|
|
|
|
args_to = script.args_to
|
|
|
|
filename = script.filename
|
|
|
|
text = file.read()
|
2022-10-02 03:18:42 +03:00
|
|
|
|
2022-10-02 21:26:06 +03:00
|
|
|
from types import ModuleType
|
2022-10-02 03:18:42 +03:00
|
|
|
|
2022-10-22 13:34:49 +03:00
|
|
|
module = cache.get(filename, None)
|
|
|
|
if module is None:
|
|
|
|
compiled = compile(text, filename, 'exec')
|
|
|
|
module = ModuleType(script.filename)
|
|
|
|
exec(compiled, module.__dict__)
|
|
|
|
cache[filename] = module
|
2022-10-02 21:26:06 +03:00
|
|
|
|
|
|
|
for key, script_class in module.__dict__.items():
|
|
|
|
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 17:21:15 +03:00
|
|
|
|
2022-10-22 12:23:45 +03:00
|
|
|
|
2022-09-04 01:29:43 +03:00
|
|
|
scripts_txt2img = ScriptRunner()
|
|
|
|
scripts_img2img = ScriptRunner()
|
2022-10-01 20:30:53 +03:00
|
|
|
|
2022-10-22 12:23:45 +03:00
|
|
|
|
2022-10-02 03:18:42 +03:00
|
|
|
def reload_script_body_only():
|
2022-10-22 13:34:49 +03:00
|
|
|
cache = {}
|
|
|
|
scripts_txt2img.reload_sources(cache)
|
|
|
|
scripts_img2img.reload_sources(cache)
|
2022-10-02 21:26:06 +03:00
|
|
|
|
2022-10-02 03:18:42 +03:00
|
|
|
|
2022-10-22 12:23:45 +03:00
|
|
|
def reload_scripts():
|
2022-10-02 21:26:06 +03:00
|
|
|
global scripts_txt2img, scripts_img2img
|
2022-10-01 20:30:53 +03:00
|
|
|
|
2022-10-22 12:23:45 +03:00
|
|
|
load_scripts()
|
2022-10-01 20:30:53 +03:00
|
|
|
|
2022-10-02 21:26:06 +03:00
|
|
|
scripts_txt2img = ScriptRunner()
|
|
|
|
scripts_img2img = ScriptRunner()
|
2022-10-22 12:23:45 +03:00
|
|
|
|