Skip to content
Closed
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
29 changes: 19 additions & 10 deletions modules/infotext_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

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]+)\)$")
Expand Down Expand Up @@ -267,17 +268,25 @@ def parse_generation_parameters(x: str, skip_fields: list[str] | None = None):

for k, v in re_param.findall(lastline):
try:
if v[0] == '"' and v[-1] == '"':
v = unquote(v)
v = v.strip()

m = re_imagesize.match(v)
if m is not None:
res[f"{k}-1"] = m.group(1)
res[f"{k}-2"] = m.group(2)
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('}'):
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

except Exception as e:
print(f"Error parsing \"{k}: {v}\": {e}")

res[k] = v

# Extract styles from prompt
if shared.opts.infotext_styles != "Ignore":
Expand Down