feat: update parse_generation_parameters

> Enhanced regular expression to support all JSON formats and plain text

https://onecompiler.com/python/42fcba277
This commit is contained in:
bluelovers 2024-06-06 03:23:47 +08:00 committed by GitHub
parent 801b72b92b
commit 9af67abfcf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -13,7 +13,8 @@ from PIL import Image
sys.modules['modules.generation_parameters_copypaste'] = sys.modules[__name__] # alias for old name
re_param_code = r'\s*(\w[\w \-/]+):\s*("(?:\\.|[^\\"])+"|[^,]*)(?:,|$)'
# Enhanced regular expression to support all JSON formats and plain text
re_param_code = r'\s*(\w[\w \-/]+):\s*({.*?}|\[.*?\]|"(?:\\.|[^\\"])*"|[^,]*)(?:,|$)'
re_param = re.compile(re_param_code)
re_imagesize = re.compile(r"^(\d+)x(\d+)$")
re_hypernet_hash = re.compile("\(([0-9a-f]+)\)$")
@ -267,17 +268,23 @@ Steps: 20, Sampler: Euler a, CFG scale: 7, Seed: 965400086, Size: 512x512, Model
for k, v in re_param.findall(lastline):
try:
if v[0] == '"' and v[-1] == '"':
if v.startswith('"') and v.endswith('"'):
v = unquote(v)
m = re_imagesize.match(v)
if m is not None:
res[f"{k}-1"] = m.group(1)
res[f"{k}-2"] = m.group(2)
elif v.startswith('[') and v.endswith(']'):
v = json.loads(v)
elif v.startswith('{') and v.endswith('}'):
v = json.loads(v)
else:
res[k] = v
except Exception:
print(f"Error parsing \"{k}: {v}\"")
m = re_imagesize.match(v)
if m:
res[f"{k}-1"] = m.group(1)
res[f"{k}-2"] = m.group(2)
continue
v = v.strip() # Remove surrounding spaces for non-JSON values
res[k] = v
except Exception as e:
print(f"Error parsing \"{k}: {v}\": {e}")
# Extract styles from prompt
if shared.opts.infotext_styles != "Ignore":