* Add support for wildcards.

* Better debug log.
* Set commands support lazy evaluation and content addition.
* Unified in one tree and processing phase.
* Supports Flux.
* Fix evaluation of string variable as truthy.
* Show invalid wildcards.
* Better detection of installed UI.
* Improved cache of results.
* Works as a ComfyUI node.
* Debug setting changed to debug level.
* Fixes in variable use.
* Added timing reporting.
* Detection of more model types.
* Installation script for requirements.
* Extension metadata for A1111.
* Lark grammar in separate file and cached.
* Improved conditions for if command.
* Avoid repeats in the processing of prompts.
* Fix multiple loading of settings.
This commit is contained in:
Antonio Cordero Balcazar
2024-08-31 14:02:46 +02:00
parent bad0d24fbe
commit 8a960df0aa
20 changed files with 3137 additions and 1660 deletions
+421 -192
View File
@@ -3,19 +3,23 @@ if __name__ == "__main__":
import sys
import os
import time
sys.path.insert(1, os.path.join(sys.path[0], ".."))
sys.path.append(os.path.join(sys.path[0], ".."))
from modules import scripts, shared, script_callbacks
from modules.processing import StableDiffusionProcessing
from modules.shared import opts
from modules.paths import models_path
import gradio as gr
from ppp import PromptPostProcessor
from ppp_logging import PromptPostProcessorLogFactory
from ppp_logging import DEBUG_LEVEL, PromptPostProcessorLogFactory
from ppp_cache import PPPLRUCache
from ppp_wildcards import PPPWildcards
class PromptPostProcessorScript(scripts.Script):
class PromptPostProcessorA1111Script(scripts.Script):
"""
This class represents a script for prompt post-processing.
It is responsible for processing prompts and applying various settings and cleanup operations.
@@ -28,6 +32,7 @@ class PromptPostProcessorScript(scripts.Script):
title(): Returns the title of the script.
show(is_img2img): Determines whether the script should be shown based on the input type.
process(p, *args, **kwargs): Processes the prompts and applies post-processing operations.
ppp_interrupt(): Interrupts the generation.
__on_ui_settings(): Callback function for UI settings.
"""
@@ -43,12 +48,15 @@ class PromptPostProcessorScript(scripts.Script):
Returns:
None
"""
if not hasattr(self, "ppp_callbacks_added"):
lf = PromptPostProcessorLogFactory()
self.ppp_logger = lf.log
self.ppp_debug = getattr(opts, "ppp_gen_debug", False) if opts is not None else False
script_callbacks.on_ui_settings(self.__on_ui_settings)
self.ppp_callbacks_added = True
lf = PromptPostProcessorLogFactory()
self.name = PromptPostProcessor.NAME
self.ppp_logger = lf.log
self.ppp_debug_level = DEBUG_LEVEL(getattr(opts, "ppp_gen_debug_level", DEBUG_LEVEL.none.value))
self.lru_cache = PPPLRUCache(1000)
grammar_filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), "../grammar.lark")
with open(grammar_filename, "r", encoding="utf-8") as file:
self.grammar_content = file.read()
self.wildcards_obj = PPPWildcards(lf.log)
def title(self):
"""
@@ -81,29 +89,155 @@ class PromptPostProcessorScript(scripts.Script):
Returns:
None
"""
t1 = time.time()
if getattr(opts, "prompt_attention", "") == "Compel parser":
self.ppp_logger.warning("Compel parser is not supported!")
is_i2i = getattr(p, "init_images", [None])[0] is not None
self.ppp_debug = getattr(opts, "ppp_gen_debug", False) if opts is not None else False
if self.ppp_debug:
self.ppp_logger.info(f"Post-processing prompts ({'i2i' if is_i2i else 't2i'} mode)")
ppp = PromptPostProcessor(self, p, shared.state, opts, is_i2i)
# processes regular prompts
if (
hasattr(p, "all_prompts")
and p.all_prompts is not None
and hasattr(p, "all_negative_prompts")
and p.all_negative_prompts is not None
):
for i, (prompt, negative_prompt) in enumerate(zip(p.all_prompts, p.all_negative_prompts)):
p.all_prompts[i], p.all_negative_prompts[i] = ppp.process_prompt(prompt, negative_prompt)
self.ppp_debug_level = DEBUG_LEVEL(getattr(opts, "ppp_gen_debug_level", DEBUG_LEVEL.none.value))
do_i2i = getattr(opts, "ppp_gen_doi2i", False)
if is_i2i and not do_i2i:
if self.ppp_debug_level != DEBUG_LEVEL.none:
self.ppp_logger.info("Not processing the prompt for i2i")
return
if self.ppp_debug_level != DEBUG_LEVEL.none:
self.ppp_logger.info(f"Post-processing prompts ({'i2i' if is_i2i else 't2i'})")
model_info = {
"models_path": models_path,
"model_filename": getattr(p.sd_model.sd_checkpoint_info, "filename", ""), # path is absolute
"is_sd1": False, # Stable Diffusion 1
"is_sd2": False, # Stable Diffusion 2
"is_sdxl": False, # Stable Diffusion XL
"is_ssd": False, # Segmind Stable Diffusion 1B
"is_sd3": False, # Stable Diffusion 3
"is_flux": False, # Flux
}
app = (
"forge"
if hasattr(p.sd_model, "model_config")
else "sdnext" if hasattr(p.sd_model, "is_sdxl") and not hasattr(p.sd_model, "is_ssd") else "a1111"
)
if app == "sdnext":
# cannot differenciate SD1 and SD2, we set True to both
# LatentDiffusion is for the original backend, StableDiffusionPipeline is for the diffusers backend
model_info["is_sd1"] = p.sd_model.__class__.__name__ in ("LatentDiffusion", "StableDiffusionPipeline")
model_info["is_sd2"] = p.sd_model.__class__.__name__ in ("LatentDiffusion", "StableDiffusionPipeline")
model_info["is_sdxl"] = p.sd_model.__class__.__name__ == "StableDiffusionXLPipeline"
model_info["is_ssd"] = False # ?
model_info["is_sd3"] = p.sd_model.__class__.__name__ == "StableDiffusion3Pipeline"
model_info["is_flux"] = False
elif app == "forge":
model_info["is_sd1"] = getattr(p.sd_model, "is_sd1", False)
model_info["is_sd2"] = getattr(p.sd_model, "is_sd2", False)
model_info["is_sdxl"] = getattr(p.sd_model, "is_sdxl", False)
model_info["is_ssd"] = False # ?
model_info["is_sd3"] = getattr(p.sd_model, "is_sd3", False)
model_info["is_flux"] = p.sd_model.model_config.__class__.__name__ == "Flux"
else: # assume A1111 compatible (p.sd_model.__class__.__name__=="DiffusionEngine")
model_info["is_sd1"] = getattr(p.sd_model, "is_sd1", False)
model_info["is_sd2"] = getattr(p.sd_model, "is_sd2", False)
model_info["is_sdxl"] = getattr(p.sd_model, "is_sdxl", False)
model_info["is_ssd"] = getattr(p.sd_model, "is_ssd", False)
model_info["is_sd3"] = getattr(p.sd_model, "is_sd3", False)
model_info["is_flux"] = False
wc_wildcards_folders = getattr(opts, "ppp_wil_wildcardsfolders", "")
if wc_wildcards_folders == "":
wc_wildcards_folders = os.getenv("WILDCARD_DIR", PPPWildcards.DEFAULT_WILDCARDS_FOLDER)
wildcards_folders = [
(f if os.path.isabs(f) else os.path.abspath(os.path.join(models_path, f)))
for f in wc_wildcards_folders.split(",")
if f.strip() != ""
]
options = {
"debug_level": getattr(opts, "ppp_gen_debug_level", DEBUG_LEVEL.none.value),
"pony_substrings": getattr(opts, "ppp_gen_ponysubstrings", PromptPostProcessor.DEFAULT_PONY_SUBSTRINGS),
"process_wildcards": getattr(opts, "ppp_wil_process_wildcards", True),
"if_wildcards": getattr(opts, "ppp_wil_ifwildcards", PromptPostProcessor.IFWILDCARDS_CHOICES.ignore.value),
"choice_separator": getattr(opts, "ppp_wil_choice_separator", PromptPostProcessor.DEFAULT_CHOICE_SEPARATOR),
"keep_choices_order": getattr(opts, "ppp_wil_keep_choices_order", False),
"stn_separator": getattr(opts, "ppp_stn_separator", PromptPostProcessor.DEFAULT_STN_SEPARATOR),
"stn_ignore_repeats": getattr(opts, "ppp_stn_ignorerepeats", True),
"stn_join_attention": getattr(opts, "ppp_stn_joinattention", True),
"cleanup_extra_spaces": getattr(opts, "ppp_cup_extraspaces", True),
"cleanup_empty_constructs": getattr(opts, "ppp_cup_emptyconstructs", True),
"cleanup_extra_separators": getattr(opts, "ppp_cup_extraseparators", True),
"cleanup_extra_separators2": getattr(opts, "ppp_cup_extraseparators2", True),
"cleanup_breaks": getattr(opts, "ppp_cup_breaks", True),
"cleanup_breaks_eol": getattr(opts, "ppp_cup_breaks_eol", False),
"cleanup_ands": getattr(opts, "ppp_cup_ands", True),
"cleanup_ands_eol": getattr(opts, "ppp_cup_ands_eol", False),
"cleanup_extranetwork_tags": getattr(opts, "ppp_cup_extranetworktags", False),
"remove_extranetwork_tags": getattr(opts, "ppp_rem_removeextranetworktags", False),
}
self.wildcards_obj.refresh_wildcards(
self.ppp_debug_level, wildcards_folders if options["process_wildcards"] else None
)
ppp = PromptPostProcessor(
self.ppp_logger, self.ppp_interrupt, model_info, options, self.grammar_content, self.wildcards_obj
)
prompts_list = []
seeds = getattr(p, "all_seeds", [])
subseeds = getattr(p, "all_subseeds", [])
subseed_strength = getattr(p, "subseed_strength", 0.0)
if subseed_strength > 0:
calculated_seeds = [
int(subseed * subseed_strength + seed * (1 - subseed_strength))
for seed, subseed in zip(seeds, subseeds)
]
else:
calculated_seeds = seeds
if len(set(calculated_seeds)) < len(calculated_seeds):
self.ppp_logger.info("Adjusting seeds because some are equal.")
calculated_seeds = [seed + i for i, seed in enumerate(calculated_seeds)]
# adds regular prompts
rpr = getattr(p, "all_prompts", None)
rnr = getattr(p, "all_negative_prompts", None)
if rpr is not None and rnr is not None:
prompts_list += [
("regular", seed, prompt, negative_prompt)
for seed, prompt, negative_prompt in zip(calculated_seeds, rpr, rnr)
if (seed, prompt, negative_prompt) not in prompts_list
]
# make it compatible with A1111 hires fix
if (
hasattr(p, "all_hr_prompts")
and p.all_hr_prompts is not None
and hasattr(p, "all_hr_negative_prompts")
and p.all_hr_negative_prompts is not None
):
for i, (hr_prompt, hr_negative_prompt) in enumerate(zip(p.all_hr_prompts, p.all_hr_negative_prompts)):
p.all_hr_prompts[i], p.all_hr_negative_prompts[i] = ppp.process_prompt(hr_prompt, hr_negative_prompt)
rph = getattr(p, "all_hr_prompts", None)
rnh = getattr(p, "all_hr_negative_prompts", None)
if rph is not None and rnh is not None:
prompts_list += [
("hiresfix", seed, prompt, negative_prompt)
for seed, prompt, negative_prompt in zip(calculated_seeds, rph, rnh)
if (seed, prompt, negative_prompt) not in prompts_list
]
# processes prompts
for i, (prompttype, seed, prompt, negative_prompt) in enumerate(prompts_list):
if self.ppp_debug_level != DEBUG_LEVEL.none:
self.ppp_logger.info(f"processing prompts[{i+1}] ({prompttype})")
if self.lru_cache.get((seed, prompt, negative_prompt)) is None:
pp, np = ppp.process_prompt(prompt, negative_prompt, seed)
self.lru_cache.put((seed, prompt, negative_prompt), (pp, np))
# adds also the result so i2i doesn't process it unnecessarily
self.lru_cache.put((seed, pp, np), (pp, np))
elif self.ppp_debug_level != DEBUG_LEVEL.none:
self.ppp_logger.info("result already in cache")
# updates the prompts
if rpr is not None and rnr is not None:
for i, (seed, prompt, negative_prompt) in enumerate(zip(calculated_seeds, rpr, rnr)):
found = self.lru_cache.get((seed, prompt, negative_prompt))
if found is not None:
rpr[i] = found[0]
rnr[i] = found[1]
if rph is not None and rnh is not None:
for i, (seed, prompt, negative_prompt) in enumerate(zip(calculated_seeds, rph, rnh)):
found = self.lru_cache.get((seed, prompt, negative_prompt))
if found is not None:
rph[i] = found[0]
rnh[i] = found[1]
t2 = time.time()
if self.ppp_debug_level != DEBUG_LEVEL.none:
self.ppp_logger.info(f"process time: {t2 - t1:.3f} seconds")
def ppp_interrupt(self):
"""
@@ -114,171 +248,266 @@ class PromptPostProcessorScript(scripts.Script):
"""
shared.state.interrupted = True
def __on_ui_settings(self):
"""
Callback function for UI settings.
Returns:
None
"""
# general settings
section = ("prompt-post-processor", PromptPostProcessor.NAME)
shared.opts.add_option(
key="ppp_gen_sep", info=shared.OptionInfo("<h2>General settings</h2>", "", gr.HTML, section=section)
)
shared.opts.add_option(
key="ppp_gen_debug",
info=shared.OptionInfo(
False,
label="Debug",
section=section,
),
)
shared.opts.add_option(
key="ppp_gen_ifwildcards",
info=shared.OptionInfo(
default=PromptPostProcessor.IFWILDCARDS_CHOICES["ignore"],
label="What to do with remaining wildcards?",
component=gr.Radio,
component_args={"choices": PromptPostProcessor.IFWILDCARDS_CHOICES.values()},
section=section,
),
)
def on_ui_settings():
"""
Callback function for UI settings.
# content removal settings
shared.opts.add_option(
key="ppp_rem_sep", info=shared.OptionInfo("<br/><h2>Content removal settings</h2>", "", gr.HTML, section=section)
)
shared.opts.add_option(
key="ppp_rem_removeextranetworktags",
info=shared.OptionInfo(
False,
label="Remove extra network tags",
section=section,
),
)
shared.opts.add_option(
key="ppp_rem_if", info=shared.OptionInfo("<p style=\"font-style:italic\">* Parsing of the 'if' commands cannot be disabled</p>", "", gr.HTML, section=section)
)
Returns:
None
"""
# send to negative settings
shared.opts.add_option(
key="ppp_stn_sep",
info=shared.OptionInfo("<br/><h2>Send to Negative settings</h2>", "", gr.HTML, section=section),
section = ("prompt-post-processor", PromptPostProcessor.NAME)
def import_old_settings(names, default):
for name in names:
if hasattr(opts, name):
return getattr(opts, name)
return default
def import_bool_to_any(name, value_false, value_true, default):
if hasattr(opts, name):
return value_true if getattr(opts, name) else value_false
return default
def new_html_title(title):
info = shared.OptionInfo(
title,
"",
gr.HTML,
section=section,
)
shared.opts.add_option(
key="ppp_stn_doi2i",
info=shared.OptionInfo(
False,
label="Apply in img2img (this includes any pass that contains an initial image, like refiner, hires fix, adetailer)",
section=section,
info.do_not_save = True
return info
# general settings
shared.opts.add_option(
key="ppp_gen_sep",
info=new_html_title("<h2>General settings</h2>"),
)
shared.opts.add_option(
key="ppp_gen_debug_level",
info=shared.OptionInfo(
default=import_bool_to_any(
"ppp_gen_debug",
DEBUG_LEVEL.minimal.value,
DEBUG_LEVEL.full.value,
DEBUG_LEVEL.minimal.value,
),
)
shared.opts.add_option(
key="ppp_stn_separator",
info=shared.OptionInfo(
PromptPostProcessor.DEFAULT_STN_SEPARATOR,
label="Separator used when adding to the negative prompt",
section=section,
label="Debug level",
component=gr.Radio,
component_args={
"choices": (
("None", DEBUG_LEVEL.none.value),
("Minimal", DEBUG_LEVEL.minimal.value),
("Full", DEBUG_LEVEL.full.value),
),
},
section=section,
),
)
shared.opts.add_option(
key="ppp_gen_ponysubstrings",
info=shared.OptionInfo(
PromptPostProcessor.DEFAULT_PONY_SUBSTRINGS,
label="Comma separated list of substrings to look for in the model full filename to flag it as Pony (case insensitive)",
section=section,
),
)
shared.opts.add_option(
key="ppp_gen_doi2i",
info=shared.OptionInfo(
False,
label="Apply in img2img",
comment_after='<span class="info">(this includes any pass that contains an initial image, like adetailer)</span>',
section=section,
),
)
# wildcard settings
shared.opts.add_option(
key="ppp_wil_sep",
info=new_html_title('<br><h2>Wildcard settings</h2>'),
)
shared.opts.add_option(
key="ppp_wil_processwildcards",
info=shared.OptionInfo(
True,
label="Process wildcards",
section=section,
),
)
shared.opts.add_option(
key="ppp_wil_wildcardsfolders",
info=shared.OptionInfo(
PPPWildcards.DEFAULT_WILDCARDS_FOLDER,
label="Wildcards folders",
comment_after='<span class="info">(absolute or relative to the models folder)</span>',
section=section,
),
)
shared.opts.add_option(
key="ppp_wil_ifwildcards",
info=shared.OptionInfo(
default=import_old_settings(
["ppp_gen_ifwildcards", "ppp_ifwildcards"],
PromptPostProcessor.IFWILDCARDS_CHOICES.ignore.value,
),
)
shared.opts.add_option(
key="ppp_stn_ignorerepeats",
info=shared.OptionInfo(
True,
label="Ignore repeated content",
section=section,
),
)
shared.opts.add_option(
key="ppp_stn_joinattention",
info=shared.OptionInfo(
True,
label="Join attention modifiers (weights) when possible",
section=section,
),
)
# clean-up settings
shared.opts.add_option(
key="ppp_cup_sep", info=shared.OptionInfo("<br/><h2>Clean-up settings</h2>", "", gr.HTML, section=section)
)
shared.opts.add_option(
key="ppp_cup_doi2i",
info=shared.OptionInfo(
False,
label="Apply in img2img (this includes any pass that contains an initial image, like refiner, hires fix, adetailer)",
section=section,
),
)
shared.opts.add_option(
key="ppp_cup_emptyconstructs",
info=shared.OptionInfo(
True,
label="Remove empty constructs (attention, alternation, scheduling)",
section=section,
),
)
shared.opts.add_option(
key="ppp_cup_extraseparators",
info=shared.OptionInfo(
True,
label="Remove extra separators",
section=section,
),
)
shared.opts.add_option(
key="ppp_cup_extraseparators2",
info=shared.OptionInfo(
True,
label="Remove additional extra separators",
section=section,
),
)
shared.opts.add_option(
key="ppp_cup_breaks",
info=shared.OptionInfo(
True,
label="Clean up around BREAKs",
section=section,
),
)
shared.opts.add_option(
key="ppp_cup_breaks_eol",
info=shared.OptionInfo(
False,
label="Use EOL instead of Space before BREAKs",
section=section,
),
)
shared.opts.add_option(
key="ppp_cup_ands",
info=shared.OptionInfo(
True,
label="Clean up around ANDs",
section=section,
),
)
shared.opts.add_option(
key="ppp_cup_ands_eol",
info=shared.OptionInfo(
False,
label="Use EOL instead of Space before ANDs",
section=section,
),
)
shared.opts.add_option(
key="ppp_cup_extranetworktags",
info=shared.OptionInfo(
False,
label="Clean up around extra network tags",
section=section,
),
)
shared.opts.add_option(
key="ppp_cup_extraspaces",
info=shared.OptionInfo(
True,
label="Remove extra spaces",
section=section,
),
)
label="What to do with remaining/invalid wildcards?",
component=gr.Radio,
component_args={
"choices": (
("Ignore", PromptPostProcessor.IFWILDCARDS_CHOICES.ignore.value),
("Remove", PromptPostProcessor.IFWILDCARDS_CHOICES.remove.value),
("Add visible warning", PromptPostProcessor.IFWILDCARDS_CHOICES.warn.value),
("Stop the generation", PromptPostProcessor.IFWILDCARDS_CHOICES.stop.value),
)
},
section=section,
),
)
shared.opts.add_option(
key="ppp_wil_choice_separator",
info=shared.OptionInfo(
PromptPostProcessor.DEFAULT_CHOICE_SEPARATOR,
label="Default separator used when adding multiple choices",
section=section,
),
)
shared.opts.add_option(
key="ppp_wil_keep_choices_order",
info=shared.OptionInfo(
False,
label="Keep the order of selected choices",
section=section,
),
)
# content removal settings
shared.opts.add_option(
key="ppp_rem_sep",
info=new_html_title('<br><h2>Content removal settings</h2>'),
)
shared.opts.add_option(
key="ppp_rem_removeextranetworktags",
info=shared.OptionInfo(
False,
label="Remove extra network tags",
section=section,
),
)
# send to negative settings
shared.opts.add_option(
key="ppp_stn_sep",
info=new_html_title('<br><h2>Send to Negative settings</h2>'),
)
shared.opts.add_option(
key="ppp_stn_separator",
info=shared.OptionInfo(
PromptPostProcessor.DEFAULT_STN_SEPARATOR,
label="Separator used when adding to the negative prompt",
section=section,
),
)
shared.opts.add_option(
key="ppp_stn_ignorerepeats",
info=shared.OptionInfo(
True,
label="Ignore repeated content",
section=section,
),
)
shared.opts.add_option(
key="ppp_stn_joinattention",
info=shared.OptionInfo(
True,
label="Join attention modifiers (weights) when possible",
section=section,
),
)
# clean-up settings
shared.opts.add_option(
key="ppp_cup_sep",
info=new_html_title('<br><h2>Clean-up settings</h2>'),
)
shared.opts.add_option(
key="ppp_cup_emptyconstructs",
info=shared.OptionInfo(
True,
label="Remove empty constructs (attention, alternation, scheduling)",
section=section,
),
)
shared.opts.add_option(
key="ppp_cup_extraseparators",
info=shared.OptionInfo(
True,
label="Remove extra separators",
section=section,
),
)
shared.opts.add_option(
key="ppp_cup_extraseparators2",
info=shared.OptionInfo(
True,
label="Remove additional extra separators",
section=section,
),
)
shared.opts.add_option(
key="ppp_cup_breaks",
info=shared.OptionInfo(
True,
label="Clean up around BREAKs",
section=section,
),
)
shared.opts.add_option(
key="ppp_cup_breaks_eol",
info=shared.OptionInfo(
False,
label="Use EOL instead of Space before BREAKs",
section=section,
),
)
shared.opts.add_option(
key="ppp_cup_ands",
info=shared.OptionInfo(
True,
label="Clean up around ANDs",
section=section,
),
)
shared.opts.add_option(
key="ppp_cup_ands_eol",
info=shared.OptionInfo(
False,
label="Use EOL instead of Space before ANDs",
section=section,
),
)
shared.opts.add_option(
key="ppp_cup_extranetworktags",
info=shared.OptionInfo(
False,
label="Clean up around extra network tags",
section=section,
),
)
shared.opts.add_option(
key="ppp_cup_extraspaces",
info=shared.OptionInfo(
True,
label="Remove extra spaces",
section=section,
),
)
# Remove old settings
# for name in ["ppp_gen_ifwildcards", "ppp_ifwildcards", "ppp_gen_debug", "ppp_stn_doi2i", "ppp_cup_doi2i"]:
# if hasattr(opts, name):
# delattr(opts, name)
script_callbacks.on_ui_settings(on_ui_settings)