From 6fa20d51dcfd7db3286bc9dad31455b69b6daf65 Mon Sep 17 00:00:00 2001 From: AUTOMATIC <16777216c@gmail.com> Date: Fri, 23 Sep 2022 17:27:30 +0300 Subject: [PATCH] prevent saving settings with bogus values --- modules/shared.py | 23 +++++++++++------------ modules/txt2img.py | 4 +--- modules/ui.py | 15 ++++++++++----- 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/modules/shared.py b/modules/shared.py index 0978f3f6..0d7b8623 100644 --- a/modules/shared.py +++ b/modules/shared.py @@ -219,6 +219,7 @@ options_templates.update(options_section(('ui', "User interface"), { class Options: data = None data_labels = options_templates + typemap = {int: float} def __init__(self): self.data = {k: v.default for k, v in self.data_labels.items()} @@ -244,25 +245,23 @@ class Options: with open(filename, "w", encoding="utf8") as file: json.dump(self.data, file) + def same_type(self, x, y): + if x is None or y is None: + return True + + type_x = self.typemap.get(type(x), type(x)) + type_y = self.typemap.get(type(y), type(y)) + + return type_x == type_y + def load(self, filename): with open(filename, "r", encoding="utf8") as file: self.data = json.load(file) - typemap = {int: float} - - def same_type(x, y): - if x is None or y is None: - return True - - type_x = typemap.get(type(x), type(x)) - type_y = typemap.get(type(y), type(y)) - - return type_x == type_y - bad_settings = 0 for k, v in self.data.items(): info = self.data_labels.get(k, None) - if info is not None and not same_type(info.default, v): + if info is not None and not self.same_type(info.default, v): print(f"Warning: bad setting value: {k}: {v} ({type(v).__name__}; expected {type(info.default).__name__})", file=sys.stderr) bad_settings += 1 diff --git a/modules/txt2img.py b/modules/txt2img.py index d2cf39ef..5368e4d0 100644 --- a/modules/txt2img.py +++ b/modules/txt2img.py @@ -37,9 +37,7 @@ def txt2img(prompt: str, negative_prompt: str, prompt_style: str, prompt_style2: print(f"\ntxt2img: {prompt}", file=shared.progress_print_out) processed = modules.scripts.scripts_txt2img.run(p, *args) - if processed is not None: - pass - else: + if processed is None: processed = process_images(p) shared.total_tqdm.clear() diff --git a/modules/ui.py b/modules/ui.py index 3f92efc6..1cbff1fd 100644 --- a/modules/ui.py +++ b/modules/ui.py @@ -831,7 +831,11 @@ def create_ui(txt2img, img2img, run_extras, run_pnginfo): components = [] def run_settings(*args): - up = [] + changed = 0 + + for key, value, comp in zip(opts.data_labels.keys(), args, components): + if not opts.same_type(value, opts.data_labels[key].default): + return f"Bad value for setting {key}: {value}; expecting {type(opts.data_labels[key].default).__name__}" for key, value, comp in zip(opts.data_labels.keys(), args, components): comp_args = opts.data_labels[key].component_args @@ -841,14 +845,15 @@ def create_ui(txt2img, img2img, run_extras, run_pnginfo): oldval = opts.data.get(key, None) opts.data[key] = value - if oldval != value and opts.data_labels[key].onchange is not None: - opts.data_labels[key].onchange() + if oldval != value: + if opts.data_labels[key].onchange is not None: + opts.data_labels[key].onchange() - up.append(comp.update(value=value)) + changed += 1 opts.save(shared.config_filename) - return 'Settings applied.' + return f'{changed} settings changed.' with gr.Blocks(analytics_enabled=False) as settings_interface: settings_submit = gr.Button(value="Apply settings", variant='primary')