Path: blob/master/modules/infotext_utils.py
3055 views
from __future__ import annotations1import base642import io3import json4import os5import re6import sys78import gradio as gr9from modules.paths import data_path10from modules import shared, ui_tempdir, script_callbacks, processing, infotext_versions, images, prompt_parser, errors11from PIL import Image1213sys.modules['modules.generation_parameters_copypaste'] = sys.modules[__name__] # alias for old name1415re_param_code = r'\s*(\w[\w \-/]+):\s*("(?:\\.|[^\\"])+"|[^,]*)(?:,|$)'16re_param = re.compile(re_param_code)17re_imagesize = re.compile(r"^(\d+)x(\d+)$")18re_hypernet_hash = re.compile("\(([0-9a-f]+)\)$")19type_of_gr_update = type(gr.update())202122class ParamBinding:23def __init__(self, paste_button, tabname, source_text_component=None, source_image_component=None, source_tabname=None, override_settings_component=None, paste_field_names=None):24self.paste_button = paste_button25self.tabname = tabname26self.source_text_component = source_text_component27self.source_image_component = source_image_component28self.source_tabname = source_tabname29self.override_settings_component = override_settings_component30self.paste_field_names = paste_field_names or []313233class PasteField(tuple):34def __new__(cls, component, target, *, api=None):35return super().__new__(cls, (component, target))3637def __init__(self, component, target, *, api=None):38super().__init__()3940self.api = api41self.component = component42self.label = target if isinstance(target, str) else None43self.function = target if callable(target) else None444546paste_fields: dict[str, dict] = {}47registered_param_bindings: list[ParamBinding] = []484950def reset():51paste_fields.clear()52registered_param_bindings.clear()535455def quote(text):56if ',' not in str(text) and '\n' not in str(text) and ':' not in str(text):57return text5859return json.dumps(text, ensure_ascii=False)606162def unquote(text):63if len(text) == 0 or text[0] != '"' or text[-1] != '"':64return text6566try:67return json.loads(text)68except Exception:69return text707172def image_from_url_text(filedata):73if filedata is None:74return None7576if type(filedata) == list and filedata and type(filedata[0]) == dict and filedata[0].get("is_file", False):77filedata = filedata[0]7879if type(filedata) == dict and filedata.get("is_file", False):80filename = filedata["name"]81is_in_right_dir = ui_tempdir.check_tmp_file(shared.demo, filename)82assert is_in_right_dir, 'trying to open image file outside of allowed directories'8384filename = filename.rsplit('?', 1)[0]85return images.read(filename)8687if type(filedata) == list:88if len(filedata) == 0:89return None9091filedata = filedata[0]9293if filedata.startswith("data:image/png;base64,"):94filedata = filedata[len("data:image/png;base64,"):]9596filedata = base64.decodebytes(filedata.encode('utf-8'))97image = images.read(io.BytesIO(filedata))98return image99100101def add_paste_fields(tabname, init_img, fields, override_settings_component=None):102103if fields:104for i in range(len(fields)):105if not isinstance(fields[i], PasteField):106fields[i] = PasteField(*fields[i])107108paste_fields[tabname] = {"init_img": init_img, "fields": fields, "override_settings_component": override_settings_component}109110# backwards compatibility for existing extensions111import modules.ui112if tabname == 'txt2img':113modules.ui.txt2img_paste_fields = fields114elif tabname == 'img2img':115modules.ui.img2img_paste_fields = fields116117118def create_buttons(tabs_list):119buttons = {}120for tab in tabs_list:121buttons[tab] = gr.Button(f"Send to {tab}", elem_id=f"{tab}_tab")122return buttons123124125def bind_buttons(buttons, send_image, send_generate_info):126"""old function for backwards compatibility; do not use this, use register_paste_params_button"""127for tabname, button in buttons.items():128source_text_component = send_generate_info if isinstance(send_generate_info, gr.components.Component) else None129source_tabname = send_generate_info if isinstance(send_generate_info, str) else None130131register_paste_params_button(ParamBinding(paste_button=button, tabname=tabname, source_text_component=source_text_component, source_image_component=send_image, source_tabname=source_tabname))132133134def register_paste_params_button(binding: ParamBinding):135registered_param_bindings.append(binding)136137138def connect_paste_params_buttons():139for binding in registered_param_bindings:140destination_image_component = paste_fields[binding.tabname]["init_img"]141fields = paste_fields[binding.tabname]["fields"]142override_settings_component = binding.override_settings_component or paste_fields[binding.tabname]["override_settings_component"]143144destination_width_component = next(iter([field for field, name in fields if name == "Size-1"] if fields else []), None)145destination_height_component = next(iter([field for field, name in fields if name == "Size-2"] if fields else []), None)146147if binding.source_image_component and destination_image_component:148need_send_dementions = destination_width_component and binding.tabname != 'inpaint'149if isinstance(binding.source_image_component, gr.Gallery):150func = send_image_and_dimensions if need_send_dementions else image_from_url_text151jsfunc = "extract_image_from_gallery"152else:153func = send_image_and_dimensions if need_send_dementions else lambda x: x154jsfunc = None155156binding.paste_button.click(157fn=func,158_js=jsfunc,159inputs=[binding.source_image_component],160outputs=[destination_image_component, destination_width_component, destination_height_component] if need_send_dementions else [destination_image_component],161show_progress=False,162)163164if binding.source_text_component is not None and fields is not None:165connect_paste(binding.paste_button, fields, binding.source_text_component, override_settings_component, binding.tabname)166167if binding.source_tabname is not None and fields is not None:168paste_field_names = ['Prompt', 'Negative prompt', 'Steps', 'Face restoration'] + (["Seed"] if shared.opts.send_seed else []) + binding.paste_field_names169binding.paste_button.click(170fn=lambda *x: x,171inputs=[field for field, name in paste_fields[binding.source_tabname]["fields"] if name in paste_field_names],172outputs=[field for field, name in fields if name in paste_field_names],173show_progress=False,174)175176binding.paste_button.click(177fn=None,178_js=f"switch_to_{binding.tabname}",179inputs=None,180outputs=None,181show_progress=False,182)183184185def send_image_and_dimensions(x):186if isinstance(x, Image.Image):187img = x188else:189img = image_from_url_text(x)190191if shared.opts.send_size and isinstance(img, Image.Image):192w = img.width193h = img.height194else:195w = gr.update()196h = gr.update()197198return img, w, h199200201def restore_old_hires_fix_params(res):202"""for infotexts that specify old First pass size parameter, convert it into203width, height, and hr scale"""204205firstpass_width = res.get('First pass size-1', None)206firstpass_height = res.get('First pass size-2', None)207208if shared.opts.use_old_hires_fix_width_height:209hires_width = int(res.get("Hires resize-1", 0))210hires_height = int(res.get("Hires resize-2", 0))211212if hires_width and hires_height:213res['Size-1'] = hires_width214res['Size-2'] = hires_height215return216217if firstpass_width is None or firstpass_height is None:218return219220firstpass_width, firstpass_height = int(firstpass_width), int(firstpass_height)221width = int(res.get("Size-1", 512))222height = int(res.get("Size-2", 512))223224if firstpass_width == 0 or firstpass_height == 0:225firstpass_width, firstpass_height = processing.old_hires_fix_first_pass_dimensions(width, height)226227res['Size-1'] = firstpass_width228res['Size-2'] = firstpass_height229res['Hires resize-1'] = width230res['Hires resize-2'] = height231232233def parse_generation_parameters(x: str, skip_fields: list[str] | None = None):234"""parses generation parameters string, the one you see in text field under the picture in UI:235```236girl with an artist's beret, determined, blue eyes, desert scene, computer monitors, heavy makeup, by Alphonse Mucha and Charlie Bowater, ((eyeshadow)), (coquettish), detailed, intricate237Negative prompt: ugly, fat, obese, chubby, (((deformed))), [blurry], bad anatomy, disfigured, poorly drawn face, mutation, mutated, (extra_limb), (ugly), (poorly drawn hands), messy drawing238Steps: 20, Sampler: Euler a, CFG scale: 7, Seed: 965400086, Size: 512x512, Model hash: 45dee52b239```240241returns a dict with field values242"""243if skip_fields is None:244skip_fields = shared.opts.infotext_skip_pasting245246res = {}247248prompt = ""249negative_prompt = ""250251done_with_prompt = False252253*lines, lastline = x.strip().split("\n")254if len(re_param.findall(lastline)) < 3:255lines.append(lastline)256lastline = ''257258for line in lines:259line = line.strip()260if line.startswith("Negative prompt:"):261done_with_prompt = True262line = line[16:].strip()263if done_with_prompt:264negative_prompt += ("" if negative_prompt == "" else "\n") + line265else:266prompt += ("" if prompt == "" else "\n") + line267268for k, v in re_param.findall(lastline):269try:270if v[0] == '"' and v[-1] == '"':271v = unquote(v)272273m = re_imagesize.match(v)274if m is not None:275res[f"{k}-1"] = m.group(1)276res[f"{k}-2"] = m.group(2)277else:278res[k] = v279except Exception:280print(f"Error parsing \"{k}: {v}\"")281282# Extract styles from prompt283if shared.opts.infotext_styles != "Ignore":284found_styles, prompt_no_styles, negative_prompt_no_styles = shared.prompt_styles.extract_styles_from_prompt(prompt, negative_prompt)285286same_hr_styles = True287if ("Hires prompt" in res or "Hires negative prompt" in res) and (infotext_ver > infotext_versions.v180_hr_styles if (infotext_ver := infotext_versions.parse_version(res.get("Version"))) else True):288hr_prompt, hr_negative_prompt = res.get("Hires prompt", prompt), res.get("Hires negative prompt", negative_prompt)289hr_found_styles, hr_prompt_no_styles, hr_negative_prompt_no_styles = shared.prompt_styles.extract_styles_from_prompt(hr_prompt, hr_negative_prompt)290if same_hr_styles := found_styles == hr_found_styles:291res["Hires prompt"] = '' if hr_prompt_no_styles == prompt_no_styles else hr_prompt_no_styles292res['Hires negative prompt'] = '' if hr_negative_prompt_no_styles == negative_prompt_no_styles else hr_negative_prompt_no_styles293294if same_hr_styles:295prompt, negative_prompt = prompt_no_styles, negative_prompt_no_styles296if (shared.opts.infotext_styles == "Apply if any" and found_styles) or shared.opts.infotext_styles == "Apply":297res['Styles array'] = found_styles298299res["Prompt"] = prompt300res["Negative prompt"] = negative_prompt301302# Missing CLIP skip means it was set to 1 (the default)303if "Clip skip" not in res:304res["Clip skip"] = "1"305306hypernet = res.get("Hypernet", None)307if hypernet is not None:308res["Prompt"] += f"""<hypernet:{hypernet}:{res.get("Hypernet strength", "1.0")}>"""309310if "Hires resize-1" not in res:311res["Hires resize-1"] = 0312res["Hires resize-2"] = 0313314if "Hires sampler" not in res:315res["Hires sampler"] = "Use same sampler"316317if "Hires schedule type" not in res:318res["Hires schedule type"] = "Use same scheduler"319320if "Hires checkpoint" not in res:321res["Hires checkpoint"] = "Use same checkpoint"322323if "Hires prompt" not in res:324res["Hires prompt"] = ""325326if "Hires negative prompt" not in res:327res["Hires negative prompt"] = ""328329if "Mask mode" not in res:330res["Mask mode"] = "Inpaint masked"331332if "Masked content" not in res:333res["Masked content"] = 'original'334335if "Inpaint area" not in res:336res["Inpaint area"] = "Whole picture"337338if "Masked area padding" not in res:339res["Masked area padding"] = 32340341restore_old_hires_fix_params(res)342343# Missing RNG means the default was set, which is GPU RNG344if "RNG" not in res:345res["RNG"] = "GPU"346347if "Schedule type" not in res:348res["Schedule type"] = "Automatic"349350if "Schedule max sigma" not in res:351res["Schedule max sigma"] = 0352353if "Schedule min sigma" not in res:354res["Schedule min sigma"] = 0355356if "Schedule rho" not in res:357res["Schedule rho"] = 0358359if "VAE Encoder" not in res:360res["VAE Encoder"] = "Full"361362if "VAE Decoder" not in res:363res["VAE Decoder"] = "Full"364365if "FP8 weight" not in res:366res["FP8 weight"] = "Disable"367368if "Cache FP16 weight for LoRA" not in res and res["FP8 weight"] != "Disable":369res["Cache FP16 weight for LoRA"] = False370371prompt_attention = prompt_parser.parse_prompt_attention(prompt)372prompt_attention += prompt_parser.parse_prompt_attention(negative_prompt)373prompt_uses_emphasis = len(prompt_attention) != len([p for p in prompt_attention if p[1] == 1.0 or p[0] == 'BREAK'])374if "Emphasis" not in res and prompt_uses_emphasis:375res["Emphasis"] = "Original"376377if "Refiner switch by sampling steps" not in res:378res["Refiner switch by sampling steps"] = False379380infotext_versions.backcompat(res)381382for key in skip_fields:383res.pop(key, None)384385return res386387388infotext_to_setting_name_mapping = [389390]391"""Mapping of infotext labels to setting names. Only left for backwards compatibility - use OptionInfo(..., infotext='...') instead.392Example content:393394infotext_to_setting_name_mapping = [395('Conditional mask weight', 'inpainting_mask_weight'),396('Model hash', 'sd_model_checkpoint'),397('ENSD', 'eta_noise_seed_delta'),398('Schedule type', 'k_sched_type'),399]400"""401402403def create_override_settings_dict(text_pairs):404"""creates processing's override_settings parameters from gradio's multiselect405406Example input:407['Clip skip: 2', 'Model hash: e6e99610c4', 'ENSD: 31337']408409Example output:410{'CLIP_stop_at_last_layers': 2, 'sd_model_checkpoint': 'e6e99610c4', 'eta_noise_seed_delta': 31337}411"""412413res = {}414415params = {}416for pair in text_pairs:417k, v = pair.split(":", maxsplit=1)418419params[k] = v.strip()420421mapping = [(info.infotext, k) for k, info in shared.opts.data_labels.items() if info.infotext]422for param_name, setting_name in mapping + infotext_to_setting_name_mapping:423value = params.get(param_name, None)424425if value is None:426continue427428res[setting_name] = shared.opts.cast_value(setting_name, value)429430return res431432433def get_override_settings(params, *, skip_fields=None):434"""Returns a list of settings overrides from the infotext parameters dictionary.435436This function checks the `params` dictionary for any keys that correspond to settings in `shared.opts` and returns437a list of tuples containing the parameter name, setting name, and new value cast to correct type.438439It checks for conditions before adding an override:440- ignores settings that match the current value441- ignores parameter keys present in skip_fields argument.442443Example input:444{"Clip skip": "2"}445446Example output:447[("Clip skip", "CLIP_stop_at_last_layers", 2)]448"""449450res = []451452mapping = [(info.infotext, k) for k, info in shared.opts.data_labels.items() if info.infotext]453for param_name, setting_name in mapping + infotext_to_setting_name_mapping:454if param_name in (skip_fields or {}):455continue456457v = params.get(param_name, None)458if v is None:459continue460461if setting_name == "sd_model_checkpoint" and shared.opts.disable_weights_auto_swap:462continue463464v = shared.opts.cast_value(setting_name, v)465current_value = getattr(shared.opts, setting_name, None)466467if v == current_value:468continue469470res.append((param_name, setting_name, v))471472return res473474475def connect_paste(button, paste_fields, input_comp, override_settings_component, tabname):476def paste_func(prompt):477if not prompt and not shared.cmd_opts.hide_ui_dir_config and not shared.cmd_opts.no_prompt_history:478filename = os.path.join(data_path, "params.txt")479try:480with open(filename, "r", encoding="utf8") as file:481prompt = file.read()482except OSError:483pass484485params = parse_generation_parameters(prompt)486script_callbacks.infotext_pasted_callback(prompt, params)487res = []488489for output, key in paste_fields:490if callable(key):491try:492v = key(params)493except Exception:494errors.report(f"Error executing {key}", exc_info=True)495v = None496else:497v = params.get(key, None)498499if v is None:500res.append(gr.update())501elif isinstance(v, type_of_gr_update):502res.append(v)503else:504try:505valtype = type(output.value)506507if valtype == bool and v == "False":508val = False509elif valtype == int:510val = float(v)511else:512val = valtype(v)513514res.append(gr.update(value=val))515except Exception:516res.append(gr.update())517518return res519520if override_settings_component is not None:521already_handled_fields = {key: 1 for _, key in paste_fields}522523def paste_settings(params):524vals = get_override_settings(params, skip_fields=already_handled_fields)525526vals_pairs = [f"{infotext_text}: {value}" for infotext_text, setting_name, value in vals]527528return gr.Dropdown.update(value=vals_pairs, choices=vals_pairs, visible=bool(vals_pairs))529530paste_fields = paste_fields + [(override_settings_component, paste_settings)]531532button.click(533fn=paste_func,534inputs=[input_comp],535outputs=[x[0] for x in paste_fields],536show_progress=False,537)538button.click(539fn=None,540_js=f"recalculate_prompts_{tabname}",541inputs=[],542outputs=[],543show_progress=False,544)545546547548