From 78278ce695beffbf59c7320bb0441922d66b1c0e Mon Sep 17 00:00:00 2001 From: AUTOMATIC <16777216c@gmail.com> Date: Sun, 4 Sep 2022 13:52:01 +0300 Subject: [PATCH] added UI config file: ui-config.json --- README.md | 1 + modules/ui.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7432a9e9c..610826c29 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,7 @@ Original script with Gradio UI was written by a kind anonymous user. This is a m - Settings page - Running custom code from UI - Mouseover hints fo most UI elements +- Possible to change defaults/mix/max/step values for UI elements via text config ## Installing and running diff --git a/modules/ui.py b/modules/ui.py index 65d53bcd3..d6b39c2fd 100644 --- a/modules/ui.py +++ b/modules/ui.py @@ -133,8 +133,15 @@ def wrap_gradio_call(func): return f -def create_ui(txt2img, img2img, run_extras, run_pnginfo): +def visit(x, func, path=""): + if hasattr(x, 'children'): + for c in x.children: + visit(c, func, path) + elif x.label is not None: + func(path + "/" + str(x.label), x) + +def create_ui(txt2img, img2img, run_extras, run_pnginfo): with gr.Blocks(analytics_enabled=False) as txt2img_interface: with gr.Row(): prompt = gr.Textbox(label="Prompt", elem_id="txt2img_prompt", show_label=False, placeholder="Prompt", lines=1) @@ -271,7 +278,6 @@ def create_ui(txt2img, img2img, run_extras, run_pnginfo): with gr.Group(): custom_inputs = modules.scripts.scripts_img2img.setup_ui(is_img2img=True) - with gr.Column(variant='panel'): with gr.Group(): img2img_gallery = gr.Gallery(label='Output', elem_id='img2img_gallery') @@ -517,6 +523,46 @@ def create_ui(txt2img, img2img, run_extras, run_pnginfo): css=css, ) + ui_config_file = os.path.join(modules.paths.script_path, 'ui-config.json') + ui_settings = {} + settings_count = len(ui_settings) + error_loading = False + + try: + if os.path.exists(ui_config_file): + with open(ui_config_file, "r", encoding="utf8") as file: + ui_settings = json.load(file) + except Exception: + error_loading = True + print("Error loading settings:", file=sys.stderr) + print(traceback.format_exc(), file=sys.stderr) + + def loadsave(path, x): + def apply_field(obj, field): + key = path + "/" + field + + saved_value = ui_settings.get(key, None) + if saved_value is None: + ui_settings[key] = getattr(obj, field) + else: + setattr(obj, field, saved_value) + + if type(x) == gr.Slider: + apply_field(x, 'value') + apply_field(x, 'minimum') + apply_field(x, 'maximum') + apply_field(x, 'step') + + if type(x) == gr.Radio: + apply_field(x, 'value') + + visit(txt2img_interface, loadsave, "txt2img") + visit(img2img_interface, loadsave, "img2img") + + if not error_loading and (not os.path.exists(ui_config_file) or settings_count != len(ui_settings)): + with open(ui_config_file, "w", encoding="utf8") as file: + json.dump(ui_settings, file, indent=4) + return demo