fix controlnet causing crash. enhance script compatability

pull/15/head
unknown 2023-05-31 10:54:56 -05:00
parent 1ce405cc51
commit 3e448e4fcc
No known key found for this signature in database
GPG Key ID: CA376082283AF69A
1 changed files with 29 additions and 34 deletions

View File

@ -289,44 +289,38 @@ class Script(scripts.Script):
Script.initialize(initial_payload=p) Script.initialize(initial_payload=p)
# strip scripts that aren't yet supported and warn user # strip scripts that aren't yet supported and warn user
controlnet = None packed_script_args: List[dict] = [] # list of api formatted per-script argument objects
arg = 0 for script in p.scripts.scripts:
while arg < len(p.script_args) - 1: if script.alwayson is not True:
try: continue
json.dumps(p.script_args[arg]) title = script.title()
except Exception:
# find which script owns the offending arguments and fix if supported # check for supported scripts
script_args_upper = None # the upper index of the offending scripts args so that we can skip the rest if title == "ControlNet":
for script in p.scripts.scripts: # grab all controlnet units
title = script.title() cn_units = []
cn_args = p.script_args[script.args_from:script.args_to]
for cn_arg in cn_args:
if type(cn_arg).__name__ == "UiControlNetUnit":
cn_units.append(cn_arg)
logger.debug(f"Detected {len(cn_units)} controlnet unit(s)")
# check for supported scripts # get api formatted controlnet
if title == "ControlNet" and script.alwayson is True: packed_script_args.append(pack_control_net(cn_units))
# grab all controlnet units
cn_units = []
for cn_arg in range(script.args_from, script.args_to + 1):
if isinstance(p.script_args[cn_arg], type(p.script_args[arg])):
cn_units.append(p.script_args[cn_arg])
logger.debug(f"Detected {len(cn_units)} controlnet unit(s)")
# get api formatted controlnet continue
controlnet: dict = pack_control_net(cn_units) else:
script_serial = {
script.name: {'args': []}
}
# ensure we don't do this more than once this_args = p.script_args[script.args_from:script.args_to]
script_args_upper = script.args_to this_args = json.dumps(this_args, default=lambda k: '<not serializable>')
continue script_serial[script.name]['args'] = this_args
# clean unsupported scripts packed_script_args.append(script_serial)
if script.args_from <= arg <= script.args_to: logger.warning(f"Distributed doesn't officially support '{title}'")
logger.warn(f"Distributed does not yet support '{title}'")
sanitized_script_args = p.script_args[:arg] + p.script_args[arg + 1:]
p.script_args = sanitized_script_args
if script_args_upper is not None:
arg = script_args_upper
arg += 1
# encapsulating the request object within a txt2imgreq object is deprecated and no longer works # encapsulating the request object within a txt2imgreq object is deprecated and no longer works
# see test/basic_features/txt2img_test.py for an example # see test/basic_features/txt2img_test.py for an example
@ -335,8 +329,9 @@ class Script(scripts.Script):
payload['scripts'] = None payload['scripts'] = None
del payload['script_args'] del payload['script_args']
if controlnet is not None: payload['alwayson_scripts'] = {}
payload['alwayson_scripts'] = controlnet for packed in packed_script_args:
payload['alwayson_scripts'].update(packed)
# TODO api for some reason returns 200 even if something failed to be set. # TODO api for some reason returns 200 even if something failed to be set.
# for now we may have to make redundant GET requests to check if actually successful... # for now we may have to make redundant GET requests to check if actually successful...