Skip to content

Commit

Permalink
rework hires prompts/sampler code to among other things support diffe…
Browse files Browse the repository at this point in the history
…rent extra networks in first/second pass

rework quoting for infotext items that have commas in them to use json (should be backwards compatible except for cases where it didn't work previously)
add some locals from processing function into the Processing class as fields
  • Loading branch information
AUTOMATIC1111 committed May 18, 2023
1 parent 5ec2c29 commit ff0e171
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 133 deletions.
36 changes: 25 additions & 11 deletions modules/generation_parameters_copypaste.py
@@ -1,5 +1,6 @@
import base64
import io
import json
import os
import re

Expand Down Expand Up @@ -34,13 +35,20 @@ def reset():


def quote(text):
if ',' not in str(text):
if ',' not in str(text) and '\n' not in str(text):
return text

text = str(text)
text = text.replace('\\', '\\\\')
text = text.replace('"', '\\"')
return f'"{text}"'
return json.dumps(text, ensure_ascii=False)


def unquote(text):
if len(text) == 0 or text[0] != '"' or text[-1] != '"':
return text

try:
return json.loads(text)
except Exception:
return text

This comment has been minimized.

Copy link
@EfourC

EfourC May 18, 2023

@AUTOMATIC1111
I'm definitely not an expert on the best practices and the pros and cons of using json, but is there any way to avoid "\n" strings in the hires prompts, and keep them encoded as 0x0A?

I'll use tools like Exiftool.exe and text editors (w/ custom syntax highlighting) to work with gen params, but the "\n" string values get in the way with some workflows and with manual copy/paste.

I suspect it's a tradeoff I'll have to live with (but I can hope!)

Example:
Showing \n strings in the Hires prompts -- harder to visually process and manually copy out the text.

masterpiece, best quality,

dungeon cat in gelatinous cube,

cage with spikes,
Negative prompt: bad quality, worst quality,

lowres
Steps: 20, Sampler: Euler a, CFG scale: 7, Seed: 1175392097, Size: 512x512, Model hash: a49140c6c5, Model: mistoonAnime_v10, Denoising strength: 0.7, Clip skip: 2, Hires prompt: "masterpiece, best quality, \n\ndungeon cat in gelatinous cube,\n\ncage with spikes,\n\n(glowing torch)", Hires negative prompt: "bad quality, worst quality,\n\nlowres\n\ngold", Hires upscale: 2, Hires steps: 20, Hires upscaler: Latent, Version: v1.2.1-267-g3d959f5b

I'm super happy you've added the HR prompting/sampler functionality, btw!

Edit: Screenshot of Exiftool output for reference

cmd_vYsquEwEjy



def image_from_url_text(filedata):
Expand Down Expand Up @@ -261,19 +269,16 @@ def parse_generation_parameters(x: str):
res["Negative prompt"] = negative_prompt

for k, v in re_param.findall(lastline):
v = v[1:-1] if v[0] == '"' and v[-1] == '"' else v
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

if k.startswith("Hires prompt"):
res["Hires prompt"] = v[1:][:-1].replace(';', ',')
elif k.startswith("Hires negative prompt"):
res["Hires negative prompt"] = v[1:][:-1].replace(';', ',')

# Missing CLIP skip means it was set to 1 (the default)
if "Clip skip" not in res:
res["Clip skip"] = "1"
Expand All @@ -286,6 +291,15 @@ def parse_generation_parameters(x: str):
res["Hires resize-1"] = 0
res["Hires resize-2"] = 0

if "Hires sampler" not in res:
res["Hires sampler"] = "Use same sampler"

if "Hires prompt" not in res:
res["Hires prompt"] = ""

if "Hires negative prompt" not in res:
res["Hires negative prompt"] = ""

restore_old_hires_fix_params(res)

# Missing RNG means the default was set, which is GPU RNG
Expand Down

2 comments on commit ff0e171

@Panchovix
Copy link

@Panchovix Panchovix commented on ff0e171 May 20, 2023

Choose a reason for hiding this comment

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

Since this commit, for some reason, when using wildcards/dynamic prompts extension (https://github.com/adieyal/sd-dynamic-prompts), it can use info of the prompt itself of other value inside the {} brackets on hi-res pass, and then, the final result can contain characteristics of another token. For example, you can use "{green|blue} eyes" and it can generate a mix of those 2 eye colours or blue, even if the eye colour choosen randomly was green. The issue is more pronounced when using dynamic prompts/wildcards with LoRAs/TIs (for example, {lora:a1:1|lora:a2:1|...}, {ti1|ti2|...})

This does not happen with https://github.com/AUTOMATIC1111/stable-diffusion-webui/tree/3885f8a63e0954ac96e6681aa8b33281f10337a2 commit.

@akx
Copy link
Collaborator

@akx akx commented on ff0e171 May 30, 2023

Choose a reason for hiding this comment

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

@Panchvzluck That might be fixed with adieyal/sd-dynamic-prompts#483 ...

Please sign in to comment.