2022-09-03 17:08:45 +08:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import traceback
|
|
|
|
|
2022-09-03 22:21:15 +08:00
|
|
|
import modules.ui as ui
|
2022-09-03 17:08:45 +08:00
|
|
|
import gradio as gr
|
|
|
|
|
2022-09-03 22:21:15 +08:00
|
|
|
from modules.processing import StableDiffusionProcessing
|
2022-09-08 21:37:13 +08:00
|
|
|
from modules import shared
|
2022-09-03 22:21:15 +08:00
|
|
|
|
2022-09-03 17:08:45 +08:00
|
|
|
class Script:
|
|
|
|
filename = None
|
2022-09-03 22:21:15 +08:00
|
|
|
args_from = None
|
|
|
|
args_to = None
|
2022-09-03 17:08:45 +08:00
|
|
|
|
|
|
|
def title(self):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
2022-09-03 22:21:15 +08:00
|
|
|
def ui(self, is_img2img):
|
|
|
|
pass
|
|
|
|
|
2022-09-04 06:29:43 +08:00
|
|
|
def show(self, is_img2img):
|
|
|
|
return True
|
|
|
|
|
2022-09-03 22:21:15 +08:00
|
|
|
def run(self, *args):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
def describe(self):
|
|
|
|
return ""
|
|
|
|
|
2022-09-03 17:08:45 +08:00
|
|
|
|
2022-09-04 06:29:43 +08:00
|
|
|
scripts_data = []
|
2022-09-03 17:08:45 +08:00
|
|
|
|
|
|
|
|
2022-09-03 22:21:15 +08:00
|
|
|
def load_scripts(basedir):
|
2022-09-04 00:09:14 +08:00
|
|
|
if not os.path.exists(basedir):
|
|
|
|
return
|
|
|
|
|
2022-09-03 17:08:45 +08:00
|
|
|
for filename in os.listdir(basedir):
|
|
|
|
path = os.path.join(basedir, filename)
|
|
|
|
|
|
|
|
if not os.path.isfile(path):
|
|
|
|
continue
|
|
|
|
|
2022-09-04 00:59:11 +08:00
|
|
|
try:
|
2022-09-14 18:20:24 +08:00
|
|
|
with open(path, "r", encoding="utf8") as file:
|
|
|
|
text = file.read()
|
|
|
|
|
2022-09-04 00:59:11 +08:00
|
|
|
from types import ModuleType
|
|
|
|
compiled = compile(text, path, 'exec')
|
|
|
|
module = ModuleType(filename)
|
|
|
|
exec(compiled, module.__dict__)
|
|
|
|
|
|
|
|
for key, script_class in module.__dict__.items():
|
|
|
|
if type(script_class) == type and issubclass(script_class, Script):
|
2022-09-04 06:29:43 +08:00
|
|
|
scripts_data.append((script_class, path))
|
2022-09-04 00:59:11 +08:00
|
|
|
|
|
|
|
except Exception:
|
|
|
|
print(f"Error loading script: {filename}", file=sys.stderr)
|
|
|
|
print(traceback.format_exc(), file=sys.stderr)
|
2022-09-03 17:08:45 +08:00
|
|
|
|
|
|
|
|
|
|
|
def wrap_call(func, filename, funcname, *args, default=None, **kwargs):
|
|
|
|
try:
|
2022-09-03 22:21:15 +08:00
|
|
|
res = func(*args, **kwargs)
|
2022-09-03 17:08:45 +08:00
|
|
|
return res
|
|
|
|
except Exception:
|
2022-09-03 22:21:15 +08:00
|
|
|
print(f"Error calling: {filename}/{funcname}", file=sys.stderr)
|
2022-09-03 17:08:45 +08:00
|
|
|
print(traceback.format_exc(), file=sys.stderr)
|
|
|
|
|
|
|
|
return default
|
|
|
|
|
|
|
|
|
2022-09-04 06:29:43 +08:00
|
|
|
class ScriptRunner:
|
|
|
|
def __init__(self):
|
|
|
|
self.scripts = []
|
|
|
|
|
|
|
|
def setup_ui(self, is_img2img):
|
|
|
|
for script_class, path in scripts_data:
|
|
|
|
script = script_class()
|
|
|
|
script.filename = path
|
|
|
|
|
|
|
|
if not script.show(is_img2img):
|
|
|
|
continue
|
|
|
|
|
|
|
|
self.scripts.append(script)
|
|
|
|
|
|
|
|
titles = [wrap_call(script.title, script.filename, "title") or f"{script.filename} [error]" for script in self.scripts]
|
|
|
|
|
|
|
|
dropdown = gr.Dropdown(label="Script", choices=["None"] + titles, value="None", type="index")
|
|
|
|
inputs = [dropdown]
|
|
|
|
|
|
|
|
for script in self.scripts:
|
|
|
|
script.args_from = len(inputs)
|
|
|
|
|
|
|
|
controls = wrap_call(script.ui, script.filename, "ui", is_img2img)
|
|
|
|
|
|
|
|
if controls is None:
|
|
|
|
continue
|
2022-09-03 22:21:15 +08:00
|
|
|
|
2022-09-04 06:29:43 +08:00
|
|
|
for control in controls:
|
|
|
|
control.visible = False
|
2022-09-03 22:21:15 +08:00
|
|
|
|
2022-09-04 06:29:43 +08:00
|
|
|
inputs += controls
|
|
|
|
script.args_to = len(inputs)
|
2022-09-03 22:21:15 +08:00
|
|
|
|
2022-09-04 06:29:43 +08:00
|
|
|
def select_script(script_index):
|
|
|
|
if 0 < script_index <= len(self.scripts):
|
|
|
|
script = self.scripts[script_index-1]
|
|
|
|
args_from = script.args_from
|
|
|
|
args_to = script.args_to
|
|
|
|
else:
|
|
|
|
args_from = 0
|
|
|
|
args_to = 0
|
2022-09-03 22:21:15 +08:00
|
|
|
|
2022-09-04 06:29:43 +08:00
|
|
|
return [ui.gr_show(True if i == 0 else args_from <= i < args_to) for i in range(len(inputs))]
|
2022-09-03 22:21:15 +08:00
|
|
|
|
2022-09-04 06:29:43 +08:00
|
|
|
dropdown.change(
|
|
|
|
fn=select_script,
|
|
|
|
inputs=[dropdown],
|
|
|
|
outputs=inputs
|
|
|
|
)
|
2022-09-03 22:21:15 +08:00
|
|
|
|
2022-09-04 06:29:43 +08:00
|
|
|
return inputs
|
2022-09-03 22:21:15 +08:00
|
|
|
|
|
|
|
|
2022-09-04 06:29:43 +08:00
|
|
|
def run(self, p: StableDiffusionProcessing, *args):
|
|
|
|
script_index = args[0]
|
2022-09-03 22:21:15 +08:00
|
|
|
|
2022-09-04 06:29:43 +08:00
|
|
|
if script_index == 0:
|
|
|
|
return None
|
2022-09-03 22:21:15 +08:00
|
|
|
|
2022-09-04 06:29:43 +08:00
|
|
|
script = self.scripts[script_index-1]
|
2022-09-03 22:21:15 +08:00
|
|
|
|
2022-09-04 06:29:43 +08:00
|
|
|
if script is None:
|
|
|
|
return None
|
2022-09-03 22:21:15 +08:00
|
|
|
|
2022-09-04 06:29:43 +08:00
|
|
|
script_args = args[script.args_from:script.args_to]
|
|
|
|
processed = script.run(p, *script_args)
|
2022-09-03 22:21:15 +08:00
|
|
|
|
2022-09-08 21:37:13 +08:00
|
|
|
shared.total_tqdm.clear()
|
|
|
|
|
2022-09-04 06:29:43 +08:00
|
|
|
return processed
|
2022-09-03 22:21:15 +08:00
|
|
|
|
|
|
|
|
2022-09-04 06:29:43 +08:00
|
|
|
scripts_txt2img = ScriptRunner()
|
|
|
|
scripts_img2img = ScriptRunner()
|