diff options
author | AUTOMATIC <16777216c@gmail.com> | 2022-09-03 09:08:45 +0000 |
---|---|---|
committer | AUTOMATIC <16777216c@gmail.com> | 2022-09-03 09:08:45 +0000 |
commit | 345028099d893f8a66726cfd13627d8cc1bcc724 (patch) | |
tree | acb1da553620b7e7139db840ef43accf71b786a8 /modules/scripts.py | |
parent | d7b67d9b40e47ede766d3beb149b0c2b74651ece (diff) | |
download | stable-diffusion-webui-gfx803-345028099d893f8a66726cfd13627d8cc1bcc724.tar.gz stable-diffusion-webui-gfx803-345028099d893f8a66726cfd13627d8cc1bcc724.tar.bz2 stable-diffusion-webui-gfx803-345028099d893f8a66726cfd13627d8cc1bcc724.zip |
split codebase into multiple files; to anyone this affects negatively: sorry
Diffstat (limited to 'modules/scripts.py')
-rw-r--r-- | modules/scripts.py | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/modules/scripts.py b/modules/scripts.py new file mode 100644 index 00000000..20f489ce --- /dev/null +++ b/modules/scripts.py @@ -0,0 +1,53 @@ +import os
+import sys
+import traceback
+
+import gradio as gr
+
+class Script:
+ filename = None
+
+ def title(self):
+ raise NotImplementedError()
+
+
+scripts = []
+
+
+def load_scripts(basedir, globs):
+ for filename in os.listdir(basedir):
+ path = os.path.join(basedir, filename)
+
+ if not os.path.isfile(path):
+ continue
+
+ with open(path, "r", encoding="utf8") as file:
+ text = file.read()
+
+ from types import ModuleType
+ compiled = compile(text, path, 'exec')
+ module = ModuleType(filename)
+ module.__dict__.update(globs)
+ exec(compiled, module.__dict__)
+
+ for key, item in module.__dict__.items():
+ if type(item) == type and issubclass(item, Script):
+ item.filename = path
+
+ scripts.append(item)
+
+
+def wrap_call(func, filename, funcname, *args, default=None, **kwargs):
+ try:
+ res = func()
+ return res
+ except Exception:
+ print(f"Error calling: {filename/funcname}", file=sys.stderr)
+ print(traceback.format_exc(), file=sys.stderr)
+
+ return default
+
+def setup_ui():
+ titles = [wrap_call(script.title, script.filename, "title") for script in scripts]
+
+ gr.Dropdown(options=[""] + titles, value="", type="index")
|