Skip to content
This repository has been archived by the owner on Jan 30, 2025. It is now read-only.

Commit

Permalink
syntax for extra params can be changed in settings now
Browse files Browse the repository at this point in the history
  • Loading branch information
h43lb1t0 committed Jul 10, 2023
1 parent 5e7967f commit 9a61f8f
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 9 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
scripts/__pycache__
scripts/settings.py
javascript/*.js
2 changes: 2 additions & 0 deletions HelpBatchCheckpointsPrompt.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ hires fix always uses the same prompts as for the first pass of the checkpoint,
- `{{count:number}}` Add this to the end of the prompt to set a different batch count for this checkpoint
- `{{clip_skip:number}}` Add this to the end of the prompt to set a different clip skip for this checkpoint
- `{{neg: negativ prompt text here}}`Add this to the end of the prompt, the text will be simply added to the back of the negative prompt

These can be changed in the setting. **you need to know Regexp to use this!** Look at the code in BatchParams.py to see how it works internally.
<hr>

🔢 adds the `@index:number` to the end of the Checkpoints and Prompts. If already there updates them.
Expand Down
29 changes: 21 additions & 8 deletions scripts/BatchParams.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,30 @@ def __repr__(self):
f"batch_count: {self.batch_count},\n "
f"clip_skip: {self.clip_skip}\n")

logger = Logger()

def get_all_batch_params(p: Union[modules.processing.StableDiffusionProcessingTxt2Img, modules.processing.StableDiffusionProcessingImg2Img], checkpoints_as_string: str, prompts_as_string: str) -> List[BatchParams]:

def getRegexFromOpts(key: str) -> Tuple[str]:
sub_pattern = getattr(shared.opts, key)
search_pattern = sub_pattern.replace("[", "([").replace("]", "])")

if not re.search(r"\[0-9\]\+|\\d\+", sub_pattern):
raise RuntimeError(f'Can\'t find a number with the regex for {key}: "{sub_pattern}"')

return search_pattern, sub_pattern

logger = Logger()
logger.debug = False
utils = Utils()

def get_batch_count_from_prompt(prompt: str) -> Tuple[int, str]:
# extracts the batch count from the prompt if specified
number_match = re.search(r"\{\{count:([0-9]+)\}\}", prompt)
search_pattern, sub_pattern = getRegexFromOpts("batchCountRegex")
number_match = re.search(search_pattern, prompt)
if number_match and number_match.group(1):
# Extract the number from the match object
number = int(number_match.group(1)) # Use group(1) to get the number inside parentheses
number = p.n_iter if number < 1 else number
prompt = re.sub(r"\{\{count:[0-9]+\}\}", '', prompt)
prompt = re.sub(sub_pattern, '', prompt)
else:
number = p.n_iter

Expand All @@ -48,20 +57,22 @@ def get_batch_count_from_prompt(prompt: str) -> Tuple[int, str]:

def get_clip_skip_from_prompt(prompt: str) -> Tuple[int, str]:
# extracts the clip skip from the prompt if specified
number_match = re.search(r"\{\{clip_skip:([0-9]+)\}\}", prompt)
search_pattern, sub_pattern = getRegexFromOpts("clipSkipRegex")
number_match = re.search(search_pattern, prompt)
if number_match and number_match.group(1):
# Extract the number from the match object
number = int(number_match.group(1))
number = shared.opts.data["CLIP_stop_at_last_layers"] if number < 1 else number
prompt = (
re.sub(r"\{\{clip_skip:[0-9]+\}\}", '', prompt))
re.sub(sub_pattern, '', prompt))
else:
number = shared.opts.data["CLIP_stop_at_last_layers"]


return number, prompt

def split_postive_and_negative_postive_prompt(prompt: str) -> Tuple[str, str]:
pattern = r'{{neg:(.*?)}}'
pattern = getattr(shared.opts, "negPromptRegex")
# Split the input string into parts
parts = re.split(pattern, prompt)
if len(parts) > 1:
Expand Down Expand Up @@ -102,10 +113,12 @@ def split_postive_and_negative_postive_prompt(prompt: str) -> Tuple[str, str]:
prompt, neg_prompt = split_postive_and_negative_postive_prompt(prompts[i])


prompt = prompt.replace("{prompt}", p.prompt)
prompt = prompt.replace(getattr(shared.opts, "promptRegex"), p.prompt)
neg_prompt = neg_prompt = p.negative_prompt + neg_prompt


all_batch_params.append(BatchParams(checkpoints[i], prompt, neg_prompt, batch_count, clip_skip))

logger.debug_log(f"batch_params: {all_batch_params[i]}", False)

return all_batch_params
42 changes: 42 additions & 0 deletions scripts/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import modules.scripts as scripts
import gradio as gr
import os

from modules import shared
from modules import script_callbacks

def on_ui_settings():
section = ("batchCP ", "batch checkpoint prompt")
shared.opts.add_option(
key = "promptRegex",
info = shared.OptionInfo(
"{prompt}",
"Prompt placeholder",
section=section)
)

shared.opts.add_option(
key = "batchCountRegex",
info = shared.OptionInfo(
"\{\{count:[0-9]+\}\}",
"Batch count Regex",
section=section)
)

shared.opts.add_option(
key = "clipSkipRegex",
info = shared.OptionInfo(
"\{\{clip_skip:[0-9]+\}\}",
"Clip skip Regex",
section=section)
)

shared.opts.add_option(
key = "negPromptRegex",
info = shared.OptionInfo(
"\{\{neg:(.*?)\}\}",
"negative Prompt Regex",
section=section)
)

script_callbacks.on_ui_settings(on_ui_settings)

0 comments on commit 9a61f8f

Please sign in to comment.