From 5a6387e189dc365c47a7979b9040d5b6fdd7ba43 Mon Sep 17 00:00:00 2001 From: AUTOMATIC <16777216c@gmail.com> Date: Sat, 19 Nov 2022 15:15:24 +0300 Subject: [PATCH] make it possible to change models etc by editing options using API --- modules/api/api.py | 7 +++---- modules/shared.py | 17 +++++++++++++++++ modules/ui.py | 22 ++++------------------ 3 files changed, 24 insertions(+), 22 deletions(-) diff --git a/modules/api/api.py b/modules/api/api.py index bf700ed0a..1e324d8d6 100644 --- a/modules/api/api.py +++ b/modules/api/api.py @@ -253,9 +253,8 @@ class Api: return options def set_config(self, req: Dict[str, Any]): - - for o in req: - setattr(shared.opts, o, req[o]) + for k, v in req.items(): + shared.opts.set(k, v) shared.opts.save(shared.config_filename) return @@ -264,7 +263,7 @@ class Api: return vars(shared.cmd_opts) def get_samplers(self): - return [{"name":sampler[0], "aliases":sampler[2], "options":sampler[3]} for sampler in sd_samplers.all_samplers] + return [{"name": sampler[0], "aliases":sampler[2], "options":sampler[3]} for sampler in sd_samplers.all_samplers] def get_upscalers(self): upscalers = [] diff --git a/modules/shared.py b/modules/shared.py index 84567c8ee..58f53e544 100644 --- a/modules/shared.py +++ b/modules/shared.py @@ -437,6 +437,23 @@ class Options: return super(Options, self).__getattribute__(item) + def set(self, key, value): + """sets an option and calls its onchange callback, returning True if the option changed and False otherwise""" + + oldval = self.data.get(key, None) + if oldval == value: + return False + + try: + setattr(self, key, value) + except RuntimeError: + return False + + if self.data_labels[key].onchange is not None: + self.data_labels[key].onchange() + + return True + def save(self, filename): assert not cmd_opts.freeze_settings, "saving settings is disabled" diff --git a/modules/ui.py b/modules/ui.py index 5dd977541..bb090c625 100644 --- a/modules/ui.py +++ b/modules/ui.py @@ -1484,16 +1484,9 @@ def create_ui(wrap_gradio_gpu_call): if comp == dummy_component: continue - oldval = opts.data.get(key, None) - try: - setattr(opts, key, value) - except RuntimeError: - continue - if oldval != value: - if opts.data_labels[key].onchange is not None: - opts.data_labels[key].onchange() - + if opts.set(key, value): changed.append(key) + try: opts.save(shared.config_filename) except RuntimeError: @@ -1504,15 +1497,8 @@ def create_ui(wrap_gradio_gpu_call): if not opts.same_type(value, opts.data_labels[key].default): return gr.update(visible=True), opts.dumpjson() - oldval = opts.data.get(key, None) - try: - setattr(opts, key, value) - except Exception: - return gr.update(value=oldval), opts.dumpjson() - - if oldval != value: - if opts.data_labels[key].onchange is not None: - opts.data_labels[key].onchange() + if not opts.set(key, value): + return gr.update(value=getattr(opts, key)), opts.dumpjson() opts.save(shared.config_filename)