Compare commits

...
8 Commits
Author SHA1 Message Date
Antonio Cordero Balcazar 3810320f6e * Simpler versioning
* Cache now takes wildcard definitions into consideration.
* ComfyUI: node is not cached if wildcard processing is active.
* Add a space with prefix and suffix if needed.
* Checks requirements before installing them.
2024-10-27 16:05:55 +01:00
Antonio Cordero Balcazar c87c24924e * Support anonymous wildcards. 2024-10-19 16:05:21 +02:00
Antonio Cordero Balcazar e9092b877c * Improved wildcard definition support. 2024-10-19 12:45:48 +02:00
Antonio Cordero Balcazar a18e5e659c * Improved processing of wildcards and choices.
* Support for prefix and suffix in wildcard options and object format of the options in json/yaml files.
2024-10-13 12:57:25 +02:00
Antonio Cordero Balcazar 93f0102897 * Fix a bug with the variable echo command.
* Fix a bug obtaining random seed.
* Reading version number from pyproject file.
* A1111: Support for forcing the same seed/variation seed to all images in a batch.
* A1111: Support for unlinking the seed from the image seed and specifying one for the prompt.
* ComfyUI: Warning on use of invalid constructs. Related settings kept but default to False.
2024-10-11 18:13:57 +02:00
Antonio Cordero Balcazar 0b03e1773f * Added pyproject.toml to publish to ComfyUI registry.
* ComfyUI: optional modelname and model, and allowed sending a classname string instead of a model (#12)
2024-10-05 11:56:26 +02:00
Antonio Cordero Balcazar 830bd4f785 Add Github Action for Publishing to Comfy Registry (#14)
This PR adds a Github Action (publish-node-action) that will publish an
updated version of your custom node to the
[registry](https://comfyregistry.org/) whenever the `pyproject.toml`
file changes.
2024-10-05 11:03:28 +02:00
snomiao b019c5604b chore(publish): Add Github Action for Publishing to Comfy Registry 2024-10-01 16:01:06 +00:00
14 changed files with 1006 additions and 284 deletions
+24
View File
@@ -0,0 +1,24 @@
name: Publish to Comfy registry
on:
workflow_dispatch:
push:
branches:
- main
- master
paths:
- "pyproject.toml"
jobs:
publish-node:
name: Publish Custom Node to registry
runs-on: ubuntu-latest
# if this is a forked repository. Skipping the workflow.
if: github.event.repository.fork == false
steps:
- name: Check out code
uses: actions/checkout@v4
- name: Publish Custom Node
uses: Comfy-Org/publish-node-action@main
with:
## Add your own personal access token to your Github Repository secrets and reference it here.
personal_access_token: ${{ secrets.REGISTRY_ACCESS_TOKEN }}
+46 -6
View File
@@ -3,9 +3,9 @@
The Prompt PostProcessor (PPP), formerly known as "sd-webui-sendtonegative", is an extension designed to process the prompt, possibly after other extensions have modified it. This extension is compatible with:
* [AUTOMATIC1111 Stable Diffusion WebUI](https://github.com/AUTOMATIC1111/stable-diffusion-webui)
* [SD.Next](https://github.com/vladmandic/automatic).
* [Forge](https://github.com/lllyasviel/stable-diffusion-webui-forge)
* [reForge](https://github.com/Panchovix/stable-diffusion-webui-reForge)
* [SD.Next](https://github.com/vladmandic/automatic).
* ...and probably other forks
* [ComfyUI](https://github.com/comfyanonymous/ComfyUI)
@@ -35,6 +35,8 @@ Notes:
In SD.Next that means only the *A1111* or *Full* parsers. It will warn you if you use the *Compel* parser.
Does not recognize tokenizer separators like "TE2:" and "TE3:", so sending to negative prompt from those sections of the prompt will not add them in the corresponding section of the negative prompt.
ComfyUI only supports natively the attention using parenthesis, so the ones with the braces will be converted. The other constructs are not natively supported but some custom nodes implement them.
2. It recognizes wildcards in the *\_\_wildcard\_\_* and *{choice|choice}* formats (and almost everything that [Dynamic Prompts](https://github.com/adieyal/sd-dynamic-prompts) supports).
3. It does not create *AND/BREAK* constructs when moving content to the negative prompt.
@@ -47,6 +49,8 @@ On A1111 compatible webuis:
3. Click the Install button
4. Restart the webui
On SD.Next I recommend you disable the native wildcard processing.
On ComfyUI:
1. Go to Manager > Custom Nodes Manager
@@ -126,9 +130,9 @@ __parameters$$wildcard'filter'(var=value)__
The parameters, the filter, and the setting of a variable are optional. The parameters follow the same format as for the choices.
The wildcard identifier can contain globbing formatting, to read multiple wildcards and merge their choices. Note that if there are no parameters specified, the globbing will use the ones from the first wildcard that matches and have parameters (sorted by keys), so if you don't want that you might want to specify them.
The wildcard identifier can contain globbing formatting, to read multiple wildcards and merge their choices. Note that if there are no parameters specified, the globbing will use the ones from the first wildcard that matches and have parameters (sorted by keys), so if you don't want that you might want to specify them. Also note that, unlike with Dynamic Prompts, the wildcard name has to be specified with its full path (unless you use globbing).
The filter can be used to filter specific choices from the wildcard. The filtering works before applying the choice conditions (if any). The surrounding quotes can be single or double. The filter is a comma separated list of an integer (positional choice index) or choice label. You can also compound them with "+". That is, the comma separated items act as an OR and the "+" inside them as an AND. Using labels can simplify the definitions of complex wildcards where you want to have direct access to specific choices on occasion (you don't need to create wildcards for each individual choice). There are some additional formats when using filters. You can specify "^wildcard" as a filter to use the filter of a previous wildcard in the chain. You can start the filter (regular or inherited) with "#" and it will not be applied to the current wildcard choices, but the filter will remain in memory to use by other descendant wildcards. You use "#" and "^" when you want to pass a filter to inner wildcards (see the test files).
The filter can be used to filter specific choices from the wildcard. The filtering works before applying the choice conditions (if any). The surrounding quotes can be single or double. The filter is a comma separated list of an integer (positional choice index; zero-based) or choice label. You can also compound them with "+". That is, the comma separated items act as an OR and the "+" inside them as an AND. Using labels can simplify the definitions of complex wildcards where you want to have direct access to specific choices on occasion (you don't need to create wildcards for each individual choice). There are some additional formats when using filters. You can specify "^wildcard" as a filter to use the filter of a previous wildcard in the chain. You can start the filter (regular or inherited) with "#" and it will not be applied to the current wildcard choices, but the filter will remain in memory to use by other descendant wildcards. You use "#" and "^" when you want to pass a filter to inner wildcards (see the test files).
The variable value only applies during the evaluation of the selected choices and is discarded afterward (the variable keeps its original value if there was one).
@@ -151,15 +155,34 @@ __path/wildcard(var=value)__ # select 1 choice using the specified variabl
A wildcard definition can be:
* A txt file. The wildcard name will be the relative path of the file, without the extension. Each line will be a choice. Lines starting with "#" or empty are ignored.
* An array or string inside a json or yaml file. The wildcard name includes the relative folder path of the file (without the name or extension) but also the path of the object inside the file.
* A txt file. The wildcard name will be the relative path of the file, without the extension. Each line will be a choice. Lines starting with "#" or empty are ignored. Doesn't support nesting.
* An array or scalar value inside a json or yaml file. The wildcard name includes the relative folder path of the file, without the extension, but also the path of the value inside the file (if there is one). If the file contains a dictionary, the filename part is not used for the wildcard name. Supports nesting by having dictionaries inside dictionaries.
The best format is a yaml file with a dictionary of wildcards inside. An editor supporting yaml syntax is recommended.
In a choice, the content after a "#" is ignored.
If the first choice follows the format of wildcard parameters, it will be used as default parameters for that wildcard (see examples in the tests folder). The choices of the wildcard follow the same format as in the choices construct, or the object format of **Dynamic Prompts** (only in structured files). If using the object format for a choice you can use a new "if" property for the condition in addition to the standard "weight" and "text"/"content".
If the first choice follows the format of wildcard parameters, it will be used as default parameters for that wildcard (see examples in the tests folder). The choices of the wildcard follow the same format as in the choices construct, or the object format of **Dynamic Prompts** (only in structured files). If using the object format for a choice you can use a new "if" property for the condition, and the "labels" property (an array of strings) in addition to the standard "weight" and "text"/"content".
```yaml
{ labels: ["some_label"], weight: 2, if: "_is_pony", content: "the text" } # "text" property can be used instead of "content"
```
Wildcard parameters in a json/yaml file can also be in object format, and support two additional properties, prefix and suffix:
```yaml
{ sampler: "~", repeating: false, count: 2, prefix: "prefix-", suffix: "-suffix", separator: "/" }
{ sampler: "~", repeating: false, from: 2, to: 3, prefix: "prefix-", suffix: "-suffix", separator: "/" }
```
The prefix and suffix are added to the result along with the selected choices and separators. They can contain other constructs, but the separator can't.
It is recommended to use the object format for the wildcard parameters and for choices with complex options.
Wildcards can contain just one choice. In json and yaml formats this allows the use of a string value for the keys, rather than an array.
A choice inside a wildcard can also be a list or a dictionary of one element containing a list. These are considered anonymous wildcards. With a list it will be an anonymous wildcard with no choice options, and with a dictionary the key will be the options for the choice containing the anonymous wildcard and the value the choices of the anonymous wildcard. Anonymous wildcards can help formatting complex choice values that are used in only one place and thus creating a regular wildcard is not necessary. See test.yaml for examples.
#### Detection of remaining wildcards
This extension should run after any other wildcard extensions, so if you don't use the internal wildcards processing, any remaining wildcards present in the prompt or negative_prompt at this point must be invalid. Usually you might not notice this problem until you check the image metadata, so this option gives you some ways to detect and treat the problem.
@@ -336,6 +359,21 @@ This should still work as intended, and the only negative point i see is the unn
## Configuration
### A1111 (and compatible UIs) UI options
* **Force equal seeds**: Changes the image seeds and variation seeds to be equal to the first of the batch. This allows using the same values for all the images in a batch.
* **Unlink seed**: Uses the specified seed for the prompt generation instead of the one from the image.
* **Seed**: The seed to use for the prompt generation. If -1 a random one will be used for each image in the batch. This seed is only used for wildcards and choices.
* **Variable seed**: If the seed is not -1 you can use this to increase it for the other images in the batch.
### ComfyUI specific inputs
* **model**: Connect here the MODEL or a string with the model class name used by ComfyUI. Needed for the model kind system variables.
* **modelname**: Name of the model. Needed for the model name system variables and detection of pony (this also requieres for the model to be SDXL).
* **seed**: Connect here the seed used. By default it is -1 (random).
* **pos_prompt**: Connect here the prompt text, or fill it as a widget.
* **neg_prompt**: Connect here the negative prompt text, or fill it as a widget.
### General settings
* **Debug level**: what to write to the console. Note: in SD.Next debug messages only show if you launch it with the --debug argument.
@@ -372,6 +410,8 @@ This should still work as intended, and the only negative point i see is the unn
* **Merge attention modifiers (weights) when possible**: it merges attention modifiers when possible (merges into one, multiplying their values). Only merges individually nested modifiers.
* **Remove extra spaces**: removes other unnecessary spaces.
Please note that ComfyUI does not support the BREAK and AND constructs, but the related settings are kept in that UI.
### Content removal settings
* **Remove extra network tags**: removes all extra network tags.
-10
View File
@@ -13,16 +13,6 @@ sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from .ppp_comfyui import PromptPostProcessorComfyUINode
NODE_CLASS_MAPPINGS = {"ACBPromptPostProcessor": PromptPostProcessorComfyUINode}
NODE_DISPLAY_NAME_MAPPINGS = {"ACBPromptPostProcessor": "ACB Prompt Post Processor"}
MANIFEST = {
"name": "ACB Prompt Post Processor",
"version": PromptPostProcessorComfyUINode.VERSION,
"author": "ACB",
"project": "https://github.com/acorderob/sd-webui-prompt-postprocessor",
"description": "Node for processing prompts",
"license": "MIT",
}
__all__ = ["NODE_CLASS_MAPPINGS", "NODE_DISPLAY_NAME_MAPPINGS"]
+10 -2
View File
@@ -1,5 +1,13 @@
import os
import launch
requirements_filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), "requirements.txt")
launch.run_pip(f'install -r "{requirements_filename}"', "requirements for Prompt Post-Processor")
try:
from modules.launch_utils import requirements_met, run_pip # A1111
if not requirements_met(requirements_filename):
run_pip(f'install -r "{requirements_filename}"', "requirements for Prompt Post-Processor")
except ImportError:
import launch
launch.run_pip(f'install -r "{requirements_filename}"', "requirements for Prompt Post-Processor")
+365 -116
View File
@@ -14,7 +14,7 @@ import lark.parsers
import numpy as np
from ppp_logging import DEBUG_LEVEL
from ppp_wildcards import PPPWildcards
from ppp_wildcards import PPPWildcard, PPPWildcards
class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-instance-attributes
@@ -22,8 +22,28 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
The PromptPostProcessor class is responsible for processing and manipulating prompt strings.
"""
@staticmethod
def get_version_from_pyproject() -> str:
"""
Reads the version from the pyproject.toml file.
Returns:
str: The version string.
"""
version_str = "0.0.0"
try:
pyproject_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "pyproject.toml")
with open(pyproject_path, "r", encoding="utf-8") as file:
for line in file:
if line.startswith("version = "):
version_str = line.split("=")[1].strip().strip('"')
break
except Exception as e: # pylint: disable=broad-exception-caught
logging.getLogger().exception(e)
return version_str
NAME = "Prompt Post-Processor"
VERSION = (2, 6, 0)
VERSION = get_version_from_pyproject()
class IFWILDCARDS_CHOICES(Enum):
ignore = "ignore"
@@ -116,6 +136,16 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
propagate_positions=True,
start="choicesoptions",
)
self.parser_condition = lark.Lark(
grammar_content,
propagate_positions=True,
start="condition",
)
self.parser_choicevalue = lark.Lark(
grammar_content,
propagate_positions=True,
start="choicevalue",
)
self.__init_sysvars()
self.user_variables = {}
@@ -135,7 +165,19 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
"""
return text.encode("unicode_escape").decode("utf-8")
def isComfyUI(self) -> bool:
"""
Checks if the current environment is ComfyUI.
Returns:
bool: True if the environment is ComfyUI, False otherwise.
"""
return self.env_info.get("app", "") == "comfyui"
def __init_sysvars(self):
"""
Initializes the system variables.
"""
self.system_variables = {}
sdchecks = {
"sd1": self.env_info.get("is_sd1", False),
@@ -345,6 +387,16 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
return text
def __processprompts(self, prompt, negative_prompt):
"""
Process the prompt and negative prompt.
Args:
prompt (str): The prompt.
negative_prompt (str): The negative prompt.
Returns:
tuple: A tuple containing the processed prompt and negative prompt.
"""
self.user_variables = {}
# Process prompt
@@ -405,7 +457,7 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
seed: int = 0,
):
"""
Process the prompt and negative prompt by moving content to the negative prompt, and cleaning up.
Initializes the random number generator and processes the prompt and negative prompt.
Args:
original_prompt (str): The original prompt.
@@ -417,7 +469,7 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
"""
try:
if seed == -1:
seed = np.random.randint(0, 2**32)
seed = np.random.randint(0, 2**32, dtype=np.int64)
self.rng = np.random.default_rng(seed & 0xFFFFFFFF)
prompt = original_prompt
negative_prompt = original_negative_prompt
@@ -447,6 +499,18 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
return original_prompt, original_negative_prompt
def parse_prompt(self, prompt_description: str, prompt: str, parser: lark.Lark, raise_parsing_error: bool = False):
"""
Parses a prompt using the specified parser.
Args:
prompt_description (str): The description of the prompt.
prompt (str): The prompt to be parsed.
parser (lark.Lark): The parser to be used.
raise_parsing_error (bool): Whether to raise a parsing error.
Returns:
Tree: The parsed prompt.
"""
t1 = time.time()
try:
if self.debug_level == DEBUG_LEVEL.full:
@@ -488,8 +552,8 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
self.__ppp = ppp
self.AccumulatedShell = namedtuple("AccumulatedShell", ["type", "data"])
self.NegTag = namedtuple("NegTag", ["start", "end", "content", "parameters", "shell"])
self.__shell: list[self.AccumulatedShell] = []
self.__negtags: list[self.NegTag] = []
self.__shell: list[self.AccumulatedShell] = [] # type: ignore
self.__negtags: list[self.NegTag] = [] # type: ignore
self.__already_processed: list[str] = []
self.__is_negative = False
self.__wildcard_filters = {}
@@ -566,35 +630,77 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
return added_result
def __get_original_node_content(self, node: lark.Tree | lark.Token, default=None) -> str:
"""
Get the original content of a node.
Args:
node (Tree|Token): The node to get the content from.
default: The default value to return if the content is not found.
Returns:
str: The original content of the node.
"""
return (
node.meta.content
if hasattr(node, "meta") and node.meta is not None and not node.meta.empty
else default
)
def __get_user_variable_value(self, name: str, default="", evaluate=True) -> str:
if evaluate:
v = self.__ppp.user_variables.get(name, default)
def __get_user_variable_value(self, name: str, evaluate=True, visit=False) -> str:
"""
Get the value of a user variable.
Args:
name (str): The name of the user variable.
evaluate (bool): Whether to evaluate the variable.
visit (bool): Whether to also visit the variable (add to result).
Returns:
str: The value of the user variable.
"""
v = self.__ppp.user_variables.get(name, None)
if v is not None:
visited = False
if isinstance(v, lark.Tree):
v = self.__visit(v, True)
else:
v = (
self.__ppp.user_variables[name]
if isinstance(self.__ppp.user_variables[name], str)
else self.__get_original_node_content(
self.__ppp.user_variables[name], default or "not evaluated yet"
)
)
if evaluate:
v = self.__visit(v, not visit)
visited = visit
else:
v = self.__get_original_node_content(v, "not evaluated yet")
if visit and not visited:
self.result += v
return v
def __set_user_variable_value(self, name: str, value: str):
"""
Set the value of a user variable.
Args:
name (str): The name of the user variable.
value (str): The value to be set.
"""
self.__ppp.user_variables[name] = value
def __remove_user_variable(self, name: str):
"""
Remove a user variable.
Args:
name (str): The name of the user variable.
"""
if name in self.__ppp.user_variables:
del self.__ppp.user_variables[name]
def __debug_end(self, construct: str, start_result: str, duration: float, info=None):
"""
Log the end of a construct processing.
Args:
construct (str): The name of the construct.
start_result (str): The initial result.
duration (float): The duration of the processing.
info: Additional information to log.
"""
if self.__ppp.debug_level == DEBUG_LEVEL.full:
info = f"({info}) " if info is not None and info != "" else ""
output = self.result[len(start_result) :]
@@ -616,7 +722,7 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
Returns:
bool: The result of the condition evaluation.
"""
var_value = self.__ppp.system_variables.get(cond_var, self.__get_user_variable_value(cond_var, None))
var_value = self.__ppp.system_variables.get(cond_var, self.__get_user_variable_value(cond_var))
if var_value is None:
var_value = ""
self.__ppp.logger.warning(f"Unknown variable {cond_var}")
@@ -717,6 +823,8 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
"""
Process a prompt composition construct in the tree.
"""
# if self.__ppp.isComfyUI():
# self.__ppp.logger.warning("Prompt composition is not supported in ComfyUI.")
start_result = self.result
t1 = time.time()
self.__visit(tree.children[0])
@@ -744,6 +852,8 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
"""
Process a scheduling construct in the tree and add it to the accumulated shell.
"""
# if self.__ppp.isComfyUI():
# self.__ppp.logger.warning("Prompt scheduling is not supported in ComfyUI.")
start_result = self.result
t1 = time.time()
before = tree.children[0]
@@ -778,6 +888,8 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
"""
Process an alternation construct in the tree and add it to the accumulated shell.
"""
# if self.__ppp.isComfyUI():
# self.__ppp.logger.warning("Prompt alternation is not supported in ComfyUI.")
start_result = self.result
t1 = time.time()
# self.__shell.append(self.AccumulatedShell("al", len(tree.children)))
@@ -832,7 +944,7 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
weight = math.floor(weight * 100) / 100 # we round to 2 decimals
weight_str = f"{weight:.2f}".rstrip("0").rstrip(".")
self.__shell.append(self.AccumulatedShell("at", weight))
if weight == 0.9:
if weight == 0.9 and not self.__ppp.isComfyUI():
starttag = "["
self.result += starttag
self.__visit(current_tree)
@@ -947,7 +1059,7 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
else:
info += " = "
self.__set_user_variable_value(variable, newvalue)
currentvalue = self.__get_user_variable_value(variable, None, False)
currentvalue = self.__get_user_variable_value(variable, False)
if currentvalue is None:
info += "not evaluated yet"
else:
@@ -973,16 +1085,14 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
"""
t1 = time.time()
start_result = self.result
value = self.__get_user_variable_value(variable, None)
if default is not None:
default_value = self.__visit(default, True) # for log
value = self.__get_user_variable_value(variable, True, True)
if value is None:
if default is not None:
value = self.__visit(default, False, True)
self.result += self.__visit(default, False, True)
else:
value = ""
self.__ppp.logger.warning(f"Unknown variable {variable}")
self.result += value
t2 = time.time()
info = variable
if default is not None:
@@ -1039,8 +1149,8 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
def __get_choices(
self,
options: lark.Tree | None,
choice_values: list[lark.Tree],
options: dict | None,
choice_values: list[dict],
filter_specifier: Optional[list[list[str]]] = None,
wildcard_key: str = None,
) -> str:
@@ -1048,40 +1158,25 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
Select choices based on the options.
Args:
options (Tree): The tree object representing the options construct.
choice_values (list[Tree]): A list of choice tree objects.
options (dict): The object representing the options construct.
choice_values (list[dict]): A list of choice objects.
filter_specifier (list[list[str]]): The filter specifier.
wildcard_key (str): The wildcard key if it is a wildcard.
Returns:
str: The selected choice.
"""
sampler: str = "~"
repeating: bool = False
from_value: int = 1
to_value: int = 1
separator: str = self.__ppp.wil_choice_separator
if options is not None:
if len(options.children) == 1:
sampler = options.children[0] if options.children[0] is not None else "~"
else:
sampler = options.children[0].children[0] if options.children[0] is not None else "~"
repeating = options.children[1].children[0] == "r" if options.children[1] is not None else False
if len(options.children) == 4:
ifrom = 2
ito = 2
isep = 3
else: # 6
ifrom = 2
ito = 3
isep = 4
from_value = int(options.children[ifrom].children[0]) if options.children[ifrom] is not None else 1
to_value = int(options.children[ito].children[0]) if options.children[ito] is not None else 1
separator = (
self.__visit(options.children[isep], False, True)
if options.children[isep] is not None
else self.__ppp.wil_choice_separator
)
if options is None:
options = {}
sampler: str = options.get("sampler", "~")
repeating: bool = options.get("repeating", False)
if "count" in options:
from_value = options["count"]
to_value = from_value
else:
from_value: int = options.get("from", 1)
to_value: int = options.get("to", 1)
separator: str = options.get("separator", self.__ppp.wil_choice_separator)
if sampler != "~":
msg = f"wildcard '{wildcard_key}'" if wildcard_key else "choices"
self.__ppp.logger.warning(f"Unsupported sampler '{sampler}' in {msg} options!")
@@ -1090,12 +1185,6 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
if filter_specifier is not None:
filtered_choice_values = []
for i, c in enumerate(choice_values):
c_label_obj = c.children[0]
choice_labels = (
[x.value.lower() for x in c_label_obj.children[1:-1]] # should be a token
if c_label_obj is not None
else []
)
passes = False
for o in filter_specifier:
tmp_pass = True
@@ -1104,7 +1193,7 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
if int(a) != i:
tmp_pass = False
break
elif a.lower() not in choice_labels:
elif a.lower() not in c.get("labels", []):
tmp_pass = False
break
if tmp_pass:
@@ -1144,55 +1233,244 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
)
)
if num_choices > 0:
available_choices: list[lark.Tree] = []
available_choices: list[dict] = []
weights = []
included_choices = 0
excluded_choices = 0
excluded_weights_sum = 0
for i, c in enumerate(filtered_choice_values):
c.choice_index = i # we index them to later sort the results
w = float(c.children[1].children[0]) if c.children[1] is not None else 1.0
if w > 0 and (c.children[2] is None or self.__evaluate_if(c.children[2].children[0])):
c["choice_index"] = i # we index them to later sort the results
weight = float(c.get("weight", 1.0))
theif = c.get("if", None)
if weight > 0 and (theif is None or self.__evaluate_if(theif)):
available_choices.append(c)
weights.append(w)
weights.append(weight)
included_choices += 1
else:
weights.append(-1)
excluded_choices += 1
excluded_weights_sum += w
excluded_weights_sum += weight
if excluded_choices > 0: # we need to redistribute the excluded weights
weights = [w + excluded_weights_sum / included_choices for w in weights if w >= 0]
weights = [weight + excluded_weights_sum / included_choices for weight in weights if weight >= 0]
weights = np.array(weights)
weights /= weights.sum() # normalize weights
selected_choices: list[lark.Tree] = list(
selected_choices: list[dict] = list(
self.__ppp.rng.choice(available_choices, size=num_choices, p=weights, replace=repeating)
)
if self.__ppp.wil_keep_choices_order:
selected_choices = sorted(selected_choices, key=lambda x: x.choice_index)
selected_choices = sorted(selected_choices, key=lambda x: x["choice_index"])
selected_choices_text = []
prefix: str = (
self.__visit(options.get("prefix", None), False, True)
if options.get("prefix", None) is not None
else ""
)
if prefix != "" and re.match(r"\w", prefix[-1]):
prefix += " "
for i, c in enumerate(selected_choices):
t1 = time.time()
choice_content_obj = c.children[3]
choice_content = self.__visit(choice_content_obj, False, True)
choice_content_obj = c.get("content", c.get("text", None))
if isinstance(choice_content_obj, str):
choice_content = choice_content_obj
else:
choice_content = self.__visit(choice_content_obj, False, True)
t2 = time.time()
if self.__ppp.debug_level == DEBUG_LEVEL.full:
self.__ppp.logger.debug(
f"Adding choice {i+1} ({t2-t1:.3f} seconds):\n"
+ textwrap.indent(re.sub(r"\n$", "", c.pretty()), " ")
+ textwrap.indent(re.sub(r"\n$", "", choice_content), " ")
)
selected_choices_text.append(choice_content)
suffix: str = (
self.__visit(options.get("suffix", None), False, True)
if options.get("suffix", None) is not None
else ""
)
if suffix != "" and re.match(r"\w", suffix[0]):
suffix = " " + suffix
# remove comments
results = [re.sub(r"\s*#[^\n]*(?:\n|$)", "", r, flags=re.DOTALL) for r in selected_choices_text]
return separator.join(results)
return prefix + separator.join(results) + suffix
return ""
def __convert_choices_options(self, options: Optional[lark.Tree]) -> dict:
"""
Convert the choices options to a dictionary.
Args:
options (Tree): The choices options tree.
Returns:
dict: The converted choices options.
"""
if options is None:
return None
the_options = {}
if len(options.children) == 1:
the_options["sampler"] = options.children[0] if options.children[0] is not None else "~"
else:
the_options["sampler"] = options.children[0].children[0] if options.children[0] is not None else "~"
the_options["repeating"] = (
options.children[1].children[0] == "r" if options.children[1] is not None else False
)
if len(options.children) == 4:
ifrom = 2
ito = 2
isep = 3
else: # 6
ifrom = 2
ito = 3
isep = 4
the_options["from"] = (
int(options.children[ifrom].children[0]) if options.children[ifrom] is not None else 1
)
the_options["to"] = int(options.children[ito].children[0]) if options.children[ito] is not None else 1
the_options["separator"] = (
self.__visit(options.children[isep], False, True)
if options.children[isep] is not None
else self.__ppp.wil_choice_separator
)
return the_options
def __convert_choice(self, choice: lark.Tree) -> dict:
"""
Convert the choice to a dictionary.
Args:
choice (Tree): The choice tree.
Returns:
dict: The converted choice.
"""
the_choice = {}
c_label_obj = choice.children[0]
the_choice["labels"] = (
[x.value.lower() for x in c_label_obj.children[1:-1]] # should be a token
if c_label_obj is not None
else []
)
the_choice["weight"] = float(choice.children[1].children[0]) if choice.children[1] is not None else 1.0
the_choice["if"] = choice.children[2].children[0] if choice.children[2] is not None else None
the_choice["content"] = choice.children[3]
return the_choice
def __check_wildcard_initialization(self, wildcard: PPPWildcard):
"""
Initializes a wildcard if it hasn't been yet.
Args:
wildcard (PPPWildcard): The wildcard to check.
"""
choice_values = wildcard.choices
options = wildcard.options
if choice_values is None:
t1 = time.time()
choice_values = []
n = 0
# we check the first choice to see if it is actually options
if isinstance(wildcard.unprocessed_choices[0], dict):
if self.__ppp.wildcard_obj.is_dict_choices_options(wildcard.unprocessed_choices[0]):
options = wildcard.unprocessed_choices[0]
prefix = options.get("prefix", None)
if prefix is not None and isinstance(prefix, str):
try:
options["prefix"] = self.__ppp.parse_prompt(
"choicevalue", prefix, self.__ppp.parser_choicevalue, True
)
except lark.exceptions.UnexpectedInput as e:
self.__ppp.logger.warning(
f"Error parsing choice prefix '{prefix}' in wildcard '{wildcard.key}'! : {e.__class__.__name__}"
)
del options["prefix"]
suffix = options.get("suffix", None)
if suffix is not None and isinstance(suffix, str):
try:
options["suffix"] = self.__ppp.parse_prompt(
"choicevalue", suffix, self.__ppp.parser_choicevalue, True
)
except lark.exceptions.UnexpectedInput as e:
self.__ppp.logger.warning(
f"Error parsing choice suffix '{suffix}' in wildcard '{wildcard.key}'! : {e.__class__.__name__}"
)
del options["suffix"]
n = 1
else:
try:
options = self.__convert_choices_options(
self.__ppp.parse_prompt(
"as choices options",
wildcard.unprocessed_choices[0],
self.__ppp.parser_choicesoptions,
True,
)
)
n = 1
except lark.exceptions.UnexpectedInput:
options = None
if self.__ppp.debug_level == DEBUG_LEVEL.full:
self.__ppp.logger.debug("Does not have options")
wildcard.options = options
# we process the choices
for cv in wildcard.unprocessed_choices[n:]:
if isinstance(cv, dict):
if self.__ppp.wildcard_obj.is_dict_choice_options(cv):
theif = cv.get("if", None)
if theif is not None and isinstance(theif, str):
try:
cv["if"] = self.__ppp.parse_prompt(
"condition", theif, self.__ppp.parser_condition, True
)
except lark.exceptions.UnexpectedInput as e:
self.__ppp.logger.warning(
f"Error parsing condition '{theif}' in wildcard '{wildcard.key}'! : {e.__class__.__name__}"
)
cv["if"] = None
content = cv.get("content", cv.get("text", None))
cv["content"] = content
if "text" in cv:
del cv["text"]
if content is not None and isinstance(content, str):
try:
cv["content"] = self.__ppp.parse_prompt(
"choicevalue", content, self.__ppp.parser_choicevalue, True
)
except lark.exceptions.UnexpectedInput as e:
self.__ppp.logger.warning(
f"Error parsing choice content '{content}' in wildcard '{wildcard.key}'! : {e.__class__.__name__}"
)
cv["content"] = None
if cv["content"] is not None:
if self.__ppp.debug_level == DEBUG_LEVEL.full:
self.__ppp.logger.debug(f"Processed choice {cv}")
choice_values.append(cv)
else:
self.__ppp.logger.warning(f"Invalid choice {cv} in wildcard '{wildcard.key}'!")
else:
self.__ppp.logger.warning(f"Invalid choice '{cv}' in wildcard '{wildcard.key}'!")
else:
try:
choice_values.append(
self.__convert_choice(
self.__ppp.parse_prompt("choice", cv, self.__ppp.parser_choice, True)
)
)
except lark.exceptions.UnexpectedInput as e:
self.__ppp.logger.warning(
f"Error parsing choice '{cv}' in wildcard '{wildcard.key}'! : {e.__class__.__name__}"
)
wildcard.choices = choice_values
t2 = time.time()
if self.__ppp.debug_level == DEBUG_LEVEL.full:
self.__ppp.logger.debug(f"Processed choices for wildcard '{wildcard.key}' ({t2-t1:.3f} seconds)")
return (options, choice_values)
def wildcard(self, tree: lark.Tree):
"""
Process a wildcard construct in the tree.
"""
t1 = time.time()
start_result = self.result
options = tree.children[0]
applied_options = self.__convert_choices_options(tree.children[0])
wildcard_key: str = tree.children[1].value # should be a token
wc = self.__get_original_node_content(tree, f"?__{wildcard_key}__")
if self.__ppp.wil_process_wildcards:
@@ -1205,7 +1483,6 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
t2 = time.time()
self.__debug_end("wildcard", start_result, t2 - t1, wc)
return
variablename = None
filter_specifier = None
filter_object = tree.children[2]
if filter_object is not None:
@@ -1237,13 +1514,15 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
f"Using a globbing wildcard '{wildcard_key}' with positional index filters is not recommended!"
)
var_object = tree.children[3]
variablename = None
variablebackup = None
if var_object is not None:
variablename = var_object.children[0] # should be a token
variablevalue = self.__visit(var_object.children[1], False, True)
variablebackup = self.__ppp.user_variables.get(variablename, None)
self.__remove_user_variable(variablename)
self.__set_user_variable_value(variablename, variablevalue)
choice_values_obj_all = []
choice_values_all = []
for wildcard in selected_wildcards:
if wildcard is None:
self.detectedWildcards.append(wc)
@@ -1251,45 +1530,15 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
t2 = time.time()
self.__debug_end("wildcard", start_result, t2 - t1, wc)
return
choice_values_obj = wildcard.choices_obj
options_obj = wildcard.options_obj
if choice_values_obj is None:
t1 = time.time()
choice_values_obj = []
try:
options_obj = self.__ppp.parse_prompt(
"as choices options", wildcard.choices[0], self.__ppp.parser_choicesoptions, True
)
n = 1
except lark.exceptions.UnexpectedInput:
options_obj = None
n = 0
if self.__ppp.debug_level == DEBUG_LEVEL.full:
self.__ppp.logger.debug("Does not have options")
wildcard.options_obj = options_obj
for cv in wildcard.choices[n:]:
try:
choice_values_obj.append(
self.__ppp.parse_prompt("choice", cv, self.__ppp.parser_choice, True)
)
except lark.exceptions.UnexpectedInput as e:
self.__ppp.logger.warning(
f"Error parsing choice '{cv}' in wildcard '{wildcard.key}'! : {e.__class__.__name__}"
)
wildcard.choices_obj = choice_values_obj
t2 = time.time()
if self.__ppp.debug_level == DEBUG_LEVEL.full:
self.__ppp.logger.debug(
f"Processed choices for wildcard '{wildcard.key}' ({t2-t1:.3f} seconds)"
)
if options_obj is not None:
if options is None:
options = options_obj
(options, choice_values) = self.__check_wildcard_initialization(wildcard)
if options is not None:
if applied_options is None:
applied_options = options
else:
if self.__ppp.debug_level == DEBUG_LEVEL.full:
self.__ppp.logger.debug(f"Options for wildcard '{wildcard.key}' are ignored!")
choice_values_obj_all += choice_values_obj
self.result += self.__get_choices(options, choice_values_obj_all, filter_specifier, wildcard_key)
choice_values_all += choice_values
self.result += self.__get_choices(applied_options, choice_values_all, filter_specifier, wildcard_key)
if wildcard_key in self.__wildcard_filters:
del self.__wildcard_filters[wildcard_key]
if variablename is not None:
@@ -1308,8 +1557,8 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
"""
t1 = time.time()
start_result = self.result
options = tree.children[0]
choice_values = tree.children[1::]
options = self.__convert_choices_options(tree.children[0])
choice_values = [self.__convert_choice(c) for c in tree.children[1::]]
ch = self.__get_original_node_content(tree, "?{...}")
if self.__ppp.wil_process_wildcards:
if self.__ppp.debug_level == DEBUG_LEVEL.full:
+2 -2
View File
@@ -4,8 +4,8 @@ from typing import Tuple
class PPPLRUCache:
ProcessInput = Tuple[int, str, str]
ProcessResult = Tuple[str, str]
ProcessInput = Tuple[int, int, str, str] # (seed, wildcards_hash, positive_prompt, negative_prompt)
ProcessResult = Tuple[str, str] # (positive_prompt, negative_prompt)
def __init__(self, capacity: int):
self.cache = OrderedDict()
+106 -36
View File
@@ -16,8 +16,6 @@ if __name__ == "__main__":
class PromptPostProcessorComfyUINode:
VERSION = PromptPostProcessor.VERSION
logger = None
def __init__(self):
@@ -27,31 +25,28 @@ class PromptPostProcessorComfyUINode:
with open(grammar_filename, "r", encoding="utf-8") as file:
self.grammar_content = file.read()
self.wildcards_obj = PPPWildcards(lf.log)
self.logger.info(f"{PromptPostProcessor.NAME} {PromptPostProcessor.VERSION} initialized")
class SmartType(str):
def __ne__(self, other):
if self == "*" or other == "*":
return False
selfset = set(self.split(","))
otherset = set(other.split(","))
return not otherset.issubset(selfset)
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"model": (
"MODEL",
{
"forceInput": True,
},
),
"modelname": (
"STRING",
{
"default": "",
"forceInput": True,
},
),
"pos_prompt": (
"STRING",
{
"multiline": True,
"default": "",
"dynamicPrompts": False,
"forceInput": True,
"defaultInput": True,
"forceInput": False,
},
),
"neg_prompt": (
@@ -60,15 +55,34 @@ class PromptPostProcessorComfyUINode:
"multiline": True,
"default": "",
"dynamicPrompts": False,
"forceInput": True,
"defaultInput": True,
"forceInput": False,
},
),
},
"optional": {
"model": (
cls.SmartType("MODEL,STRING"),
{
"default": "",
"placeholder": "internal model class name",
"forceInput": True,
},
),
"modelname": (
"STRING",
{
"default": "",
"placeholder": "full path of the model",
"defaultInput": True,
"forceInput": False,
},
),
"seed": (
"INT",
{
"default": -1,
"defaultInput": True,
"forceInput": False,
},
),
@@ -77,6 +91,8 @@ class PromptPostProcessorComfyUINode:
{
"default": DEBUG_LEVEL.minimal.value,
"tooltip": "Debug level",
"defaultInput": False,
"forceInput": False,
},
),
"pony_substrings": (
@@ -85,6 +101,8 @@ class PromptPostProcessorComfyUINode:
"default": PromptPostProcessor.DEFAULT_PONY_SUBSTRINGS,
"placeholder": "comma separated list",
"tooltip": "Comma separated list of substrings to look for in the modelname to determine if the model is a pony model",
"defaultInput": False,
"forceInput": False,
},
),
"wc_process_wildcards": (
@@ -94,6 +112,8 @@ class PromptPostProcessorComfyUINode:
"tooltip": "Process wildcards in the prompt",
"label_on": "Yes",
"label_off": "No",
"defaultInput": False,
"forceInput": False,
},
),
"wc_wildcards_folders": (
@@ -101,6 +121,8 @@ class PromptPostProcessorComfyUINode:
{
"default": "",
"tooltip": "Comma separated list of wildcards folders",
"defaultInput": False,
"forceInput": False,
},
),
"wc_if_wildcards": (
@@ -108,6 +130,8 @@ class PromptPostProcessorComfyUINode:
{
"default": PromptPostProcessor.IFWILDCARDS_CHOICES.ignore.value,
"tooltip": "How to handle invalid wildcards in the prompt",
"defaultInput": False,
"forceInput": False,
},
),
"wc_choice_separator": (
@@ -115,6 +139,8 @@ class PromptPostProcessorComfyUINode:
{
"default": PromptPostProcessor.DEFAULT_CHOICE_SEPARATOR,
"tooltip": "Default separator for selected choices",
"defaultInput": False,
"forceInput": False,
},
),
"wc_keep_choices_order": (
@@ -124,6 +150,8 @@ class PromptPostProcessorComfyUINode:
"tooltip": "Keep the order of the choices in the prompt",
"label_on": "Yes",
"label_off": "No",
"defaultInput": False,
"forceInput": False,
},
),
"stn_separator": (
@@ -131,6 +159,8 @@ class PromptPostProcessorComfyUINode:
{
"default": PromptPostProcessor.DEFAULT_STN_SEPARATOR,
"tooltip": "Separator for the content added to the negative prompt",
"defaultInput": False,
"forceInput": False,
},
),
"stn_ignore_repeats": (
@@ -140,6 +170,8 @@ class PromptPostProcessorComfyUINode:
"tooltip": "Ignore repeated content added to the negative prompt",
"label_on": "Yes",
"label_off": "No",
"defaultInput": False,
"forceInput": False,
},
),
"cleanup_extra_spaces": (
@@ -149,6 +181,8 @@ class PromptPostProcessorComfyUINode:
"tooltip": "Remove extra spaces",
"label_on": "Yes",
"label_off": "No",
"defaultInput": False,
"forceInput": False,
},
),
"cleanup_empty_constructs": (
@@ -158,6 +192,8 @@ class PromptPostProcessorComfyUINode:
"tooltip": "Remove empty constructs",
"label_on": "Yes",
"label_off": "No",
"defaultInput": False,
"forceInput": False,
},
),
"cleanup_extra_separators": (
@@ -167,6 +203,8 @@ class PromptPostProcessorComfyUINode:
"tooltip": "Remove extra separators",
"label_on": "Yes",
"label_off": "No",
"defaultInput": False,
"forceInput": False,
},
),
"cleanup_extra_separators2": (
@@ -176,15 +214,19 @@ class PromptPostProcessorComfyUINode:
"tooltip": "Remove extra separators (additional cases)",
"label_on": "Yes",
"label_off": "No",
"defaultInput": False,
"forceInput": False,
},
),
"cleanup_breaks": (
"BOOLEAN",
{
"default": True,
"default": False,
"tooltip": "Cleanup around BREAKs",
"label_on": "Yes",
"label_off": "No",
"defaultInput": False,
"forceInput": False,
},
),
"cleanup_breaks_eol": (
@@ -194,15 +236,19 @@ class PromptPostProcessorComfyUINode:
"tooltip": "Set BREAKs in their own line",
"label_on": "Yes",
"label_off": "No",
"defaultInput": False,
"forceInput": False,
},
),
"cleanup_ands": (
"BOOLEAN",
{
"default": True,
"default": False,
"tooltip": "Cleanup around ANDs",
"label_on": "Yes",
"label_off": "No",
"defaultInput": False,
"forceInput": False,
},
),
"cleanup_ands_eol": (
@@ -212,6 +258,8 @@ class PromptPostProcessorComfyUINode:
"tooltip": "Set ANDs in their own line",
"label_on": "Yes",
"label_off": "No",
"defaultInput": False,
"forceInput": False,
},
),
"cleanup_extranetwork_tags": (
@@ -221,6 +269,8 @@ class PromptPostProcessorComfyUINode:
"tooltip": "Clean up around extra network tags",
"label_on": "Yes",
"label_off": "No",
"defaultInput": False,
"forceInput": False,
},
),
"cleanup_merge_attention": (
@@ -230,6 +280,8 @@ class PromptPostProcessorComfyUINode:
"tooltip": "Merge nested attention constructs",
"label_on": "Yes",
"label_off": "No",
"defaultInput": False,
"forceInput": False,
},
),
"remove_extranetwork_tags": (
@@ -239,11 +291,26 @@ class PromptPostProcessorComfyUINode:
"tooltip": "Remove extra network tags",
"label_on": "Yes",
"label_off": "No",
"defaultInput": False,
"forceInput": False,
},
),
},
}
@classmethod
def VALIDATE_INPUTS(cls, input_types: dict[str, str]):
it = cls.INPUT_TYPES()
expected = {
k: cls.SmartType("COMBO,STRING") if isinstance(v[0], list) else v[0] # we allow string for combos
for k, v in {**it["required"], **it["optional"]}.items()
}
for input_name, input_type in input_types.items():
t = expected[input_name]
if input_type != t:
return f"Invalid type for input '{input_name}': {input_type} (expected {t})"
return True
RETURN_TYPES = (
"STRING",
"STRING",
@@ -286,7 +353,9 @@ class PromptPostProcessorComfyUINode:
cleanup_merge_attention,
remove_extranetwork_tags,
):
new_run = {
if wc_process_wildcards:
return float("NaN") # since we can't detect changes in wildcards we assume they are always changed when enabled
new_run = { # everything except debug_level
"model": model,
"modelname": modelname,
"pos_prompt": pos_prompt,
@@ -343,26 +412,27 @@ class PromptPostProcessorComfyUINode:
cleanup_merge_attention,
remove_extranetwork_tags,
):
modelclass = (
model.model.model_config.__class__.__name__ if model is not None and not isinstance(model, str) else model
) or ""
if modelclass == "":
self.logger.warning("Model class is not provided. System variables might not be properly set.")
if modelname == "":
self.logger.warning("Modelname is not provided. System variables will not be properly set.")
env_info = {
"app": "comfyui",
"models_path": folder_paths.models_dir,
"model_filename": modelname, # path is relative to checkpoints folder
"model_class": model.model.model_config.__class__.__name__,
"is_sd1": model.model.model_config.__class__.__name__ in ("SD15", "SD15_instructpix2pix"),
"is_sd2": model.model.model_config.__class__.__name__ in ("SD20", "SD21UnclipL", "SD21UnclipH"),
"is_sdxl": model.model.model_config.__class__.__name__
in (
"SDXL",
"SDXLRefiner",
"SDXL_instructpix2pix",
"Segmind_Vega",
"KOALA_700M",
"KOALA_1B",
"model_filename": modelname or "", # path is relative to checkpoints folder
"model_class": modelclass,
"is_sd1": modelclass in ("SD15", "SD15_instructpix2pix"),
"is_sd2": modelclass in ("SD20", "SD21UnclipL", "SD21UnclipH"),
"is_sdxl": (
modelclass in ("SDXL", "SDXLRefiner", "SDXL_instructpix2pix", "Segmind_Vega", "KOALA_700M", "KOALA_1B")
),
"is_ssd": model.model.model_config.__class__.__name__ in ("SSD1B"),
"is_sd3": model.model.model_config.__class__.__name__ in ("SD3"),
"is_flux": model.model.model_config.__class__.__name__ in ("Flux"),
"is_auraflow": model.model.model_config.__class__.__name__ in ("AuraFlow"),
"is_ssd": modelclass in ("SSD1B",),
"is_sd3": modelclass in ("SD3",),
"is_flux": modelclass in ("Flux",),
"is_auraflow": modelclass in ("AuraFlow",),
}
# SVD_img2vid, SVD3D_u, SVD3_p, Stable_Zero123, SD_X4Upscaler,
# Stable_Cascade_C, Stable_Cascade_B, StableAudio
+256 -83
View File
@@ -2,59 +2,112 @@ import fnmatch
import os
import json
from typing import Optional
import logging
import yaml
from ppp_logging import DEBUG_LEVEL
def deep_freeze(obj):
"""
Deep freeze an object.
Args:
obj (object): The object to freeze.
Returns:
object: The frozen object.
"""
if isinstance(obj, dict):
return tuple((k, deep_freeze(v)) for k, v in sorted(obj.items()))
elif isinstance(obj, list):
return tuple(deep_freeze(i) for i in obj)
elif isinstance(obj, set):
return tuple(deep_freeze(i) for i in sorted(obj))
else:
return obj
class PPPWildcard:
"""
A wildcard object.
Attributes:
key (str): The key of the wildcard.
file (str): The path to the file where the wildcard is defined.
unprocessed_choices (list[str]): The unprocessed choices of the wildcard.
choices (list[dict]): The processed choices of the wildcard.
options (dict): The options of the wildcard.
"""
def __init__(self, fullpath: str, key: str, choices: list[str]):
self.key: str = key
self.file: str = fullpath
self.choices: list[str] = choices
self.choices_obj: list[object] = None
self.options_obj: object = None
self.unprocessed_choices: list[str] = choices
self.choices: list[dict] = None
self.options: dict = None
def __hash__(self) -> int:
t = (self.key, deep_freeze(self.unprocessed_choices))
return hash(t)
class PPPWildcards:
"""
A class to manage wildcards.
Attributes:
wildcards (dict[str, PPPWildcard]): The wildcards.
"""
DEFAULT_WILDCARDS_FOLDER = "wildcards"
def __init__(self, logger):
self.logger = logger
self.debug_level = DEBUG_LEVEL.none
self.wildcards_folders = []
self.__logger: logging.Logger = logger
self.__debug_level = DEBUG_LEVEL.none
self.__wildcards_folders = []
self.__wildcard_files = {}
self.wildcards: dict[str, PPPWildcard] = {}
self.wildcard_files = {}
def __hash__(self) -> int:
return hash(deep_freeze(self.wildcards))
def refresh_wildcards(self, debug_level: DEBUG_LEVEL, wildcards_folders: Optional[list[str]]):
"""
Initialize the wildcards.
"""
self.debug_level = debug_level
self.wildcards_folders = wildcards_folders
self.__debug_level = debug_level
self.__wildcards_folders = wildcards_folders
if wildcards_folders is not None:
# if self.debug_level != DEBUG_LEVEL.none:
# self.logger.info("Initializing wildcards...")
# t1 = time.time()
for fullpath in list(self.wildcard_files.keys()):
for fullpath in list(self.__wildcard_files.keys()):
path = os.path.dirname(fullpath)
if not os.path.exists(fullpath) or not any(
os.path.commonpath([path, folder]) == folder for folder in self.wildcards_folders
os.path.commonpath([path, folder]) == folder for folder in self.__wildcards_folders
):
self.__remove_wildcards_from_file(fullpath)
for f in self.wildcards_folders:
for f in self.__wildcards_folders:
self.__get_wildcards_in_directory(f, f)
# t2 = time.time()
# if self.debug_level != DEBUG_LEVEL.none:
# self.logger.info(f"Wildcards init time: {t2 - t1:.3f} seconds")
else:
self.wildcards_folders = []
self.__wildcards_folders = []
self.wildcards = {}
self.wildcard_files = {}
self.__wildcard_files = {}
def get_wildcards(self, key: str) -> list[PPPWildcard]:
"""
Get all wildcards that match a key.
Args:
key (str): The key to match.
Returns:
list: A list of all wildcards that match the key.
"""
keys = sorted(fnmatch.filter(self.wildcards.keys(), key))
return [self.wildcards[k] for k in keys]
@@ -104,11 +157,11 @@ class PPPWildcards:
full_path (str): The path to the file.
debug (bool): Whether to print debug messages or not.
"""
last_modified_cached = self.wildcard_files.get(full_path, None)
if debug and last_modified_cached is not None and self.debug_level != DEBUG_LEVEL.none:
self.logger.debug(f"Removing wildcards from file: {full_path}")
if full_path in self.wildcard_files.keys():
del self.wildcard_files[full_path]
last_modified_cached = self.__wildcard_files.get(full_path, None)
if debug and last_modified_cached is not None and self.__debug_level != DEBUG_LEVEL.none:
self.__logger.debug(f"Removing wildcards from file: {full_path}")
if full_path in self.__wildcard_files.keys():
del self.__wildcard_files[full_path]
for key in list(self.wildcards.keys()):
if self.wildcards[key].file == full_path:
del self.wildcards[key]
@@ -122,87 +175,207 @@ class PPPWildcards:
full_path (str): The path to the file.
"""
last_modified = os.path.getmtime(full_path)
last_modified_cached = self.wildcard_files.get(full_path, None)
if last_modified_cached is not None and last_modified == self.wildcard_files[full_path]:
last_modified_cached = self.__wildcard_files.get(full_path, None)
if last_modified_cached is not None and last_modified == self.__wildcard_files[full_path]:
return
filename = os.path.basename(full_path)
name, extension = os.path.splitext(filename)
_, extension = os.path.splitext(filename)
if extension not in (".txt", ".json", ".yaml", ".yml"):
return
self.__remove_wildcards_from_file(full_path, False)
if last_modified_cached is not None and self.debug_level != DEBUG_LEVEL.none:
self.logger.debug(f"Updating wildcards from file: {full_path}")
relfolders = os.path.relpath(os.path.dirname(full_path), base)
if relfolders == ".":
relfolders = ""
elif relfolders != "":
relfolders += "/"
if last_modified_cached is not None and self.__debug_level != DEBUG_LEVEL.none:
self.__logger.debug(f"Updating wildcards from file: {full_path}")
if extension == ".txt":
self.__get_wildcards_in_text_file(full_path, relfolders, name)
self.__get_wildcards_in_text_file(full_path, base)
elif extension in (".json", ".yaml", ".yml"):
self.__get_wildcards_in_structured_file(full_path, relfolders, extension)
self.wildcard_files[full_path] = last_modified
self.__get_wildcards_in_structured_file(full_path, base)
self.__wildcard_files[full_path] = last_modified
def __get_wildcards_in_structured_file(self, full_path, relfolders, extension):
def is_dict_choices_options(self, d: dict) -> bool:
"""
Check if a dictionary is a valid choices options dictionary.
Args:
d (dict): The dictionary to check.
Returns:
bool: Whether the dictionary is a valid choices options dictionary or not.
"""
return all(
k in ["sampler", "repeating", "count", "from", "to", "prefix", "suffix", "separator"] for k in d.keys()
)
def is_dict_choice_options(self, d: dict) -> bool:
"""
Check if a dictionary is a valid choice options dictionary.
Args:
d (dict): The dictionary to check.
Returns:
bool: Whether the dictionary is a valid choice options dictionary or not.
"""
return all(k in ["labels", "weight", "if", "content", "text"] for k in d.keys())
def __get_choices(self, obj: object, full_path: str, key_parts: list[str]) -> list:
"""
We process the choices in the object and return them as a list.
Args:
obj (object): the value of a wildcard
full_path (str): path to the file where the wildcard is defined
key_parts (list[str]): parts of the key for the wildcard
Returns:
list: list of choices
"""
choices = None
if obj is not None:
if isinstance(obj, (str, dict)):
choices = [obj]
elif isinstance(obj, (int, float, bool)):
choices = [str(obj)]
elif isinstance(obj, list) and len(obj) > 0:
choices = []
for i, c in enumerate(obj):
invalid_choice = False
if isinstance(c, str):
choice = c
elif isinstance(c, (int, float, bool)):
choice = str(c)
elif isinstance(c, list):
# we create an anonymous wildcard
choice = self.__create_anonymous_wildcard(full_path, key_parts, i, c)
elif isinstance(c, dict):
if self.is_dict_choices_options(c) or self.is_dict_choice_options(c):
# we assume it is a choice or wildcard parameters in object format
choice = c
choice_content = choice.get("content", choice.get("text", None))
if choice_content is not None and isinstance(choice_content, list):
# we create an anonymous wildcard
choice["content"] = self.__create_anonymous_wildcard(
full_path, key_parts, i, choice_content
)
if "text" in choice:
del choice["text"]
elif len(c) == 1:
# we assume it is an anonymous wildcard with options
firstkey = list(c.keys())[0]
choice = self.__create_anonymous_wildcard(full_path, key_parts, i, c[firstkey], firstkey)
else:
invalid_choice = True
else:
invalid_choice = True
if invalid_choice:
self.__logger.warning(
f"Invalid choice {i+1} in wildcard '{'/'.join(key_parts)}' in file '{full_path}'!"
)
else:
choices.append(choice)
return choices
def __create_anonymous_wildcard(self, full_path, key_parts, i, content, options=None):
"""
Create an anonymous wildcard.
Args:
full_path (str): The path to the file that contains it.
key_parts (list[str]): The parts of the key.
i (int): The index of the wildcard.
content (object): The content of the wildcard.
options (str): The options for the choice where the wildcard is defined.
Returns:
str: The resulting value for the choice.
"""
new_parts = key_parts + [f"#ANON_{i}"]
self.__add_wildcard(content, full_path, new_parts)
value = f"__{'/'.join(new_parts)}__"
if options is not None:
value = f"{options}::{value}"
return value
def __add_wildcard(self, content: object, full_path: str, external_key_parts: list[str]):
"""
Add a wildcard to the wildcards dictionary.
Args:
content (object): The content of the wildcard.
full_path (str): The path to the file that contains it.
external_key_parts (list[str]): The parts of the key.
"""
key_parts = external_key_parts.copy()
if isinstance(content, dict):
key_parts.pop()
keys = self.__get_keys_in_dict(content)
for key in keys:
tmp_key_parts = key_parts.copy()
tmp_key_parts.extend(key.split("/"))
fullkey = "/".join(tmp_key_parts)
if self.wildcards.get(fullkey, None) is not None:
self.__logger.warning(
f"Duplicate wildcard '{fullkey}' in file '{full_path}' and '{self.wildcards[fullkey].file}'!"
)
else:
obj = self.__get_nested(content, key)
choices = self.__get_choices(obj, full_path, tmp_key_parts)
if choices is None:
self.__logger.warning(f"Invalid wildcard '{fullkey}' in file '{full_path}'!")
else:
self.wildcards[fullkey] = PPPWildcard(full_path, fullkey, choices)
return
if isinstance(content, str):
content = [content]
elif isinstance(content, (int, float, bool)):
content = [str(content)]
if not isinstance(content, list):
self.__logger.warning(f"Invalid wildcard in file '{full_path}'!")
return
fullkey = "/".join(key_parts)
if self.wildcards.get(fullkey, None) is not None:
self.__logger.warning(
f"Duplicate wildcard '{fullkey}' in file '{full_path}' and '{self.wildcards[fullkey].file}'!"
)
else:
choices = self.__get_choices(content, full_path, key_parts)
if choices is None:
self.__logger.warning(f"Invalid wildcard '{fullkey}' in file '{full_path}'!")
else:
self.wildcards[fullkey] = PPPWildcard(full_path, fullkey, choices)
def __get_wildcards_in_structured_file(self, full_path, base):
"""
Get all wildcards in a structured file.
Args:
full_path (str): The path to the file.
base (str): The base path for the wildcards.
"""
external_key: str = os.path.relpath(os.path.splitext(full_path)[0], base)
external_key_parts = external_key.split(os.sep)
_, extension = os.path.splitext(full_path)
with open(full_path, "r", encoding="utf-8") as file:
if extension == ".json":
content = json.loads(file.read())
else:
content = yaml.safe_load(file)
keys = self.__get_keys_in_dict(content)
for key in keys:
fullkey = f"{relfolders}{key}"
if self.wildcards.get(fullkey, None) is not None:
self.logger.warning(
f"Duplicate wildcard '{fullkey}' in file '{full_path}' and '{self.wildcards[fullkey].file}'!"
)
else:
obj = self.__get_nested(content, key)
if obj is not None:
if isinstance(obj, str):
choices = [obj]
elif isinstance(obj, (int, float, bool)):
choices = [str(obj)]
elif isinstance(obj, list) and len(obj) > 0:
choices = []
for c in obj:
if isinstance(c, str):
choices.append(c)
elif isinstance(c, dict): # we convert the dict to a string
d = ""
if "weight" in c.keys():
d += str(c["weight"])
if "if" in c.keys():
d += f" if {c['if']}"
if d != "":
d += "::"
if "text" in c.keys():
d += c["text"]
elif "content" in c.keys():
d += c["content"]
choices.append(d)
else:
obj = None
if obj is None:
self.logger.warning(f"Invalid wildcard '{fullkey}' in file '{full_path}'!")
else:
self.wildcards[fullkey] = PPPWildcard(full_path, fullkey, choices)
self.__add_wildcard(content, full_path, external_key_parts)
def __get_wildcards_in_text_file(self, full_path, relfolders, name):
def __get_wildcards_in_text_file(self, full_path, base):
"""
Get all wildcards in a text file.
Args:
full_path (str): The path to the file.
base (str): The base path for the wildcards.
"""
external_key: str = os.path.relpath(os.path.splitext(full_path)[0], base)
external_key_parts = external_key.split(os.sep)
with open(full_path, "r", encoding="utf-8") as file:
text_content = map(lambda x: x.strip("\n\r"), file.readlines())
text_content = list(filter(lambda x: x.strip() != "" and not x.strip().startswith("#"), text_content))
text_content = [x.split("#")[0].rstrip() if len(x.split("#")) > 1 else x for x in text_content]
fullkey = f"{relfolders}{name}"
if self.wildcards.get(fullkey, None) is not None:
self.logger.warning(
f"Duplicate wildcard '{fullkey}' in file '{full_path}' and '{self.wildcards[fullkey].file}'!"
)
else:
if len(text_content) == 0:
self.logger.warning(f"Invalid wildcard in file '{full_path}'!")
else:
self.wildcards[fullkey] = PPPWildcard(full_path, fullkey, text_content)
self.__add_wildcard(text_content, full_path, external_key_parts)
def __get_wildcards_in_directory(self, base: str, directory: str):
"""
@@ -213,7 +386,7 @@ class PPPWildcards:
directory (str): The path to the directory.
"""
if not os.path.exists(directory):
self.logger.warning(f"Wildcard directory '{directory}' does not exist!")
self.__logger.warning(f"Wildcard directory '{directory}' does not exist!")
return
for filename in os.listdir(directory):
full_path = os.path.abspath(os.path.join(directory, filename))
+15
View File
@@ -0,0 +1,15 @@
[project]
name = "sd-webui-prompt-postprocessor"
description = "Stable Diffusion WebUI & ComfyUI extension to post-process the prompt, including sending content from the prompt to the negative prompt and wildcards."
version = "2.8.0"
license = {file = "LICENSE.txt"}
dependencies = ["lark"]
[project.urls]
Repository = "https://github.com/acorderob/sd-webui-prompt-postprocessor"
# Used by Comfy Registry https://comfyregistry.org
[tool.comfy]
PublisherId = "acorderob"
DisplayName = "sd-webui-prompt-postprocessor"
Icon = ""
+86 -24
View File
@@ -4,6 +4,7 @@ if __name__ == "__main__":
import sys
import os
import time
import numpy as np
sys.path.append(os.path.join(sys.path[0], ".."))
@@ -57,6 +58,7 @@ class PromptPostProcessorA1111Script(scripts.Script):
with open(grammar_filename, "r", encoding="utf-8") as file:
self.grammar_content = file.read()
self.wildcards_obj = PPPWildcards(lf.log)
self.ppp_logger.info(f"{PromptPostProcessor.NAME} {PromptPostProcessor.VERSION} initialized")
def title(self):
"""
@@ -79,7 +81,48 @@ class PromptPostProcessorA1111Script(scripts.Script):
"""
return scripts.AlwaysVisible
def process(self, p: StableDiffusionProcessing, *args, **kwargs): # pylint: disable=unused-argument
def ui(self, is_img2img):
with gr.Accordion(PromptPostProcessor.NAME, open=False):
force_equal_seeds = gr.Checkbox(
label="Force equal seeds",
info="Force all image seeds and variation seeds to be equal to the first one, disabling the default autoincrease.",
default=False,
# show_label=True,
elem_id="ppp_force_equal_seeds",
)
gr.HTML(
"""<br><div>Unlink the seed to use the specified one for the prompts instead of the image seed.
This seed will only change for each image in the batch if the value is -1 or 'variable seed' is checked.</div>
<div>Seeds are only used for the wildcards and choice constructs.</div>"""
)
unlink_seed = gr.Checkbox(
label="Unlink seed",
default=False,
# show_label=True,
elem_id="ppp_unlink_seed",
)
seed = gr.Number(
label="Seed",
default=-1,
precision=0,
# minimum=-1,
# maximum=2**32 - 1,
# step=1,
# show_label=True,
min_width=100,
elem_id="ppp_seed",
)
variable_seed = gr.Checkbox(
label="Variable seed",
default=False,
# show_label=True,
elem_id="ppp_variable_seed",
)
return [force_equal_seeds, unlink_seed, seed, variable_seed]
def process(
self, p: StableDiffusionProcessing, input_force_equal_seeds, input_unlink_seed, input_seed, input_variable_seed
): # pylint: disable=arguments-differ
"""
Processes the prompts and applies post-processing operations.
@@ -138,7 +181,7 @@ class PromptPostProcessorA1111Script(scripts.Script):
env_info["is_ssd"] = False # ?
env_info["is_sd3"] = getattr(p.sd_model, "is_sd3", False)
env_info["is_flux"] = p.sd_model.model_config.__class__.__name__ == "Flux"
env_info["is_auraflow"] = False # p.sd_model.model_config.__class__.__name__ == "AuraFlow"
env_info["is_auraflow"] = False # p.sd_model.model_config.__class__.__name__ == "AuraFlow"
else: # assume A1111 compatible (p.sd_model.__class__.__name__=="DiffusionEngine")
env_info["model_class"] = p.sd_model.__class__.__name__
env_info["is_sd1"] = getattr(p.sd_model, "is_sd1", False)
@@ -185,19 +228,38 @@ class PromptPostProcessorA1111Script(scripts.Script):
)
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)
]
if input_force_equal_seeds:
if self.ppp_debug_level != DEBUG_LEVEL.none:
self.ppp_logger.info("Forcing equal seeds")
seeds = getattr(p, "all_seeds", [])
subseeds = getattr(p, "all_subseeds", [])
p.all_seeds = [seeds[0] for _ in seeds]
p.all_subseeds = [subseeds[0] for _ in subseeds]
if input_unlink_seed:
if self.ppp_debug_level != DEBUG_LEVEL.none:
self.ppp_logger.info("Using unlinked seed")
num_seeds = len(getattr(p, "all_seeds", []))
if input_seed == -1:
calculated_seeds = np.random.randint(0, 2**32, size=num_seeds, dtype=np.int64)
elif input_variable_seed:
calculated_seeds = [input_seed + i for i in range(num_seeds)]
else:
calculated_seeds = [input_seed for _ in range(num_seeds)]
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)]
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)
]
# 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)]
else:
calculated_seeds = seeds
# adds regular prompts
rpr = getattr(p, "all_prompts", None)
@@ -222,24 +284,24 @@ class PromptPostProcessorA1111Script(scripts.Script):
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))
if self.lru_cache.get((seed, hash(self.wildcards_obj), prompt, negative_prompt)) is None:
posp, negp = ppp.process_prompt(prompt, negative_prompt, seed)
self.lru_cache.put((seed, hash(self.wildcards_obj), prompt, negative_prompt), (posp, negp))
# adds also the result so i2i doesn't process it unnecessarily
self.lru_cache.put((seed, pp, np), (pp, np))
self.lru_cache.put((seed, hash(self.wildcards_obj), posp, negp), (posp, negp))
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))
found = self.lru_cache.get((seed, hash(self.wildcards_obj), 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))
found = self.lru_cache.get((seed, hash(self.wildcards_obj), prompt, negative_prompt))
if found is not None:
rph[i] = found[0]
rnh[i] = found[1]
@@ -336,7 +398,7 @@ def on_ui_settings():
# wildcard settings
shared.opts.add_option(
key="ppp_wil_sep",
info=new_html_title('<br><h2>Wildcard settings</h2>'),
info=new_html_title("<br><h2>Wildcard settings</h2>"),
)
shared.opts.add_option(
key="ppp_wil_processwildcards",
@@ -395,7 +457,7 @@ def on_ui_settings():
# content removal settings
shared.opts.add_option(
key="ppp_rem_sep",
info=new_html_title('<br><h2>Content removal settings</h2>'),
info=new_html_title("<br><h2>Content removal settings</h2>"),
)
shared.opts.add_option(
key="ppp_rem_removeextranetworktags",
@@ -409,7 +471,7 @@ def on_ui_settings():
# send to negative settings
shared.opts.add_option(
key="ppp_stn_sep",
info=new_html_title('<br><h2>Send to Negative settings</h2>'),
info=new_html_title("<br><h2>Send to Negative settings</h2>"),
)
shared.opts.add_option(
key="ppp_stn_separator",
@@ -430,7 +492,7 @@ def on_ui_settings():
# clean-up settings
shared.opts.add_option(
key="ppp_cup_sep",
info=new_html_title('<br><h2>Clean-up settings</h2>'),
info=new_html_title("<br><h2>Clean-up settings</h2>"),
)
shared.opts.add_option(
key="ppp_cup_emptyconstructs",
+56 -2
View File
@@ -101,6 +101,18 @@ class TestPromptPostProcessor(unittest.TestCase):
self.__grammar_content,
self.__wildcards_obj,
)
self.__comfyuippp = PromptPostProcessor(
self.__ppp_logger,
self.__interrupt,
{
**self.__def_env_info,
"app": "comfyui",
"model_class": "SDXL",
},
self.__defopts,
self.__grammar_content,
self.__wildcards_obj,
)
def __interrupt(self):
self.__interrupted = True
@@ -575,8 +587,8 @@ class TestPromptPostProcessor(unittest.TestCase):
self.__process(
PromptPair("__bad_wildcard__", "{option1|option2}"),
PromptPair(
PromptPostProcessor.WILDCARD_STOP + "__bad_wildcard__",
PromptPostProcessor.WILDCARD_STOP + "{option1|option2}",
PromptPostProcessor.WILDCARD_STOP.format("__bad_wildcard__") + "__bad_wildcard__",
"{option1|option2}",
),
ppp=PromptPostProcessor(
self.__ppp_logger,
@@ -593,6 +605,24 @@ class TestPromptPostProcessor(unittest.TestCase):
interrupted=True,
)
def test_wcinvar_warn(self): # wildcards in var with warn option
self.__process(
PromptPair("${v=__bad_wildcard__}${v}", ""),
PromptPair(PromptPostProcessor.WILDCARD_WARNING + "__bad_wildcard__", ""),
ppp=PromptPostProcessor(
self.__ppp_logger,
self.__interrupt,
self.__def_env_info,
{
**self.__defopts,
"process_wildcards": False,
"if_wildcards": PromptPostProcessor.IFWILDCARDS_CHOICES.warn.value,
},
self.__grammar_content,
self.__wildcards_obj,
),
)
def test_wc_wildcard1a_text(self): # simple text wildcard
self.__process(
PromptPair("the choices are: __text/wildcard1__", ""),
@@ -780,6 +810,30 @@ class TestPromptPostProcessor(unittest.TestCase):
ppp=self.__nocupppp,
)
def test_wc_wildcardPS_yaml(self): # yaml wildcard with object formatted choices and options and prefix and suffix
self.__process(
PromptPair("the choices are: __yaml/wildcardPS__", ""),
PromptPair("the choices are: prefix-choice2/choice3-suffix", ""),
ppp=self.__nocupppp,
)
def test_wc_anonymouswildcard_yaml(self): # yaml anonymous wildcard
self.__process(
PromptPair("the choices are: __yaml/anonwildcards__", ""),
PromptPair("the choices are: six", ""),
ppp=self.__nocupppp,
)
# ComfyUI tests
def test_comfyui_attention(self): # attention conversion
self.__process(
PromptPair("(test1) (test2:1.5) [test3] [(test4)]", ""),
PromptPair("(test1) (test2:1.5) (test3:0.9) (test4:0.99)", ""),
ppp=self.__comfyuippp,
)
# def test_mix(self):
# self.__process(
# PromptPair(
+36 -3
View File
@@ -8,7 +8,7 @@ yaml:
- ~r2-3$$-
- "'label1,label2'4::choice1"
- "3:: choice2 "
- "'label1,label3'2::choice3"
- { labels: ["label1", "label3"], weight: 2, content: choice3 }
- 5 if _is_sd1::choice4
wildcard2bis:
@@ -28,9 +28,42 @@ yaml:
- { weight: 2, text: choice1 }
- { weight: 3, content: choice2 }
- { text: choice3 }
- { weight: 4, if: "_is_ssd", text: choice4 }
- { weight: 4, if: _is_ssd, text: choice4 }
wildcard7:
- <lora:test1:1>
- <lora:test2:1>
- <lora:test3:1>
- <lora:test3:1>
wildcardPS:
- {
sampler: "~",
repeating: false,
count: 2,
prefix: "prefix-",
suffix: "-suffix",
separator: "/",
}
- { weight: 3, text: choice1 }
- { weight: 2, text: choice2 }
- { weight: 1, text: choice3 }
more_nested:
even_more_nested: # this would be __yaml/more_nested/even_more_nested__
- one
- two
anonwildcards:
- one
- two
- # choice without options and anonymous wildcard
- three
- four
- 3 if _is_sdxl: # choice with options and anonymous wildcard
- five
- six
- { weight: 1, text: [seven, eight] } # anonymous wildcard used in a choice in object format
- # choice without options and anonymous wildcard with parameters
- { count: 2, prefix: "#" }
- nine
- ten
+3
View File
@@ -0,0 +1,3 @@
- one
- 2
- three
+1
View File
@@ -0,0 +1 @@
one choice