Skip to content

Commit

Permalink
make it possible to change models etc by editing options using API
Browse files Browse the repository at this point in the history
  • Loading branch information
AUTOMATIC1111 committed Nov 19, 2022
1 parent 84a6f21 commit 5a6387e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
7 changes: 3 additions & 4 deletions modules/api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,8 @@ def get_config(self):
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
Expand All @@ -264,7 +263,7 @@ def get_cmd_flags(self):
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 = []
Expand Down
17 changes: 17 additions & 0 deletions modules/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,23 @@ def __getattr__(self, item):

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"

Expand Down
22 changes: 4 additions & 18 deletions modules/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -1484,16 +1484,9 @@ def run_settings(*args):
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:
Expand All @@ -1504,15 +1497,8 @@ def run_settings_single(value, key):
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)

Expand Down

0 comments on commit 5a6387e

Please sign in to comment.