| from __future__ import annotations
|
|
|
| import base64
|
| import io
|
| import json
|
| import os
|
| import re
|
| from functools import partial
|
| from typing import Any, Callable
|
|
|
| import gradio as gr
|
| from PIL import Image
|
|
|
| from modules import images, processing, prompt_parser, script_callbacks, shared, ui_tempdir
|
| from modules.paths import data_path
|
|
|
| re_param_code = r'\s*(\w[\w \-/]+):\s*("(?:\\.|[^\\"])+"|[^,]*)(?:,|$)'
|
| re_param = re.compile(re_param_code)
|
| re_imagesize = re.compile(r"^(\d+)x(\d+)$")
|
| type_of_gr_update = type(gr.skip())
|
|
|
|
|
| class ParamBinding:
|
| def __init__(self, paste_button, tabname, source_text_component=None, source_image_component=None, source_tabname=None, override_settings_component=None, paste_field_names=None, *, is_paste=False):
|
| self.paste_button: gr.Button = paste_button
|
| self.tabname: str = tabname
|
| self.source_text_component: gr.Textbox = source_text_component
|
| self.source_image_component: gr.Gallery = source_image_component
|
| self.source_tabname: str = source_tabname
|
| self.override_settings_component: list[gr.components.IOComponent] = override_settings_component
|
| self.paste_field_names: list[str] = paste_field_names or []
|
| self.is_paste: bool = is_paste
|
|
|
|
|
| class PasteField(tuple):
|
| def __new__(cls, component, target, *, api=None):
|
| return super().__new__(cls, (component, target))
|
|
|
| def __init__(self, component, target, *, api=None):
|
| super().__init__()
|
|
|
| self.api = api
|
| self.component = component
|
| self.label = target if isinstance(target, str) else None
|
| self.function = target if callable(target) else None
|
|
|
|
|
| paste_fields: dict[str, dict] = {}
|
| registered_param_bindings: list[ParamBinding] = []
|
|
|
|
|
| def reset():
|
| paste_fields.clear()
|
| registered_param_bindings.clear()
|
|
|
|
|
| def quote(text: str) -> str:
|
| if "," not in str(text) and "\n" not in str(text) and ":" not in str(text):
|
| return text
|
|
|
| return json.dumps(text, ensure_ascii=False)
|
|
|
|
|
| def unquote(text: str) -> str:
|
| if len(text) == 0 or text[0] != '"' or text[-1] != '"':
|
| return text
|
|
|
| try:
|
| return json.loads(text)
|
| except Exception:
|
| return text
|
|
|
|
|
| def image_from_url_text(filedata: str | dict) -> Image.Image:
|
| if filedata is None:
|
| return None
|
|
|
| if type(filedata) == list and filedata and type(filedata[0]) == dict and filedata[0].get("is_file", False):
|
| filedata = filedata[0]
|
|
|
| if type(filedata) == dict and filedata.get("is_file", False):
|
| filename: str = filedata["name"]
|
| is_in_right_dir = ui_tempdir.check_tmp_file(shared.demo, filename)
|
| assert is_in_right_dir, "trying to open image file outside of allowed directories"
|
|
|
| filename = filename.rsplit("?", 1)[0]
|
| return Image.open(filename)
|
|
|
| if type(filedata) == list:
|
| if len(filedata) == 0:
|
| return None
|
|
|
| filedata = filedata[0]
|
|
|
| if filedata.startswith("data:image/png;base64,"):
|
| filedata = filedata[22:]
|
|
|
| filedata = base64.decodebytes(filedata.encode("utf-8"))
|
| image = Image.open(io.BytesIO(filedata))
|
| return image
|
|
|
|
|
| def add_paste_fields(tabname, init_img, fields, override_settings_component=None):
|
|
|
| if fields:
|
| for i in range(len(fields)):
|
| if not isinstance(fields[i], PasteField):
|
| fields[i] = PasteField(*fields[i])
|
|
|
| paste_fields[tabname] = {"init_img": init_img, "fields": fields, "override_settings_component": override_settings_component}
|
|
|
|
|
| import modules.ui
|
|
|
| if tabname == "txt2img":
|
| modules.ui.txt2img_paste_fields = fields
|
| elif tabname == "img2img":
|
| modules.ui.img2img_paste_fields = fields
|
|
|
|
|
| def create_buttons(tabs_list):
|
| buttons = {}
|
| for tab in tabs_list:
|
| buttons[tab] = gr.Button(f"Send to {tab}", elem_id=f"{tab}_tab")
|
| return buttons
|
|
|
|
|
| def bind_buttons(*args, **kwargs):
|
| raise NotImplementedError("use register_paste_params_button instead")
|
|
|
|
|
| def register_paste_params_button(binding: ParamBinding):
|
| registered_param_bindings.append(binding)
|
|
|
|
|
| def _parse_info(output: gr.components.IOComponent, key: str | Callable, params: dict[str, Any]):
|
| if callable(key):
|
| v = key(params)
|
| else:
|
| v = params.get(key, None)
|
|
|
| if v is None:
|
| return gr.skip()
|
| if isinstance(v, type_of_gr_update):
|
| return v
|
|
|
| try:
|
| valtype = type(output.value)
|
|
|
| if valtype == bool and v == "False":
|
| val = False
|
| elif valtype == int:
|
| val = float(v)
|
| else:
|
| val = valtype(v)
|
|
|
| return gr.update(value=val)
|
| except Exception:
|
| return gr.skip()
|
|
|
|
|
| def connect_paste_params_buttons():
|
| for binding in registered_param_bindings:
|
| if binding.tabname not in paste_fields:
|
| continue
|
| destination_image_component = paste_fields[binding.tabname]["init_img"]
|
| fields = paste_fields[binding.tabname]["fields"]
|
| override_settings_component = binding.override_settings_component or paste_fields[binding.tabname]["override_settings_component"]
|
|
|
| destination_width_component = next(iter([field for field, name in fields if name == "Size-1"] if fields else []), None)
|
| destination_height_component = next(iter([field for field, name in fields if name == "Size-2"] if fields else []), None)
|
|
|
| if binding.source_image_component and destination_image_component:
|
| if isinstance(binding.source_image_component, gr.Gallery):
|
| func = send_image_and_dimensions if destination_width_component else image_from_url_text
|
| jsfunc = f'(gal) => {{ return extract_image_from_gallery(gal, "{binding.source_tabname}"); }}'
|
| else:
|
| func = send_image_and_dimensions if destination_width_component else lambda x: x
|
| jsfunc = None
|
|
|
| binding.paste_button.click(
|
| fn=func,
|
| _js=jsfunc,
|
| inputs=[binding.source_image_component],
|
| outputs=[destination_image_component, destination_width_component, destination_height_component] if destination_width_component else [destination_image_component],
|
| show_progress=False,
|
| )
|
|
|
| if binding.source_text_component is not None and fields is not None:
|
| connect_paste(binding.paste_button, fields, binding.source_text_component, override_settings_component, binding.tabname, is_paste=binding.is_paste)
|
|
|
| if binding.source_tabname is not None and fields is not None:
|
| paste_field_names = [
|
| *["Prompt", "Negative prompt", "Steps", "Face restoration"],
|
| *(["Seed"] if shared.opts.send_seed else []),
|
| *(["CFG scale"] if shared.opts.send_cfg else []),
|
| *binding.paste_field_names,
|
| ]
|
|
|
| if binding.source_tabname == "txt2img" and shared.opts.send_image_info_t2i_to_i2i:
|
|
|
| def read_infotext(gallery: list[dict], index: int, paste_fields: list[tuple]):
|
| res = []
|
|
|
| if len(gallery) == 0:
|
| for _ in paste_fields:
|
| res.append(gr.skip())
|
| return res
|
|
|
| image = image_from_url_text(gallery[index])
|
| info, _ = images.read_info_from_image(image)
|
|
|
| if not info:
|
| for _ in paste_fields:
|
| res.append(gr.skip())
|
| return res
|
|
|
| params = parse_generation_parameters(info)
|
| script_callbacks.infotext_pasted_callback(info, params)
|
|
|
| for output, key in paste_fields:
|
| res.append(_parse_info(output, key, params))
|
|
|
| return res
|
|
|
| binding.paste_button.click(
|
| fn=partial(read_infotext, paste_fields=[(field, name) for field, name in fields if name in paste_field_names]),
|
| inputs=[binding.source_image_component, shared.t2i_gallery_index],
|
| outputs=[field for field, name in fields if name in paste_field_names],
|
| show_progress=False,
|
| )
|
|
|
| else:
|
| binding.paste_button.click(
|
| fn=lambda *args: args,
|
| inputs=[field for field, name in paste_fields[binding.source_tabname]["fields"] if name in paste_field_names],
|
| outputs=[field for field, name in fields if name in paste_field_names],
|
| show_progress=False,
|
| )
|
|
|
| binding.paste_button.click(
|
| fn=None,
|
| _js=f"switch_to_{binding.tabname}",
|
| show_progress=False,
|
| )
|
|
|
|
|
| def send_image_and_dimensions(x: Any) -> tuple[Image.Image, int, int]:
|
| if isinstance(x, Image.Image):
|
| img = x
|
| else:
|
| img = image_from_url_text(x)
|
|
|
| if shared.opts.send_size and isinstance(img, Image.Image):
|
| w = img.width
|
| h = img.height
|
| else:
|
| w = gr.skip()
|
| h = gr.skip()
|
|
|
| return img, w, h
|
|
|
|
|
| def restore_old_hires_fix_params(res: dict):
|
| """
|
| for infotexts that specify old First pass size parameter, convert it into width, height, and hr scale
|
| """
|
|
|
| firstpass_width = res.get("First pass size-1", None)
|
| firstpass_height = res.get("First pass size-2", None)
|
|
|
| if shared.opts.use_old_hires_fix_width_height:
|
| hires_width = int(res.get("Hires resize-1", 0))
|
| hires_height = int(res.get("Hires resize-2", 0))
|
|
|
| if hires_width and hires_height:
|
| res["Size-1"] = hires_width
|
| res["Size-2"] = hires_height
|
| return
|
|
|
| if firstpass_width is None or firstpass_height is None:
|
| return
|
|
|
| firstpass_width, firstpass_height = int(firstpass_width), int(firstpass_height)
|
| width = int(res.get("Size-1", 512))
|
| height = int(res.get("Size-2", 512))
|
|
|
| if firstpass_width == 0 or firstpass_height == 0:
|
| firstpass_width, firstpass_height = processing.old_hires_fix_first_pass_dimensions(width, height)
|
|
|
| res["Size-1"] = firstpass_width
|
| res["Size-2"] = firstpass_height
|
| res["Hires resize-1"] = width
|
| res["Hires resize-2"] = height
|
|
|
|
|
| def parse_generation_parameters(x: str, skip_fields: list[str] | None = None):
|
| """
|
| parses generation parameters string, the one you see in text field under the picture in UI:
|
| ```
|
| girl with an artist's beret, determined, blue eyes, desert scene, computer monitors, heavy makeup, by Alphonse Mucha and Charlie Bowater, ((eyeshadow)), (coquettish), detailed, intricate
|
| Negative prompt: ugly, fat, obese, chubby, (((deformed))), [blurry], bad anatomy, disfigured, poorly drawn face, mutation, mutated, (extra_limb), (ugly), (poorly drawn hands), messy drawing
|
| Steps: 20, Sampler: Euler a, CFG scale: 7, Seed: 965400086, Size: 512x512, Model hash: 45dee52b
|
| ```
|
|
|
| returns a dict with field values
|
| """
|
| if skip_fields is None:
|
| skip_fields = shared.opts.infotext_skip_pasting
|
|
|
| res = {}
|
|
|
| prompt = ""
|
| negative_prompt = ""
|
|
|
| done_with_prompt = False
|
|
|
| *lines, lastline = x.strip().split("\n")
|
| if len(re_param.findall(lastline)) < 3:
|
| lines.append(lastline)
|
| lastline = ""
|
|
|
| for line in lines:
|
| line = line.strip()
|
| if line.startswith("Negative prompt:"):
|
| done_with_prompt = True
|
| line = line[16:].strip()
|
| if done_with_prompt:
|
| negative_prompt += ("" if negative_prompt == "" else "\n") + line
|
| else:
|
| prompt += ("" if prompt == "" else "\n") + line
|
|
|
| if shared.opts.infotext_styles != "Ignore":
|
| found_styles, prompt, negative_prompt = shared.prompt_styles.extract_styles_from_prompt(prompt, negative_prompt)
|
|
|
| if shared.opts.infotext_styles == "Apply":
|
| res["Styles array"] = found_styles
|
| elif shared.opts.infotext_styles == "Apply if any" and found_styles:
|
| res["Styles array"] = found_styles
|
|
|
| res["Prompt"] = prompt
|
| res["Negative prompt"] = negative_prompt
|
|
|
| for k, v in re_param.findall(lastline):
|
| try:
|
| if v[0] == '"' and v[-1] == '"':
|
| 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)
|
| else:
|
| res[k] = v
|
| except Exception:
|
| print(f'Error parsing "{k}: {v}"')
|
|
|
| if "Clip skip" not in res:
|
| res["Clip skip"] = "1"
|
|
|
| if "Hires resize-1" not in res:
|
| res["Hires resize-1"] = 0
|
| res["Hires resize-2"] = 0
|
|
|
| if "Hires sampler" not in res:
|
| res["Hires sampler"] = "Use same sampler"
|
|
|
| if "Hires schedule type" not in res:
|
| res["Hires schedule type"] = "Use same scheduler"
|
|
|
| if "Hires checkpoint" not in res:
|
| res["Hires checkpoint"] = "Use same checkpoint"
|
|
|
| if "Hires prompt" not in res:
|
| res["Hires prompt"] = ""
|
|
|
| if "Hires negative prompt" not in res:
|
| res["Hires negative prompt"] = ""
|
|
|
| if "Mask mode" not in res:
|
| res["Mask mode"] = "Inpaint masked"
|
|
|
| if "Masked content" not in res:
|
| res["Masked content"] = "original"
|
|
|
| if "Inpaint area" not in res:
|
| res["Inpaint area"] = "Whole picture"
|
|
|
| if "Masked area padding" not in res:
|
| res["Masked area padding"] = 32
|
|
|
| restore_old_hires_fix_params(res)
|
|
|
| if "RNG" not in res:
|
| res["RNG"] = "GPU"
|
|
|
| if "Schedule type" not in res:
|
| res["Schedule type"] = "Automatic"
|
|
|
| if "Schedule max sigma" not in res:
|
| res["Schedule max sigma"] = 0
|
|
|
| if "Schedule min sigma" not in res:
|
| res["Schedule min sigma"] = 0
|
|
|
| if "Schedule rho" not in res:
|
| res["Schedule rho"] = 0
|
|
|
| if "VAE Encoder" not in res:
|
| res["VAE Encoder"] = "Full"
|
|
|
| if "VAE Decoder" not in res:
|
| res["VAE Decoder"] = "Full"
|
|
|
| if "FP8 weight" not in res:
|
| res["FP8 weight"] = False
|
|
|
| if "Emphasis" not in res:
|
| p_attention = prompt_parser.parse_prompt_attention(prompt)
|
| n_attention = prompt_parser.parse_prompt_attention(negative_prompt)
|
| prompt_attention = [*p_attention, *n_attention]
|
| prompt_with_attention = [p for p in prompt_attention if p[1] == 1.0 or p[0] == "BREAK"]
|
| if len(prompt_attention) != len(prompt_with_attention):
|
| res["Emphasis"] = "Original"
|
|
|
| for key in skip_fields:
|
| res.pop(key, None)
|
|
|
| return res
|
|
|
|
|
| infotext_to_setting_name_mapping: list[tuple[str, str]] = []
|
| """
|
| Mapping of infotext labels to setting names. Only left for backwards compatibility - use OptionInfo(..., infotext='...') instead.
|
| Example content:
|
|
|
| infotext_to_setting_name_mapping = [
|
| ('Conditional mask weight', 'inpainting_mask_weight'),
|
| ('Model hash', 'sd_model_checkpoint'),
|
| ('ENSD', 'eta_noise_seed_delta'),
|
| ('Schedule type', 'k_sched_type'),
|
| ]
|
| """
|
|
|
|
|
| def create_override_settings_dict(text_pairs: list[str]) -> dict[str, Any]:
|
| """
|
| creates processing's override_settings parameters from gradio's multiselect
|
|
|
| Example input:
|
| ['Clip skip: 2', 'Model hash: e6e99610c4', 'ENSD: 31337']
|
|
|
| Example output:
|
| {'CLIP_stop_at_last_layers': 2, 'sd_model_checkpoint': 'e6e99610c4', 'eta_noise_seed_delta': 31337}
|
| """
|
|
|
| res = {}
|
|
|
| params = {}
|
| for pair in text_pairs:
|
| k, v = pair.split(":", maxsplit=1)
|
|
|
| params[k] = v.strip()
|
|
|
| mapping = [(info.infotext, k) for k, info in shared.opts.data_labels.items() if info.infotext]
|
| for param_name, setting_name in mapping + infotext_to_setting_name_mapping:
|
| value = params.get(param_name, None)
|
|
|
| if value is None:
|
| continue
|
|
|
| res[setting_name] = shared.opts.cast_value(setting_name, value)
|
|
|
| return res
|
|
|
|
|
| def get_override_settings(params, *, skip_fields=None):
|
| """
|
| Returns a list of settings overrides from the infotext parameters dictionary.
|
|
|
| This function checks the `params` dictionary for any keys that correspond to settings in `shared.opts` and returns
|
| a list of tuples containing the parameter name, setting name, and new value cast to correct type.
|
|
|
| It checks for conditions before adding an override:
|
| - ignores settings that match the current value
|
| - ignores parameter keys present in skip_fields argument.
|
|
|
| Example input:
|
| {"Clip skip": "2"}
|
|
|
| Example output:
|
| [("Clip skip", "CLIP_stop_at_last_layers", 2)]
|
| """
|
|
|
| res = []
|
|
|
| mapping = [(info.infotext, k) for k, info in shared.opts.data_labels.items() if info.infotext]
|
| for param_name, setting_name in mapping + infotext_to_setting_name_mapping:
|
| if param_name in (skip_fields or {}):
|
| continue
|
|
|
| v = params.get(param_name, None)
|
| if v is None:
|
| continue
|
|
|
| if setting_name == "sd_model_checkpoint" and shared.opts.disable_weights_auto_swap:
|
| continue
|
|
|
| v = shared.opts.cast_value(setting_name, v)
|
| current_value = getattr(shared.opts, setting_name, None)
|
|
|
| if v == current_value:
|
| continue
|
|
|
| res.append((param_name, setting_name, v))
|
|
|
| return res
|
|
|
|
|
| def connect_paste(button, paste_fields, input_comp, override_settings_component, tabname, *, is_paste=False):
|
| def paste_func(prompt):
|
| res = []
|
|
|
| if is_paste and not prompt:
|
| filename = os.path.join(data_path, "params.txt")
|
| try:
|
| with open(filename, "r", encoding="utf8") as file:
|
| prompt = file.read()
|
| except OSError:
|
| prompt = None
|
|
|
| if not prompt:
|
| for _, _ in paste_fields:
|
| res.append(gr.skip())
|
| return res
|
|
|
| params = parse_generation_parameters(prompt)
|
| script_callbacks.infotext_pasted_callback(prompt, params)
|
|
|
| for output, key in paste_fields:
|
| res.append(_parse_info(output, key, params))
|
|
|
| return res
|
|
|
| if override_settings_component is not None:
|
| already_handled_fields = {key: 1 for _, key in paste_fields}
|
|
|
| def paste_settings(params):
|
| vals = get_override_settings(params, skip_fields=already_handled_fields)
|
|
|
| vals_pairs = [f"{infotext_text}: {value}" for infotext_text, setting_name, value in vals]
|
|
|
| return gr.Dropdown.update(value=vals_pairs, choices=vals_pairs, visible=bool(vals_pairs))
|
|
|
| paste_fields = paste_fields + [(override_settings_component, paste_settings)]
|
|
|
| button.click(
|
| fn=paste_func,
|
| inputs=[input_comp],
|
| outputs=[x[0] for x in paste_fields],
|
| show_progress=False,
|
| )
|
| button.click(
|
| fn=None,
|
| _js=f"recalculate_prompts_{'txt2img' if tabname == 'txt2img' else 'img2img'}",
|
| show_progress=False,
|
| )
|
|
|
|
|
| import sys
|
|
|
|
|
| sys.modules["modules.generation_parameters_copypaste"] = sys.modules[__name__]
|
|
|