Skip to content

Commit

Permalink
further support for extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
AUTOMATIC1111 committed Oct 22, 2022
1 parent 2b91251 commit 6398dc9
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ notification.mp3
/SwinIR
/textual_inversion
.vscode
/extensions
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ Check the [custom scripts](https://github.com/AUTOMATIC1111/stable-diffusion-web
- Estimated completion time in progress bar
- API
- Support for dedicated [inpainting model](https://github.com/runwayml/stable-diffusion#inpainting-with-stable-diffusion) by RunwayML.
- Aesthetic Gradients, a way to generate images with a specific aesthetic by using clip images embds (implementation of [https://github.com/vicgalle/stable-diffusion-aesthetic-gradients](https://github.com/vicgalle/stable-diffusion-aesthetic-gradients))

- via extension: [Aesthetic Gradients](https://github.com/AUTOMATIC1111/stable-diffusion-webui-aesthetic-gradients), a way to generate images with a specific aesthetic by using clip images embds (implementation of [https://github.com/vicgalle/stable-diffusion-aesthetic-gradients](https://github.com/vicgalle/stable-diffusion-aesthetic-gradients))

## Installation and Running
Make sure the required [dependencies](https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Dependencies) are met and follow the instructions available for both [NVidia](https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Install-and-Run-on-NVidia-GPUs) (recommended) and [AMD](https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Install-and-Run-on-AMD-GPUs) GPUs.
Expand Down
44 changes: 35 additions & 9 deletions modules/scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,39 @@ def list_scripts(scriptdirname, extension):
if os.path.exists(extdir):
for dirname in sorted(os.listdir(extdir)):
dirpath = os.path.join(extdir, dirname)
if not os.path.isdir(dirpath):
scriptdirpath = os.path.join(dirpath, scriptdirname)

if not os.path.isdir(scriptdirpath):
continue

for filename in sorted(os.listdir(os.path.join(dirpath, scriptdirname))):
scripts_list.append(ScriptFile(dirpath, filename, os.path.join(dirpath, scriptdirname, filename)))
for filename in sorted(os.listdir(scriptdirpath)):
scripts_list.append(ScriptFile(dirpath, filename, os.path.join(scriptdirpath, filename)))

scripts_list = [x for x in scripts_list if os.path.splitext(x.path)[1].lower() == extension and os.path.isfile(x.path)]

return scripts_list


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


def load_scripts():
global current_basedir
scripts_data.clear()
Expand Down Expand Up @@ -276,7 +298,7 @@ def run_alwayson_scripts(self, p):
print(f"Error running alwayson script: {script.filename}", file=sys.stderr)
print(traceback.format_exc(), file=sys.stderr)

def reload_sources(self):
def reload_sources(self, cache):
for si, script in list(enumerate(self.scripts)):
with open(script.filename, "r", encoding="utf8") as file:
args_from = script.args_from
Expand All @@ -286,9 +308,12 @@ def reload_sources(self):

from types import ModuleType

compiled = compile(text, filename, 'exec')
module = ModuleType(script.filename)
exec(compiled, module.__dict__)
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

for key, script_class in module.__dict__.items():
if type(script_class) == type and issubclass(script_class, Script):
Expand All @@ -303,8 +328,9 @@ def reload_sources(self):


def reload_script_body_only():
scripts_txt2img.reload_sources()
scripts_img2img.reload_sources()
cache = {}
scripts_txt2img.reload_sources(cache)
scripts_img2img.reload_sources(cache)


def reload_scripts():
Expand Down
19 changes: 10 additions & 9 deletions modules/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -1636,13 +1636,15 @@ def request_restart():

interfaces += [(settings_interface, "Settings", "settings")]

with open(os.path.join(script_path, "style.css"), "r", encoding="utf8") as file:
css = file.read()
css = ""

for cssfile in modules.scripts.list_files_with_name("style.css"):
with open(cssfile, "r", encoding="utf8") as file:
css += file.read() + "\n"

if os.path.exists(os.path.join(script_path, "user.css")):
with open(os.path.join(script_path, "user.css"), "r", encoding="utf8") as file:
usercss = file.read()
css += usercss
css += file.read() + "\n"

if not cmd_opts.no_progressbar_hiding:
css += css_hide_progressbar
Expand Down Expand Up @@ -1865,9 +1867,9 @@ def load_javascript(raw_response):
with open(os.path.join(script_path, "script.js"), "r", encoding="utf8") as jsfile:
javascript = f'<script>{jsfile.read()}</script>'

jsdir = os.path.join(script_path, "javascript")
for filename in sorted(os.listdir(jsdir)):
with open(os.path.join(jsdir, filename), "r", encoding="utf8") as jsfile:
scripts_list = modules.scripts.list_scripts("javascript", ".js")
for basedir, filename, path in scripts_list:
with open(path, "r", encoding="utf8") as jsfile:
javascript += f"\n<!-- {filename} --><script>{jsfile.read()}</script>"

if cmd_opts.theme is not None:
Expand All @@ -1885,6 +1887,5 @@ def template_response(*args, **kwargs):
gradio.routes.templates.TemplateResponse = template_response


reload_javascript = partial(load_javascript,
gradio.routes.templates.TemplateResponse)
reload_javascript = partial(load_javascript, gradio.routes.templates.TemplateResponse)
reload_javascript()
2 changes: 1 addition & 1 deletion style.css
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ input[type="range"]{
padding: 0;
}

#refresh_sd_model_checkpoint, #refresh_sd_hypernetwork, #refresh_train_hypernetwork_name, #refresh_train_embedding_name, #refresh_localization, #refresh_aesthetic_embeddings{
#refresh_sd_model_checkpoint, #refresh_sd_hypernetwork, #refresh_train_hypernetwork_name, #refresh_train_embedding_name, #refresh_localization{
max-width: 2.5em;
min-width: 2.5em;
height: 2.4em;
Expand Down

0 comments on commit 6398dc9

Please sign in to comment.