Skip to content

Instantly share code, notes, and snippets.

@opparco
Last active April 25, 2023 00:45
Show Gist options
  • Save opparco/0e4412bb5c320d9da245344a3202888a to your computer and use it in GitHub Desktop.
Save opparco/0e4412bb5c320d9da245344a3202888a to your computer and use it in GitHub Desktop.
Prompt S/R for LoRA and LyCORIS arguments
diff --git a/scripts/xyz_grid.py b/scripts/xyz_grid.py
index 3895a795..a5765c3a 100644
--- a/scripts/xyz_grid.py
+++ b/scripts/xyz_grid.py
@@ -36,11 +36,24 @@ def apply_field(field):
def apply_prompt(p, x, xs):
- if xs[0] not in p.prompt and xs[0] not in p.negative_prompt:
- raise RuntimeError(f"Prompt S/R did not find {xs[0]} in prompt or negative prompt.")
+ string = xs[0]
+ if x == string:
+ return
- p.prompt = p.prompt.replace(xs[0], x)
- p.negative_prompt = p.negative_prompt.replace(xs[0], x)
+ if string not in p.prompt and string not in p.negative_prompt:
+ if string.find('?') == -1:
+ raise RuntimeError(f"Prompt S/R did not find {string} in prompt or negative prompt.")
+
+ repl = string.replace('?', x)
+ pattern = re.compile(re.escape(string).replace(r'\?', r'.+?'))
+
+ # lora S/R
+ p.prompt = pattern.sub(repl, p.prompt)
+ p.negative_prompt = pattern.sub(repl, p.negative_prompt)
+ else:
+ # prompt S/R
+ p.prompt = p.prompt.replace(string, x)
+ p.negative_prompt = p.negative_prompt.replace(string, x)
def apply_order(p, x, xs):
@opparco
Copy link
Author

opparco commented Apr 24, 2023

for LoRA

prompt: "keywords <lora:model_name:1>"
S/R values: "<lora:model-name:?>,0.5,0.8"

for LoRA Block Weight

prompt: "keywords <lora:model_name:1:WEIGHT>"
S/R values: "<lora:model_name:?:WEIGHT>,0.5,0.8"  # replace multiplier.
S/R values: "<lora:model_name:1:?>,WEIGHT_A,WEIGHT_B"  # replace weights (or identifier).

for LyCORIS

S/R values: "<lyco:model_name:1:?:0>,0.5,0.8"  # replace unet multiplier.
S/R values: "<lyco:model_name:?:?:0>,0.5,0.8"  # replace te+unet multiplier (same value).

for LyCORIS + LoRA Block Weight

S/R values: "<lyco:model-name:1:1:0:?>,LyCOWEIGHT_A,LyCOWEIGHT_B"  # replace weights (or identifier).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment