From a0b861ee16b12dc302092764d94ba189821bc252 Mon Sep 17 00:00:00 2001 From: bluelovers Date: Thu, 6 Jun 2024 03:34:04 +0800 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20Update=20tool.py=20=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E6=89=80=E6=9C=89=20JSON=20=E5=BD=A2=E5=BC=8F?= =?UTF-8?q?=E5=92=8C=E6=99=AE=E9=80=9A=E6=96=87=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增强后的正则表达式以支持所有 JSON 形式和普通文字 Enhanced regular expression to support all JSON formats and plain text https://onecompiler.com/python/42fcba277 --- scripts/iib/tool.py | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/scripts/iib/tool.py b/scripts/iib/tool.py index c115c8c6..3d99f601 100644 --- a/scripts/iib/tool.py +++ b/scripts/iib/tool.py @@ -450,7 +450,9 @@ def read_sd_webui_gen_info_from_image(image: Image, path="") -> str: return geninfo -re_param_code = r'\s*([\w ]+):\s*("(?:\\"[^,]|\\"|\\|[^\"])+"|[^,]*)(?:,|$)' +# 增强后的正则表达式以支持所有 JSON 形式和普通文字 +# 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_lora_prompt = re.compile("", re.IGNORECASE) @@ -526,22 +528,26 @@ def parse_generation_parameters(x: str): else: prompt += ("" if prompt == "" else "\n") + line + # 增加解析和处理数组和对象的逻辑 for k, v in re_param.findall(lastline): try: - if len(v) == 0: - res[k] = v - continue - 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}") prompt_parse_res = parse_prompt(prompt) lora = prompt_parse_res["lora"] From cb3cb96792535f4436da5ea39313489202578187 Mon Sep 17 00:00:00 2001 From: bluelovers Date: Thu, 6 Jun 2024 07:28:45 +0800 Subject: [PATCH 2/3] Update tool.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 減少一行代碼 但閱讀性差了一點 --- scripts/iib/tool.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/scripts/iib/tool.py b/scripts/iib/tool.py index 3d99f601..8beda1b4 100644 --- a/scripts/iib/tool.py +++ b/scripts/iib/tool.py @@ -533,9 +533,7 @@ def parse_generation_parameters(x: str): try: if v.startswith('"') and v.endswith('"'): v = unquote(v) - elif v.startswith('[') and v.endswith(']'): - v = json.loads(v) - elif v.startswith('{') and v.endswith('}'): + elif v.startswith('[') and v.endswith(']') or v.startswith('{') and v.endswith('}'): v = json.loads(v) else: m = re_imagesize.match(v) From 9cb68f09e3a04a149999286afa5e50223aec34ac Mon Sep 17 00:00:00 2001 From: bluelovers Date: Thu, 6 Jun 2024 07:44:52 +0800 Subject: [PATCH 3/3] =?UTF-8?q?fix:=20=E9=98=B2=E6=AD=A2=E7=84=A1=E6=B3=95?= =?UTF-8?q?=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/iib/tool.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/iib/tool.py b/scripts/iib/tool.py index 8beda1b4..4f8ae44e 100644 --- a/scripts/iib/tool.py +++ b/scripts/iib/tool.py @@ -542,10 +542,10 @@ def parse_generation_parameters(x: str): 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}") + + res[k] = v prompt_parse_res = parse_prompt(prompt) lora = prompt_parse_res["lora"]