Skip to content

Commit

Permalink
add initial version of the extensions tab
Browse files Browse the repository at this point in the history
fix broken Restart Gradio button
  • Loading branch information
AUTOMATIC1111 committed Oct 31, 2022
1 parent 9b384df commit 910a097
Show file tree
Hide file tree
Showing 9 changed files with 333 additions and 30 deletions.
24 changes: 24 additions & 0 deletions javascript/extensions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

function extensions_apply(_, _){
disable = []
update = []
gradioApp().querySelectorAll('#extensions input[type="checkbox"]').forEach(function(x){
if(x.name.startsWith("enable_") && ! x.checked)
disable.push(x.name.substr(7))

if(x.name.startsWith("update_") && x.checked)
update.push(x.name.substr(7))
})

restart_reload()

return [JSON.stringify(disable), JSON.stringify(update)]
}

function extensions_check(){
gradioApp().querySelectorAll('#extensions .extension_status').forEach(function(x){
x.innerHTML = "Loading..."
})

return []
}
83 changes: 83 additions & 0 deletions modules/extensions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import os
import sys
import traceback

import git

from modules import paths, shared


extensions = []
extensions_dir = os.path.join(paths.script_path, "extensions")


def active():
return [x for x in extensions if x.enabled]


class Extension:
def __init__(self, name, path, enabled=True):
self.name = name
self.path = path
self.enabled = enabled
self.status = ''
self.can_update = False

repo = None
try:
if os.path.exists(os.path.join(path, ".git")):
repo = git.Repo(path)
except Exception:
print(f"Error reading github repository info from {path}:", file=sys.stderr)
print(traceback.format_exc(), file=sys.stderr)

if repo is None or repo.bare:
self.remote = None
else:
self.remote = next(repo.remote().urls, None)
self.status = 'unknown'

def list_files(self, subdir, extension):
from modules import scripts

dirpath = os.path.join(self.path, subdir)
if not os.path.isdir(dirpath):
return []

res = []
for filename in sorted(os.listdir(dirpath)):
res.append(scripts.ScriptFile(dirpath, filename, os.path.join(dirpath, filename)))

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

return res

def check_updates(self):
repo = git.Repo(self.path)
for fetch in repo.remote().fetch("--dry-run"):
if fetch.flags != fetch.HEAD_UPTODATE:
self.can_update = True
self.status = "behind"
return

self.can_update = False
self.status = "latest"

def pull(self):
repo = git.Repo(self.path)
repo.remotes.origin.pull()


def list_extensions():
extensions.clear()

if not os.path.isdir(extensions_dir):
return

for dirname in sorted(os.listdir(extensions_dir)):
path = os.path.join(extensions_dir, dirname)
if not os.path.isdir(path):
continue

extension = Extension(name=dirname, path=path, enabled=dirname not in shared.opts.disabled_extensions)
extensions.append(extension)
5 changes: 5 additions & 0 deletions modules/generation_parameters_copypaste.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
bind_list = []


def reset():
paste_fields.clear()
bind_list.clear()


def quote(text):
if ',' not in str(text):
return text
Expand Down
21 changes: 4 additions & 17 deletions modules/scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import gradio as gr

from modules.processing import StableDiffusionProcessing
from modules import shared, paths, script_callbacks
from modules import shared, paths, script_callbacks, extensions

AlwaysVisible = object()

Expand Down Expand Up @@ -107,17 +107,8 @@ def list_scripts(scriptdirname, extension):
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)
scriptdirpath = os.path.join(dirpath, scriptdirname)

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

for filename in sorted(os.listdir(scriptdirpath)):
scripts_list.append(ScriptFile(dirpath, filename, os.path.join(scriptdirpath, filename)))
for ext in extensions.active():
scripts_list += ext.list_files(scriptdirname, extension)

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

Expand All @@ -127,11 +118,7 @@ def list_scripts(scriptdirname, extension):
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))]
dirs = [paths.script_path] + [ext.path for ext in extensions.active()]

for dirpath in dirs:
if not os.path.isdir(dirpath):
Expand Down
10 changes: 9 additions & 1 deletion modules/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ class State:
current_image = None
current_image_sampling_step = 0
textinfo = None
need_restart = False

def skip(self):
self.skipped = True
Expand Down Expand Up @@ -354,6 +355,12 @@ def options_section(section_identifier, options_dict):
'eta_noise_seed_delta': OptionInfo(0, "Eta noise seed delta", gr.Number, {"precision": 0}),
}))

options_templates.update(options_section((None, "Hidden options"), {
"disabled_extensions": OptionInfo([], "Disable those extensions"),
}))

options_templates.update()


class Options:
data = None
Expand All @@ -365,8 +372,9 @@ def __init__(self):

def __setattr__(self, key, value):
if self.data is not None:
if key in self.data:
if key in self.data or key in self.data_labels:
self.data[key] = value
return

return super(Options, self).__setattr__(key, value)

Expand Down
16 changes: 11 additions & 5 deletions modules/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from PIL import Image, PngImagePlugin


from modules import sd_hijack, sd_models, localization, script_callbacks
from modules import sd_hijack, sd_models, localization, script_callbacks, ui_extensions
from modules.paths import script_path

from modules.shared import opts, cmd_opts, restricted_opts
Expand Down Expand Up @@ -671,6 +671,7 @@ def create_ui(wrap_gradio_gpu_call):
import modules.img2img
import modules.txt2img

parameters_copypaste.reset()

with gr.Blocks(analytics_enabled=False) as txt2img_interface:
txt2img_prompt, roll, txt2img_prompt_style, txt2img_negative_prompt, txt2img_prompt_style2, submit, _, _, txt2img_prompt_style_apply, txt2img_save_style, txt2img_paste, token_counter, token_button = create_toprow(is_img2img=False)
Expand Down Expand Up @@ -1511,8 +1512,9 @@ def run_settings_single(value, key):
column = None
with gr.Row(elem_id="settings").style(equal_height=False):
for i, (k, item) in enumerate(opts.data_labels.items()):
section_must_be_skipped = item.section[0] is None

if previous_section != item.section:
if previous_section != item.section and not section_must_be_skipped:
if cols_displayed < settings_cols and (items_displayed >= items_per_col or previous_section is None):
if column is not None:
column.__exit__()
Expand All @@ -1531,6 +1533,8 @@ def run_settings_single(value, key):
if k in quicksettings_names and not shared.cmd_opts.freeze_settings:
quicksettings_list.append((i, k, item))
components.append(dummy_component)
elif section_must_be_skipped:
components.append(dummy_component)
else:
component = create_setting_component(k)
component_dict[k] = component
Expand Down Expand Up @@ -1572,9 +1576,10 @@ def reload_scripts():

def request_restart():
shared.state.interrupt()
settings_interface.gradio_ref.do_restart = True
shared.state.need_restart = True

restart_gradio.click(

fn=request_restart,
inputs=[],
outputs=[],
Expand Down Expand Up @@ -1612,14 +1617,15 @@ def request_restart():
interfaces += script_callbacks.ui_tabs_callback()
interfaces += [(settings_interface, "Settings", "settings")]

extensions_interface = ui_extensions.create_ui()
interfaces += [(extensions_interface, "Extensions", "extensions")]

with gr.Blocks(css=css, analytics_enabled=False, title="Stable Diffusion") as demo:
with gr.Row(elem_id="quicksettings"):
for i, k, item in quicksettings_list:
component = create_setting_component(k, is_quicksettings=True)
component_dict[k] = component

settings_interface.gradio_ref = demo

parameters_copypaste.integrate_settings_paste_fields(component_dict)
parameters_copypaste.run_bind()

Expand Down
Loading

0 comments on commit 910a097

Please sign in to comment.