Compare commits

...
1 Commits
Author SHA1 Message Date
Antonio Cordero Balcazar 8a960df0aa * Add support for wildcards.
* Better debug log.
* Set commands support lazy evaluation and content addition.
* Unified in one tree and processing phase.
* Supports Flux.
* Fix evaluation of string variable as truthy.
* Show invalid wildcards.
* Better detection of installed UI.
* Improved cache of results.
* Works as a ComfyUI node.
* Debug setting changed to debug level.
* Fixes in variable use.
* Added timing reporting.
* Detection of more model types.
* Installation script for requirements.
* Extension metadata for A1111.
* Lark grammar in separate file and cached.
* Improved conditions for if command.
* Avoid repeats in the processing of prompts.
* Fix multiple loading of settings.
2024-08-31 14:02:46 +02:00
20 changed files with 3137 additions and 1660 deletions
+1 -1
View File
@@ -6,7 +6,7 @@
"configurations": [ "configurations": [
{ {
"name": "Tests", "name": "Tests",
"type": "python", "type": "debugpy",
"request": "launch", "request": "launch",
"program": "tests/tests.py", "program": "tests/tests.py",
"console": "integratedTerminal", "console": "integratedTerminal",
+1 -1
View File
@@ -9,7 +9,7 @@
], ],
"python.testing.pytestEnabled": false, "python.testing.pytestEnabled": false,
"python.testing.unittestEnabled": true, "python.testing.unittestEnabled": true,
"python.analysis.typeCheckingMode": "basic", "python.analysis.typeCheckingMode": "off",
"black-formatter.args": [ "black-formatter.args": [
"--line-length=120" "--line-length=120"
] ]
+179 -56
View File
@@ -1,57 +1,60 @@
# Prompt Postprocessor for Stable Diffusion WebUI # Prompt Postprocessor for Stable Diffusion WebUI and ComfyUI
The Prompt Postprocessor for Stable Diffusion WebUI, formerly known as "sd-webui-sendtonegative", is an extension designed to process the prompt after other extensions have potentially modified it. This extension is compatible with the [AUTOMATIC1111 Stable Diffusion WebUI](https://github.com/AUTOMATIC1111/stable-diffusion-webui) and [SD.Next](https://github.com/vladmandic/automatic). 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:
* [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)
* ...and probably other forks
* [ComfyUI](https://github.com/comfyanonymous/ComfyUI)
Currently this extension has these functions: Currently this extension has these functions:
* Allows marking parts of the prompt and moves them to the negative prompt. This allows for useful tricks when using a wildcard extension since you can add negative content from choices made in the positive prompt. * Sending parts of the prompt to the negative prompt. This allows for useful tricks when using wildcards since you can add negative content from choices made in the positive prompt.
* Set values to local variables. * Set and modify local variables.
* Filter content based on the loaded SD model version or a set variable. * Filter content based on the loaded SD model or a variable.
* Detect invalid wildcards and act on them. * Process wildcards. Compatible with Dynamic Prompts formats. Can also detect invalid wildcards and act as you choose.
* Clean up the prompt and negative prompt. * Clean up the prompt and negative prompt.
Note: The extension must be loaded after the installed wildcards extension (or any other that modifies the prompt or has it's own syntax expressions). Extensions load by their folder name in alphanumeric order. 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.
With the ["Dynamic Prompts" extension](https://github.com/adieyal/sd-dynamic-prompts) this happens by default due to default folder names for both extensions. But if this is not the case, you can just rename this extension's folder so the ordering works out.
With the ["AUTOMATIC1111 Wildcards" extension](https://github.com/AUTOMATIC1111/stable-diffusion-webui-wildcards) you will have to rename one of the folders, so that it loads before than this extension.
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.
Notes: Notes:
1. It only recognizes regular A1111 prompt formats. So: 1. Other than its own commands, it only recognizes regular A1111 prompt formats. So:
* **Attention**: `\[prompt\] (prompt) (prompt:weight)` * **Attention**: `\[prompt\] (prompt) (prompt:weight)`
* **Alternation**: `\[prompt1|prompt2|...\]` * **Alternation**: `\[prompt1|prompt2|...\]`
* **Scheduling**: `\[prompt1:prompt2:step\]` * **Scheduling**: `\[prompt1:prompt2:step\]`
* **Extra networks**: `\<kind:model...\>` * **Extra networks**: `\<kind:model...\>`
* **BREAK**: `prompt1 BREAK prompt2` * **BREAK**: `prompt1 BREAK prompt2`
* **Composable Diffusion**: `prompt1 AND prompt2` * **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. In SD.Next that means only the *A1111* or *Full* parsers. It will warn you if you use the *Compel* parser.
2. It only recognizes wildcards in the *\_\_wildcard\_\_* and *{choice|choice}* formats. 2. It recognizes wildcards in the *\_\_wildcard\_\_* and *{choice|choice}* formats (and anything that [Dynamic Prompts](https://github.com/adieyal/sd-dynamic-prompts) supports).
3. Since it should run after other extensions that apply to the prompt, the content should have already been processed by them and there should't be any non recognized syntax anymore. 3. It does not create *AND/BREAK* constructs when moving content to the negative prompt.
4. It does not create *AND/BREAK* constructs when moving content to the negative prompt.
## Installation ## Installation
On A1111 compatible webuis:
1. Go to Extensions > Install from URL 1. Go to Extensions > Install from URL
2. Paste <https://github.com/acorderob/sd-webui-prompt-postprocessor> in the URL for extension's git repository text field 2. Paste <https://github.com/acorderob/sd-webui-prompt-postprocessor> in the URL for extension's git repository text field
3. Click the Install button 3. Click the Install button
4. Restart the webui 4. Restart the webui
On ComfyUI:
1. Go to Manager > Custom Nodes Manager
2. Install through ComfyUI Manager
3. Click Install via Git URL and enter <https://github.com/acorderob/sd-webui-prompt-postprocessor>
4. Restart
## Usage ## Usage
### Detection of remaining wildcards
This extension should run after any wildcard extensions, so any remaining wildcards present in the prompt or negative_prompt at this point of processing 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.
If you choose to not ignore wildcards, the extension will look for any *\_\_wildcard\_\_* or *{choice|choice}* constructs and act as configured.
### Commands ### Commands
The extension uses now a new format for its commands. The format is similar to an extranetwork, but it has a "ppp:" prefix followed by the command, and then a space and any parameters (if any). The extension uses a format for its commands similar to an extranetwork, but it has a "ppp:" prefix followed by the command, and then a space and any parameters (if any).
```text ```text
<ppp:command parameters> <ppp:command parameters>
@@ -63,7 +66,79 @@ When a command is associated with any content, it will be between an opening and
<ppp:command parameters>content<ppp:/command> <ppp:command parameters>content<ppp:/command>
``` ```
The `set` and `if` commands are the first to be processed. For wildcards and choices it uses the formats from the Dynamic Prompts extension, but sometimes with some additional options for more functionality.
### Choices
The generic format is:
```text
{parameters$$opt1::choice1|opt2::choice2|opt3::choice3}
```
Both the construct parameters (up to the '$$') and the individual choice options (up to the '::') are optional.
There is also a format where instead of "parameters$$" you just put the sampler, for compatibility with Dynamic Prompts.
The construct parameters can be written with the following options (all are optional):
* "**~**" or "**@**": sampler (for compatibility with Dynamic Prompts), but only "**~**" (random) is allowed.
* "**r**": means it allows repetition of the choices.
* "**n**" or "**n-m**" or "**n-**" or "**-m**": number or range of choices to select. Allows zero as the start of a range. Default is 1.
* "**$$sep**": separator when multiple choices are selected. Default is set in settings.
* "**$$**": end of the parameters.
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.
* "**::**": end of choice options
Whitespace is allowed between parameters.
These are examples of formats you can use to insert a choice construct:
```text
{opt1|5::opt2|3::opt3} # select 1 choice, two have weights
{3$$opt1|5 if _is_sd1::opt2|opt3} # select 3 choices, one has a weight and a condition
{2-3$$opt1|opt2|opt3} # select 2 to 3 choices
{r2-3$$opt1|opt2|opt3} # select 2 to 3 choices allowing repetition
{2-3$$ / $$opt1|opt2|opt3} # select 2 to 3 choices with separator " / "
```
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.
### Wildcards
The generic format is:
```text
__parameters$$path/to/wildcard(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).
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".
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.
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.
```
#### 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.
### Set command ### Set command
@@ -73,6 +148,27 @@ The format is:
```text ```text
<ppp:set varname>value<ppp:/set> <ppp:set varname>value<ppp:/set>
<ppp:set varname evaluate>value<ppp:/set>
<ppp:set varname add>value<ppp:/set>
<ppp:set varname evaluate add>value<ppp:/set>
```
The `evaluate` parameter makes it so the value of the variable is evaluated at this moment, instead of when it is used.
With the `add` parameter the value is added to the current value of the variable. It does not force an immediate evaluation of the old nor the added value.
The Dynamic Prompts format also works:
```text
${var=value}
${var=!value} # immmediate evaluation
```
If also supports the addition as an extension of the Dynamic Prompts format:
```text
${var+=value}
${var+=!value}
``` ```
### Echo command ### Echo command
@@ -83,23 +179,57 @@ The format is:
```text ```text
<ppp:echo varname> <ppp:echo varname>
<ppp:echo varname>default<ppp:/echo>
```
The Dynamic Prompts format is:
```text
${var}
${var:default}
``` ```
### If command ### If command
This command allows you to filter content based on conditions. This command allows you to filter content based on conditions.
The format is: The full format is:
```text ```text
<ppp:if condition1>content one<ppp:elif condition2>content two<ppp:else>other content<ppp:/if> <ppp:if condition1>content one<ppp:elif condition2>content two<ppp:else>other content<ppp:/if>
``` ```
The *conditionN* compares a variable with a value. The operation can be `eq`, `ne`, `gt`, `lt`, `ge`, `le` and the value can be a quoted string or an integer. The *conditionN* compares a variable with a value or a list of values. The allowed formats are:
The variable can be one set with the `set` command or special variables like: ```text
[not] variable
[not] variable operation value
variable [not] operation value
[not] variable operation (value1,value2...)
variable [not] operation (value1,value2...)
```
When there is no value it will check if the variable is truthy.
For a simple value the allowed operations are `eq`, `ne`, `gt`, `lt`, `ge`, `le`, `contains` and the value can be a quoted string or an integer.
For a list of values the allowed operations are `contains`, `in` and the value of the variable is checked against all the elements of the list until one matches.
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"`) * `_sd` : the loaded model version (`"sd1"`, `"sd2"`, `"sdxl"`)
* `_sdname` : the loaded model filename (without path)
* `_sdfullname`: the loaded model filename (with path)
* `_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
* `_is_sdxl`: true if the loaded model version is SDXL (includes Pony models)
* `_is_ssd`: true if the loaded model version is SSD (Segmind Stable Diffusion 1B). Note that for an SSD model `_is_sdxl` will also be true.
* `_is_sdxl_no_ssd`: true if the loaded model version is SDXL and not an SSD model.
* `_is_pony`: true if the loaded model version is SDXL and a Pony model (based on its filename). Note that for a pony model `_is_sdxl` will also be true.
* `_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
Any `elif`s (there can be multiple) and the `else` are optional. Any `elif`s (there can be multiple) and the `else` are optional.
@@ -108,9 +238,9 @@ Any `elif`s (there can be multiple) and the `else` are optional.
(multiline to be easier to read) (multiline to be easier to read)
```text ```text
<ppp:if _sd eq "sd1"><lora:test_sd1> test sd1 <ppp:if _is_sd1><lora:test_sd1> test sd1x
<ppp:elif _sd eq "sd2"><lora:test_sd2> test sd2 <ppp:elif _sd_pony><lora:test_pony> test pony
<ppp:elif _sd eq "sdxl"><lora:test_sdxl> test sdxl <ppp:elif _sd_sdxl><lora:test_sdxl> test sdxl
<ppp:else>unknown model <ppp:else>unknown model
<ppp:/if> <ppp:/if>
``` ```
@@ -153,23 +283,7 @@ Then, if that option is chosen this extension will process it later and move tha
#### Old format #### Old format
The old format is still supported (for now) and is like this: The old format (`<!...!>`) is not supported anymore.
```text
<!content!>
```
And an optional position can be specified like this:
```text
<!!position!content!>
```
With the insertion point like this:
```text
<!!iN!!>
```
### Notes on negative commands ### Notes on negative commands
@@ -198,35 +312,44 @@ This should still work as intended, and the only negative point i see is the unn
### General settings ### General settings
* **Debug**: writes debugging information to the console. * **Debug level**: what to write to the console. Note: in SD.Next debug messages only show if you launch it with the --debug argument.
* **Pony substrings**: list of substrings to detect a Pony model.
* **Apply in img2img**: check if you want to do the processing in img2img processes (does not apply to ComfyUI node).
### Wildcard settings
* **Process wildcards**: you can choose to process them with this extension or use a different one.
* **Wildcards folders**: you can enter multiple folders separated by commas. In ComfyUI you can leave it empty and add a "wildcards" entry in the extra_model_paths.yaml file.
* **What to do with remaining wildcards?**: select what do you want to do with any found wildcards. * **What to do with remaining wildcards?**: select what do you want to do with any found wildcards.
* **Ignore**: do not try to detect wildcards. * **Ignore**: do not try to detect wildcards.
* **Remove**: detect wildcards and remove them. * **Remove**: detect wildcards and remove them.
* **Add visible warning**: detect wildcards and add a warning text to the prompt, that hopefully produces a noticeable generation. * **Add visible warning**: detect wildcards and add a warning text to the prompt, that hopefully produces a noticeable generation.
* **Stop the generation**: detect wildcards and stop the generation. * **Stop the generation**: detect wildcards and stop the generation.
* **Default separator used when adding multiple choices**: what do you want to use by default to separate multiple choices when the options allow it (by default it's ", ").
### Content removal settings * **Keep the order of selected choices**: if checked, a multiple choice construct will return them in the order they are in the construct.
* **Remove extra network tags**: removes all extra network tags.
### Send to negative prompt settings ### Send to negative prompt settings
* **Apply in img2img**: check if you want to do this processing in img2img processes.
* **Separator used when adding to the negative prompt**: you can specify the separator used when adding to the negative prompt (by default it's ", "). * **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. * **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). * **Join attention modifiers (weights) when possible**: it joins attention modifiers when possible (joins into one, multipliying their values).
### Clean up settings ### Clean up settings
* **Apply in img2img**: check if you want to do this processing in img2img processes.
* **Remove empty constructs**: removes attention/scheduling/alternation constructs when they are invalid. * **Remove empty constructs**: removes attention/scheduling/alternation constructs when they are invalid.
* **Remove extra separators**: removes unnecessary separators. This applies to the configured separator and regular commas. * **Remove extra separators**: removes unnecessary separators. This applies to the configured separator and regular commas.
* **Remove additional extra separators**: removes unnecessary separators at start or end of lines. This applies to the configured separator and regular commas. * **Remove additional extra separators**: removes unnecessary separators at start or end of lines. This applies to the configured separator and regular commas.
* **Clean up around BREAKs**: removes consecutive BREAKs and unnecessary commas and space around them. * **Clean up around BREAKs**: removes consecutive BREAKs and unnecessary commas and space around them.
* **Use EOL instead of Space before BREAKs**: add a newline before BREAKs.
* **Clean up around ANDs**: removes consecutive ANDs and unnecessary commas and space around them. * **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. * **Clean up around extra network tags**: removes spaces around them.
* **Remove extra spaces**: removes other unnecessary spaces. * **Remove extra spaces**: removes other unnecessary spaces.
### Content removal settings
* **Remove extra network tags**: removes all extra network tags.
## License ## License
MIT MIT
+28
View File
@@ -0,0 +1,28 @@
"""
@author: ACB
@title: Prompt Post Processor
@nickname: ACB PPP
@description: Node for processing prompts. Includes the following options: send to negative prompt, set variables, if/elif/else command for conditional content, wildcards and choices.
"""
import sys
import os
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"]
+98
View File
@@ -0,0 +1,98 @@
%import common (LETTER, DIGIT, INT, CNAME, SIGNED_NUMBER, NUMBER)
_WHITESPACE: /\s+/
STRING: /("(?!"").*?(?<!\\)(\\\\)*?"|'(?!'').*?(?<!\\)(\\\\)*?')/i
BOOLEAN: /true|false/i
// plain text and weights
?plain: /((?!__|\bAND\b|\${)[^\\()\[\]:<>${]|\\.)+/s // exclude only the starting ones
?plain_choice: /((?!__|\bAND\b|\${|\$\$)[^\\()\[\]:<>${|}~@]|\\.)+/s // add the specific internal choice ones
?plain_alternate: /((?!__|\bAND\b|\${)[^\\()\[\]:<>${|]|\\.)+/s // add the specific internal alternate ones
?plain_var: /((?!__|\bAND\b|\${)[^\\()\[\]:<>${}]|\\.)+/s // add the specific internal var ones
?specialchars: /[_{()\[\]:<>]|\$(?![{$])/ // include only the starting ones
?specialchars_negtag: /[_{()\[\]:<>!|}]|\$(?![{$])/ // add the internal negtag ones
?specialchars_alternate: /[_{()\[\]:<>|]|\$(?![{$])/ // add the internal alternate ones
?specialchars_choice: /[_{()\[\]:<>|}]|\$(?![{$])/ // add the internal choice ones
?specialchars_var: /[_{()\[\]:<>}]|\$(?![{$])/ // add the internal var ones
?numpar: _WHITESPACE? SIGNED_NUMBER _WHITESPACE?
start: promptcomp | content
// prompt composition with AND
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)+
// attention modifiers
emphasized: "(" content [":" numpar] ")"
deemphasized: "[" content "]"
// prompt scheduling and alternation
alternate: "[" alternateoption ("|" alternateoption)+ "]"
alternateoption: content_alternate
scheduled: "[" [content ":"] content ":" numpar "]"
// extra network tags
extranetworktag: "<" /(?!ppp:)[^>]+/ ">"
// command: stn (send to negative)
commandstn: "<ppp:stn" [_WHITESPACE /s|e|p\d/] _WHITESPACE? ">" content_negtag "<ppp:/stn>"
commandstni: "<ppp:stn" _WHITESPACE /i\d/ _WHITESPACE? ">"
// command: if
commandif.2: commandif_if commandif_elif* commandif_else? "<ppp:/if>"
commandif_if: "<ppp:if" _WHITESPACE condition _WHITESPACE? ">" ifvalue
commandif_elif: "<ppp:elif" _WHITESPACE condition _WHITESPACE? ">" ifvalue
commandif_else: "<ppp:else" _WHITESPACE? ">" ifvalue
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
IDENTIFIER: CNAME
SIMPLEVALUE: STRING | INT | BOOLEAN
listvalue: "(" _WHITESPACE? SIMPLEVALUE (_WHITESPACE? "," _WHITESPACE? SIMPLEVALUE)* _WHITESPACE? ")"
// command: set
commandset: "<ppp:set" _WHITESPACE IDENTIFIER [ _WHITESPACE? /evaluate/ ] [ _WHITESPACE? /add/ ] _WHITESPACE? ">" content "<ppp:/set>"
// command: echo
commandecho: "<ppp:echo" _WHITESPACE IDENTIFIER _WHITESPACE? ">" [ content "<ppp:/echo>" ]
// variable set
variableset.2: "${" _WHITESPACE? IDENTIFIER _WHITESPACE? [/\+/] "=" [/!/] varvalue "}"
// variable use
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 ")"
// choices
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_sampler: /[~@]/ // ~ for random, @ for cyclical
choicesoptions_rep: /r/
choicesoptions_num: INT
choicesoptions_from: INT
choicesoptions_to: INT
choicesoptions_sep: "$$" plain
choice: [[_WHITESPACE? choiceweight] [_WHITESPACE? choiceif] _WHITESPACE? "::"] choicevalue
choiceweight: NUMBER
choiceif: "if" _WHITESPACE condition
choicevalue: content_choice
+5
View File
@@ -0,0 +1,5 @@
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")
+5
View File
@@ -0,0 +1,5 @@
[Extension]
Name = sd-webui-prompt-postprocessor
[Scripts]
After = sd-dynamic-prompts, stable-diffusion-webui-wildcards
+1091 -1127
View File
File diff suppressed because it is too large Load Diff
+24
View File
@@ -0,0 +1,24 @@
from collections import OrderedDict
from typing import Tuple
class PPPLRUCache:
ProcessInput = Tuple[int, str, str]
ProcessResult = Tuple[str, str]
def __init__(self, capacity: int):
self.cache = OrderedDict()
self.capacity = capacity
def get(self, key: ProcessInput) -> ProcessResult:
if key not in self.cache:
return None
self.cache.move_to_end(key)
return self.cache[key]
def put(self, key: ProcessInput, value: ProcessResult) -> None:
self.cache[key] = value
self.cache.move_to_end(key)
if len(self.cache) > self.capacity:
self.cache.popitem(last=False)
+406
View File
@@ -0,0 +1,406 @@
# pylint: disable=missing-module-docstring, missing-class-docstring, missing-function-docstring, invalid-name
import os
# pylint: disable=import-error
import folder_paths # type: ignore
import nodes # type: ignore
from .ppp import PromptPostProcessor
from .ppp_logging import DEBUG_LEVEL, PromptPostProcessorLogFactory
from .ppp_wildcards import PPPWildcards
if __name__ == "__main__":
raise SystemExit("This script must be run from ComfyUI")
class PromptPostProcessorComfyUINode:
VERSION = PromptPostProcessor.VERSION
logger = None
def __init__(self):
lf = PromptPostProcessorLogFactory()
self.logger = lf.log
grammar_filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), "grammar.lark")
with open(grammar_filename, "r", encoding="utf-8") as file:
self.grammar_content = file.read()
self.wildcards_obj = PPPWildcards(lf.log)
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"model": (
"MODEL",
{
"forceInput": True,
},
),
"modelname": (
"STRING",
{
"default": "",
"forceInput": True,
},
),
"seed": (
"INT",
{
"default": None,
"forceInput": False,
},
),
"pos_prompt": (
"STRING",
{
"multiline": True,
"default": "",
"forceInput": True,
},
),
"neg_prompt": (
"STRING",
{
"multiline": True,
"default": "",
"forceInput": True,
},
),
},
"optional": {
"debug_level": (
[e.value for e in DEBUG_LEVEL],
{
"default": DEBUG_LEVEL.minimal.value,
"tooltip": "Debug level",
},
),
"pony_substrings": (
"STRING",
{
"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",
},
),
"wc_process_wildcards": (
"BOOLEAN",
{
"default": True,
"tooltip": "Process wildcards in the prompt",
"label_on": "Yes",
"label_off": "No",
},
),
"wc_wildcards_folders": (
"STRING",
{
"default": "",
"tooltip": "Comma separated list of wildcards folders",
},
),
"wc_if_wildcards": (
[e.value for e in PromptPostProcessor.IFWILDCARDS_CHOICES],
{
"default": PromptPostProcessor.IFWILDCARDS_CHOICES.ignore.value,
"tooltip": "How to handle invalid wildcards in the prompt",
},
),
"wc_choice_separator": (
"STRING",
{
"default": PromptPostProcessor.DEFAULT_CHOICE_SEPARATOR,
"tooltip": "Default separator for selected choices",
},
),
"wc_keep_choices_order": (
"BOOLEAN",
{
"default": True,
"tooltip": "Keep the order of the choices in the prompt",
"label_on": "Yes",
"label_off": "No",
},
),
"stn_separator": (
"STRING",
{
"default": PromptPostProcessor.DEFAULT_STN_SEPARATOR,
"tooltip": "Separator for the content added to the negative prompt",
},
),
"stn_ignore_repeats": (
"BOOLEAN",
{
"default": True,
"tooltip": "Ignore repeated content added to the negative prompt",
"label_on": "Yes",
"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",
{
"default": True,
"tooltip": "Remove extra spaces",
"label_on": "Yes",
"label_off": "No",
},
),
"cleanup_empty_constructs": (
"BOOLEAN",
{
"default": True,
"tooltip": "Remove empty constructs",
"label_on": "Yes",
"label_off": "No",
},
),
"cleanup_extra_separators": (
"BOOLEAN",
{
"default": True,
"tooltip": "Remove extra separators",
"label_on": "Yes",
"label_off": "No",
},
),
"cleanup_extra_separators2": (
"BOOLEAN",
{
"default": True,
"tooltip": "Remove extra separators (additional cases)",
"label_on": "Yes",
"label_off": "No",
},
),
"cleanup_breaks": (
"BOOLEAN",
{
"default": True,
"tooltip": "Cleanup around BREAKs",
"label_on": "Yes",
"label_off": "No",
},
),
"cleanup_breaks_eol": (
"BOOLEAN",
{
"default": False,
"tooltip": "Set BREAKs in their own line",
"label_on": "Yes",
"label_off": "No",
},
),
"cleanup_ands": (
"BOOLEAN",
{
"default": True,
"tooltip": "Cleanup around ANDs",
"label_on": "Yes",
"label_off": "No",
},
),
"cleanup_ands_eol": (
"BOOLEAN",
{
"default": False,
"tooltip": "Set ANDs in their own line",
"label_on": "Yes",
"label_off": "No",
},
),
"cleanup_extranetwork_tags": (
"BOOLEAN",
{
"default": False,
"tooltip": "Clean up around extra network tags",
"label_on": "Yes",
"label_off": "No",
},
),
"remove_extranetwork_tags": (
"BOOLEAN",
{
"default": False,
"tooltip": "Remove extra network tags",
"label_on": "Yes",
"label_off": "No",
},
),
},
}
RETURN_TYPES = (
"STRING",
"STRING",
)
RETURN_NAMES = (
"pos_prompt",
"neg_prompt",
)
FUNCTION = "process"
CATEGORY = "ACB"
@classmethod
def IS_CHANGED(
cls,
model,
modelname,
pos_prompt,
neg_prompt,
seed,
debug_level, # pylint: disable=unused-argument
pony_substrings,
wc_process_wildcards,
wc_wildcards_folders,
wc_if_wildcards,
wc_choice_separator,
wc_keep_choices_order,
stn_separator,
stn_ignore_repeats,
stn_join_attention,
cleanup_extra_spaces,
cleanup_empty_constructs,
cleanup_extra_separators,
cleanup_extra_separators2,
cleanup_breaks,
cleanup_breaks_eol,
cleanup_ands,
cleanup_ands_eol,
cleanup_extranetwork_tags,
remove_extranetwork_tags,
):
new_run = {
"model": model,
"modelname": modelname,
"pos_prompt": pos_prompt,
"neg_prompt": neg_prompt,
"seed": seed,
"pony_substrings": pony_substrings,
"process_wildcards": wc_process_wildcards,
"wildcards_folders": wc_wildcards_folders,
"if_wildcards": wc_if_wildcards,
"choice_separator": wc_choice_separator,
"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,
"cleanup_extra_separators2": cleanup_extra_separators2,
"cleanup_breaks": cleanup_breaks,
"cleanup_breaks_eol": cleanup_breaks_eol,
"cleanup_ands": cleanup_ands,
"cleanup_ands_eol": cleanup_ands_eol,
"cleanup_extranetwork_tags": cleanup_extranetwork_tags,
"remove_extranetwork_tags": remove_extranetwork_tags,
}
return new_run.__hash__
# return float("NaN")
def process(
self,
model,
modelname,
pos_prompt,
neg_prompt,
seed,
debug_level,
pony_substrings,
wc_process_wildcards,
wc_wildcards_folders,
wc_if_wildcards,
wc_choice_separator,
wc_keep_choices_order,
stn_separator,
stn_ignore_repeats,
stn_join_attention,
cleanup_extra_spaces,
cleanup_empty_constructs,
cleanup_extra_separators,
cleanup_extra_separators2,
cleanup_breaks,
cleanup_breaks_eol,
cleanup_ands,
cleanup_ands_eol,
cleanup_extranetwork_tags,
remove_extranetwork_tags,
):
model_info = {
"models_path": folder_paths.models_dir,
"model_filename": modelname, # path is relative to checkpoints folder
"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",
),
"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"),
}
# SVD_img2vid, SVD3D_u, SVD3_p, Stable_Zero123, SD_X4Upscaler,
# Stable_Cascade_C, Stable_Cascade_B, StableAudio
if wc_wildcards_folders == "":
wc_wildcards_folders = ",".join(folder_paths.get_folder_paths("wildcards") or [])
if wc_wildcards_folders == "":
wc_wildcards_folders = os.getenv("WILDCARD_DIR", PPPWildcards.DEFAULT_WILDCARDS_FOLDER)
wildcards_folders = [
(f if os.path.isabs(f) else os.path.abspath(os.path.join(folder_paths.models_dir, f)))
for f in wc_wildcards_folders.split(",")
if f.strip() != ""
]
options = {
"debug_level": debug_level,
"pony_substrings": pony_substrings,
"process_wildcards": wc_process_wildcards,
"if_wildcards": wc_if_wildcards,
"choice_separator": wc_choice_separator,
"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,
"cleanup_extra_separators2": cleanup_extra_separators2,
"cleanup_breaks": cleanup_breaks,
"cleanup_breaks_eol": cleanup_breaks_eol,
"cleanup_ands": cleanup_ands,
"cleanup_ands_eol": cleanup_ands_eol,
"cleanup_extranetwork_tags": cleanup_extranetwork_tags,
"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
)
pos_prompt, neg_prompt = ppp.process_prompt(pos_prompt, neg_prompt, seed if seed is not None else 1)
return (
pos_prompt,
neg_prompt,
)
def interrupt(self):
nodes.interrupt_processing(True)
+11 -6
View File
@@ -1,8 +1,15 @@
from enum import Enum
import logging import logging
import sys import sys
import copy import copy
class DEBUG_LEVEL(Enum):
none = "none"
minimal = "minimal"
full = "full"
class PromptPostProcessorLogFactory: # pylint: disable=too-few-public-methods class PromptPostProcessorLogFactory: # pylint: disable=too-few-public-methods
""" """
Factory class for creating loggers for the PromptPostProcessor module. Factory class for creating loggers for the PromptPostProcessor module.
@@ -42,7 +49,7 @@ class PromptPostProcessorLogFactory: # pylint: disable=too-few-public-methods
colored_record = copy.copy(record) colored_record = copy.copy(record)
levelname = colored_record.levelname levelname = colored_record.levelname
seq = self.COLORS.get(levelname, self.COLORS["RESET"]) seq = self.COLORS.get(levelname, self.COLORS["RESET"])
colored_record.levelname = f"{seq}{levelname}{self.COLORS['RESET']}" colored_record.levelname = f"{seq}{levelname:8s}{self.COLORS['RESET']}"
return super().format(colored_record) return super().format(colored_record)
def __init__(self): def __init__(self):
@@ -61,9 +68,9 @@ class PromptPostProcessorLogFactory: # pylint: disable=too-few-public-methods
ppplog.propagate = False ppplog.propagate = False
if not ppplog.handlers: if not ppplog.handlers:
handler = logging.StreamHandler(sys.stdout) handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(self.ColoredFormatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")) handler.setFormatter(self.ColoredFormatter("%(asctime)s %(levelname)s %(message)s")) # Used in A1111 / Forge / reForge / ComfyUI, but not in SD.Next
ppplog.addHandler(handler) ppplog.addHandler(handler)
ppplog.setLevel(logging.INFO) ppplog.setLevel(logging.DEBUG)
self.log = PromptPostProcessorLogCustomAdapter(ppplog) self.log = PromptPostProcessorLogCustomAdapter(ppplog)
@@ -76,12 +83,10 @@ class PromptPostProcessorLogCustomAdapter(logging.LoggerAdapter):
def process(self, msg, kwargs): def process(self, msg, kwargs):
""" """
Process the log message and keyword arguments. Process the log message and keyword arguments.
Args: Args:
msg (str): The log message. msg (str): The log message.
kwargs (dict): The keyword arguments. kwargs (dict): The keyword arguments.
Returns: Returns:
tuple: A tuple containing the processed log message and keyword arguments. tuple: A tuple containing the processed log message and keyword arguments.
""" """
return f"[PromptPostProcessor] {msg}", kwargs return f"[PPP] {msg}", kwargs
+209
View File
@@ -0,0 +1,209 @@
import os
import json
from typing import Optional
import yaml
from ppp_logging import DEBUG_LEVEL
class PPPWildcards:
DEFAULT_WILDCARDS_FOLDER = "wildcards"
def __init__(self, logger):
self.logger = logger
self.debug_level = DEBUG_LEVEL.none
self.wildcards_folders = []
self.wildcards = {}
self.wildcard_files = {}
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
if wildcards_folders is not None:
# if self.debug_level != DEBUG_LEVEL.none:
# self.logger.info("Initializing wildcards...")
# t1 = time.time()
for fullpath in 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
):
self.__remove_wildcards_from_file(fullpath)
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 = {}
self.wildcard_files = {}
def __get_keys_in_dict(self, dictionary: dict, prefix="") -> list[str]:
"""
Get all keys in a dictionary.
Args:
dictionary (dict): The dictionary to check.
prefix (str): The prefix for the current key.
Returns:
list: A list of all keys in the dictionary, including nested keys.
"""
keys = []
for key in dictionary.keys():
if isinstance(dictionary[key], dict):
keys.extend(self.__get_keys_in_dict(dictionary[key], prefix + key + "/"))
else:
keys.append(prefix + str(key))
return keys
def __get_nested(self, dictionary: dict, keys: str) -> object:
"""
Get a nested value from a dictionary.
Args:
dictionary (dict): The dictionary to check.
keys (str): The keys to get the value from.
Returns:
object: The value of the nested keys in the dictionary.
"""
keys = keys.split("/")
current_dict = dictionary
for key in keys:
current_dict = current_dict.get(key)
if current_dict is None:
return None
return current_dict
def __remove_wildcards_from_file(self, full_path: str):
"""
Clear all wildcards in a file.
Args:
full_path (str): The path to the file.
"""
last_modified_cached = self.wildcard_files.get(full_path, None)
if 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]
def __get_wildcards_in_file(self, base, full_path: str):
"""
Get all wildcards in a file.
Args:
base (str): The base path for the wildcards.
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]:
return
filename = os.path.basename(full_path)
name, extension = os.path.splitext(filename)
if extension not in (".txt", ".json", ".yaml", ".yml"):
return
self.__remove_wildcards_from_file(full_path)
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 extension == ".txt":
self.__get_wildcards_in_text_file(full_path, name, relfolders)
elif extension in (".json", ".yaml", ".yml"):
self.__get_wildcards_in_structured_file(full_path, extension, relfolders)
self.wildcard_files[full_path] = last_modified
def __get_wildcards_in_structured_file(self, full_path, extension, relfolders):
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) 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] = {"file": full_path, "choices": choices}
def __get_wildcards_in_text_file(self, full_path, name, relfolders):
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:
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] = {"file": full_path, "choices": text_content}
def __get_wildcards_in_directory(self, base: str, directory: str):
"""
Get all wildcards in a directory.
Args:
base (str): The base path for the wildcards.
directory (str): The path to the directory.
"""
if not os.path.exists(directory):
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))
if os.path.basename(full_path).startswith("."):
continue
if os.path.isdir(full_path):
self.__get_wildcards_in_directory(base, full_path)
elif os.path.isfile(full_path):
self.__get_wildcards_in_file(base, full_path)
+1
View File
@@ -0,0 +1 @@
lark
+421 -192
View File
@@ -3,19 +3,23 @@ if __name__ == "__main__":
import sys import sys
import os import os
import time
sys.path.insert(1, os.path.join(sys.path[0], "..")) sys.path.append(os.path.join(sys.path[0], ".."))
from modules import scripts, shared, script_callbacks from modules import scripts, shared, script_callbacks
from modules.processing import StableDiffusionProcessing from modules.processing import StableDiffusionProcessing
from modules.shared import opts from modules.shared import opts
from modules.paths import models_path
import gradio as gr import gradio as gr
from ppp import PromptPostProcessor from ppp import PromptPostProcessor
from ppp_logging import PromptPostProcessorLogFactory from ppp_logging import DEBUG_LEVEL, PromptPostProcessorLogFactory
from ppp_cache import PPPLRUCache
from ppp_wildcards import PPPWildcards
class PromptPostProcessorScript(scripts.Script): class PromptPostProcessorA1111Script(scripts.Script):
""" """
This class represents a script for prompt post-processing. This class represents a script for prompt post-processing.
It is responsible for processing prompts and applying various settings and cleanup operations. It is responsible for processing prompts and applying various settings and cleanup operations.
@@ -28,6 +32,7 @@ class PromptPostProcessorScript(scripts.Script):
title(): Returns the title of the script. title(): Returns the title of the script.
show(is_img2img): Determines whether the script should be shown based on the input type. show(is_img2img): Determines whether the script should be shown based on the input type.
process(p, *args, **kwargs): Processes the prompts and applies post-processing operations. process(p, *args, **kwargs): Processes the prompts and applies post-processing operations.
ppp_interrupt(): Interrupts the generation.
__on_ui_settings(): Callback function for UI settings. __on_ui_settings(): Callback function for UI settings.
""" """
@@ -43,12 +48,15 @@ class PromptPostProcessorScript(scripts.Script):
Returns: Returns:
None None
""" """
if not hasattr(self, "ppp_callbacks_added"): lf = PromptPostProcessorLogFactory()
lf = PromptPostProcessorLogFactory() self.name = PromptPostProcessor.NAME
self.ppp_logger = lf.log self.ppp_logger = lf.log
self.ppp_debug = getattr(opts, "ppp_gen_debug", False) if opts is not None else False self.ppp_debug_level = DEBUG_LEVEL(getattr(opts, "ppp_gen_debug_level", DEBUG_LEVEL.none.value))
script_callbacks.on_ui_settings(self.__on_ui_settings) self.lru_cache = PPPLRUCache(1000)
self.ppp_callbacks_added = True grammar_filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), "../grammar.lark")
with open(grammar_filename, "r", encoding="utf-8") as file:
self.grammar_content = file.read()
self.wildcards_obj = PPPWildcards(lf.log)
def title(self): def title(self):
""" """
@@ -81,29 +89,155 @@ class PromptPostProcessorScript(scripts.Script):
Returns: Returns:
None None
""" """
t1 = time.time()
if getattr(opts, "prompt_attention", "") == "Compel parser":
self.ppp_logger.warning("Compel parser is not supported!")
is_i2i = getattr(p, "init_images", [None])[0] is not None is_i2i = getattr(p, "init_images", [None])[0] is not None
self.ppp_debug = getattr(opts, "ppp_gen_debug", False) if opts is not None else False self.ppp_debug_level = DEBUG_LEVEL(getattr(opts, "ppp_gen_debug_level", DEBUG_LEVEL.none.value))
if self.ppp_debug: do_i2i = getattr(opts, "ppp_gen_doi2i", False)
self.ppp_logger.info(f"Post-processing prompts ({'i2i' if is_i2i else 't2i'} mode)") if is_i2i and not do_i2i:
ppp = PromptPostProcessor(self, p, shared.state, opts, is_i2i) if self.ppp_debug_level != DEBUG_LEVEL.none:
# processes regular prompts self.ppp_logger.info("Not processing the prompt for i2i")
if ( return
hasattr(p, "all_prompts") if self.ppp_debug_level != DEBUG_LEVEL.none:
and p.all_prompts is not None self.ppp_logger.info(f"Post-processing prompts ({'i2i' if is_i2i else 't2i'})")
and hasattr(p, "all_negative_prompts") model_info = {
and p.all_negative_prompts is not None "models_path": models_path,
): "model_filename": getattr(p.sd_model.sd_checkpoint_info, "filename", ""), # path is absolute
for i, (prompt, negative_prompt) in enumerate(zip(p.all_prompts, p.all_negative_prompts)): "is_sd1": False, # Stable Diffusion 1
p.all_prompts[i], p.all_negative_prompts[i] = ppp.process_prompt(prompt, negative_prompt) "is_sd2": False, # Stable Diffusion 2
"is_sdxl": False, # Stable Diffusion XL
"is_ssd": False, # Segmind Stable Diffusion 1B
"is_sd3": False, # Stable Diffusion 3
"is_flux": False, # Flux
}
app = (
"forge"
if hasattr(p.sd_model, "model_config")
else "sdnext" if hasattr(p.sd_model, "is_sdxl") and not hasattr(p.sd_model, "is_ssd") else "a1111"
)
if app == "sdnext":
# cannot differenciate SD1 and SD2, we set True to both
# LatentDiffusion is for the original backend, StableDiffusionPipeline is for the diffusers backend
model_info["is_sd1"] = p.sd_model.__class__.__name__ in ("LatentDiffusion", "StableDiffusionPipeline")
model_info["is_sd2"] = p.sd_model.__class__.__name__ in ("LatentDiffusion", "StableDiffusionPipeline")
model_info["is_sdxl"] = p.sd_model.__class__.__name__ == "StableDiffusionXLPipeline"
model_info["is_ssd"] = False # ?
model_info["is_sd3"] = p.sd_model.__class__.__name__ == "StableDiffusion3Pipeline"
model_info["is_flux"] = False
elif app == "forge":
model_info["is_sd1"] = getattr(p.sd_model, "is_sd1", False)
model_info["is_sd2"] = getattr(p.sd_model, "is_sd2", False)
model_info["is_sdxl"] = getattr(p.sd_model, "is_sdxl", False)
model_info["is_ssd"] = False # ?
model_info["is_sd3"] = getattr(p.sd_model, "is_sd3", False)
model_info["is_flux"] = p.sd_model.model_config.__class__.__name__ == "Flux"
else: # assume A1111 compatible (p.sd_model.__class__.__name__=="DiffusionEngine")
model_info["is_sd1"] = getattr(p.sd_model, "is_sd1", False)
model_info["is_sd2"] = getattr(p.sd_model, "is_sd2", False)
model_info["is_sdxl"] = getattr(p.sd_model, "is_sdxl", False)
model_info["is_ssd"] = getattr(p.sd_model, "is_ssd", False)
model_info["is_sd3"] = getattr(p.sd_model, "is_sd3", False)
model_info["is_flux"] = False
wc_wildcards_folders = getattr(opts, "ppp_wil_wildcardsfolders", "")
if wc_wildcards_folders == "":
wc_wildcards_folders = os.getenv("WILDCARD_DIR", PPPWildcards.DEFAULT_WILDCARDS_FOLDER)
wildcards_folders = [
(f if os.path.isabs(f) else os.path.abspath(os.path.join(models_path, f)))
for f in wc_wildcards_folders.split(",")
if f.strip() != ""
]
options = {
"debug_level": getattr(opts, "ppp_gen_debug_level", DEBUG_LEVEL.none.value),
"pony_substrings": getattr(opts, "ppp_gen_ponysubstrings", PromptPostProcessor.DEFAULT_PONY_SUBSTRINGS),
"process_wildcards": getattr(opts, "ppp_wil_process_wildcards", True),
"if_wildcards": getattr(opts, "ppp_wil_ifwildcards", PromptPostProcessor.IFWILDCARDS_CHOICES.ignore.value),
"choice_separator": getattr(opts, "ppp_wil_choice_separator", PromptPostProcessor.DEFAULT_CHOICE_SEPARATOR),
"keep_choices_order": getattr(opts, "ppp_wil_keep_choices_order", False),
"stn_separator": getattr(opts, "ppp_stn_separator", PromptPostProcessor.DEFAULT_STN_SEPARATOR),
"stn_ignore_repeats": getattr(opts, "ppp_stn_ignorerepeats", True),
"stn_join_attention": getattr(opts, "ppp_stn_joinattention", True),
"cleanup_extra_spaces": getattr(opts, "ppp_cup_extraspaces", True),
"cleanup_empty_constructs": getattr(opts, "ppp_cup_emptyconstructs", True),
"cleanup_extra_separators": getattr(opts, "ppp_cup_extraseparators", True),
"cleanup_extra_separators2": getattr(opts, "ppp_cup_extraseparators2", True),
"cleanup_breaks": getattr(opts, "ppp_cup_breaks", True),
"cleanup_breaks_eol": getattr(opts, "ppp_cup_breaks_eol", False),
"cleanup_ands": getattr(opts, "ppp_cup_ands", True),
"cleanup_ands_eol": getattr(opts, "ppp_cup_ands_eol", False),
"cleanup_extranetwork_tags": getattr(opts, "ppp_cup_extranetworktags", False),
"remove_extranetwork_tags": getattr(opts, "ppp_rem_removeextranetworktags", False),
}
self.wildcards_obj.refresh_wildcards(
self.ppp_debug_level, wildcards_folders if options["process_wildcards"] else None
)
ppp = PromptPostProcessor(
self.ppp_logger, self.ppp_interrupt, model_info, options, self.grammar_content, self.wildcards_obj
)
prompts_list = []
seeds = getattr(p, "all_seeds", [])
subseeds = getattr(p, "all_subseeds", [])
subseed_strength = getattr(p, "subseed_strength", 0.0)
if subseed_strength > 0:
calculated_seeds = [
int(subseed * subseed_strength + seed * (1 - subseed_strength))
for seed, subseed in zip(seeds, subseeds)
]
else:
calculated_seeds = seeds
if len(set(calculated_seeds)) < len(calculated_seeds):
self.ppp_logger.info("Adjusting seeds because some are equal.")
calculated_seeds = [seed + i for i, seed in enumerate(calculated_seeds)]
# adds regular prompts
rpr = getattr(p, "all_prompts", None)
rnr = getattr(p, "all_negative_prompts", None)
if rpr is not None and rnr is not None:
prompts_list += [
("regular", seed, prompt, negative_prompt)
for seed, prompt, negative_prompt in zip(calculated_seeds, rpr, rnr)
if (seed, prompt, negative_prompt) not in prompts_list
]
# make it compatible with A1111 hires fix # make it compatible with A1111 hires fix
if ( rph = getattr(p, "all_hr_prompts", None)
hasattr(p, "all_hr_prompts") rnh = getattr(p, "all_hr_negative_prompts", None)
and p.all_hr_prompts is not None if rph is not None and rnh is not None:
and hasattr(p, "all_hr_negative_prompts") prompts_list += [
and p.all_hr_negative_prompts is not None ("hiresfix", seed, prompt, negative_prompt)
): for seed, prompt, negative_prompt in zip(calculated_seeds, rph, rnh)
for i, (hr_prompt, hr_negative_prompt) in enumerate(zip(p.all_hr_prompts, p.all_hr_negative_prompts)): if (seed, prompt, negative_prompt) not in prompts_list
p.all_hr_prompts[i], p.all_hr_negative_prompts[i] = ppp.process_prompt(hr_prompt, hr_negative_prompt) ]
# processes prompts
for i, (prompttype, seed, prompt, negative_prompt) in enumerate(prompts_list):
if self.ppp_debug_level != DEBUG_LEVEL.none:
self.ppp_logger.info(f"processing prompts[{i+1}] ({prompttype})")
if self.lru_cache.get((seed, prompt, negative_prompt)) is None:
pp, np = ppp.process_prompt(prompt, negative_prompt, seed)
self.lru_cache.put((seed, prompt, negative_prompt), (pp, np))
# adds also the result so i2i doesn't process it unnecessarily
self.lru_cache.put((seed, pp, np), (pp, np))
elif self.ppp_debug_level != DEBUG_LEVEL.none:
self.ppp_logger.info("result already in cache")
# updates the prompts
if rpr is not None and rnr is not None:
for i, (seed, prompt, negative_prompt) in enumerate(zip(calculated_seeds, rpr, rnr)):
found = self.lru_cache.get((seed, prompt, negative_prompt))
if found is not None:
rpr[i] = found[0]
rnr[i] = found[1]
if rph is not None and rnh is not None:
for i, (seed, prompt, negative_prompt) in enumerate(zip(calculated_seeds, rph, rnh)):
found = self.lru_cache.get((seed, prompt, negative_prompt))
if found is not None:
rph[i] = found[0]
rnh[i] = found[1]
t2 = time.time()
if self.ppp_debug_level != DEBUG_LEVEL.none:
self.ppp_logger.info(f"process time: {t2 - t1:.3f} seconds")
def ppp_interrupt(self): def ppp_interrupt(self):
""" """
@@ -114,171 +248,266 @@ class PromptPostProcessorScript(scripts.Script):
""" """
shared.state.interrupted = True shared.state.interrupted = True
def __on_ui_settings(self):
"""
Callback function for UI settings.
Returns: def on_ui_settings():
None """
""" Callback function for UI settings.
# general settings
section = ("prompt-post-processor", PromptPostProcessor.NAME)
shared.opts.add_option(
key="ppp_gen_sep", info=shared.OptionInfo("<h2>General settings</h2>", "", gr.HTML, section=section)
)
shared.opts.add_option(
key="ppp_gen_debug",
info=shared.OptionInfo(
False,
label="Debug",
section=section,
),
)
shared.opts.add_option(
key="ppp_gen_ifwildcards",
info=shared.OptionInfo(
default=PromptPostProcessor.IFWILDCARDS_CHOICES["ignore"],
label="What to do with remaining wildcards?",
component=gr.Radio,
component_args={"choices": PromptPostProcessor.IFWILDCARDS_CHOICES.values()},
section=section,
),
)
# content removal settings Returns:
shared.opts.add_option( None
key="ppp_rem_sep", info=shared.OptionInfo("<br/><h2>Content removal settings</h2>", "", gr.HTML, section=section) """
)
shared.opts.add_option(
key="ppp_rem_removeextranetworktags",
info=shared.OptionInfo(
False,
label="Remove extra network tags",
section=section,
),
)
shared.opts.add_option(
key="ppp_rem_if", info=shared.OptionInfo("<p style=\"font-style:italic\">* Parsing of the 'if' commands cannot be disabled</p>", "", gr.HTML, section=section)
)
# send to negative settings section = ("prompt-post-processor", PromptPostProcessor.NAME)
shared.opts.add_option(
key="ppp_stn_sep", def import_old_settings(names, default):
info=shared.OptionInfo("<br/><h2>Send to Negative settings</h2>", "", gr.HTML, section=section), for name in names:
if hasattr(opts, name):
return getattr(opts, name)
return default
def import_bool_to_any(name, value_false, value_true, default):
if hasattr(opts, name):
return value_true if getattr(opts, name) else value_false
return default
def new_html_title(title):
info = shared.OptionInfo(
title,
"",
gr.HTML,
section=section,
) )
shared.opts.add_option( info.do_not_save = True
key="ppp_stn_doi2i", return info
info=shared.OptionInfo(
False, # general settings
label="Apply in img2img (this includes any pass that contains an initial image, like refiner, hires fix, adetailer)", shared.opts.add_option(
section=section, key="ppp_gen_sep",
info=new_html_title("<h2>General settings</h2>"),
)
shared.opts.add_option(
key="ppp_gen_debug_level",
info=shared.OptionInfo(
default=import_bool_to_any(
"ppp_gen_debug",
DEBUG_LEVEL.minimal.value,
DEBUG_LEVEL.full.value,
DEBUG_LEVEL.minimal.value,
), ),
) label="Debug level",
shared.opts.add_option( component=gr.Radio,
key="ppp_stn_separator", component_args={
info=shared.OptionInfo( "choices": (
PromptPostProcessor.DEFAULT_STN_SEPARATOR, ("None", DEBUG_LEVEL.none.value),
label="Separator used when adding to the negative prompt", ("Minimal", DEBUG_LEVEL.minimal.value),
section=section, ("Full", DEBUG_LEVEL.full.value),
),
},
section=section,
),
)
shared.opts.add_option(
key="ppp_gen_ponysubstrings",
info=shared.OptionInfo(
PromptPostProcessor.DEFAULT_PONY_SUBSTRINGS,
label="Comma separated list of substrings to look for in the model full filename to flag it as Pony (case insensitive)",
section=section,
),
)
shared.opts.add_option(
key="ppp_gen_doi2i",
info=shared.OptionInfo(
False,
label="Apply in img2img",
comment_after='<span class="info">(this includes any pass that contains an initial image, like adetailer)</span>',
section=section,
),
)
# wildcard settings
shared.opts.add_option(
key="ppp_wil_sep",
info=new_html_title('<br><h2>Wildcard settings</h2>'),
)
shared.opts.add_option(
key="ppp_wil_processwildcards",
info=shared.OptionInfo(
True,
label="Process wildcards",
section=section,
),
)
shared.opts.add_option(
key="ppp_wil_wildcardsfolders",
info=shared.OptionInfo(
PPPWildcards.DEFAULT_WILDCARDS_FOLDER,
label="Wildcards folders",
comment_after='<span class="info">(absolute or relative to the models folder)</span>',
section=section,
),
)
shared.opts.add_option(
key="ppp_wil_ifwildcards",
info=shared.OptionInfo(
default=import_old_settings(
["ppp_gen_ifwildcards", "ppp_ifwildcards"],
PromptPostProcessor.IFWILDCARDS_CHOICES.ignore.value,
), ),
) label="What to do with remaining/invalid wildcards?",
shared.opts.add_option( component=gr.Radio,
key="ppp_stn_ignorerepeats", component_args={
info=shared.OptionInfo( "choices": (
True, ("Ignore", PromptPostProcessor.IFWILDCARDS_CHOICES.ignore.value),
label="Ignore repeated content", ("Remove", PromptPostProcessor.IFWILDCARDS_CHOICES.remove.value),
section=section, ("Add visible warning", PromptPostProcessor.IFWILDCARDS_CHOICES.warn.value),
), ("Stop the generation", PromptPostProcessor.IFWILDCARDS_CHOICES.stop.value),
) )
shared.opts.add_option( },
key="ppp_stn_joinattention", section=section,
info=shared.OptionInfo( ),
True, )
label="Join attention modifiers (weights) when possible", shared.opts.add_option(
section=section, key="ppp_wil_choice_separator",
), info=shared.OptionInfo(
) PromptPostProcessor.DEFAULT_CHOICE_SEPARATOR,
# clean-up settings label="Default separator used when adding multiple choices",
shared.opts.add_option( section=section,
key="ppp_cup_sep", info=shared.OptionInfo("<br/><h2>Clean-up settings</h2>", "", gr.HTML, section=section) ),
) )
shared.opts.add_option( shared.opts.add_option(
key="ppp_cup_doi2i", key="ppp_wil_keep_choices_order",
info=shared.OptionInfo( info=shared.OptionInfo(
False, False,
label="Apply in img2img (this includes any pass that contains an initial image, like refiner, hires fix, adetailer)", label="Keep the order of selected choices",
section=section, section=section,
), ),
) )
shared.opts.add_option(
key="ppp_cup_emptyconstructs", # content removal settings
info=shared.OptionInfo( shared.opts.add_option(
True, key="ppp_rem_sep",
label="Remove empty constructs (attention, alternation, scheduling)", info=new_html_title('<br><h2>Content removal settings</h2>'),
section=section, )
), shared.opts.add_option(
) key="ppp_rem_removeextranetworktags",
shared.opts.add_option( info=shared.OptionInfo(
key="ppp_cup_extraseparators", False,
info=shared.OptionInfo( label="Remove extra network tags",
True, section=section,
label="Remove extra separators", ),
section=section, )
),
) # send to negative settings
shared.opts.add_option( shared.opts.add_option(
key="ppp_cup_extraseparators2", key="ppp_stn_sep",
info=shared.OptionInfo( info=new_html_title('<br><h2>Send to Negative settings</h2>'),
True, )
label="Remove additional extra separators", shared.opts.add_option(
section=section, key="ppp_stn_separator",
), info=shared.OptionInfo(
) PromptPostProcessor.DEFAULT_STN_SEPARATOR,
shared.opts.add_option( label="Separator used when adding to the negative prompt",
key="ppp_cup_breaks", section=section,
info=shared.OptionInfo( ),
True, )
label="Clean up around BREAKs", shared.opts.add_option(
section=section, key="ppp_stn_ignorerepeats",
), info=shared.OptionInfo(
) True,
shared.opts.add_option( label="Ignore repeated content",
key="ppp_cup_breaks_eol", section=section,
info=shared.OptionInfo( ),
False, )
label="Use EOL instead of Space before BREAKs", shared.opts.add_option(
section=section, key="ppp_stn_joinattention",
), info=shared.OptionInfo(
) True,
shared.opts.add_option( label="Join attention modifiers (weights) when possible",
key="ppp_cup_ands", section=section,
info=shared.OptionInfo( ),
True, )
label="Clean up around ANDs", # clean-up settings
section=section, shared.opts.add_option(
), key="ppp_cup_sep",
) info=new_html_title('<br><h2>Clean-up settings</h2>'),
shared.opts.add_option( )
key="ppp_cup_ands_eol", shared.opts.add_option(
info=shared.OptionInfo( key="ppp_cup_emptyconstructs",
False, info=shared.OptionInfo(
label="Use EOL instead of Space before ANDs", True,
section=section, label="Remove empty constructs (attention, alternation, scheduling)",
), section=section,
) ),
shared.opts.add_option( )
key="ppp_cup_extranetworktags", shared.opts.add_option(
info=shared.OptionInfo( key="ppp_cup_extraseparators",
False, info=shared.OptionInfo(
label="Clean up around extra network tags", True,
section=section, label="Remove extra separators",
), section=section,
) ),
shared.opts.add_option( )
key="ppp_cup_extraspaces", shared.opts.add_option(
info=shared.OptionInfo( key="ppp_cup_extraseparators2",
True, info=shared.OptionInfo(
label="Remove extra spaces", True,
section=section, label="Remove additional extra separators",
), section=section,
) ),
)
shared.opts.add_option(
key="ppp_cup_breaks",
info=shared.OptionInfo(
True,
label="Clean up around BREAKs",
section=section,
),
)
shared.opts.add_option(
key="ppp_cup_breaks_eol",
info=shared.OptionInfo(
False,
label="Use EOL instead of Space before BREAKs",
section=section,
),
)
shared.opts.add_option(
key="ppp_cup_ands",
info=shared.OptionInfo(
True,
label="Clean up around ANDs",
section=section,
),
)
shared.opts.add_option(
key="ppp_cup_ands_eol",
info=shared.OptionInfo(
False,
label="Use EOL instead of Space before ANDs",
section=section,
),
)
shared.opts.add_option(
key="ppp_cup_extranetworktags",
info=shared.OptionInfo(
False,
label="Clean up around extra network tags",
section=section,
),
)
shared.opts.add_option(
key="ppp_cup_extraspaces",
info=shared.OptionInfo(
True,
label="Remove extra spaces",
section=section,
),
)
# Remove old settings
# for name in ["ppp_gen_ifwildcards", "ppp_ifwildcards", "ppp_gen_debug", "ppp_stn_doi2i", "ppp_cup_doi2i"]:
# if hasattr(opts, name):
# delattr(opts, name)
script_callbacks.on_ui_settings(on_ui_settings)
+601 -277
View File
@@ -1,27 +1,18 @@
from collections import namedtuple
import logging import logging
import unittest import unittest
import sys import sys
import os import os
sys.path.insert(1, os.path.join(sys.path[0], "..")) from ppp_wildcards import PPPWildcards
sys.path.append(os.path.join(sys.path[0], ".."))
from ppp import PromptPostProcessor from ppp import PromptPostProcessor
from ppp_logging import PromptPostProcessorLogFactory from ppp_logging import DEBUG_LEVEL, PromptPostProcessorLogFactory
class DictToObj: # pylint: disable=too-few-public-methods PromptPair = namedtuple("PromptPair", ["prompt", "negative_prompt"], defaults=["", ""])
"""
Converts a dictionary to an object with attribute access.
from https://joelmccune.com/python-dictionary-as-object/
"""
def __init__(self, in_dict: dict):
assert isinstance(in_dict, dict)
for key, val in in_dict.items():
if isinstance(val, (list, tuple)):
setattr(self, key, [DictToObj(x) if isinstance(x, dict) else x for x in val])
else:
setattr(self, key, DictToObj(val) if isinstance(val, dict) else val)
class TestPromptPostProcessor(unittest.TestCase): class TestPromptPostProcessor(unittest.TestCase):
@@ -34,334 +25,667 @@ class TestPromptPostProcessor(unittest.TestCase):
Set up the test case by initializing the necessary objects and configurations. Set up the test case by initializing the necessary objects and configurations.
""" """
lf = PromptPostProcessorLogFactory() lf = PromptPostProcessorLogFactory()
self.ppp_logger = lf.log self.__ppp_logger = lf.log
self.ppp_logger.setLevel(logging.DEBUG) self.__ppp_logger.setLevel(logging.DEBUG)
self.__defopts = DictToObj( self.__defopts = {
{ "debug_level": DEBUG_LEVEL.full.value,
"ppp_gen_debug": True, "pony_substrings": PromptPostProcessor.DEFAULT_PONY_SUBSTRINGS,
"ppp_gen_ifwildcards": PromptPostProcessor.IFWILDCARDS_CHOICES["ignore"], "process_wildcards": True,
"ppp_stn_doi2i": False, "if_wildcards": PromptPostProcessor.IFWILDCARDS_CHOICES.ignore.value,
"ppp_stn_separator": ", ", "choice_separator": ", ",
"ppp_stn_ignore_repeats": True, "keep_choices_order": False,
"ppp_stn_join_attention": True, "stn_separator": ", ",
"ppp_cup_doi2i": False, "stn_ignore_repeats": True,
"ppp_cup_emptyconstructs": True, "stn_join_attention": True,
"ppp_cup_extraseparators": True, "cleanup_empty_constructs": True,
"ppp_cup_extraseparators2": True, "cleanup_extra_separators": True,
"ppp_cup_extraspaces": True, "cleanup_extra_separators2": True,
"ppp_cup_breaks": True, "cleanup_extra_spaces": True,
"ppp_cup_breaks_eol": False, "cleanup_breaks": True,
"ppp_cup_ands": True, "cleanup_breaks_eol": False,
"ppp_cup_ands_eol": False, "cleanup_ands": True,
"ppp_cup_extranetworktags": True, "cleanup_ands_eol": False,
"ppp_rem_removeextranetworktags": False, "cleanup_extranetwork_tags": True,
} "remove_extranetwork_tags": False,
}
self.__def_model_info = {
"is_sd1": False,
"is_sd2": False,
"is_sdxl": True,
"is_ssd": False,
"is_sd3": False,
"is_flux": False,
"models_path": "./webui/models",
"model_filename": "./webui/models/Stable-diffusion/testmodel.safetensors",
}
self.__interrupted = False
self.__wildcards_obj = PPPWildcards(lf.log)
self.__wildcards_obj.refresh_wildcards(
DEBUG_LEVEL.full,
[
os.path.abspath(os.path.join(os.path.dirname(__file__), "wildcards")),
os.path.abspath(os.path.join(os.path.dirname(__file__), "wildcards2")),
],
) )
self.__nocupopts = DictToObj( grammar_filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), "../grammar.lark")
with open(grammar_filename, "r", encoding="utf-8") as file:
self.__grammar_content = file.read()
self.__defppp = PromptPostProcessor(
self.__ppp_logger,
self.__interrupt,
self.__def_model_info,
self.__defopts,
self.__grammar_content,
self.__wildcards_obj,
)
self.__nocupppp = PromptPostProcessor(
self.__ppp_logger,
self.__interrupt,
self.__def_model_info,
{ {
"ppp_gen_debug": True, **self.__defopts,
"ppp_gen_ifwildcards": PromptPostProcessor.IFWILDCARDS_CHOICES["ignore"], "cleanup_empty_constructs": False,
"ppp_stn_doi2i": False, "cleanup_extra_separators": False,
"ppp_stn_separator": ", ", "cleanup_extra_separators2": False,
"ppp_stn_ignore_repeats": True, "cleanup_extra_spaces": False,
"ppp_stn_join_attention": True, "cleanup_breaks": False,
"ppp_cup_doi2i": False, "cleanup_breaks_eol": False,
"ppp_cup_emptyconstructs": False, "cleanup_ands": False,
"ppp_cup_extraseparators": False, "cleanup_ands_eol": False,
"ppp_cup_extraseparators2": False, "cleanup_extranetwork_tags": False,
"ppp_cup_extraspaces": False, },
"ppp_cup_breaks": False, self.__grammar_content,
"ppp_cup_breaks_eol": False, self.__wildcards_obj,
"ppp_cup_ands": False,
"ppp_cup_ands_eol": False,
"ppp_cup_extranetworktags": False,
"ppp_rem_removeextranetworktags": False,
}
) )
self.__defprocessing = DictToObj({"sd_model": DictToObj({"is_sd1": False, "is_sd2": False, "is_sdxl": True})})
self.__defstate = None
self.defppp = PromptPostProcessor(self, self.__defprocessing, self.__defstate, self.__defopts)
self.nocupppp = PromptPostProcessor(self, self.__defprocessing, self.__defstate, self.__nocupopts)
def process( def __interrupt(self):
self.__interrupted = True
def __process(
self, self,
prompt, input_prompts: PromptPair,
negative_prompt, expected_output_prompts: PromptPair | list[PromptPair],
expected_prompt, seed: int = 1,
expected_negative_prompt,
ppp=None, ppp=None,
interrupted=False,
): ):
""" """
Process the prompt and compare the results with the expected prompts. Process the prompt and compare the results with the expected prompts.
Args: Args:
prompt (str): The input prompt. input_prompts (PromptPair): The input prompts.
negative_prompt (str): The input negative prompt. expected_output_prompts (PromptPair | list[PromptPair]): The expected prompts.
expected_prompt (str): The expected output prompt. seed (int, optional): The seed value. Defaults to 1.
expected_negative_prompt (str): The expected output negative prompt.
ppp (object, optional): The post-processor object. Defaults to None. ppp (object, optional): The post-processor object. Defaults to None.
interrupted (bool, optional): The interrupted flag. Defaults to False.
Returns: Returns:
None None
""" """
the_obj = self.defppp if ppp is None else ppp the_obj = ppp or self.__defppp
result_prompt, result_negative_prompt = the_obj.process_prompt(prompt, negative_prompt) out = expected_output_prompts if isinstance(expected_output_prompts, list) else [expected_output_prompts]
self.assertEqual(result_prompt, expected_prompt, f"Prompt should be '{expected_prompt}'") for eo in out:
self.assertEqual( result_prompt, result_negative_prompt = the_obj.process_prompt(
result_negative_prompt, input_prompts.prompt,
expected_negative_prompt, input_prompts.negative_prompt,
f"Negative Prompt should be '{expected_negative_prompt}'", seed,
) )
self.assertEqual(self.__interrupted, interrupted, "Interrupted flag is incorrect")
if not self.__interrupted:
self.assertEqual(result_prompt, eo.prompt, "Incorrect prompt")
self.assertEqual(result_negative_prompt, eo.negative_prompt, "Incorrect negative prompt")
seed += 1
# Send To Negative tests # Send To Negative tests
def test_nt_simple_oldformat(self): # negtags with different parameters and separations def test_stn_simple(self): # negtags with different parameters and separations
self.process( self.__process(
"flowers<!red!>, <!!s!green!>, <!!e!blue!><!!p0!yellow!>, <!!p1!purple!><!!p2!black!>", PromptPair(
"<!!i0!!>normal quality<!!i1!!>, worse quality<!!i2!!>", "flowers<ppp:stn>red<ppp:/stn>, <ppp:stn s>green<ppp:/stn>, <ppp:stn e>blue<ppp:/stn><ppp:stn p0>yellow<ppp:/stn>, <ppp:stn p1>purple<ppp:/stn><ppp:stn p2>black<ppp:/stn>",
"flowers", "<ppp:stn i0>normal quality<ppp:stn i1>, worse quality<ppp:stn i2>",
"red, green, yellow, normal quality, purple, worse quality, black, blue", ),
PromptPair("flowers", "red, green, yellow, normal quality, purple, worse quality, black, blue"),
) )
def test_nt_simple(self): # negtags with different parameters and separations def test_stn_complex(self): # complex negtags
self.process( self.__process(
"flowers<ppp:stn>red<ppp:/stn>, <ppp:stn s>green<ppp:/stn>, <ppp:stn e>blue<ppp:/stn><ppp:stn p0>yellow<ppp:/stn>, <ppp:stn p1>purple<ppp:/stn><ppp:stn p2>black<ppp:/stn>", PromptPair(
"<ppp:stn i0>normal quality<ppp:stn i1>, worse quality<ppp:stn i2>", "<ppp:stn>red<ppp:/stn> ((<ppp:stn s>pink<ppp:/stn>)), flowers <ppp:stn e>purple<ppp:/stn>, <ppp:stn p0>mauve<ppp:/stn><ppp:stn e>blue<ppp:/stn>, <ppp:stn p0>yellow<ppp:/stn> <ppp:stn p1>green<ppp:/stn>",
"flowers", "normal quality, <ppp:stn i0>, bad quality<ppp:stn i1>, worse quality",
"red, green, yellow, normal quality, purple, worse quality, black, blue", ),
) PromptPair(
"flowers",
def test_nt_complex(self): # complex negtags "red, (pink:1.21), normal quality, mauve, yellow, bad quality, green, worse quality, purple, blue",
self.process(
"<ppp:stn>red<ppp:/stn> ((<ppp:stn s>pink<ppp:/stn>)), flowers <ppp:stn e>purple<ppp:/stn>, <ppp:stn p0>mauve<ppp:/stn><ppp:stn e>blue<ppp:/stn>, <ppp:stn p0>yellow<ppp:/stn> <ppp:stn p1>green<ppp:/stn>",
"normal quality, <ppp:stn i0>, bad quality<ppp:stn i1>, worse quality",
"flowers",
"red, (pink:1.21), normal quality, mauve, yellow, bad quality, green, worse quality, purple, blue",
)
def test_nt_complex_nocleanup(self): # complex negtags with no cleanup
self.process(
"<ppp:stn>red<ppp:/stn> ((<ppp:stn s>pink<ppp:/stn>)), flowers <ppp:stn e>purple<ppp:/stn>, <ppp:stn p0>mauve<ppp:/stn><ppp:stn e>blue<ppp:/stn>, <ppp:stn p0>yellow<ppp:/stn> <ppp:stn p1>green<ppp:/stn>",
"normal quality, <ppp:stn i0>, bad quality<ppp:stn i1>, worse quality",
" (()), flowers , , ",
"red, (pink:1.21), normal quality, mauve, yellow, bad quality, green, worse quality, purple, blue",
self.nocupppp,
)
def test_nt_inside_attention(self): # negtag inside attention
self.process(
"[<ppp:stn>neg1<ppp:/stn>] this is a ((test<ppp:stn e>neg2<ppp:/stn>) (test:2.0): 1.5 ) (red<ppp:stn>[square]<ppp:/stn>:1.5)",
"normal quality",
"this is a ((test) (test:2.0):1.5) (red:1.5)",
"[neg1], ([square]:1.5), normal quality, (neg2:1.65)",
)
def test_nt_inside_alternation(self): # negtag inside alternation
self.process(
"this is a (([complex<ppp:stn>neg1<ppp:/stn>|simple<ppp:stn>neg2<ppp:/stn>|regular<ppp:stn>neg3<ppp:/stn>] test)(test:2.0):1.5)",
"normal quality",
"this is a (([complex|simple|regular] test)(test:2.0):1.5)",
"([neg1||]:1.65), ([|neg2|]:1.65), ([||neg3]:1.65), normal quality",
)
def test_nt_inside_alternation_recursive(self): # negtag inside alternation (recursive alternation)
self.process(
"this is a (([complex<ppp:stn>neg1<ppp:/stn>[one|two<ppp:stn>neg12<ppp:/stn>||three|four(<ppp:stn>neg14<ppp:/stn>)]|simple<ppp:stn>neg2<ppp:/stn>|regular<ppp:stn>neg3<ppp:/stn>] test)(test:2.0):1.5)",
"normal quality",
"this is a (([complex[one|two||three|four]|simple|regular] test)(test:2.0):1.5)",
"([neg1||]:1.65), ([[|neg12|||]||]:1.65), ([[||||(neg14)]||]:1.65), ([|neg2|]:1.65), ([||neg3]:1.65), normal quality",
)
def test_nt_inside_scheduling(self): # negtag inside scheduling
self.process(
"this is [abc<ppp:stn>neg1<ppp:/stn>:def<ppp:stn e>neg2<ppp:/stn>: 5 ]",
"normal quality",
"this is [abc:def:5]",
"[neg1::5], normal quality, [neg2:5]",
)
def test_nt_complex_features(self): # complex negtags with AND, BREAK and other features
self.process(
"[<ppp:stn>neg5<ppp:/stn>] this \\(is\\): a (([complex|simple<ppp:stn>neg6<ppp:/stn>|regular] test<ppp:stn>neg1<ppp:/stn>)(test:2.0):1.5) \nBREAK, BREAK with [abc<ppp:stn>neg4<ppp:/stn>:def<ppp:stn p0>neg2(neg3:1.6)<ppp:/stn>:5]:0.5 AND loraword <lora:xxx:1> AND AND hypernetword <hypernet:yyy>:0.3",
"normal quality, <ppp:stn i0>",
"this \\(is\\): a (([complex|simple|regular] test)(test:2.0):1.5) \nBREAK with [abc:def:5]:0.5 AND loraword <lora:xxx:1> AND hypernetword <hypernet:yyy>:0.3",
"[neg5], ([|neg6|]:1.65), (neg1:1.65), [neg4::5], normal quality, [neg2(neg3:1.6):5]",
)
def test_nt_complex_features_newformat(self): # complex negtags with AND, BREAK and other features (new format)
self.process(
"[<ppp:stn>neg5<ppp:/stn>] this \\(is\\): a (([complex|simple<ppp:stn>neg6<ppp:/stn>|regular] test<ppp:stn>neg1<ppp:/stn>)(test:2.0):1.5) \nBREAK, BREAK with [abc<ppp:stn>neg4<ppp:/stn>:def<ppp:stn p0>neg2(neg3:1.6)<ppp:/stn>:5]:0.5 AND loraword <lora:xxx:1> AND AND hypernetword <hypernet:yyy>:0.3",
"normal quality, <ppp:stn i0>",
"this \\(is\\): a (([complex|simple|regular] test)(test:2.0):1.5) \nBREAK with [abc:def:5]:0.5 AND loraword <lora:xxx:1> AND hypernetword <hypernet:yyy>:0.3",
"[neg5], ([|neg6|]:1.65), (neg1:1.65), [neg4::5], normal quality, [neg2(neg3:1.6):5]",
)
# Wildcard tests
def test_wc_ignore(self): # wildcards with ignore option
self.process(
"__bad_wildcard__",
"{option1|option2}",
"__bad_wildcard__",
"{option1|option2}",
PromptPostProcessor(
self,
self.__defprocessing,
self.__defstate,
DictToObj(
{
**self.__defopts.__dict__,
"ppp_gen_ifwildcards": PromptPostProcessor.IFWILDCARDS_CHOICES["ignore"],
}
),
), ),
) )
def test_wc_remove(self): # wildcards with remove option def test_stn_complex_nocleanup(self): # complex negtags with no cleanup
self.process( self.__process(
"[<ppp:stn>neg5<ppp:/stn>] this is: __bad_wildcard__ a (([complex|simple<ppp:stn>neg6<ppp:/stn>|regular] test<ppp:stn>neg1<ppp:/stn>)(test:2.0):1.5) \nBREAK, BREAK with [abc<ppp:stn>neg4<ppp:/stn>:def<ppp:stn p0>neg2(neg3:1.6)<ppp:/stn>:5] <lora:xxx:1>", PromptPair(
"normal quality, <ppp:stn i0> {option1|option2}", "<ppp:stn>red<ppp:/stn> ((<ppp:stn s>pink<ppp:/stn>)), flowers <ppp:stn e>purple<ppp:/stn>, <ppp:stn p0>mauve<ppp:/stn><ppp:stn e>blue<ppp:/stn>, <ppp:stn p0>yellow<ppp:/stn> <ppp:stn p1>green<ppp:/stn>",
"this is: a (([complex|simple|regular] test)(test:2.0):1.5) \nBREAK with [abc:def:5]<lora:xxx:1>", "normal quality, <ppp:stn i0>, bad quality<ppp:stn i1>, worse quality",
"[neg5], ([|neg6|]:1.65), (neg1:1.65), [neg4::5], normal quality, [neg2(neg3:1.6):5]", ),
PromptPostProcessor( PromptPair(
self, " (()), flowers , , ",
self.__defprocessing, "red, (pink:1.21), normal quality, mauve, yellow, bad quality, green, worse quality, purple, blue",
self.__defstate, ),
DictToObj( ppp=self.__nocupppp,
{ )
**self.__defopts.__dict__,
"ppp_gen_ifwildcards": PromptPostProcessor.IFWILDCARDS_CHOICES["remove"], def test_stn_inside_attention(self): # negtag inside attention
} self.__process(
), PromptPair(
"[<ppp:stn>neg1<ppp:/stn>] this is a ((test<ppp:stn e>neg2<ppp:/stn>) (test:2.0): 1.5 ) (red<ppp:stn>[square]<ppp:/stn>:1.5)",
"normal quality",
),
PromptPair(
"this is a ((test) (test:2.0):1.5) (red:1.5)", "[neg1], ([square]:1.5), normal quality, (neg2:1.65)"
), ),
) )
def test_wc_warn(self): # wildcards with warn option def test_stn_inside_alternation(self): # negtag inside alternation
self.process( self.__process(
"__bad_wildcard__", PromptPair(
"{option1|option2}", "this is a (([complex<ppp:stn>neg1<ppp:/stn>|simple<ppp:stn>neg2<ppp:/stn>|regular<ppp:stn>neg3<ppp:/stn>] test)(test:2.0):1.5)",
PromptPostProcessor.WILDCARD_WARNING + "__bad_wildcard__", "normal quality",
"{option1|option2}", ),
PromptPostProcessor( PromptPair(
self, "this is a (([complex|simple|regular] test)(test:2.0):1.5)",
self.__defprocessing, "([neg1||]:1.65), ([|neg2|]:1.65), ([||neg3]:1.65), normal quality",
self.__defstate,
DictToObj(
{
**self.__defopts.__dict__,
"ppp_gen_ifwildcards": PromptPostProcessor.IFWILDCARDS_CHOICES["warn"],
}
),
), ),
) )
def test_wc_stop(self): # wildcards with stop option def test_stn_inside_alternation_recursive(self): # negtag inside alternation (recursive alternation)
self.process( self.__process(
"__bad_wildcard__", PromptPair(
"{option1|option2}", "this is a (([complex<ppp:stn>neg1<ppp:/stn>[one|two<ppp:stn>neg12<ppp:/stn>||three|four(<ppp:stn>neg14<ppp:/stn>)]|simple<ppp:stn>neg2<ppp:/stn>|regular<ppp:stn>neg3<ppp:/stn>] test)(test:2.0):1.5)",
PromptPostProcessor.WILDCARD_STOP + "__bad_wildcard__", "normal quality",
PromptPostProcessor.WILDCARD_STOP + "{option1|option2}", ),
PromptPostProcessor( PromptPair(
self, "this is a (([complex[one|two||three|four]|simple|regular] test)(test:2.0):1.5)",
self.__defprocessing, "([neg1||]:1.65), ([[|neg12|||]||]:1.65), ([[||||(neg14)]||]:1.65), ([|neg2|]:1.65), ([||neg3]:1.65), normal quality",
self.__defstate, ),
DictToObj( )
{
**self.__defopts.__dict__, def test_stn_inside_scheduling(self): # negtag inside scheduling
"ppp_gen_ifwildcards": PromptPostProcessor.IFWILDCARDS_CHOICES["stop"], self.__process(
} PromptPair("this is [abc<ppp:stn>neg1<ppp:/stn>:def<ppp:stn e>neg2<ppp:/stn>: 5 ]", "normal quality"),
), [PromptPair("this is [abc:def:5]", "[neg1::5], normal quality, [neg2:5]")],
)
def test_stn_complex_features(self): # complex negtags with AND, BREAK and other features
self.__process(
PromptPair(
"[<ppp:stn>neg5<ppp:/stn>] this \\(is\\): a (([complex|simple<ppp:stn>neg6<ppp:/stn>|regular] test<ppp:stn>neg1<ppp:/stn>)(test:2.0):1.5) \nBREAK, BREAK with [abc<ppp:stn>neg4<ppp:/stn>:def<ppp:stn p0>neg2(neg3:1.6)<ppp:/stn>:5]:0.5 AND loratrigger <lora:xxx:1> AND AND hypernettrigger <hypernet:yyy>:0.3",
"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",
"[neg5], ([|neg6|]:1.65), (neg1:1.65), [neg4::5], normal quality, [neg2(neg3:1.6):5]",
),
)
def test_stn_complex_features_newformat(self): # complex negtags with AND, BREAK and other features (new format)
self.__process(
PromptPair(
"[<ppp:stn>neg5<ppp:/stn>] this \\(is\\): a (([complex|simple<ppp:stn>neg6<ppp:/stn>|regular] test<ppp:stn>neg1<ppp:/stn>)(test:2.0):1.5) \nBREAK, BREAK with [abc<ppp:stn>neg4<ppp:/stn>:def<ppp:stn p0>neg2(neg3:1.6)<ppp:/stn>:5]:0.5 AND loratrigger <lora:xxx:1> AND AND hypernettrigger <hypernet:yyy>:0.3",
"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",
"[neg5], ([|neg6|]:1.65), (neg1:1.65), [neg4::5], normal quality, [neg2(neg3:1.6):5]",
), ),
) )
# Cleanup tests # Cleanup tests
def test_cl_simple(self): # simple cleanup def test_cl_simple(self): # simple cleanup
self.process( self.__process(
" this is a ((test ), , () [] ( , test ,:2.0):1.5) (red:1.5) ", PromptPair(" this is a ((test ), , , (), , [] ( , test ,:2.0):1.5) (red:1.5) ", " normal quality "),
" normal quality ", PromptPair("this is a ((test), (test,:2.0):1.5) (red:1.5)", "normal quality"),
"this is a ((test), (test,:2.0):1.5) (red:1.5)",
"normal quality",
) )
def test_cl_complex(self): # complex cleanup def test_cl_complex(self): # complex cleanup
self.process( self.__process(
" this is BREAKABLE a ((test), ,AND AND() [] <lora:test> ANDERSON (test:2.0):1.5) :o BREAK \n BREAK (red:1.5) ", PromptPair(
" [:hands, feet, :0.15]normal quality ", " this is BREAKABLE a ((test)), ,AND AND(() [] <lora:test> ANDERSON (test:2.0):1.5) :o BREAK \n BREAK (red:1.5) ",
"this is BREAKABLE a ((test) AND <lora:test> ANDERSON (test:2.0):1.5) :o BREAK (red:1.5)", " [:hands, feet, :0.15]normal quality ",
"[: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)",
"[:hands, feet, :0.15]normal quality",
),
) )
def test_cl_removenetworktags(self): # remove network tags def test_cl_removenetworktags(self): # remove network tags
self.process( self.__process(
"this is a <lora:test> test", PromptPair("this is a <lora:test> test", ""),
"", PromptPair("this is a test", ""),
"this is a test", ppp=PromptPostProcessor(
"", self.__ppp_logger,
PromptPostProcessor( self.__interrupt,
self, self.__def_model_info,
self.__defprocessing, {**self.__defopts, "remove_extranetwork_tags": True},
self.__defstate,
DictToObj({**self.__defopts.__dict__, "ppp_rem_removeextranetworktags": True}),
), ),
) )
def test_cl_dontremoveseparatorsoneol(self): # dont remove separators on eol def test_cl_dontremoveseparatorsoneol(self): # dont remove separators on eol
self.process( self.__process(
"this is a test,\nsecond line", PromptPair("this is a test,\nsecond line", ""),
"", PromptPair("this is a test,\nsecond line", ""),
"this is a test,\nsecond line", ppp=PromptPostProcessor(
"", self.__ppp_logger,
PromptPostProcessor( self.__interrupt,
self, self.__def_model_info,
self.__defprocessing, {**self.__defopts, "cleanup_extra_separators2": False},
self.__defstate,
DictToObj({**self.__defopts.__dict__, "ppp_cup_extraseparators2": False}),
), ),
) )
# Command tests # Command tests
def test_cmd_stn_complex_features(self): # complex stn command with AND, BREAK and other features def test_cmd_stn_complex_features(self): # complex stn command with AND, BREAK and other features
self.process( self.__process(
"[<ppp:stn>neg5<ppp:/stn>] this \\(is\\): a (([complex|simple<ppp:stn>neg6<ppp:/stn>|regular] test<ppp:stn>neg1<ppp:/stn>)(test:2.0):1.5) \nBREAK, BREAK with [abc<ppp:stn>neg4<ppp:/stn>:def<ppp:stn p0>neg2(neg3:1.6)<ppp:/stn>:5]:0.5 AND loraword <lora:xxx:1> AND AND hypernetword <hypernet:yyy>:0.3", PromptPair(
"normal quality, <ppp:stn i0>", "[<ppp:stn>neg5<ppp:/stn>] this \\(is\\): a (([complex|simple<ppp:stn>neg6<ppp:/stn>|regular] test<ppp:stn>neg1<ppp:/stn>)(test:2.0):1.5) \nBREAK, BREAK with [abc<ppp:stn>neg4<ppp:/stn>:def<ppp:stn p0>neg2(neg3:1.6)<ppp:/stn>:5]:0.5 AND loratrigger <lora:xxx:1> AND AND hypernettrigger <hypernet:yyy>:0.3",
"this \\(is\\): a (([complex|simple|regular] test)(test:2.0):1.5) \nBREAK with [abc:def:5]:0.5 AND loraword <lora:xxx:1> AND hypernetword <hypernet:yyy>:0.3", "normal quality, <ppp:stn i0>",
"[neg5], ([|neg6|]:1.65), (neg1:1.65), [neg4::5], normal quality, [neg2(neg3:1.6):5]", ),
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",
"[neg5], ([|neg6|]:1.65), (neg1:1.65), [neg4::5], normal quality, [neg2(neg3:1.6):5]",
),
) )
def test_cmd_if_complex_features(self): # complex if command def test_cmd_if_complex_features(self): # complex if command
self.process( self.__process(
"this \\(is\\): a (([complex|simple|regular] test)(test:2.0):1.5) \nBREAK, BREAK <ppp:if _sd eq 'sd1'>with [abc<ppp:stn>neg4<ppp:/stn>:def:5]<ppp:/if>:0.5 AND <ppp:if _sd eq 'sd1'>loraword <lora:xxx:1><ppp:elif _sd eq 'sdxl'>hypernetword <hypernet:yyy><ppp:else>nothing<ppp:/if>:0.3", PromptPair(
"normal quality", "this \\(is\\): a (([complex|simple|regular] test)(test:2.0):1.5) \nBREAK, BREAK <ppp:if _is_sd1>with [abc<ppp:stn>neg4<ppp:/stn>:def:5]<ppp:/if>:0.5 AND <ppp:if _is_sd1>loratrigger <lora:xxx:1><ppp:elif _is_sdxl>hypernettrigger <hypernet:yyy><ppp:else>nothing<ppp:/if>:0.3",
"this \\(is\\): a (([complex|simple|regular] test)(test:2.0):1.5) \nBREAK :0.5 AND hypernetword <hypernet:yyy>:0.3", "normal quality",
"normal quality", ),
PromptPair(
"this \\(is\\): a (([complex|simple|regular] test)(test:2.0):1.5)\nBREAK :0.5 AND hypernettrigger <hypernet:yyy>:0.3",
"normal quality",
),
) )
def test_cmd_if_nested(self): # nested if command def test_cmd_if_nested(self): # nested if command
self.process( self.__process(
"this is <ppp:if _sd eq 'sd1'>SD1<ppp:else><ppp:if _sd eq 'sdxl'>SDXL<ppp:else>SD2<ppp:/if><ppp:/if>", PromptPair(
"", "this is <ppp:if _sd eq 'sd1'>SD1<ppp:else><ppp:if _is_pony>PONY<ppp:else>SD2<ppp:/if><ppp:/if>", ""
"this is SDXL", ),
"", PromptPair("this is PONY", ""),
ppp=PromptPostProcessor(
self.__ppp_logger,
self.__interrupt,
{
**self.__def_model_info,
"model_filename": "./webui/models/Stable-diffusion/ponymodel.safetensors",
},
self.__defopts,
),
) )
def test_cmd_set_if(self): # set and if commands def test_cmd_set_if(self): # set and if commands
self.process( self.__process(
"<ppp:set v>value<ppp:/set>this test is <ppp:if v eq 'value'>OK<ppp:else>not OK<ppp:/if>", PromptPair("<ppp:set v>value<ppp:/set>this test is <ppp:if v>OK<ppp:else>not OK<ppp:/if>", ""),
"", PromptPair("this test is OK", ""),
"this test is OK", )
"",
def test_cmd_set_eval_if(self): # set and if commands
self.__process(
PromptPair("<ppp:set v evaluate>value<ppp:/set>this test is <ppp:if v>OK<ppp:else>not OK<ppp:/if>", ""),
PromptPair("this test is OK", ""),
) )
def test_cmd_set_if_echo_nested(self): # nested set, if and echo commands def test_cmd_set_if_echo_nested(self): # nested set, if and echo commands
self.process( self.__process(
"<ppp:set v1>1<ppp:/set><ppp:if v1 gt 0><ppp:set v2>OK<ppp:/set><ppp:/if><ppp:if v2 eq 'OK'><ppp:echo v2><ppp:else>not OK<ppp:/if>", PromptPair(
"", "<ppp:set v1>1<ppp:/set><ppp:if v1 gt 0><ppp:set v2>OK<ppp:/set><ppp:/if><ppp:if v2 eq 'OK'><ppp:echo v2><ppp:else>not OK<ppp:/if> <ppp:echo v2>NOK<ppp:/echo> <ppp:echo v3>OK<ppp:/echo>",
"OK", "",
"", ),
PromptPair("OK OK OK", ""),
) )
def test_cmd_set_if2(self): # set and more complex if commands
self.__process(
PromptPair(
"First: <ppp:set v>value1<ppp:/set>this test is <ppp:if v in ('value1','value2')>OK<ppp:else>not OK<ppp:/if>\nSecond: <ppp:set v2>value3<ppp:/set>this test is <ppp:if not v2 in ('value1','value2')>OK<ppp:else>not OK<ppp:/if>",
"",
),
PromptPair("First: this test is OK\nSecond: this test is OK", ""),
)
def test_cmd_set_add_if(self): # set, add and if commands
self.__process(
PromptPair(
"<ppp:set v>value<ppp:/set><ppp:set v add>2<ppp:/set>this test is <ppp:if v eq 'value2'>OK<ppp:else>not OK<ppp:/if>",
"",
),
PromptPair("this test is OK", ""),
)
def test_cmd_set_add_DP_if(self): # set, add (DP format) and if commands
self.__process(
PromptPair(
"${v=value}${v+=2}this test is <ppp:if v eq 'value2'>OK<ppp:else>not OK<ppp:/if>",
"",
),
PromptPair("this test is OK", ""),
)
def test_cmd_set_immediateeval(self): # set (DP format) with mixed evaluation
self.__process(
PromptPair(
"${var=!__yaml/wildcard1__}the choices are: ${var}, ${var}, ${var2:default}, ${var3=__yaml/wildcard1__}${var3}, ${var3}",
"",
),
PromptPair("the choices are: choice2, choice2, default, choice3, choice1", ""),
ppp=self.__nocupppp,
)
def test_cmd_set_mixeval(self): # set and add (DP format) with mixed evaluation
self.__process(
PromptPair(
"${var=__yaml/wildcard1__}the choices are: ${var}, ${var}, ${var+=, __yaml/wildcard2__}${var}, ${var}, ${var+=!, __yaml/wildcard3__}${var}, ${var}",
"",
),
PromptPair(
"the choices are: choice2, choice3, choice1, choice1- choice2 -choice3, choice2, choice2 -choice1-choice3, choice2, choice3-choice1- choice2 , choice1, choice2 , choice2, choice3-choice1- choice2 , choice1, choice2 ",
"",
),
ppp=self.__nocupppp,
)
# Choices tests
def test_ch_choices(self): # simple choices with weights
self.__process(
PromptPair("the choices are: {3::choice1|2::choice2|choice3}", ""),
PromptPair("the choices are: choice2", ""),
ppp=self.__nocupppp,
)
def test_ch_unsupportedsampler(self): # unsupported sampler
self.__process(
PromptPair("the choices are: {@choice1|choice2|choice3}", ""),
PromptPair("", ""),
ppp=self.__nocupppp,
interrupted=True,
)
def test_ch_choices_withcomments(self): # choices with comments and multiline
self.__process(
PromptPair(
"the choices are: {\n3::choice1 # this is option 1\n|2::choice2\n# this was option 2\n|choice3 # this is option 3\n}",
"",
),
PromptPair("the choices are: choice2", ""),
ppp=self.__nocupppp,
)
def test_ch_choices_multiple(self): # choices with multiple selection
self.__process(
PromptPair("the choices are: {~2$$, $$3::choice1|2:: choice2 |choice3}", ""),
PromptPair("the choices are: choice2 , choice3", ""),
ppp=self.__nocupppp,
)
def test_ch_choices_if_multiple(self): # choices with if and multiple selection
self.__process(
PromptPair("the choices are: {2$$, $$3::choice1|2 if _is_sd1::choice2|choice3}", ""),
PromptPair("the choices are: choice1, choice3", ""),
ppp=self.__nocupppp,
)
def test_ch_choices_set_if_multiple(self): # choices with if user variable and multiple selection
self.__process(
PromptPair("${var=test}the choices are: {2$$, $$3::choice1|2 if not var eq 'test'::choice2|choice3}", ""),
PromptPair("the choices are: choice1, choice3", ""),
ppp=self.__nocupppp,
)
def test_ch_choices_set_if_nested(self): # nested choices with if user variable and multiple selection
self.__process(
PromptPair(
"${var=test}the choices are: {2$$, $$3::choice1${var2=test2} {if var2 eq 'test2'::choice11|choice12}|2 if not var eq 'test'::choice2|choice3}",
"",
),
PromptPair("the choices are: choice1 choice11, choice3", ""),
ppp=self.__nocupppp,
)
# Wildcards tests
def test_wc_ignore(self): # wildcards with ignore option
self.__process(
PromptPair("__bad_wildcard__", "{option1|option2}"),
PromptPair("__bad_wildcard__", "{option1|option2}"),
ppp=PromptPostProcessor(
self.__ppp_logger,
self.__interrupt,
self.__def_model_info,
{
**self.__defopts,
"process_wildcards": False,
"if_wildcards": PromptPostProcessor.IFWILDCARDS_CHOICES.ignore.value,
},
),
)
def test_wc_remove(self): # wildcards with remove option
self.__process(
PromptPair(
"[<ppp:stn>neg5<ppp:/stn>] this is: __bad_wildcard__ a (([complex|simple<ppp:stn>neg6<ppp:/stn>|regular] test<ppp:stn>neg1<ppp:/stn>)(test:2.0):1.5) \nBREAK, BREAK with [abc<ppp:stn>neg4<ppp:/stn>:def<ppp:stn p0>neg2(neg3:1.6)<ppp:/stn>:5] <lora:xxx:1>",
"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>",
"[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.__defopts,
"process_wildcards": False,
"if_wildcards": PromptPostProcessor.IFWILDCARDS_CHOICES.remove.value,
},
),
)
def test_wc_warn(self): # wildcards with warn option
self.__process(
PromptPair("__bad_wildcard__", "{option1|option2}"),
PromptPair(PromptPostProcessor.WILDCARD_WARNING + "__bad_wildcard__", "{option1|option2}"),
ppp=PromptPostProcessor(
self.__ppp_logger,
self.__interrupt,
self.__def_model_info,
{
**self.__defopts,
"process_wildcards": False,
"if_wildcards": PromptPostProcessor.IFWILDCARDS_CHOICES.warn.value,
},
),
)
def test_wc_stop(self): # wildcards with stop option
self.__process(
PromptPair("__bad_wildcard__", "{option1|option2}"),
PromptPair(
PromptPostProcessor.WILDCARD_STOP + "__bad_wildcard__",
PromptPostProcessor.WILDCARD_STOP + "{option1|option2}",
),
ppp=PromptPostProcessor(
self.__ppp_logger,
self.__interrupt,
self.__def_model_info,
{
**self.__defopts,
"process_wildcards": False,
"if_wildcards": PromptPostProcessor.IFWILDCARDS_CHOICES.stop.value,
},
),
interrupted=True,
)
def test_wc_wildcard1a_text(self): # simple text wildcard
self.__process(
PromptPair("the choices are: __text/wildcard1__", ""),
PromptPair("the choices are: choice2", ""),
ppp=self.__nocupppp,
)
def test_wc_wildcard1a_json(self): # simple json wildcard
self.__process(
PromptPair("the choices are: __json/wildcard1__", ""),
PromptPair("the choices are: choice2", ""),
ppp=self.__nocupppp,
)
def test_wc_wildcard1a_yaml(self): # simple yaml wildcard
self.__process(
PromptPair("the choices are: __yaml/wildcard1__", ""),
PromptPair("the choices are: choice2", ""),
ppp=self.__nocupppp,
)
def test_wc_wildcard1b_text(self): # simple text wildcard with multiple choices
self.__process(
PromptPair("the choices are: __2-$$text/wildcard1__", ""),
PromptPair("the choices are: choice3, choice1", ""),
ppp=self.__nocupppp,
)
def test_wc_wildcard1b_json(self): # simple json wildcard with multiple choices
self.__process(
PromptPair("the choices are: __2-$$json/wildcard1__", ""),
PromptPair("the choices are: choice3, choice1", ""),
ppp=self.__nocupppp,
)
def test_wc_wildcard1b_yaml(self): # simple yaml wildcard with multiple choices
self.__process(
PromptPair("the choices are: __2-$$yaml/wildcard1__", ""),
PromptPair("the choices are: choice3, choice1", ""),
ppp=self.__nocupppp,
)
def test_wc_wildcard2_text(self): # simple text wildcard with default options
self.__process(
PromptPair("the choices are: __text/wildcard2__", ""),
PromptPair("the choices are: choice3-choice1", ""),
ppp=self.__nocupppp,
)
def test_wc_wildcard2_json(self): # simple json wildcard with default options
self.__process(
PromptPair("the choices are: __json/wildcard2__", ""),
PromptPair("the choices are: choice3-choice1", ""),
ppp=self.__nocupppp,
)
def test_wc_wildcard2_yaml(self): # simple yaml wildcard with default options
self.__process(
PromptPair("the choices are: __yaml/wildcard2__", ""),
PromptPair("the choices are: choice3-choice1", ""),
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__", ""),
PromptPair("the choices are: choice3,choice1- choice2 ,choice3", ""),
ppp=self.__nocupppp,
)
def test_wc_nested_wildcard_json(self): # nested json wildcard with repeating multiple choices
self.__process(
PromptPair("the choices are: __r3$$-$$json/wildcard3__", ""),
PromptPair("the choices are: choice3,choice1- choice2 ,choice3", ""),
ppp=self.__nocupppp,
)
def test_wc_nested_wildcard_yaml(self): # nested yaml wildcard with repeating multiple choices
self.__process(
PromptPair("the choices are: __r3$$-$$yaml/wildcard3__", ""),
PromptPair("the choices are: choice3,choice1- choice2 ,choice3", ""),
ppp=self.__nocupppp,
)
def test_wc_wildcard4_yaml(self): # simple yaml wildcard with one option
self.__process(
PromptPair("the choices are: __yaml/wildcard4__", ""),
PromptPair("the choices are: inline text", ""),
ppp=self.__nocupppp,
)
def test_wc_wildcard6_yaml(self): # simple yaml wildcard with object formatted choices
self.__process(
PromptPair("the choices are: __yaml/wildcard6__", ""),
PromptPair("the choices are: choice2", ""),
ppp=self.__nocupppp,
)
def test_wc_choice_wildcard_mix(self): # choices with wildcard mix
self.__process(
PromptPair("the choices are: {__~2$$yaml/wildcard2__|choice0}", ""),
[
PromptPair("the choices are: choice0", ""),
PromptPair("the choices are: choice1, choice3", ""),
PromptPair("the choices are: choice1, choice3", ""),
],
ppp=self.__nocupppp,
)
def test_wc_unsupportedsampler(self): # unsupported sampler
self.__process(
PromptPair("the choices are: __@yaml/wildcard2__", ""),
PromptPair("", ""),
ppp=self.__nocupppp,
interrupted=True,
)
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 ", ""),
ppp=self.__nocupppp,
)
def test_wc_wildcardwithvar(self): # wildcard with inline variable
self.__process(
PromptPair("the choices are: __yaml/wildcard5(var=test)__, __yaml/wildcard5__", ""),
PromptPair("the choices are: inline test, inline default", ""),
ppp=self.__nocupppp,
)
# def test_mix(self):
# self.__process(
# PromptPair(
# "__text/wildcard1__ (__text/wildcard2__) (__text/wildcard3__:1.5) [__text/wildcard1__] [__text/wildcard2__:__text/wildcard3__:0.5] [__text/wildcard1__|__text/wildcard2__] # <lora:__text/wildcard3__:1> {opt1_1|opt1_2} ({opt2_1|opt2_2}) ({opt3_1|opt3_2}:1.5) [{opt4_1|opt4_2}] [{opt5_1|opt5_2}:{opt6_1|opt6_2}:0.5] [{opt7_1|opt7_2}|{opt8_1|opt8_2}] # <lora:{opt9_1|opt9_2}:1> {opt1_1|__text/wildcard1__} ({opt2_1|__text/wildcard2__}) ({opt3_1|__text/wildcard3__}:1.5) [{opt4_1|__text/wildcard1__}] [{opt5_1|__text/wildcard2__}# :{opt6_1|__text/wildcard3__}:0.5] [{opt7_1|__text/wildcard1__}|{opt8_1|__text/wildcard2__}] {<lora:opt9_1:1>|<lora:__text/wildcard3__:1>}",
# "",
# ),
# PromptPair(
# "choice2 ( choice2 -choice1) (choice1, choice2 :1.5) [choice1] [choice1-choice1:choice3, choice2 :0.5] [choice2| choice2 - choice2 ] <lora: choice2 ,choice1:1> opt1_1 # (opt2_2) (opt3_1:1.5) [opt4_2] [opt5_1:opt6_2:0.5] [opt7_1|opt8_1] <lora:opt9_2:1> choice3 (opt2_1) (choice1,choice3:1.5) [choice1] [choice3-choice3:opt6_1:0.5] [opt7_1|# opt8_1] <lora:choice1, choice2 :1>",
# "",
# ),
# ppp=self.__nocupppp,
# )
# def test_real(self):
# self.__wildcards_obj.refresh_wildcards(
# DEBUG_LEVEL.full,
# ["D:\\AI\\SD\\_configuraciones\\acb-wildcards\\wildcards"],
# )
# self.__process(
# PromptPair(
# "${separator=()}, __quality/high__ __misc/sep__, photograph of a __character__",
# "__negatives/ng_generic__",
# ),
# PromptPair("", ""),
# )
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()
+19
View File
@@ -0,0 +1,19 @@
{
"json": {
"wildcard1": [
"choice1",
"choice2",
"choice3"
],
"wildcard2": [
"r2-3$$-",
"4::choice1",
"3:: choice2 ",
"2::choice3",
"5 if _is_sd1::choice4"
],
"wildcard3": [
"__2$$,$$json/wildcard2__"
]
}
}
+25
View File
@@ -0,0 +1,25 @@
yaml:
wildcard1:
- choice1
- choice2
- choice3
wildcard2:
- ~r2-3$$-
- 4::choice1
- "3:: choice2 "
- 2::choice3
- 5 if _is_sd1::choice4
wildcard3:
- __2$$,$$yaml/wildcard2__
wildcard4: inline text
wildcard5: inline ${var:default}
wildcard6:
- { weight: 2, text: choice1 }
- { weight: 3, content: choice2 }
- { text: choice3 }
- { weight: 4, if: "_is_ssd", text: choice4 }
+4
View File
@@ -0,0 +1,4 @@
# wildcard1
choice1
choice2
choice3
+6
View File
@@ -0,0 +1,6 @@
# wildcard2
r2-3$$-
4::choice1
3:: choice2
2::choice3
5 if _is_sd1::choice4
+2
View File
@@ -0,0 +1,2 @@
# wildcard3
__2$$,$$text/wildcard2__