Compare commits

..
3 Commits
Author SHA1 Message Date
Antonio Cordero Balcazar 42f7fc65cc * Fixed "dictionary changed size during iteration" error.
* Support for choice labels and their use for filtering wildcard choices.
* New system variable _modelclass and renaming of _sd->_model, _sdname->_modelname and _sdfullname->_modelfullname.
* Support for auraflow models with _is_auraflow.
* Option to "join attention" when sending to negative replaced and generalized as "merge attention".
* Improved processing of extranetwork tags.
2024-09-29 22:51:39 +02:00
Antonio Cordero Balcazar 956a47adbc * Fix process wildcards setting. 2024-09-05 19:26:54 +02:00
Antonio Cordero Balcazar faa55704cc * Fix problem with seed property in ComfyUI, and allow a value of -1 to get a random one. 2024-09-04 22:22:01 +02:00
8 changed files with 574 additions and 274 deletions
+49 -23
View File
@@ -1,6 +1,6 @@
# Prompt Postprocessor for Stable Diffusion WebUI and ComfyUI
# Prompt PostProcessor for Stable Diffusion WebUI and ComfyUI
The Prompt Postprocessor, 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:
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).
@@ -19,6 +19,8 @@ Currently this extension has these functions:
Note: when used in an A1111 compatible webui, the extension must be loaded after any other extension that modifies the prompt (like another wildcards extension). Usually extensions load by their folder name in alphanumeric order, so if the extensions are not loading in the correct order just rename this extension's folder so the ordering works out. When in doubt, just rename this extension's folder with a "z" in front (for example) so that it is the last one to load, or manually set such folder name when installing it.
If the extension runs before others, like Dynamic Prompts, and the "Process wildcards" is enabled, the wildcards will be processed by PPP and those extensions will not get them. If you disable processing the wildcards, and intend another extension to process them, you should keep the "What to do with remaining wildcards?" option as "ignore".
Notes:
1. Other than its own commands, it only recognizes regular A1111 prompt formats. So:
@@ -31,7 +33,9 @@ Notes:
* **Composable Diffusion**: `prompt1:weight1 AND prompt2:weight2`
In SD.Next that means only the *A1111* or *Full* parsers. It will warn you if you use the *Compel* parser.
2. It recognizes wildcards in the *\_\_wildcard\_\_* and *{choice|choice}* formats (and anything that [Dynamic Prompts](https://github.com/adieyal/sd-dynamic-prompts) supports).
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.
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.
## Installation
@@ -90,8 +94,9 @@ The construct parameters can be written with the following options (all are opti
The choice options are as follows:
* "**n**": weight of the choice (default 1)
* "**if condition**": filters out the choice if the condition is false (this is an extension to the Dynamic Prompts syntax). Same conditions as in the `if` command.
* "**'identifiers'**": comma separated labels for the choice (optional, quotes can be single or double). Only makes sense inside a wildcard definition. Can be used when specifying the wildcard to select this specific choice. It's case insensitive.
* "**n**": weight of the choice (optional, default 1).
* "**if condition**": filters out the choice if the condition is false (optional; this is an extension to the Dynamic Prompts syntax). Same conditions as in the `if` command.
* "**::**": end of choice options
Whitespace is allowed between parameters.
@@ -109,33 +114,52 @@ These are examples of formats you can use to insert a choice construct:
Notes:
* The Dynamic Prompts format `{2$$__flavours__}` does not work as expected. It will only output one value. You can write is as `{r2$$__flavours__}` to get two values, but they may repeat since the evaluation of the wildcard is independent of the choices selection.
* Whitespace in the choices is not ignored like in Dynamic Prompts, but will be cleaned up if the appropiate settings are checked.
* Whitespace in the choices is not ignored like in Dynamic Prompts, but will be cleaned up if the appropriate settings are checked.
### Wildcards
The generic format is:
```text
__parameters$$path/to/wildcard(var=value)__
__parameters$$wildcard'filter'(var=value)__
```
The parameters and the setting of a variable are optional. The parameters follow the same format as for the choices. 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).
The parameters, the filter, and the setting of a variable are optional. The parameters follow the same format as for the choices.
In the wildcard definition (which supports the text, json and yaml formats), if the first choice follows the format of these 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. 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".
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.
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.
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 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).
These are examples of formats you can use to insert a wildcard:
```text
__path/wildcard__ # select 1 choice
__3$$path/wildcard__ # select 3 choices
__2-3$$path/wildcard__ # select 2 to 3 choices
__r2-3$$path/wildcard__ # select 2 to 3 choices allowing repetition
__2-3$$ / $$path/wildcard__ # select 2 to 3 choices with separator " / "
__path/wildcard(var=value)__ # select 1 choice using the specified variable value in the evaluation.
__path/wildcard__ # select 1 choice
__path/wildcard'0'__ # select the first choice
__path/wildcard'label'__ # select the choices with label "label"
__path/wildcard'0,label1,label2'__ # select the first choice and those with labels "label1" or "label2"
__path/wildcard'0,label1+label2'__ # select the first choice and those with both labels "label1" and "label2"
__3$$path/wildcard__ # select 3 choices
__2-3$$path/wildcard__ # select 2 to 3 choices
__r2-3$$path/wildcard__ # select 2 to 3 choices allowing repetition
__2-3$$ / $$path/wildcard__ # select 2 to 3 choices with separator " / "
__path/wildcard(var=value)__ # select 1 choice using the specified variable value in the evaluation.
```
#### Wildcard definitions
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.
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".
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.
#### 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.
@@ -161,7 +185,7 @@ The Dynamic Prompts format also works:
```text
${var=value}
${var=!value} # immmediate evaluation
${var=!value} # immediate evaluation
```
If also supports the addition as an extension of the Dynamic Prompts format:
@@ -217,9 +241,10 @@ For a list of values the allowed operations are `contains`, `in` and the value o
The variable can be one set with the `set` or `add` commands or you can use internal variables like these (names starting with an underscore are reserved):
* `_sd` : the loaded model version (`"sd1"`, `"sd2"`, `"sdxl"`)
* `_sdname` : the loaded model filename (without path)
* `_sdfullname`: the loaded model filename (with path)
* `_model` : the loaded model identifier (`"sd1"`, `"sd2"`, `"sdxl"`, `"sd3"`, `"flux"`, `"auraflow"`). `_sd` also works but is deprecated.
* `_modelname` : the loaded model filename (without path). `_sdname` also works but is deprecated.
* `_modelfullname`: the loaded model filename (with path). `_sdfullname` also works but is deprecated.
* `_modelclass`: the class used for the model. Note that this is dependent on the webui. In A1111 all SD versions use the same class. Can be used for new models that are not supported yet with the `_is_*` variables.
* `_is_sd`: true if the loaded model version is any version of SD
* `_is_sd1`: true if the loaded model version is SD 1.x
* `_is_sd2`: true if the loaded model version is SD 2.x
@@ -230,6 +255,7 @@ The variable can be one set with the `set` or `add` commands or you can use inte
* `_is_sdxl_no_pony`: true if the loaded model version is SDXL and not a Pony model.
* `_is_sd3`: true if the loaded model version is SD 3.x
* `_is_flux`: true if the loaded model is Flux
* `_is_auraflow`: true if the loaded model is AuraFlow
Any `elif`s (there can be multiple) and the `else` are optional.
@@ -296,8 +322,8 @@ The content of the negative commands is not processed and is copied as-is to the
They will be translated to the negative prompt. For example:
* `(red<ppp:stn>square<ppp:/stn>:1.5)` will end up as `(square:1.5)` in the negative prompt
* `(red[<ppp:stn>square<ppp:/stn>]:1.5)` will end up as `(square:1.35)` in the negative prompt (weight=1.5*0.9)
* However `(red<ppp:stn>[square]<ppp:/stn>:1.5)` will end up as `([square]:1.5)` in the negative prompt. The content of the negative tag is copied as is, and not joined with the surrounding modifier.
* `(red[<ppp:stn>square<ppp:/stn>]:1.5)` will end up as `(square:1.35)` in the negative prompt (weight=1.5*0.9) if the merge attention option is enabled or `([square]:1.5)` otherwise.
* However `(red<ppp:stn>[square]<ppp:/stn>:1.5)` will end up as `([square]:1.5)` in the negative prompt. The content of the negative tag is copied as is, and is not merged with the surrounding modifier because the insertions happen after the attention merging.
#### Prompt editing constructs (alternation and scheduling)
@@ -332,7 +358,6 @@ This should still work as intended, and the only negative point i see is the unn
* **Separator used when adding to the negative prompt**: you can specify the separator used when adding to the negative prompt (by default it's ", ").
* **Ignore repeated content**: it ignores repeated content to avoid repetitions in the negative prompt.
* **Join attention modifiers (weights) when possible**: it joins attention modifiers when possible (joins into one, multipliying their values).
### Clean up settings
@@ -344,6 +369,7 @@ This should still work as intended, and the only negative point i see is the unn
* **Clean up around ANDs**: removes consecutive ANDs and unnecessary commas and space around them.
* **Use EOL instead of Space before ANDs**: add a newline before ANDs.
* **Clean up around extra network tags**: removes spaces around them.
* **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.
### Content removal settings
+37 -30
View File
@@ -1,8 +1,10 @@
%import common (LETTER, DIGIT, INT, CNAME, SIGNED_NUMBER, NUMBER)
_WHITESPACE: /\s+/
STRING: /("(?!"").*?(?<!\\)(\\\\)*?"|'(?!'').*?(?<!\\)(\\\\)*?')/i
BOOLEAN: /true|false/i
_WHITESPACE: /\s+/
STRING: /("(?!"").*?(?<!\\)(\\\\)*?"|'(?!'').*?(?<!\\)(\\\\)*?')/i
BOOLEAN: /true|false/i
INDEX: INT | IDENTIFIER
WILDCARD_NAME: /(?:(?!__|\$\$|[('"])\S)+/
// plain text and weights
?plain: /((?!__|\bAND\b|\${)[^\\()\[\]:<>${]|\\.)+/s // exclude only the starting ones
@@ -19,33 +21,33 @@ BOOLEAN: /true|false/i
start: promptcomp | content
// prompt composition with AND
promptcomp.4: promptcomppart ([":" numpar] (/\bAND\b/ promptcomppart [":" numpar])+)+
promptcomp.4: promptcomppart ( [ ":" numpar ] ( /\bAND\b/ promptcomppart [ ":" numpar ] )+ )+
promptcomppart: content
// simple prompts
?content.2: (old_content | new_content | plain | specialchars)*
?content_choice.2: (old_content | new_content | plain_choice | specialchars_choice)*
?content_var.2: (old_content | new_content | plain_var | specialchars_var)*
?content_negtag.2: (old_content | new_content_negtag | plain | specialchars_negtag)*
?content_alternate.2: (old_content | new_content | plain_alternate | specialchars_alternate)*
?old_content.2: (emphasized | deemphasized | scheduled | alternate | extranetworktag)+
?new_content.3: (variableset | variableuse | commandstn | commandstni | commandset | commandecho | commandif | wildcard | choices)+
?new_content_negtag.3: (variableset | variableuse | commandset | commandecho | commandif | wildcard | choices)+
?content.2: ( old_content | new_content | plain | specialchars )*
?inside_content.2: ( new_content | plain | specialchars )*
?content_choice.2: ( old_content | new_content | plain_choice | specialchars_choice )*
?content_var.2: ( old_content | new_content | plain_var | specialchars_var )*
?content_negtag.2: ( old_content | new_content_negtag | plain | specialchars_negtag )*
?content_alternate.2: ( old_content | new_content | plain_alternate | specialchars_alternate )*
?old_content.2: ( attention | scheduled | alternate | extranetworktag )+
?new_content.3: ( variableset | variableuse | commandstn | commandstni | commandset | commandecho | commandif | wildcard | choices )+
?new_content_negtag.3: ( variableset | variableuse | commandset | commandecho | commandif | wildcard | choices )+
// attention modifiers
emphasized: "(" content [":" numpar] ")"
deemphasized: "[" content "]"
attention: ( "(" content [ ":" numpar ] ")" ) | ( "[" content "]" )
// prompt scheduling and alternation
alternate: "[" alternateoption ("|" alternateoption)+ "]"
alternate: "[" alternateoption ( "|" alternateoption )+ "]"
alternateoption: content_alternate
scheduled: "[" [content ":"] content ":" numpar "]"
scheduled: "[" [ content ":" ] content ":" numpar "]"
// extra network tags
extranetworktag: "<" /(?!ppp:)[^>]+/ ">"
extranetworktag: "<" /(?!ppp:)\w+:/ inside_content ">"
// command: stn (send to negative)
commandstn: "<ppp:stn" [_WHITESPACE /s|e|p\d/] _WHITESPACE? ">" content_negtag "<ppp:/stn>"
commandstn: "<ppp:stn" [ _WHITESPACE /s|e|p\d/ ] _WHITESPACE? ">" content_negtag "<ppp:/stn>"
commandstni: "<ppp:stn" _WHITESPACE /i\d/ _WHITESPACE? ">"
// command: if
@@ -57,12 +59,12 @@ ifvalue: content
// conditions
condition: conditionsimplevalue | conditionlistvalue | conditionnocomparison
conditionnocomparison: (/not/ _WHITESPACE)? IDENTIFIER
conditionsimplevalue: (/not/ _WHITESPACE)? IDENTIFIER _WHITESPACE (/not/ _WHITESPACE)? /eq|ne|gt|lt|ge|le|contains/ _WHITESPACE SIMPLEVALUE
conditionlistvalue: (/not/ _WHITESPACE)? IDENTIFIER _WHITESPACE (/not/ _WHITESPACE)? /contains|in/ _WHITESPACE listvalue
conditionnocomparison: ( /not/ _WHITESPACE )? IDENTIFIER
conditionsimplevalue: ( /not/ _WHITESPACE )? IDENTIFIER _WHITESPACE ( /not/ _WHITESPACE )? /eq|ne|gt|lt|ge|le|contains/ _WHITESPACE SIMPLEVALUE
conditionlistvalue: ( /not/ _WHITESPACE )? IDENTIFIER _WHITESPACE ( /not/ _WHITESPACE )? /contains|in/ _WHITESPACE listvalue
IDENTIFIER: CNAME
SIMPLEVALUE: STRING | INT | BOOLEAN
listvalue: "(" _WHITESPACE? SIMPLEVALUE (_WHITESPACE? "," _WHITESPACE? SIMPLEVALUE)* _WHITESPACE? ")"
SIMPLEVALUE: STRING | NUMBER | BOOLEAN
listvalue: "(" _WHITESPACE? SIMPLEVALUE ( _WHITESPACE? "," _WHITESPACE? SIMPLEVALUE )* _WHITESPACE? ")"
// command: set
commandset: "<ppp:set" _WHITESPACE IDENTIFIER [ _WHITESPACE? /evaluate/ ] [ _WHITESPACE? /add/ ] _WHITESPACE? ">" content "<ppp:/set>"
@@ -71,20 +73,23 @@ commandset: "<ppp:set" _WHITESPACE IDENTIFIER [ _WHITESPACE? /evaluate/ ] [ _WHI
commandecho: "<ppp:echo" _WHITESPACE IDENTIFIER _WHITESPACE? ">" [ content "<ppp:/echo>" ]
// variable set
variableset.2: "${" _WHITESPACE? IDENTIFIER _WHITESPACE? [/\+/] "=" [/!/] varvalue "}"
variableset.2: "${" _WHITESPACE? IDENTIFIER _WHITESPACE? [ /\+/ ] "=" [ /!/ ] varvalue "}"
// variable use
variableuse.2: "${" _WHITESPACE? IDENTIFIER _WHITESPACE? [":" varvalue] "}"
variableuse.2: "${" _WHITESPACE? IDENTIFIER _WHITESPACE? [ ":" varvalue ] "}"
varvalue: content_var
// wildcards
wildcard.2: "__" [choicesoptions_sampler | (choicesoptions _WHITESPACE? "$$")] /(?:(?!__|\$\$|\()\S)+/ [ wildcard_var ] "__"
wildcard_var: "(" _WHITESPACE? IDENTIFIER _WHITESPACE? "=" varvalue ")"
wildcard.2: "__" [ choicesoptions_sampler | ( choicesoptions _WHITESPACE? "$$" ) ] WILDCARD_NAME [ wc_filter ] [ wildcardvar ] "__"
wc_filter: /["']/ ( [ /#/ ] wc_filter_or | ( /#?\^/ WILDCARD_NAME ) ) /["']/
wc_filter_or: wc_filter_and ( _WHITESPACE? "," _WHITESPACE? wc_filter_and )*
wc_filter_and: INDEX ( _WHITESPACE? "+" _WHITESPACE? INDEX )*
wildcardvar: "(" _WHITESPACE? IDENTIFIER _WHITESPACE? "=" varvalue ")"
// choices
choices.2: "{" [choicesoptions_sampler | (choicesoptions _WHITESPACE? "$$")] choice ("|" choice)* "}"
choices.2: "{" [ choicesoptions_sampler | ( choicesoptions _WHITESPACE? "$$" ) ] choice ( "|" choice )* "}"
choicesoptions: [choicesoptions_sampler] [_WHITESPACE? choicesoptions_rep] (([_WHITESPACE? choicesoptions_from] "-" [_WHITESPACE? choicesoptions_to]) | [_WHITESPACE? choicesoptions_num] ) [_WHITESPACE? choicesoptions_sep]
choicesoptions: [ choicesoptions_sampler ] [ _WHITESPACE? choicesoptions_rep ] ( ( [ _WHITESPACE? choicesoptions_from ] "-" [ _WHITESPACE? choicesoptions_to ] ) | [ _WHITESPACE? choicesoptions_num ] ) [ _WHITESPACE? choicesoptions_sep ]
choicesoptions_sampler: /[~@]/ // ~ for random, @ for cyclical
choicesoptions_rep: /r/
choicesoptions_num: INT
@@ -92,7 +97,9 @@ choicesoptions_from: INT
choicesoptions_to: INT
choicesoptions_sep: "$$" plain
choice: [[_WHITESPACE? choiceweight] [_WHITESPACE? choiceif] _WHITESPACE? "::"] choicevalue
choice: [ [ _WHITESPACE? choicelabels ] [ _WHITESPACE? choiceweight ] [ _WHITESPACE? choiceif ] _WHITESPACE? "::" ] choicevalue
choicelabels: /["']/ IDENTIFIER ( _WHITESPACE? "," _WHITESPACE? IDENTIFIER )* /["']/
choiceweight: NUMBER
choiceif: "if" _WHITESPACE condition
choicevalue: content_choice
+223 -117
View File
@@ -1,4 +1,4 @@
import fnmatch
from functools import reduce
import logging
import math
import os
@@ -23,7 +23,7 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
"""
NAME = "Prompt Post-Processor"
VERSION = (2, 5, 0)
VERSION = (2, 6, 0)
class IFWILDCARDS_CHOICES(Enum):
ignore = "ignore"
@@ -42,7 +42,7 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
self,
logger: logging.Logger,
interrupt: Optional[Callable],
model_info: dict[str, any],
env_info: dict[str, any],
options: Optional[dict[str, any]] = None,
grammar_content: Optional[str] = None,
wildcards_obj: PPPWildcards = None,
@@ -53,7 +53,7 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
Args:
logger: The logger object.
interrupt: The interrupt function.
model_info: A dictionary with information for the loaded model.
env_info: A dictionary with information for the environment and loaded model.
options: Optional. The options dictionary for configuring PPP behavior.
grammar_content: Optional. The grammar content to be used for parsing.
wildcards_obj: Optional. The wildcards object to be used for processing wildcards.
@@ -62,7 +62,7 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
self.rng = np.random.default_rng() # gets seeded on each process prompt call
self.the_interrupt = interrupt
self.options = options
self.model_info = model_info
self.env_info = env_info
self.wildcard_obj = wildcards_obj
# General options
@@ -74,10 +74,11 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
self.wil_process_wildcards = options.get("process_wildcards", True)
self.wil_keep_choices_order = options.get("keep_choices_order", False)
self.wil_choice_separator = options.get("choice_separator", self.DEFAULT_CHOICE_SEPARATOR)
self.wil_ifwildcards = self.IFWILDCARDS_CHOICES(options.get("if_wildcards", self.IFWILDCARDS_CHOICES.ignore.value))
self.wil_ifwildcards = self.IFWILDCARDS_CHOICES(
options.get("if_wildcards", self.IFWILDCARDS_CHOICES.ignore.value)
)
# Send to negative options
self.stn_ignore_repeats = options.get("stn_ignore_repeats", True)
self.stn_join_attention = options.get("stn_join_attention", True)
self.stn_separator = options.get("stn_separator", self.DEFAULT_STN_SEPARATOR)
# Cleanup options
self.cup_extraspaces = options.get("cleanup_extra_spaces", True)
@@ -89,11 +90,12 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
self.cup_ands = options.get("cleanup_ands", True)
self.cup_ands_eol = options.get("cleanup_ands_eol", False)
self.cup_extranetworktags = options.get("cleanup_extranetwork_tags", False)
self.cup_mergeattention = options.get("cleanup_merge_attention", True)
# Remove options
self.rem_removeextranetworktags = options.get("remove_extranetwork_tags", False)
# if self.debug_level != DEBUG_LEVEL.none:
# self.logger.info(f"Detected model info: {model_info}")
# self.logger.info(f"Detected environment info: {env_info}")
# Process with lark (debug with https://www.lark-parser.org/ide/)
if grammar_content is None:
@@ -136,19 +138,24 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
def __init_sysvars(self):
self.system_variables = {}
sdchecks = {
"sd1": self.model_info.get("is_sd1", False),
"sd2": self.model_info.get("is_sd2", False),
"sdxl": self.model_info.get("is_sdxl", False),
"sd3": self.model_info.get("is_sd3", False),
"flux": self.model_info.get("is_flux", False),
"sd1": self.env_info.get("is_sd1", False),
"sd2": self.env_info.get("is_sd2", False),
"sdxl": self.env_info.get("is_sdxl", False),
"sd3": self.env_info.get("is_sd3", False),
"flux": self.env_info.get("is_flux", False),
"auraflow": self.env_info.get("is_auraflow", False),
"": True,
}
self.system_variables["_sd"] = [k for k, v in sdchecks.items() if v][0]
model_filename = self.model_info.get("model_filename", "")
self.system_variables["_model"] = [k for k, v in sdchecks.items() if v][0]
self.system_variables["_sd"] = self.system_variables["_model"] # deprecated
model_filename = self.env_info.get("model_filename", "")
is_pony = any(s in model_filename.lower() for s in self.pony_substrings)
is_ssd = self.model_info.get("is_ssd", False)
self.system_variables["_sdfullname"] = model_filename
self.system_variables["_sdname"] = os.path.basename(model_filename)
is_ssd = self.env_info.get("is_ssd", False)
self.system_variables["_sdfullname"] = model_filename # deprecated
self.system_variables["_modelfullname"] = model_filename
self.system_variables["_sdname"] = os.path.basename(model_filename) # deprecated
self.system_variables["_modelname"] = os.path.basename(model_filename)
self.system_variables["_modelclass"] = self.env_info.get("model_class", "")
self.system_variables["_is_sd1"] = sdchecks["sd1"]
self.system_variables["_is_sd2"] = sdchecks["sd2"]
self.system_variables["_is_sdxl"] = sdchecks["sdxl"]
@@ -159,6 +166,7 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
self.system_variables["_is_sd3"] = sdchecks["sd3"]
self.system_variables["_is_sd"] = sdchecks["sd1"] or sdchecks["sd2"] or sdchecks["sdxl"] or sdchecks["sd3"]
self.system_variables["_is_flux"] = sdchecks["flux"]
self.system_variables["_is_auraflow"] = sdchecks["auraflow"]
def __add_to_insertion_points(
self, negative_prompt: str, add_at_insertion_point: list[str], insertion_at: list[tuple[int, int]]
@@ -369,7 +377,10 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
foundP = len(p_processor.detectedWildcards) > 0
foundNP = len(n_processor.detectedWildcards) > 0
if foundP or foundNP:
self.logger.error("Found unprocessed wildcards!")
if self.wil_ifwildcards == self.IFWILDCARDS_CHOICES.stop:
self.logger.error("Found unprocessed wildcards!")
else:
self.logger.info("Found unprocessed wildcards.")
ppwl = ", ".join(p_processor.detectedWildcards)
npwl = ", ".join(n_processor.detectedWildcards)
if foundP:
@@ -405,6 +416,8 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
tuple: A tuple containing the processed prompt and negative prompt.
"""
try:
if seed == -1:
seed = np.random.randint(0, 2**32)
self.rng = np.random.default_rng(seed & 0xFFFFFFFF)
prompt = original_prompt
negative_prompt = original_negative_prompt
@@ -479,6 +492,7 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
self.__negtags: list[self.NegTag] = []
self.__already_processed: list[str] = []
self.__is_negative = False
self.__wildcard_filters = {}
self.add_at: dict = {"start": [], "insertion_point": [[] for x in range(10)], "end": []}
self.insertion_at: list[tuple[int, int]] = [None for x in range(10)]
self.detectedWildcards: list[str] = []
@@ -665,45 +679,39 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
Returns:
bool: The result of the if condition evaluation.
"""
get_value = lambda n: n.value # pylint: disable=unnecessary-lambda-assignment
# if hasattr(condition, "children"):
get_children = lambda n: n.children # pylint: disable=unnecessary-lambda-assignment
# else:
# get_children = lambda n: n # pylint: disable=unnecessary-lambda-assignment
# get_value = lambda n: n # pylint: disable=unnecessary-lambda-assignment
individualcondition = get_children(condition)[0]
individualcondition: lark.Tree = condition.children[0]
# we get the name of the variable and check for a preceding not
invert = False
first = get_value(get_children(individualcondition)[0])
first = individualcondition.children[0].value # it should be a Token
if first == "not":
invert = True
cond_var = get_value(get_children(individualcondition)[1])
cond_var = individualcondition.children[1].value # it should be a Token
poscomp = 2
else:
cond_var = first
poscomp = 1
if poscomp >= len(get_children(individualcondition)):
if poscomp >= len(individualcondition.children):
# no condition, just a variable
cond_comp = "truthy"
cond_value = "true"
else:
# we get the comparison (with possible not) and the value
cond_comp = get_value(get_children(individualcondition)[poscomp])
cond_comp = individualcondition.children[poscomp].value # it should be a Token
if cond_comp == "not":
invert = not invert
poscomp += 1
cond_comp = get_value(get_children(individualcondition)[poscomp])
cond_comp = individualcondition.children[poscomp].value # it should be a Token
poscomp += 1
cond_value_node = get_children(individualcondition)[poscomp]
cond_value_node = individualcondition.children[poscomp]
cond_value = (
list(get_value(v) for v in get_children(cond_value_node))
list(v.value for v in cond_value_node.children)
if isinstance(cond_value_node, (lark.Tree, list))
else cond_value_node.value if isinstance(cond_value_node, lark.Token) else cond_value_node
)
condresult = self.__eval_condition(cond_var, cond_comp, cond_value)
cond_result = self.__eval_condition(cond_var, cond_comp, cond_value)
if invert:
condresult = not condresult
return condresult
cond_result = not cond_result
return cond_result
def promptcomp(self, tree: lark.Tree):
"""
@@ -789,52 +797,63 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
t2 = time.time()
self.__debug_end("alternate", start_result, t2 - t1)
def emphasized(self, tree: lark.Tree):
def attention(self, tree: lark.Tree):
"""
Process a attention change construct in the tree and add it to the accumulated shell.
"""
start_result = self.result
t1 = time.time()
weight_str = tree.children[-1]
if weight_str is not None:
weight = float(weight_str)
if len(tree.children) == 2:
weight_str = tree.children[-1]
if weight_str is not None:
weight = float(weight_str)
else:
weight = 1.1
weight_str = "1.1"
else:
weight_str = ""
weight = 1.1
weight = 0.9
weight_str = "0.9"
if self.__ppp.debug_level == DEBUG_LEVEL.full:
self.__ppp.logger.debug(f"Shell attention with weight {weight}")
current_tree = tree.children[0]
if self.__ppp.cup_mergeattention:
while isinstance(current_tree, lark.Tree) and current_tree.data == "attention":
# we merge the weights
if len(current_tree.children) == 2:
inner_weight = current_tree.children[-1]
if inner_weight is not None:
inner_weight = float(inner_weight)
else:
inner_weight = 1.1
else:
inner_weight = 0.9
weight *= inner_weight
current_tree = current_tree.children[0]
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))
self.result += "("
self.__visit(tree.children[:-1])
if self.__ppp.cup_emptyconstructs and self.result == start_result + "(":
if weight == 0.9:
starttag = "["
self.result += starttag
self.__visit(current_tree)
endtag = "]"
elif weight == 1.1:
starttag = "("
self.result += starttag
self.__visit(current_tree)
endtag = ")"
else:
starttag = "("
self.result += starttag
self.__visit(current_tree)
endtag = f":{weight_str})"
if self.__ppp.cup_emptyconstructs and self.result == start_result + starttag:
self.result = start_result
else:
if weight_str != "":
self.result += f":{weight_str}"
self.result += ")"
self.result += endtag
self.__shell.pop()
t2 = time.time()
self.__debug_end("emphasized", start_result, t2 - t1, weight_str)
def deemphasized(self, tree: lark.Tree):
"""
Process a decrease attention construct in the tree and add it to the accumulated shell.
"""
start_result = self.result
t1 = time.time()
weight = 0.9
if self.__ppp.debug_level == DEBUG_LEVEL.full:
self.__ppp.logger.debug(f"Shell attention with weight {weight}")
self.__shell.append(self.AccumulatedShell("at", weight))
self.result += "["
self.__visit(tree.children)
if self.__ppp.cup_emptyconstructs and self.result == start_result + "[":
self.result = start_result
else:
self.result += "]"
self.__shell.pop()
t2 = time.time()
self.__debug_end("deemphasized", start_result, t2 - t1)
self.__debug_end("attention", start_result, t2 - t1, weight_str)
def commandstn(self, tree: lark.Tree):
"""
@@ -846,7 +865,7 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
if not self.__is_negative:
negtagparameters = tree.children[0]
if negtagparameters is not None:
parameters = negtagparameters.value
parameters = negtagparameters.value # should be a token
else:
parameters = ""
content = self.__visit(tree.children[1::], False, True)
@@ -870,7 +889,7 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
if self.__is_negative:
negtagparameters = tree.children[0]
if negtagparameters is not None:
parameters = negtagparameters.value
parameters = negtagparameters.value # should be a token
else:
parameters = ""
self.__negtags.append(
@@ -1012,19 +1031,27 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
t1 = time.time()
start_result = self.result
if not self.__ppp.rem_removeextranetworktags:
# keep extra network construct
self.result += self.__get_original_node_content(tree, f"<{tree.children[0]}>")
self.result += f"<{tree.children[0]}"
self.__visit(tree.children[1])
self.result += ">"
t2 = time.time()
self.__debug_end("extranetworktag", start_result, t2 - t1)
def __get_choices(self, options: lark.Tree | None, choice_values: list[lark.Tree]) -> str:
def __get_choices(
self,
options: lark.Tree | None,
choice_values: list[lark.Tree],
filter_specifier: Optional[list[list[str]]] = None,
wildcard_key: str = None,
) -> str:
"""
Select choices based on the options.
Args:
is_wildcard (bool): A flag indicating whether the choices are from a wildcard.
options (Tree): The tree object representing the options construct.
choice_values (list[Tree]): A list of choice tree 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.
@@ -1056,35 +1083,77 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
else self.__ppp.wil_choice_separator
)
if sampler != "~":
self.__ppp.logger.warning(f"Unsupported sampler '{sampler}' in wildcard/choices options!")
msg = f"wildcard '{wildcard_key}'" if wildcard_key else "choices"
self.__ppp.logger.warning(f"Unsupported sampler '{sampler}' in {msg} options!")
self.__ppp.interrupt()
return ""
if from_value < 0:
from_value = 1
elif from_value > len(choice_values):
from_value = len(choice_values)
if to_value < 1:
to_value = 1
elif (to_value > len(choice_values) and not repeating) or from_value > to_value:
to_value = len(choice_values)
num_choices = (
self.__ppp.rng.integers(from_value, to_value, endpoint=True) if from_value < to_value else from_value
)
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
for a in o:
if a.isdecimal():
if int(a) != i:
tmp_pass = False
break
elif a.lower() not in choice_labels:
tmp_pass = False
break
if tmp_pass:
passes = True
break
if passes:
filtered_choice_values.append(c)
if len(filtered_choice_values) == 0:
self.__ppp.logger.warning(
f"Wildcard filter specifier '{','.join(['+'.join(y for y in x) for x in filter_specifier])}' found no matches in choices for wildcard '{wildcard_key}'!"
)
else:
filtered_choice_values = choice_values.copy()
if len(filtered_choice_values) == 0:
num_choices = 0
else:
if from_value < 0:
from_value = 1
elif from_value > len(filtered_choice_values):
from_value = len(filtered_choice_values)
if to_value < 1:
to_value = 1
elif (to_value > len(filtered_choice_values) and not repeating) or from_value > to_value:
to_value = len(filtered_choice_values)
num_choices = (
self.__ppp.rng.integers(from_value, to_value, endpoint=True)
if from_value < to_value
else from_value
)
if num_choices < 2:
repeating = False
if self.__ppp.debug_level == DEBUG_LEVEL.full:
self.__ppp.logger.debug(
self.__ppp.formatOutput(
f"Selecting {'repeating ' if repeating else ''}{num_choices} choices and separating with '{separator}'"
f"Selecting {'repeating ' if repeating else ''}{num_choices} choice"
+ (f"s and separating with '{separator}'" if num_choices > 1 else "")
)
)
if num_choices > 0:
available_choices: list[lark.Tree] = []
weights = []
included_choices = 0
excluded_choices = 0
excluded_weights_sum = 0
for i, c in enumerate(choice_values):
for i, c in enumerate(filtered_choice_values):
c.choice_index = i # we index them to later sort the results
w = float(c.children[0].children[0]) if c.children[0] is not None else 1.0
if w > 0 and (c.children[1] is None or self.__evaluate_if(c.children[1].children[0])):
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])):
available_choices.append(c)
weights.append(w)
included_choices += 1
else:
@@ -1092,18 +1161,19 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
excluded_choices += 1
excluded_weights_sum += w
if excluded_choices > 0: # we need to redistribute the excluded weights
weights = [w + excluded_weights_sum / included_choices if w >= 0 else 0.0 for w in weights]
weights = [w + excluded_weights_sum / included_choices for w in weights if w >= 0]
weights = np.array(weights)
weights /= weights.sum() # normalize weights
selected_choices: list[lark.Tree] = list(
self.__ppp.rng.choice(choice_values, size=num_choices, p=weights, replace=repeating)
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_text = []
for i, c in enumerate(selected_choices):
t1 = time.time()
choice_content = self.__visit(c.children[2], False, True)
choice_content_obj = c.children[3]
choice_content = self.__visit(choice_content_obj, False, True)
t2 = time.time()
if self.__ppp.debug_level == DEBUG_LEVEL.full:
self.__ppp.logger.debug(
@@ -1123,43 +1193,72 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
t1 = time.time()
start_result = self.result
options = tree.children[0]
wildcard_key = tree.children[1].value
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:
if self.__ppp.debug_level == DEBUG_LEVEL.full:
self.__ppp.logger.debug(f"Processing wildcard: {wildcard_key}")
wildcard_keys = fnmatch.filter(self.__ppp.wildcard_obj.wildcards.keys(), wildcard_key)
if len(wildcard_keys) == 0:
selected_wildcards = self.__ppp.wildcard_obj.get_wildcards(wildcard_key)
if len(selected_wildcards) == 0:
self.detectedWildcards.append(wc)
self.result += wc
t2 = time.time()
self.__debug_end("wildcard", start_result, t2 - t1, wc)
return
variablename = None
if tree.children[2] is not None:
variablename = tree.children[2].children[0] # should be a token
variablevalue = self.__visit(tree.children[2].children[1], False, True)
filter_specifier = None
filter_object = tree.children[2]
if filter_object is not None:
if (
isinstance(filter_object.children[1], lark.Token)
and filter_object.children[1] is not None
and "^" in filter_object.children[1]
):
filter_specifier = self.__wildcard_filters.get(filter_object.children[2].value, None)
if self.__ppp.debug_level == DEBUG_LEVEL.full:
self.__ppp.logger.debug("Filtering choices with inherited filter")
else:
filter_specifier = [[y.value for y in x.children] for x in filter_object.children[2].children]
if self.__ppp.debug_level == DEBUG_LEVEL.full:
self.__ppp.logger.debug("Filtering choices")
self.__wildcard_filters[wildcard_key] = filter_specifier
if (
filter_object.children[1] is not None and "#" in filter_object.children[1]
): # means do not use the filter in this wildcard
if self.__ppp.debug_level == DEBUG_LEVEL.full:
self.__ppp.logger.debug("Ignoring filter")
filter_specifier = None
if (
len(selected_wildcards) > 1
and filter_specifier is not None
and any(x.isdecimal() for x in reduce(lambda x, y: x + y, filter_specifier))
):
self.__ppp.logger.warning(
f"Using a globbing wildcard '{wildcard_key}' with positional index filters is not recommended!"
)
var_object = tree.children[3]
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 = []
for key in wildcard_keys:
wildcard = self.__ppp.wildcard_obj.wildcards.get(key, None)
for wildcard in selected_wildcards:
if wildcard is None:
self.detectedWildcards.append(wc)
self.result += wc
t2 = time.time()
self.__debug_end("wildcard", start_result, t2 - t1, wc)
return
choice_values_obj = wildcard.get("choices_obj", None)
options_obj = wildcard.get("options_obj", None)
choice_values_obj = wildcard.choices_obj
options_obj = wildcard.options_obj
if choice_values_obj is None:
t1 = time.time()
choice_values_obj = []
choices = wildcard["choices"]
try:
options_obj = self.__ppp.parse_prompt(
"as choices options", choices[0], self.__ppp.parser_choicesoptions, True
"as choices options", wildcard.choices[0], self.__ppp.parser_choicesoptions, True
)
n = 1
except lark.exceptions.UnexpectedInput:
@@ -1167,28 +1266,32 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
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 choices[n:]:
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 '{key}'! : {e.__class__.__name__}"
f"Error parsing choice '{cv}' in wildcard '{wildcard.key}'! : {e.__class__.__name__}"
)
wildcard["choices_obj"] = choice_values_obj
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 '{key}' ({t2-t1:.3f} seconds)")
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
else:
if self.__ppp.debug_level == DEBUG_LEVEL.full:
self.__ppp.logger.debug(f"Options for wildcard '{key}' are ignored!")
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)
self.result += self.__get_choices(options, choice_values_obj_all, filter_specifier, wildcard_key)
if wildcard_key in self.__wildcard_filters:
del self.__wildcard_filters[wildcard_key]
if variablename is not None:
self.__remove_user_variable(variablename)
if variablebackup is not None:
@@ -1231,14 +1334,16 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
self.__visit(tree.children)
# process the found negative tags
for negtag in self.__negtags:
if self.__ppp.stn_join_attention:
if self.__ppp.cup_mergeattention:
# join consecutive attention elements
for i in range(len(negtag.shell) - 1, 0, -1):
if negtag.shell[i].type == "at" and negtag.shell[i - 1].type == "at":
new_weight = ( # we limit the new weight to two decimals
math.floor(100 * negtag.shell[i - 1].data * negtag.shell[i].data) / 100
)
negtag.shell[i - 1] = self.AccumulatedShell(
"at",
math.floor(100 * negtag.shell[i - 1].data * negtag.shell[i].data)
/ 100, # we limit the new weight to two decimals
new_weight,
)
negtag.shell.pop(i)
start = ""
@@ -1254,7 +1359,8 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
end = ")" + end
else:
start += "("
end = f":{s.data})" + end
weight_str = f"{s.data:.2f}".rstrip("0").rstrip(".")
end = f":{weight_str})" + end
# case "sc":
case "scb":
start += "["
+28 -23
View File
@@ -45,18 +45,12 @@ class PromptPostProcessorComfyUINode:
"forceInput": True,
},
),
"seed": (
"INT",
{
"default": None,
"forceInput": False,
},
),
"pos_prompt": (
"STRING",
{
"multiline": True,
"default": "",
"dynamicPrompts": False,
"forceInput": True,
},
),
@@ -65,11 +59,19 @@ class PromptPostProcessorComfyUINode:
{
"multiline": True,
"default": "",
"dynamicPrompts": False,
"forceInput": True,
},
),
},
"optional": {
"seed": (
"INT",
{
"default": -1,
"forceInput": False,
},
),
"debug_level": (
[e.value for e in DEBUG_LEVEL],
{
@@ -140,15 +142,6 @@ class PromptPostProcessorComfyUINode:
"label_off": "No",
},
),
"stn_join_attention": (
"BOOLEAN",
{
"default": True,
"tooltip": "Merge attention in the content added to the negative prompt",
"label_on": "Yes",
"label_off": "No",
},
),
"cleanup_extra_spaces": (
"BOOLEAN",
{
@@ -230,6 +223,15 @@ class PromptPostProcessorComfyUINode:
"label_off": "No",
},
),
"cleanup_merge_attention": (
"BOOLEAN",
{
"default": True,
"tooltip": "Merge nested attention constructs",
"label_on": "Yes",
"label_off": "No",
},
),
"remove_extranetwork_tags": (
"BOOLEAN",
{
@@ -272,7 +274,6 @@ class PromptPostProcessorComfyUINode:
wc_keep_choices_order,
stn_separator,
stn_ignore_repeats,
stn_join_attention,
cleanup_extra_spaces,
cleanup_empty_constructs,
cleanup_extra_separators,
@@ -282,6 +283,7 @@ class PromptPostProcessorComfyUINode:
cleanup_ands,
cleanup_ands_eol,
cleanup_extranetwork_tags,
cleanup_merge_attention,
remove_extranetwork_tags,
):
new_run = {
@@ -298,7 +300,6 @@ class PromptPostProcessorComfyUINode:
"keep_choices_order": wc_keep_choices_order,
"stn_separator": stn_separator,
"stn_ignore_repeats": stn_ignore_repeats,
"stn_join_attention": stn_join_attention,
"cleanup_extra_spaces": cleanup_extra_spaces,
"cleanup_empty_constructs": cleanup_empty_constructs,
"cleanup_extra_separators": cleanup_extra_separators,
@@ -308,6 +309,7 @@ class PromptPostProcessorComfyUINode:
"cleanup_ands": cleanup_ands,
"cleanup_ands_eol": cleanup_ands_eol,
"cleanup_extranetwork_tags": cleanup_extranetwork_tags,
"cleanup_merge_attention": cleanup_merge_attention,
"remove_extranetwork_tags": remove_extranetwork_tags,
}
return new_run.__hash__
@@ -329,7 +331,6 @@ class PromptPostProcessorComfyUINode:
wc_keep_choices_order,
stn_separator,
stn_ignore_repeats,
stn_join_attention,
cleanup_extra_spaces,
cleanup_empty_constructs,
cleanup_extra_separators,
@@ -339,11 +340,14 @@ class PromptPostProcessorComfyUINode:
cleanup_ands,
cleanup_ands_eol,
cleanup_extranetwork_tags,
cleanup_merge_attention,
remove_extranetwork_tags,
):
model_info = {
env_info = {
"app": "comfyui",
"models_path": folder_paths.models_dir,
"model_filename": modelname, # path is relative to checkpoints folder
"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__
@@ -358,6 +362,7 @@ class PromptPostProcessorComfyUINode:
"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"),
}
# SVD_img2vid, SVD3D_u, SVD3_p, Stable_Zero123, SD_X4Upscaler,
# Stable_Cascade_C, Stable_Cascade_B, StableAudio
@@ -380,7 +385,6 @@ class PromptPostProcessorComfyUINode:
"keep_choices_order": wc_keep_choices_order,
"stn_separator": stn_separator,
"stn_ignore_repeats": stn_ignore_repeats,
"stn_join_attention": stn_join_attention,
"cleanup_extra_spaces": cleanup_extra_spaces,
"cleanup_empty_constructs": cleanup_empty_constructs,
"cleanup_extra_separators": cleanup_extra_separators,
@@ -390,11 +394,12 @@ class PromptPostProcessorComfyUINode:
"cleanup_ands": cleanup_ands,
"cleanup_ands_eol": cleanup_ands_eol,
"cleanup_extranetwork_tags": cleanup_extranetwork_tags,
"cleanup_merge_attention": cleanup_merge_attention,
"remove_extranetwork_tags": remove_extranetwork_tags,
}
self.wildcards_obj.refresh_wildcards(debug_level, wildcards_folders if options["process_wildcards"] else None)
ppp = PromptPostProcessor(
self.logger, self.interrupt, model_info, options, self.grammar_content, self.wildcards_obj
self.logger, self.interrupt, env_info, options, self.grammar_content, self.wildcards_obj
)
pos_prompt, neg_prompt = ppp.process_prompt(pos_prompt, neg_prompt, seed if seed is not None else 1)
return (
+32 -16
View File
@@ -1,3 +1,4 @@
import fnmatch
import os
import json
from typing import Optional
@@ -6,6 +7,16 @@ import yaml
from ppp_logging import DEBUG_LEVEL
class PPPWildcard:
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
class PPPWildcards:
DEFAULT_WILDCARDS_FOLDER = "wildcards"
@@ -14,7 +25,7 @@ class PPPWildcards:
self.logger = logger
self.debug_level = DEBUG_LEVEL.none
self.wildcards_folders = []
self.wildcards = {}
self.wildcards: dict[str, PPPWildcard] = {}
self.wildcard_files = {}
def refresh_wildcards(self, debug_level: DEBUG_LEVEL, wildcards_folders: Optional[list[str]]):
@@ -27,7 +38,7 @@ class PPPWildcards:
# if self.debug_level != DEBUG_LEVEL.none:
# self.logger.info("Initializing wildcards...")
# t1 = time.time()
for fullpath in 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
@@ -43,6 +54,10 @@ class PPPWildcards:
self.wildcards = {}
self.wildcard_files = {}
def get_wildcards(self, key: str) -> list[PPPWildcard]:
keys = sorted(fnmatch.filter(self.wildcards.keys(), key))
return [self.wildcards[k] for k in keys]
def __get_keys_in_dict(self, dictionary: dict, prefix="") -> list[str]:
"""
Get all keys in a dictionary.
@@ -81,20 +96,21 @@ class PPPWildcards:
return None
return current_dict
def __remove_wildcards_from_file(self, full_path: str):
def __remove_wildcards_from_file(self, full_path: str, debug=True):
"""
Clear all wildcards in a file.
Args:
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 last_modified_cached is not None and self.debug_level != DEBUG_LEVEL.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:
if self.wildcards[key].file == full_path:
del self.wildcards[key]
def __get_wildcards_in_file(self, base, full_path: str):
@@ -113,7 +129,7 @@ class PPPWildcards:
name, extension = os.path.splitext(filename)
if extension not in (".txt", ".json", ".yaml", ".yml"):
return
self.__remove_wildcards_from_file(full_path)
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)
@@ -122,12 +138,12 @@ class PPPWildcards:
elif relfolders != "":
relfolders += "/"
if extension == ".txt":
self.__get_wildcards_in_text_file(full_path, name, relfolders)
self.__get_wildcards_in_text_file(full_path, relfolders, name)
elif extension in (".json", ".yaml", ".yml"):
self.__get_wildcards_in_structured_file(full_path, extension, relfolders)
self.__get_wildcards_in_structured_file(full_path, relfolders, extension)
self.wildcard_files[full_path] = last_modified
def __get_wildcards_in_structured_file(self, full_path, extension, relfolders):
def __get_wildcards_in_structured_file(self, full_path, relfolders, extension):
with open(full_path, "r", encoding="utf-8") as file:
if extension == ".json":
content = json.loads(file.read())
@@ -136,9 +152,9 @@ class PPPWildcards:
keys = self.__get_keys_in_dict(content)
for key in keys:
fullkey = f"{relfolders}{key}"
if self.wildcards.get(fullkey) is not None:
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']}'!"
f"Duplicate wildcard '{fullkey}' in file '{full_path}' and '{self.wildcards[fullkey].file}'!"
)
else:
obj = self.__get_nested(content, key)
@@ -170,23 +186,23 @@ class PPPWildcards:
if obj is None:
self.logger.warning(f"Invalid wildcard '{fullkey}' in file '{full_path}'!")
else:
self.wildcards[fullkey] = {"file": full_path, "choices": choices}
self.wildcards[fullkey] = PPPWildcard(full_path, fullkey, choices)
def __get_wildcards_in_text_file(self, full_path, name, relfolders):
def __get_wildcards_in_text_file(self, full_path, relfolders, name):
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) is not None:
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']}'!"
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] = {"file": full_path, "choices": text_content}
self.wildcards[fullkey] = PPPWildcard(full_path, fullkey, text_content)
def __get_wildcards_in_directory(self, base: str, directory: str):
"""
+46 -37
View File
@@ -101,44 +101,53 @@ class PromptPostProcessorA1111Script(scripts.Script):
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 = {
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"
)
env_info = {
"app": app,
"models_path": models_path,
"model_filename": getattr(p.sd_model.sd_checkpoint_info, "filename", ""), # path is absolute
"model_filename": getattr(p.sd_model.sd_checkpoint_info, "filename", ""),
"model_class": "",
"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
"is_auraflow": False, # AuraFlow
}
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
# cannot differentiate 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
env_info["model_class"] = p.sd_model.__class__.__name__
env_info["is_sd1"] = p.sd_model.__class__.__name__ in ("LatentDiffusion", "StableDiffusionPipeline")
env_info["is_sd2"] = p.sd_model.__class__.__name__ in ("LatentDiffusion", "StableDiffusionPipeline")
env_info["is_sdxl"] = p.sd_model.__class__.__name__ == "StableDiffusionXLPipeline"
env_info["is_ssd"] = False # ?
env_info["is_sd3"] = p.sd_model.__class__.__name__ == "StableDiffusion3Pipeline"
env_info["is_flux"] = p.sd_model.__class__.__name__ == "FluxPipeline"
env_info["is_auraflow"] = p.sd_model.__class__.__name__ == "AuraFlowPipeline"
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"
env_info["model_class"] = p.sd_model.model_config.__class__.__name__
env_info["is_sd1"] = getattr(p.sd_model, "is_sd1", False)
env_info["is_sd2"] = getattr(p.sd_model, "is_sd2", False)
env_info["is_sdxl"] = getattr(p.sd_model, "is_sdxl", False)
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"
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
env_info["model_class"] = p.sd_model.__class__.__name__
env_info["is_sd1"] = getattr(p.sd_model, "is_sd1", False)
env_info["is_sd2"] = getattr(p.sd_model, "is_sd2", False)
env_info["is_sdxl"] = getattr(p.sd_model, "is_sdxl", False)
env_info["is_ssd"] = getattr(p.sd_model, "is_ssd", False)
env_info["is_sd3"] = getattr(p.sd_model, "is_sd3", False)
env_info["is_flux"] = False
env_info["is_auraflow"] = False
wc_wildcards_folders = getattr(opts, "ppp_wil_wildcardsfolders", "")
if wc_wildcards_folders == "":
wc_wildcards_folders = os.getenv("WILDCARD_DIR", PPPWildcards.DEFAULT_WILDCARDS_FOLDER)
@@ -150,13 +159,12 @@ class PromptPostProcessorA1111Script(scripts.Script):
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),
"process_wildcards": getattr(opts, "ppp_wil_processwildcards", 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),
@@ -166,13 +174,14 @@ class PromptPostProcessorA1111Script(scripts.Script):
"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),
"cleanup_merge_attention": getattr(opts, "ppp_cup_mergeattention", True),
"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
self.ppp_logger, self.ppp_interrupt, env_info, options, self.grammar_content, self.wildcards_obj
)
prompts_list = []
@@ -418,14 +427,6 @@ def on_ui_settings():
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",
@@ -503,6 +504,14 @@ def on_ui_settings():
section=section,
),
)
shared.opts.add_option(
key="ppp_cup_mergeattention",
info=shared.OptionInfo(
True,
label="Merge attention modifiers (weights) when possible",
section=section,
),
)
# Remove old settings
# for name in ["ppp_gen_ifwildcards", "ppp_ifwildcards", "ppp_gen_debug", "ppp_stn_doi2i", "ppp_cup_doi2i"]:
+146 -26
View File
@@ -36,7 +36,6 @@ class TestPromptPostProcessor(unittest.TestCase):
"keep_choices_order": False,
"stn_separator": ", ",
"stn_ignore_repeats": True,
"stn_join_attention": True,
"cleanup_empty_constructs": True,
"cleanup_extra_separators": True,
"cleanup_extra_separators2": True,
@@ -46,15 +45,19 @@ class TestPromptPostProcessor(unittest.TestCase):
"cleanup_ands": True,
"cleanup_ands_eol": False,
"cleanup_extranetwork_tags": True,
"cleanup_merge_attention": True,
"remove_extranetwork_tags": False,
}
self.__def_model_info = {
self.__def_env_info = {
"app": "tests",
"is_sd1": False,
"is_sd2": False,
"is_sdxl": True,
"is_ssd": False,
"is_sd3": False,
"is_flux": False,
"is_auraflow": False,
"model_class": "DiffusionEngine",
"models_path": "./webui/models",
"model_filename": "./webui/models/Stable-diffusion/testmodel.safetensors",
}
@@ -73,7 +76,7 @@ class TestPromptPostProcessor(unittest.TestCase):
self.__defppp = PromptPostProcessor(
self.__ppp_logger,
self.__interrupt,
self.__def_model_info,
self.__def_env_info,
self.__defopts,
self.__grammar_content,
self.__wildcards_obj,
@@ -81,7 +84,7 @@ class TestPromptPostProcessor(unittest.TestCase):
self.__nocupppp = PromptPostProcessor(
self.__ppp_logger,
self.__interrupt,
self.__def_model_info,
self.__def_env_info,
{
**self.__defopts,
"cleanup_empty_constructs": False,
@@ -93,6 +96,7 @@ class TestPromptPostProcessor(unittest.TestCase):
"cleanup_ands": False,
"cleanup_ands_eol": False,
"cleanup_extranetwork_tags": False,
"cleanup_merge_attention": False,
},
self.__grammar_content,
self.__wildcards_obj,
@@ -167,7 +171,7 @@ class TestPromptPostProcessor(unittest.TestCase):
),
PromptPair(
" (()), flowers , , ",
"red, (pink:1.21), normal quality, mauve, yellow, bad quality, green, worse quality, purple, blue",
"red, ((pink)), normal quality, mauve, yellow, bad quality, green, worse quality, purple, blue",
),
ppp=self.__nocupppp,
)
@@ -179,7 +183,7 @@ class TestPromptPostProcessor(unittest.TestCase):
"normal quality",
),
PromptPair(
"this is a ((test) (test:2.0):1.5) (red:1.5)", "[neg1], ([square]:1.5), normal quality, (neg2:1.65)"
"this is a ((test) (test:2):1.5) (red:1.5)", "[neg1], ([square]:1.5), normal quality, (neg2:1.65)"
),
)
@@ -190,7 +194,7 @@ class TestPromptPostProcessor(unittest.TestCase):
"normal quality",
),
PromptPair(
"this is a (([complex|simple|regular] test)(test:2.0):1.5)",
"this is a (([complex|simple|regular] test)(test:2):1.5)",
"([neg1||]:1.65), ([|neg2|]:1.65), ([||neg3]:1.65), normal quality",
),
)
@@ -202,7 +206,7 @@ class TestPromptPostProcessor(unittest.TestCase):
"normal quality",
),
PromptPair(
"this is a (([complex[one|two||three|four]|simple|regular] test)(test:2.0):1.5)",
"this is a (([complex[one|two||three|four]|simple|regular] test)(test:2):1.5)",
"([neg1||]:1.65), ([[|neg12|||]||]:1.65), ([[||||(neg14)]||]:1.65), ([|neg2|]:1.65), ([||neg3]:1.65), normal quality",
),
)
@@ -220,7 +224,7 @@ class TestPromptPostProcessor(unittest.TestCase):
"normal quality, <ppp:stn i0>",
),
PromptPair(
"this \\(is\\): a (([complex|simple|regular] test)(test:2.0):1.5)\nBREAK with [abc:def:5]:0.5 AND loratrigger <lora:xxx:1> AND hypernettrigger <hypernet:yyy>:0.3",
"this \\(is\\): a (([complex|simple|regular] test)(test:2):1.5)\nBREAK with [abc:def:5]:0.5 AND loratrigger <lora:xxx:1> AND hypernettrigger <hypernet:yyy>:0.3",
"[neg5], ([|neg6|]:1.65), (neg1:1.65), [neg4::5], normal quality, [neg2(neg3:1.6):5]",
),
)
@@ -232,17 +236,30 @@ class TestPromptPostProcessor(unittest.TestCase):
"normal quality, <ppp:stn i0>",
),
PromptPair(
"this \\(is\\): a (([complex|simple|regular] test)(test:2.0):1.5)\nBREAK with [abc:def:5]:0.5 AND loratrigger <lora:xxx:1> AND hypernettrigger <hypernet:yyy>:0.3",
"this \\(is\\): a (([complex|simple|regular] test)(test:2):1.5)\nBREAK with [abc:def:5]:0.5 AND loratrigger <lora:xxx:1> AND hypernettrigger <hypernet:yyy>:0.3",
"[neg5], ([|neg6|]:1.65), (neg1:1.65), [neg4::5], normal quality, [neg2(neg3:1.6):5]",
),
)
def test_stn_inside_alternation_recursive_2(self): # negtag inside alternation (recursive alternation)
self.__process(
PromptPair(
"[pos1<ppp:stn>neg1<ppp:/stn>[pos11|pos12<ppp:stn>neg12<ppp:/stn>||pos14|pos15<ppp:stn>neg15<ppp:/stn>]|pos2<ppp:stn>neg2<ppp:/stn>|pos3<ppp:stn>neg3<ppp:/stn>]",
"",
),
PromptPair(
"[pos1[pos11|pos12||pos14|pos15]|pos2|pos3]",
"[neg1||], [[|neg12|||]||], [[||||neg15]||], [|neg2|], [||neg3]",
# "[neg1[|neg12|||neg15]|neg2|neg3]", # expected output if the constructs were unified
),
)
# Cleanup tests
def test_cl_simple(self): # simple cleanup
self.__process(
PromptPair(" this is a ((test ), , , (), , [] ( , test ,:2.0):1.5) (red:1.5) ", " normal quality "),
PromptPair("this is a ((test), (test,:2.0):1.5) (red:1.5)", "normal quality"),
PromptPair("this is a ((test), (test,:2):1.5) (red:1.5)", "normal quality"),
)
def test_cl_complex(self): # complex cleanup
@@ -252,32 +269,48 @@ class TestPromptPostProcessor(unittest.TestCase):
" [:hands, feet, :0.15]normal quality ",
),
PromptPair(
"this is BREAKABLE a ((test)) AND(<lora:test> ANDERSON (test:2.0):1.5) :o BREAK (red:1.5)",
"this is BREAKABLE a (test:1.21) AND(<lora:test> ANDERSON (test:2):1.5) :o BREAK (red:1.5)",
"[:hands, feet, :0.15]normal quality",
),
)
def test_cl_removenetworktags(self): # remove network tags
self.__process(
PromptPair("this is a <lora:test> test", ""),
PromptPair("this is a <lora:test:1> test__yaml/wildcard7__", ""),
PromptPair("this is a test", ""),
ppp=PromptPostProcessor(
self.__ppp_logger,
self.__interrupt,
self.__def_model_info,
self.__def_env_info,
{**self.__defopts, "remove_extranetwork_tags": True},
self.__grammar_content,
self.__wildcards_obj,
),
)
def test_cl_dontremoveseparatorsoneol(self): # dont remove separators on eol
def test_cl_dontremoveseparatorsoneol(self): # don't remove separators on eol
self.__process(
PromptPair("this is a test,\nsecond line", ""),
PromptPair("this is a test,\nsecond line", ""),
ppp=PromptPostProcessor(
self.__ppp_logger,
self.__interrupt,
self.__def_model_info,
self.__def_env_info,
{**self.__defopts, "cleanup_extra_separators2": False},
self.__grammar_content,
self.__wildcards_obj,
),
)
def test_cl_mergeattention(self): # merge attention
self.__process(
PromptPair(
"this is (a test:1.5) of (attention (merging:1.2)) where ((this)) ((is joined:1.2)) and ([this too]:1.3)",
"",
),
PromptPair(
"this is (a test:1.5) of (attention (merging:1.2)) where (this:1.21) (is joined:1.32) and (this too:1.17)",
"",
),
)
@@ -290,7 +323,7 @@ class TestPromptPostProcessor(unittest.TestCase):
"normal quality, <ppp:stn i0>",
),
PromptPair(
"this \\(is\\): a (([complex|simple|regular] test)(test:2.0):1.5)\nBREAK with [abc:def:5]:0.5 AND loratrigger <lora:xxx:1> AND hypernettrigger <hypernet:yyy>:0.3",
"this \\(is\\): a (([complex|simple|regular] test)(test:2):1.5)\nBREAK with [abc:def:5]:0.5 AND loratrigger <lora:xxx:1> AND hypernettrigger <hypernet:yyy>:0.3",
"[neg5], ([|neg6|]:1.65), (neg1:1.65), [neg4::5], normal quality, [neg2(neg3:1.6):5]",
),
)
@@ -302,7 +335,7 @@ class TestPromptPostProcessor(unittest.TestCase):
"normal quality",
),
PromptPair(
"this \\(is\\): a (([complex|simple|regular] test)(test:2.0):1.5)\nBREAK :0.5 AND hypernettrigger <hypernet:yyy>:0.3",
"this \\(is\\): a (([complex|simple|regular] test)(test:2):1.5)\nBREAK :0.5 AND hypernettrigger <hypernet:yyy>:0.3",
"normal quality",
),
)
@@ -317,10 +350,12 @@ class TestPromptPostProcessor(unittest.TestCase):
self.__ppp_logger,
self.__interrupt,
{
**self.__def_model_info,
**self.__def_env_info,
"model_filename": "./webui/models/Stable-diffusion/ponymodel.safetensors",
},
self.__defopts,
self.__grammar_content,
self.__wildcards_obj,
),
)
@@ -453,6 +488,27 @@ class TestPromptPostProcessor(unittest.TestCase):
ppp=self.__nocupppp,
)
def test_ch_choicesinsidelora(self): # simple choices inside a lora
self.__process(
PromptPair("<lora:test1:1><lora:test2:{0.2|0.5|0.7|1}>", ""),
PromptPair("<lora:test1:1><lora:test2:0.7>", ""),
ppp=self.__nocupppp,
)
def test_ch_removelorawithchoices(self): # remove lora with choices inside
self.__process(
PromptPair("<lora:test1:1><lora:test2:{0.2|0.5|0.7|1}>", ""),
PromptPair("", ""),
ppp=PromptPostProcessor(
self.__ppp_logger,
self.__interrupt,
self.__def_env_info,
{**self.__defopts, "remove_extranetwork_tags": True},
self.__grammar_content,
self.__wildcards_obj,
),
)
# Wildcards tests
def test_wc_ignore(self): # wildcards with ignore option
@@ -462,12 +518,14 @@ class TestPromptPostProcessor(unittest.TestCase):
ppp=PromptPostProcessor(
self.__ppp_logger,
self.__interrupt,
self.__def_model_info,
self.__def_env_info,
{
**self.__defopts,
"process_wildcards": False,
"if_wildcards": PromptPostProcessor.IFWILDCARDS_CHOICES.ignore.value,
},
self.__grammar_content,
self.__wildcards_obj,
),
)
@@ -478,18 +536,20 @@ class TestPromptPostProcessor(unittest.TestCase):
"normal quality, <ppp:stn i0> {option1|option2}",
),
PromptPair(
"this is: a (([complex|simple|regular] test)(test:2.0):1.5)\nBREAK with [abc:def:5]<lora:xxx:1>",
"this is: a (([complex|simple|regular] test)(test:2):1.5)\nBREAK with [abc:def:5]<lora:xxx:1>",
"[neg5], ([|neg6|]:1.65), (neg1:1.65), [neg4::5], normal quality, [neg2(neg3:1.6):5]",
),
ppp=PromptPostProcessor(
self.__ppp_logger,
self.__interrupt,
self.__def_model_info,
self.__def_env_info,
{
**self.__defopts,
"process_wildcards": False,
"if_wildcards": PromptPostProcessor.IFWILDCARDS_CHOICES.remove.value,
},
self.__grammar_content,
self.__wildcards_obj,
),
)
@@ -500,12 +560,14 @@ class TestPromptPostProcessor(unittest.TestCase):
ppp=PromptPostProcessor(
self.__ppp_logger,
self.__interrupt,
self.__def_model_info,
self.__def_env_info,
{
**self.__defopts,
"process_wildcards": False,
"if_wildcards": PromptPostProcessor.IFWILDCARDS_CHOICES.warn.value,
},
self.__grammar_content,
self.__wildcards_obj,
),
)
@@ -519,12 +581,14 @@ class TestPromptPostProcessor(unittest.TestCase):
ppp=PromptPostProcessor(
self.__ppp_logger,
self.__interrupt,
self.__def_model_info,
self.__def_env_info,
{
**self.__defopts,
"process_wildcards": False,
"if_wildcards": PromptPostProcessor.IFWILDCARDS_CHOICES.stop.value,
},
self.__grammar_content,
self.__wildcards_obj,
),
interrupted=True,
)
@@ -592,6 +656,62 @@ class TestPromptPostProcessor(unittest.TestCase):
ppp=self.__nocupppp,
)
def test_wc_wildcard_filter_index(self): # wildcard with positional index filter
self.__process(
PromptPair("the choice is: __yaml/wildcard2'2'__", ""),
PromptPair("the choice is: choice3-choice3", ""),
ppp=self.__nocupppp,
)
def test_wc_wildcard_filter_label(self): # wildcard with label filter
self.__process(
PromptPair("the choice is: __yaml/wildcard2'label1'__", ""),
PromptPair("the choice is: choice3-choice1", ""),
ppp=self.__nocupppp,
)
def test_wc_wildcard_filter_label2(self): # wildcard with label filter in multiple choices
self.__process(
PromptPair("the choice is: __yaml/wildcard2'label2'__", ""),
PromptPair("the choice is: choice1-choice1", ""),
ppp=self.__nocupppp,
)
def test_wc_wildcard_filter_label3(self): # wildcard with multiple label filter
self.__process(
PromptPair("the choice is: __yaml/wildcard2'label1,label2'__", ""),
PromptPair("the choice is: choice3-choice1", ""),
ppp=self.__nocupppp,
)
def test_wc_wildcard_filter_indexlabel(self): # wildcard with mixed index and label filter
self.__process(
PromptPair("the choice is: __yaml/wildcard2'2,label2'__", ""),
PromptPair("the choice is: choice3-choice1", ""),
ppp=self.__nocupppp,
)
def test_wc_wildcard_filter_compound(self): # wildcard with compound filter
self.__process(
PromptPair("the choice is: __yaml/wildcard2'label1+label3'__", ""),
PromptPair("the choice is: choice3-choice3", ""),
ppp=self.__nocupppp,
)
def test_wc_wildcard_filter_compound2(self): # wildcard with inherited compound filter
self.__process(
PromptPair("the choice is: __yaml/wildcard2bis'#label1+label3'__", ""),
PromptPair("the choice is: choice3bis", ""),
ppp=self.__nocupppp,
)
def test_wc_wildcard_filter_compound3(self): # wildcard with doubly inherited compound filter
self.__process(
PromptPair("the choice is: __yaml/wildcard2bisbis'#label1+label3'__", ""),
PromptPair("the choice is: choice3bisbis", ""),
ppp=self.__nocupppp,
)
def test_wc_nested_wildcard_text(self): # nested text wildcard with repeating multiple choices
self.__process(
PromptPair("the choices are: __r3$$-$$text/wildcard3__", ""),
@@ -648,8 +768,8 @@ class TestPromptPostProcessor(unittest.TestCase):
def test_wc_wildcard_globbing(self): # wildcard with globbing
self.__process(
PromptPair("the choices are: __yaml/wildcard[12]__, __yaml/wildcard*__", ""),
PromptPair("the choices are: choice3-choice2, choice3-choice1- choice2 ", ""),
PromptPair("the choices are: __yaml/wildcard[12]__, __yaml/wildcard?__", ""),
PromptPair("the choices are: choice3-choice2, <lora:test2:1>- choice2 -choice3", ""),
ppp=self.__nocupppp,
)
+13 -2
View File
@@ -6,11 +6,17 @@ yaml:
wildcard2:
- ~r2-3$$-
- 4::choice1
- "'label1,label2'4::choice1"
- "3:: choice2 "
- 2::choice3
- "'label1,label3'2::choice3"
- 5 if _is_sd1::choice4
wildcard2bis:
- __1$$yaml/wildcard2'^yaml/wildcard2bis'__bis
wildcard2bisbis:
- __1$$yaml/wildcard2bis'#^yaml/wildcard2bisbis'__bis
wildcard3:
- __2$$,$$yaml/wildcard2__
@@ -23,3 +29,8 @@ yaml:
- { weight: 3, content: choice2 }
- { text: choice3 }
- { weight: 4, if: "_is_ssd", text: choice4 }
wildcard7:
- <lora:test1:1>
- <lora:test2:1>
- <lora:test3:1>