From 9af67abfcfca997d0c3c00e1ee8c18c46a233849 Mon Sep 17 00:00:00 2001 From: bluelovers Date: Thu, 6 Jun 2024 03:23:47 +0800 Subject: [PATCH] feat: update parse_generation_parameters > Enhanced regular expression to support all JSON formats and plain text https://onecompiler.com/python/42fcba277 --- modules/infotext_utils.py | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/modules/infotext_utils.py b/modules/infotext_utils.py index f1e8f54ba..e06d8a30d 100644 --- a/modules/infotext_utils.py +++ b/modules/infotext_utils.py @@ -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":