compact infotaxt info_json

This commit is contained in:
w-e-w 2024-01-23 02:17:21 +09:00
parent 8a6a4ad894
commit 36fb876da6
3 changed files with 34 additions and 2 deletions

View File

@ -18,6 +18,34 @@ re_param = re.compile(re_param_code)
re_imagesize = re.compile(r"^(\d+)x(\d+)$")
re_hypernet_hash = re.compile("\(([0-9a-f]+)\)$")
type_of_gr_update = type(gr.update())
quote_swap = str.maketrans('\'"', '"\'')
info_json_keys = set()
def info_json_dumps(data):
"""encode data into json string, but swap single and double quotes to reduce escaping issues"""
return json.dumps(data, ensure_ascii=False).translate(quote_swap)
def info_json_loads(info_json):
"""decode json string into info data, but swap single and double quotes to reduce escaping issues"""
return json.loads(info_json.translate(quote_swap))
def build_infotext(info: dict):
for info_json_key in info_json_keys:
if info_json_key in info:
info[info_json_key] = info_json_dumps(info[info_json_key])
return ", ".join([k if k == v else f'{k}: {quote(v)}' for k, v in info.items() if v is not None])
def register_info_json(key):
"""register an infotext key as infojson
after a key is registered, a json compatible data structure like dict or list can be used as a value in
generation_parameters and extra_generation_parameters
"""
global info_json_keys
info_json_keys.add(key)
class ParamBinding:
@ -356,6 +384,10 @@ Steps: 20, Sampler: Euler a, CFG scale: 7, Seed: 965400086, Size: 512x512, Model
if "Cache FP16 weight for LoRA" not in res and res["FP8 weight"] != "Disable":
res["Cache FP16 weight for LoRA"] = False
for key in info_json_keys:
if key in res:
res[key] = info_json_loads(res[key])
infotext_versions.backcompat(res)
for key in skip_fields:

View File

@ -84,7 +84,7 @@ def run_postprocessing(extras_mode, image, image_folder, input_dir, output_dir,
basename = ''
forced_filename = None
infotext = ", ".join([k if k == v else f'{k}: {infotext_utils.quote(v)}' for k, v in pp.info.items() if v is not None])
infotext = infotext_utils.build_infotext(pp.info)
if opts.enable_pnginfo:
pp.image.info = existing_pnginfo

View File

@ -747,7 +747,7 @@ def create_infotext(p, all_prompts, all_seeds, all_subseeds, comments=None, iter
"User": p.user if opts.add_user_name_to_info else None,
}
generation_params_text = ", ".join([k if k == v else f'{k}: {infotext_utils.quote(v)}' for k, v in generation_params.items() if v is not None])
generation_params_text = infotext_utils.build_infotext(generation_params)
prompt_text = p.main_prompt if use_main_prompt else all_prompts[index]
negative_prompt_text = f"\nNegative prompt: {p.main_negative_prompt if use_main_prompt else all_negative_prompts[index]}" if all_negative_prompts[index] else ""