Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Update tool.py 支持所有 JSON 形式和普通文字 #648

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions scripts/iib/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -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*({.*?}|\[.*?\]|"(?:\\.|[^\\"])*"|[^,]*)(?:,|$)'
Copy link
Owner

@zanllp zanllp Jun 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个正则式一般是和sd-webui的保持一致。IIB是为了要独立运行所以才自己搞了一个没有直接import
image

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

因為原版也有這個BUG 無法完整分析

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

因為原版也有這個BUG 無法完整分析

能提供下图片源文件(会发生bug的那个)吗?

我这边有个文件夹专门收集这些边缘case

image

re_param = re.compile(re_param_code)
re_imagesize = re.compile(r"^(\d+)x(\d+)$")
re_lora_prompt = re.compile("<lora:([\w_\s.]+)(?::([\d.]+))+>", re.IGNORECASE)
Expand Down Expand Up @@ -526,22 +528,24 @@ 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(']') or 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
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"]
Expand Down