Compare commits

..
5 Commits
Author SHA1 Message Date
Antonio Cordero Balcazar a1958db81f * Improved conditions with bool operations.
* Enhance PromptPostProcessorComfyUINode to return variables.
* New PromptPostProcessorSelectVariableComfyUINode for selecting variables.
* Fixed detection of Flux Schnell in Forge.
* Improved documentation.
2025-02-09 21:22:30 +01:00
Antonio Cordero Balcazar ce7b76f200 * refactoring
* additional tests
2025-01-25 16:47:43 +01:00
Antonio Cordero Balcazar e059a6d821 * add ifundefined modifier when setting variables.
* ComfyUI: better flux detection.
* Saving of UI settings and prompt/negative prompt to metadata.
2024-12-30 14:20:45 +01:00
Antonio Cordero Balcazar b06bcfa677 * Turned pony detection into detection of any kind of variant models based on filename. Added illustrious. Breaking change: the setting is different and changed pony substrings are not imported.
* Fixed and refactored some UI settings. Slight change in the generation of batched seeds.
* Detection of reforge.
* Small refactoring when getting choices.
2024-12-24 12:26:34 +01:00
Antonio Cordero Balcazar ee7ba94a30 * fix error in i2i detection.
* check for lingering special character sequences.
* small refactorings.
2024-11-23 14:00:43 +01:00
16 changed files with 1076 additions and 647 deletions
+2 -1
View File
@@ -1,5 +1,6 @@
{
"default": true,
"MD013": false,
"MD024": false
"MD024": false,
"MD033": false
}
+6
View File
@@ -4,6 +4,12 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python Debugger: Attach using Process Id",
"type": "debugpy",
"request": "attach",
"processId": "${command:pickProcess}"
},
{
"name": "Tests",
"type": "debugpy",
+18 -369
View File
@@ -5,7 +5,7 @@ The Prompt PostProcessor (PPP), formerly known as "sd-webui-sendtonegative", is
* [AUTOMATIC1111 Stable Diffusion WebUI](https://github.com/AUTOMATIC1111/stable-diffusion-webui)
* [Forge](https://github.com/lllyasviel/stable-diffusion-webui-forge)
* [reForge](https://github.com/Panchovix/stable-diffusion-webui-reForge)
* [SD.Next](https://github.com/vladmandic/automatic).
* [SD.Next](https://github.com/vladmandic/automatic)
* ...and probably other forks
* [ComfyUI](https://github.com/comfyanonymous/ComfyUI)
@@ -17,404 +17,53 @@ Currently this extension has these functions:
* Process wildcards. Compatible with Dynamic Prompts formats. Can also detect invalid wildcards and act as you choose.
* Clean up the prompt and negative prompt.
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.
Note: when used in an *A1111* compatible webui, the extension must be loaded after any other extension that modifies the prompt (like another wildcards extension). Usually extensions load by their folder name in alphanumeric order, so if the extensions are not loading in the correct order just rename this extension's folder so the ordering works out. When in doubt, just rename this extension's folder with a "z" in front (for example) so that it is the last one to load, or manually set such folder name when installing it.
If the extension runs before others, like Dynamic Prompts, and the "Process wildcards" is enabled, the wildcards will be processed by PPP and those extensions will not get them. If you disable processing the wildcards, and intend another extension to process them, you should keep the "What to do with remaining wildcards?" option as "ignore".
Notes:
1. Other than its own commands, it only recognizes regular A1111 prompt formats. So:
1. Other than its own commands, it only recognizes regular *A1111* prompt formats. So:
* **Attention**: `\[prompt\] (prompt) (prompt:weight)`
* **Alternation**: `\[prompt1|prompt2|...\]`
* **Scheduling**: `\[prompt1:prompt2:step\]`
* **Extra networks**: `\<kind:model...\>`
* **Attention**: `[prompt] (prompt) (prompt:weight)`
* **Alternation**: `[prompt1|prompt2|...]`
* **Scheduling**: `[prompt1:prompt2:step]`
* **Extra networks**: `<kind:model...>`
* **BREAK**: `prompt1 BREAK 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.
Does not recognize tokenizer separators like "TE2:" and "TE3:", so sending to negative prompt from those sections of the prompt will not add them in the corresponding section of the negative prompt.
Does not recognize tokenizer separators like `TE2:` and `TE3:`, so sending to negative prompt from those sections of the prompt will not add them in the corresponding section of the negative prompt.
ComfyUI only supports natively the attention using parenthesis, so the ones with the braces will be converted. The other constructs are not natively supported but some custom nodes implement them.
2. It recognizes wildcards in the *\_\_wildcard\_\_* and *{choice|choice}* formats (and almost everything that [Dynamic Prompts](https://github.com/adieyal/sd-dynamic-prompts) supports).
*ComfyUI* only supports natively the attention using parenthesis, so the ones with the braces will be converted. The other constructs are not natively supported but some custom nodes implement them.
2. It recognizes wildcards in the `__wildcard__` and {choice|choice} formats (and almost everything that [Dynamic Prompts](https://github.com/adieyal/sd-dynamic-prompts) supports).
3. It does not create *AND/BREAK* constructs when moving content to the negative prompt.
## Installation
On A1111 compatible webuis:
On *A1111* compatible webuis:
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
3. Click the Install button
4. Restart the webui
On SD.Next I recommend you disable the native wildcard processing.
On *SD.Next* I recommend you disable the native wildcard processing.
On ComfyUI:
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
2. Search for "Prompt PostProcessor" and install or click Install via Git URL and enter <https://github.com/acorderob/sd-webui-prompt-postprocessor>
3. Restart
## Usage
### Commands
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
<ppp:command parameters>
```
When a command is associated with any content, it will be between an opening and a closing command:
```text
<ppp:command parameters>content<ppp:/command>
```
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:
* "**'identifiers'**": comma separated labels for the choice (optional, quotes can be single or double). Only makes sense inside a wildcard definition. Can be used when specifying the wildcard to select this specific choice. It's case insensitive.
* "**n**": weight of the choice (optional, default 1).
* "**if condition**": filters out the choice if the condition is false (optional; this is an extension to the Dynamic Prompts syntax). Same conditions as in the `if` command.
* "**::**": end of choice options
Whitespace is allowed between parameters.
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 appropriate settings are checked.
### Wildcards
The generic format is:
```text
__parameters$$wildcard'filter'(var=value)__
```
The parameters, the filter, and the setting of a variable are optional. The parameters follow the same format as for the choices.
The wildcard identifier can contain globbing formatting, to read multiple wildcards and merge their choices. Note that if there are no parameters specified, the globbing will use the ones from the first wildcard that matches and have parameters (sorted by keys), so if you don't want that you might want to specify them. Also note that, unlike with Dynamic Prompts, the wildcard name has to be specified with its full path (unless you use globbing).
The filter can be used to filter specific choices from the wildcard. The filtering works before applying the choice conditions (if any). The surrounding quotes can be single or double. The filter is a comma separated list of an integer (positional choice index; zero-based) or choice label. You can also compound them with "+". That is, the comma separated items act as an OR and the "+" inside them as an AND. Using labels can simplify the definitions of complex wildcards where you want to have direct access to specific choices on occasion (you don't need to create wildcards for each individual choice). There are some additional formats when using filters. You can specify "^wildcard" as a filter to use the filter of a previous wildcard in the chain. You can start the filter (regular or inherited) with "#" and it will not be applied to the current wildcard choices, but the filter will remain in memory to use by other descendant wildcards. You use "#" and "^" when you want to pass a filter to inner wildcards (see the test files).
The variable value only applies during the evaluation of the selected choices and is discarded afterward (the variable keeps its original value if there was one).
These are examples of formats you can use to insert a wildcard:
```text
__path/wildcard__ # select 1 choice
__path/wildcard'0'__ # select the first choice
__path/wildcard'label'__ # select the choices with label "label"
__path/wildcard'0,label1,label2'__ # select the first choice and those with labels "label1" or "label2"
__path/wildcard'0,label1+label2'__ # select the first choice and those with both labels "label1" and "label2"
__3$$path/wildcard__ # select 3 choices
__2-3$$path/wildcard__ # select 2 to 3 choices
__r2-3$$path/wildcard__ # select 2 to 3 choices allowing repetition
__2-3$$ / $$path/wildcard__ # select 2 to 3 choices with separator " / "
__path/wildcard(var=value)__ # select 1 choice using the specified variable value in the evaluation.
```
#### Wildcard definitions
A wildcard definition can be:
* A txt file. The wildcard name will be the relative path of the file, without the extension. Each line will be a choice. Lines starting with "#" or empty are ignored. Doesn't support nesting.
* An array or scalar value inside a json or yaml file. The wildcard name includes the relative folder path of the file, without the extension, but also the path of the value inside the file (if there is one). If the file contains a dictionary, the filename part is not used for the wildcard name. Supports nesting by having dictionaries inside dictionaries.
The best format is a yaml file with a dictionary of wildcards inside. An editor supporting yaml syntax is recommended.
In a choice, the content after a "#" is ignored.
If the first choice follows the format of wildcard parameters, it will be used as default parameters for that wildcard (see examples in the tests folder). The choices of the wildcard follow the same format as in the choices construct, or the object format of **Dynamic Prompts** (only in structured files). If using the object format for a choice you can use a new "if" property for the condition, and the "labels" property (an array of strings) in addition to the standard "weight" and "text"/"content".
```yaml
{ labels: ["some_label"], weight: 2, if: "_is_pony", content: "the text" } # "text" property can be used instead of "content"
```
Wildcard parameters in a json/yaml file can also be in object format, and support two additional properties, prefix and suffix:
```yaml
{ sampler: "~", repeating: false, count: 2, prefix: "prefix-", suffix: "-suffix", separator: "/" }
{ sampler: "~", repeating: false, from: 2, to: 3, prefix: "prefix-", suffix: "-suffix", separator: "/" }
```
The prefix and suffix are added to the result along with the selected choices and separators. They can contain other constructs, but the separator can't.
It is recommended to use the object format for the wildcard parameters and for choices with complex options.
Wildcards can contain just one choice. In json and yaml formats this allows the use of a string value for the keys, rather than an array.
A choice inside a wildcard can also be a list or a dictionary of one element containing a list. These are considered anonymous wildcards. With a list it will be an anonymous wildcard with no choice options, and with a dictionary the key will be the options for the choice containing the anonymous wildcard and the value the choices of the anonymous wildcard. Anonymous wildcards can help formatting complex choice values that are used in only one place and thus creating a regular wildcard is not necessary. See test.yaml for examples.
#### Detection of remaining wildcards
This extension should run after any other wildcard extensions, so if you don't use the internal wildcards processing, any remaining wildcards present in the prompt or negative_prompt at this point must be invalid. Usually you might not notice this problem until you check the image metadata, so this option gives you some ways to detect and treat the problem.
### Set command
This command sets the value of a variable that can be checked later.
The format is:
```text
<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} # immediate evaluation
```
If also supports the addition as an extension of the Dynamic Prompts format:
```text
${var+=value}
${var+=!value}
```
### Echo command
This command prints the value of a variable.
The format is:
```text
<ppp:echo varname>
<ppp:echo varname>default<ppp:/echo>
```
The Dynamic Prompts format is:
```text
${var}
${var:default}
```
### If command
This command allows you to filter content based on conditions.
The full format is:
```text
<ppp:if condition1>content one<ppp:elif condition2>content two<ppp:else>other content<ppp:/if>
```
The *conditionN* compares a variable with a value or a list of values. The allowed formats are:
```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):
* `_model` : the loaded model identifier (`"sd1"`, `"sd2"`, `"sdxl"`, `"sd3"`, `"flux"`, `"auraflow"`). `_sd` also works but is deprecated.
* `_modelname` : the loaded model filename (without path). `_sdname` also works but is deprecated.
* `_modelfullname`: the loaded model filename (with path). `_sdfullname` also works but is deprecated.
* `_modelclass`: the class used for the model. Note that this is dependent on the webui. In A1111 all SD versions use the same class. Can be used for new models that are not supported yet with the `_is_*` variables.
* `_is_sd`: true if the loaded model version is any version of SD
* `_is_sd1`: true if the loaded model version is SD 1.x
* `_is_sd2`: true if the loaded model version is SD 2.x
* `_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
* `_is_auraflow`: true if the loaded model is AuraFlow
Any `elif`s (there can be multiple) and the `else` are optional.
#### Example
(multiline to be easier to read)
```text
<ppp:if _is_sd1><lora:test_sd1> test sd1x
<ppp:elif _sd_pony><lora:test_pony> test pony
<ppp:elif _sd_sdxl><lora:test_sdxl> test sdxl
<ppp:else>unknown model
<ppp:/if>
```
Only one of the options will end up in the prompt, depending on the loaded model.
### Sending content to the negative prompt
The new format for this command is like this:
```text
<ppp:stn position>content<ppp:/stn>
```
Where position is optional (defaults to the start) and can be:
* **s**: at the start of the negative prompt
* **e**: at the end of the negative prompt
* **pN**: at the position of the insertion point in the negative prompt with N being 0-9
The format of the insertion point to be used in the negative prompt is:
```text
<ppp:stn iN>
```
If the insertion point is not found it inserts at the start.
#### Example
You have a wildcard for hair colors (\_\_haircolors\_\_) with one being strawberry blonde, but you don't want strawberries. So in that option you add a command to add to the negative prompt, like so:
```text
blonde
strawberry blonde <ppp:stn>strawberry<ppp:/stn>
brunette
```
Then, if that option is chosen this extension will process it later and move that part to the negative prompt.
#### Old format
The old format (`<!...!>`) is not supported anymore.
### Notes on negative commands
Positional insertion commands have less priority that start/end commands, so even if they are at the start or end of the negative prompt, they will end up inside any start/end (and default position) commands.
The content of the negative commands is not processed and is copied as-is to the negative prompt. Other modifiers around the commands are processed in the following way.
#### Attention modifiers (weights)
They will be translated to the negative prompt. For example:
* `(red<ppp:stn>square<ppp:/stn>:1.5)` will end up as `(square:1.5)` in the negative prompt
* `(red[<ppp:stn>square<ppp:/stn>]:1.5)` will end up as `(square:1.35)` in the negative prompt (weight=1.5*0.9) if the merge attention option is enabled or `([square]:1.5)` otherwise.
* However `(red<ppp:stn>[square]<ppp:/stn>:1.5)` will end up as `([square]:1.5)` in the negative prompt. The content of the negative tag is copied as is, and is not merged with the surrounding modifier because the insertions happen after the attention merging.
#### Prompt editing constructs (alternation and scheduling)
Negative commands inside such constructs will copy the construct to the negative prompt, but separating its elements. For example:
* **Alternation**: `[red<ppp:stn>square<ppp:/stn>|blue<ppp:stn>circle<ppp:/stn>]` will end up as `[square|], [|circle]` in the negative prompt, instead of `[square|circle]`
* **Scheduling**: `[red<ppp:stn>square<ppp:/stn>:blue<ppp:stn>circle<ppp:/stn>:0.5]` will end up as `[square::0.5], [:circle:0.5]` instead of `[square:circle:0.5]`
This should still work as intended, and the only negative point i see is the unnecessary separators.
See the [syntax documentation](docs/SYNTAX.md).
## Configuration
### A1111 (and compatible UIs) UI options
* **Force equal seeds**: Changes the image seeds and variation seeds to be equal to the first of the batch. This allows using the same values for all the images in a batch.
* **Unlink seed**: Uses the specified seed for the prompt generation instead of the one from the image.
* **Seed**: The seed to use for the prompt generation. If -1 a random one will be used for each image in the batch. This seed is only used for wildcards and choices.
* **Variable seed**: If the seed is not -1 you can use this to increase it for the other images in the batch.
### ComfyUI specific inputs
* **model**: Connect here the MODEL or a string with the model class name used by ComfyUI. Needed for the model kind system variables.
* **modelname**: Name of the model. Needed for the model name system variables and detection of pony (this also requieres for the model to be SDXL).
* **seed**: Connect here the seed used. By default it is -1 (random).
* **pos_prompt**: Connect here the prompt text, or fill it as a widget.
* **neg_prompt**: Connect here the negative prompt text, or fill it as a widget.
### General settings
* **Debug level**: what to write to the console. Note: in SD.Next debug messages only show if you launch it with the --debug argument.
* **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.
* **Ignore**: do not try to detect wildcards.
* **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.
* **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 ", ").
* **Keep the order of selected choices**: if checked, a multiple choice construct will return them in the order they are in the construct.
### Send to negative prompt settings
* **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.
### Clean up settings
* **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 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.
* **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.
* **Use EOL instead of Space before ANDs**: add a newline before ANDs.
* **Clean up around extra network tags**: removes spaces around them.
* **Merge attention modifiers (weights) when possible**: it merges attention modifiers when possible (merges into one, multiplying their values). Only merges individually nested modifiers.
* **Remove extra spaces**: removes other unnecessary spaces.
Please note that ComfyUI does not support the BREAK and AND constructs, but the related settings are kept in that UI.
### Content removal settings
* **Remove extra network tags**: removes all extra network tags.
See the [configuration documentation](docs/CONFIG.md).
## License
+9 -3
View File
@@ -10,9 +10,15 @@ import os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from .ppp_comfyui import PromptPostProcessorComfyUINode
from .ppp_comfyui import PromptPostProcessorComfyUINode, PromptPostProcessorSelectVariableComfyUINode
NODE_CLASS_MAPPINGS = {"ACBPromptPostProcessor": PromptPostProcessorComfyUINode}
NODE_DISPLAY_NAME_MAPPINGS = {"ACBPromptPostProcessor": "ACB Prompt Post Processor"}
NODE_CLASS_MAPPINGS = {
"ACBPromptPostProcessor": PromptPostProcessorComfyUINode,
"ACBPPPSelectVariable": PromptPostProcessorSelectVariableComfyUINode,
}
NODE_DISPLAY_NAME_MAPPINGS = {
"ACBPromptPostProcessor": "ACB Prompt Post Processor",
"ACBPPPSelectVariable": "ACB PPP Select Variable",
}
__all__ = ["NODE_CLASS_MAPPINGS", "NODE_DISPLAY_NAME_MAPPINGS"]
+78
View File
@@ -0,0 +1,78 @@
# Prompt PostProcessor configuration
## ComfyUI specific (ACB Prompt Post Processor node)
### Inputs
* **model**: Connect here the MODEL or a string with the model class name used by *ComfyUI*. Needed for the model kind system variables.
* **modelname**: Name of the model. Needed for the model name system variables and detection of pony (this also requieres for the model to be SDXL).
* **seed**: Connect here the seed used. By default it is -1 (random).
* **pos_prompt**: Connect here the prompt text, or fill it as a widget.
* **neg_prompt**: Connect here the negative prompt text, or fill it as a widget.
Other common settings (see [below](#common-settings)) also appear as inputs or widgets.
### Outputs
The outputs are the final positive and negative prompt and a variables dictionary.
You can use the "**ACB PPP Select Variable**" node to choose one and output its value. You can use this to send only part of the prompt to, for example, a detailer node. For example:
With this prompt: `__quality__, 1girl, ${head:__eyes__, __hair__, __expression__}, __body__, __clothes__, __background__, __style__` then you extract the `head` variable and send that as prompt for the head/face detailer.
## A1111 (and compatible UIs) panel options
* **Force equal seeds**: Changes the image seeds and variation seeds to be equal to the first of the batch. This allows using the same values for all the images in a batch.
* **Unlink seed**: Uses the specified seed for the prompt generation instead of the one from the image. This seed is only used for wildcards and choices.
* **Prompt seed**: The seed to use for the prompt generation. If -1 a random one will be used.
* **Incremental seed**: When using a batch you can use this to set the rest of the prompt seeds with consecutive values.
## Common settings
### General settings
* **Debug level**: what to write to the console. Note: in *SD.Next* debug messages only show if you launch it with the `--debug` argument.
* **Model variant definitions**: definitions for model variants to be recognized based on strings found in the full filename.
The format for each line is (with *kind* being one of the base model identifiers or not defined):
```name(kind)=comma separated list of substrings (case insensitive)```
The default value defines strings for *Pony* and *Illustrious* models.
* **Apply in img2img**: check if you want to do the processing in img2img processes (*does not apply to the 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.
* **Ignore**: do not try to detect wildcards.
* **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.
* **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 ", ").
* **Keep the order of selected choices**: if checked, a multiple choice construct will return them in the order they are in the construct.
### Send to negative prompt settings
* **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.
### Clean up settings
* **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 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.
* **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.
* **Use EOL instead of Space before ANDs**: add a newline before ANDs.
* **Clean up around extra network tags**: removes spaces around them.
* **Merge attention modifiers (weights) when possible**: it merges attention modifiers when possible (merges into one, multiplying their values). Only merges individually nested modifiers.
* **Remove extra spaces**: removes other unnecessary spaces.
Please note that *ComfyUI* does not natively support the `BREAK` and `AND` constructs, but the related settings are kept in that UI.
### Content removal settings
* **Remove extra network tags**: removes all extra network tags.
+276
View File
@@ -0,0 +1,276 @@
# Prompt PostProcessor syntax
## Commands
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).
`<ppp:command parameters>`
When a command is associated with any content, it will be between an opening and a closing command:
`<ppp:command parameters>content<ppp:/command>`
For wildcards and choices it uses the formats from the *Dynamic Prompts* extension, but sometimes with some additional options for extra functionality.
## Choices
The generic format is: `{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 supported.
* "**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:
* "**'identifiers'**": comma separated labels for the choice (optional, quotes can be single or double). Only makes sense inside a wildcard definition. Can be used when specifying the wildcard to select this specific choice. It's case insensitive.
* "**n**": weight of the choice (optional, default 1).
* "**if condition**": filters out the choice if the condition is false (optional; this is an extension to the *Dynamic Prompts* syntax). Same conditions as in the `if` command.
* "**::**": end of choice options
Whitespace is allowed between parameters/options.
These are examples of formats you can use to insert a choice construct:
| Construct | Result |
| --------- | ------ |
| `{choice1\|5::choice2\|3::choice3}` | select 1 choice, two of them have weights |
| `{3$$choice1\|5 if _is_sd1::choice2\|choice3}` | select 3 choices, one has a weight and a condition |
| `{2-3$$2::choice1\|choice2\|choice3}` | select 2 to 3 choices, one of them has a weight |
| `{r2-3$$choice1\|choice2\|choice3}` | select 2 to 3 choices allowing repetition |
| `{2-3$$ / $$choice1\|choice2\|choice3}` | 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 around the choices is not ignored like in *Dynamic Prompts*, but will be cleaned up if the appropriate cleaning settings are selected.
## Wildcards
The generic format is: `__parameters$$wildcard'filter'(var=value)__`
The parameters, the filter, and the setting of a variable are optional. The parameters follow the same format as for the choices.
The wildcard identifier can have a relative path and contain globbing formatting, to read multiple wildcards and merge their choices. Note that if there are no parameters specified, the globbing will use the ones from the first wildcard that matches and have parameters (sorted by keys), so if you don't want that you might want to specify them. Also note that, unlike with *Dynamic Prompts*, the wildcard name has to be specified with its full path (unless you use globbing).
The filter can be used to filter specific choices from the wildcard. The filtering works before applying the choice conditions (if any). The surrounding quotes can be single or double. The filter is a comma separated list of an integer (positional choice index; zero-based) or choice label. You can also compound them with `+`. That is, the comma separated items act as an OR and the `+` inside them as an AND. Using labels can simplify the definitions of complex wildcards where you want to have direct access to specific choices on occasion (you don't need to create wildcards for each individual choice). There are some additional formats when using filters. You can specify `^wildcard` as a filter to use the filter of a previous wildcard in the chain. You can start the filter (regular or inherited) with `#` and it will not be applied to the current wildcard choices, but the filter will remain in memory to use by other descendant wildcards. You use `#` and `^` when you want to pass a filter to inner wildcards (see the test files).
The variable value only applies during the evaluation of the selected choices and is discarded afterward (the variable keeps its original value if there was one).
These are examples of formats you can use to insert a wildcard:
| Construct | Result |
| --------- | ------ |
| `__wildcard__` | select 1 choice |
| `__path/wildcard'0'__` | select the first choice |
| `__path/wildcard'label'__` | select the choices with label "label" |
| `__path/wildcard'0,label1,label2'__` | select the first choice and those with labels "label1" or "label2" |
| `__path/wildcard'0,label1+label2'__` | select the first choice and those with both labels "label1" and "label2" |
| `__3$$path/wildcard__` | select 3 choices |
| `__2-3$$path/wildcard__` | select 2 to 3 choices |
| `__r2-3$$path/wildcard__` | select 2 to 3 choices allowing repetition |
| `__2-3$$ / $$path/wildcard__` | select 2 to 3 choices with separator " / " |
| `__path/wildcard(var=value)__` | select 1 choice using the specified variable value in the evaluation. |
### Wildcard definitions
A wildcard definition can be:
* A txt file. The wildcard name will be the relative path of the file, without the extension. Each line will be a choice. Lines starting with `#` or empty are ignored. Doesn't support nesting.
* An array or scalar value inside a json or yaml file. The wildcard name includes the relative folder path of the file, without the extension, but also the path of the value inside the file (if there is one). If the file contains a dictionary, the filename part is not used for the wildcard name. Supports nesting by having dictionaries inside dictionaries.
The best format is a yaml file with a dictionary of wildcards inside. An editor supporting yaml syntax and linting is recommended (f.e. vscode).
In a choice, the content after a `#` is ignored.
If the first choice follows the format of wildcard parameters, it will be used as default parameters for that wildcard (see examples in the tests folder). The choices of the wildcard follow the same format as in the choices construct, or the object format of *Dynamic Prompts* (only in structured files). If using the object format for a choice you can use a new `if` property for the condition, and the `labels` property (an array of strings) in addition to the standard `weight` and `text`/`content`.
```yaml
{ labels: ["some_label"], weight: 2, if: "_is_pony", content: "the text" } # "text" property can be used instead of "content"
```
Wildcard parameters in a json/yaml file can also be in object format, and support two additional properties, prefix and suffix:
```yaml
{ sampler: "~", repeating: false, count: 2, prefix: "prefix-", suffix: "-suffix", separator: "/" }
{ sampler: "~", repeating: false, from: 2, to: 3, prefix: "prefix-", suffix: "-suffix", separator: "/" }
```
The prefix and suffix are added to the result along with the selected choices and separators. They can contain other constructs, but the separator can't.
It is recommended to use the object format for the wildcard parameters and for choices with complex options.
Wildcards can contain just one choice. In json and yaml formats this allows the use of a string value for the keys, rather than an array.
A choice inside a wildcard can also be a list or a dictionary of one element containing a list. These are considered anonymous wildcards. With a list it will be an anonymous wildcard with no choice options, and with a dictionary the key will be the options for the choice containing the anonymous wildcard and the value the choices of the anonymous wildcard. Anonymous wildcards can help formatting complex choice values that are used in only one place and thus creating a regular wildcard is not necessary. See test.yaml for examples.
### Detection of remaining wildcards
This extension should run after any other wildcard extensions, so if you don't use the internal wildcards processing, any remaining wildcards present in the prompt or negative_prompt at this point must be invalid. Usually you might not notice this problem until you check the image metadata, so this option gives you some ways to detect and treat the problem.
## Set command
This command sets the value of a variable that can be checked later.
The format is: `<ppp:set varname [modifiers]>value<ppp:/set>`
These are the available optional modifiers:
* `evaluate`: the value of the variable is evaluated at this moment, instead of when it is used.
* `add`: 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.
* `ifundefined`: the value will only be set if the variable is undefined.
The `add` and `ifundefined` modifiers are mutually exclusive and cannot be used together.
The *Dynamic Prompts* format also works:
| Construct | Meaning |
| --------- | ------- |
| `${var=value}` | regular evaluation |
| `${var=!value}` | immediate evaluation |
If also supports the addition and undefined check as an extension of the *Dynamic Prompts* format:
| Construct | Meaning |
| --------- | ------- |
| `${var+=value}` | equivalent to "add" |
| `${var+=!value}` | equivalent to "evaluate add" |
| `${var?=value}` | equivalent to "ifundefined" |
| `${var?=!value}` | equivalent to "evaluate ifundefined" |
## Echo command
This command prints the value of a variable, or the specified default if it doesn't exist.
The format is:
| Construct |
| --------- |
| `<ppp:echo varname>` |
| `<ppp:echo varname>default<ppp:/echo>` |
The *Dynamic Prompts* format is:
| Construct |
| --------- |
| `${varname}` |
| `${varname:default}` |
## If command
This command allows you to filter content based on conditions.
The full format is:
`<ppp:if condition1>content one<ppp:elif condition2>content two<ppp:else>other content<ppp:/if>`
Any `elif`s (there can be multiple) and the `else` are optional.
The `conditionN` can be:
| Construct | Meaning |
| --------- | ------- |
| `variable` | check truthyness of the variable |
| `variable [not] operation value` | check the variable against a value |
| `variable [not] operation (value1,value2,...)` | check the variable against a list of values |
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 operation can be preceded by `not` for readability, instead of using it in the front.
You can also build complex conditions joining them with boolean operators and/or/not and parentheses.
The variable can be one set with the `set` or `add` commands (user variables) or you can use system variables like these (names starting with an underscore are reserved for system variables):
| System variable | Value |
| --------------- | ----- |
| `_model` | the loaded model identifier (`"sd1"`, `"sd2"`, `"sdxl"`, `"sd3"`, `"flux"`, `"auraflow"`). `_sd` also works but is deprecated. |
| `_modelname` | the loaded model filename (without path). `_sdname` also works but is deprecated. |
| `_modelfullname` | the loaded model filename (with path). `_sdfullname` also works but is deprecated. |
| `_modelclass` | the class used for the model. Note that this is dependent on the webui. In A1111 all SD versions use the same class. Can be used for new models that are not supported yet with the `_is_*` variables. The debug setting will show all system variables when generating in case you need to see which one to use for a certain model. |
| `_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_sd3` | true if the loaded model version is SD 3.x |
| `_is_flux` | true if the loaded model is Flux |
| `_is_auraflow` | true if the loaded model is AuraFlow |
| `_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_sdxl_no_pony` | true if the loaded model version is SDXL and not a Pony model (the "pony" variant must be defined in settings). Kept to maintain compatibility with previous versions. |
| `_is_vvvv` | true if the loaded model matches the *vvvv* model variant definition (based on its filename). Note that the corresponding variable for the model kind will also be true. |
| `_is_pure_kkkk` | true if the loaded model is of kind *kkkk* (f.e. sdxl) and not a variant. |
| `_is_variant_kkkk` | true if the loaded model version is any variant of model kind *kkkk* and not the pure version. Note that the corresponding variable for the model kind will also be true.|
### Example
(multiline to be easier to read)
```text
<ppp:if _is_sd1><lora:test_sd1> test sd1x
<ppp:elif _sd_pony><lora:test_pony> test pony
<ppp:elif _sd_pure_sdxl><lora:test_sdxl> test sdxl
<ppp:else>unknown model
<ppp:/if>
```
Only one of the options will end up in the prompt, depending on the loaded model.
## Sending content to the negative prompt
The new format for this command is like this:
| Construct | Meaning |
| --------- | ------- |
| `<ppp:stn position>content<ppp:/stn>` | send to negative prompt |
| `<ppp:stn iN>` | insertion point to be used in the negative prompt as destination for the pN position |
Where position is optional (defaults to the start) and can be:
* **s**: at the start of the negative prompt
* **e**: at the end of the negative prompt
* **pN**: at the position of the insertion point in the negative prompt with N being 0-9. If the insertion point is not found it inserts at the start.
### Example
You have a wildcard for hair colors (`__haircolors__`) with one being strawberry blonde, but you don't want strawberries. So in that option you add a command to add to the negative prompt, like so:
```text
blonde
strawberry blonde <ppp:stn>strawberry<ppp:/stn>
brunette
```
Then, if that option is chosen this extension will process it later and move that part to the negative prompt.
### Old format
The old format (`<!...!>`) is not supported anymore.
### Notes
Positional insertion commands have less priority that start/end commands, so even if they are at the start or end of the negative prompt, they will end up inside any start/end (and default position) commands.
The content of the negative commands is not processed and is copied as-is to the negative prompt. Other modifiers around the commands are processed in the following way.
### Attention modifiers (weights)
They will be translated to the negative prompt. For example:
* `(red<ppp:stn>square<ppp:/stn>:1.5)` will end up as `(square:1.5)` in the negative prompt
* `(red[<ppp:stn>square<ppp:/stn>]:1.5)` will end up as `(square:1.35)` in the negative prompt (weight=1.5*0.9) if the merge attention option is enabled or `([square]:1.5)` otherwise.
* However `(red<ppp:stn>[square]<ppp:/stn>:1.5)` will end up as `([square]:1.5)` in the negative prompt. The content of the negative tag is copied as is, and is not merged with the surrounding modifier because the insertions happen after the attention merging.
### Prompt editing constructs (alternation and scheduling)
Negative commands inside such constructs will copy the construct to the negative prompt, but separating its elements. For example:
* **Alternation**: `[red<ppp:stn>square<ppp:/stn>|blue<ppp:stn>circle<ppp:/stn>]` will end up as `[square|], [|circle]` in the negative prompt, instead of `[square|circle]`
* **Scheduling**: `[red<ppp:stn>square<ppp:/stn>:blue<ppp:stn>circle<ppp:/stn>:0.5]` will end up as `[square::0.5], [:circle:0.5]` instead of `[square:circle:0.5]`
This should still work as intended, and the only negative point i see is the unnecessary separators.
+18 -9
View File
@@ -3,8 +3,10 @@
_WHITESPACE: /\s+/
STRING: /("(?!"").*?(?<!\\)(\\\\)*?"|'(?!'').*?(?<!\\)(\\\\)*?')/i
BOOLEAN: /true|false/i
INDEX: INT | IDENTIFIER
WILDCARD_NAME: /(?:(?!__|\$\$|[('"])\S)+/
INDEX: INT | IDENTIFIER
IDENTIFIER: CNAME
SIMPLEVALUE: STRING | NUMBER | BOOLEAN
// plain text and weights
?plain: /((?!__|\bAND\b|\${)[^\\()\[\]:<>${]|\\.)+/s // exclude only the starting ones
@@ -58,22 +60,29 @@ 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 | NUMBER | BOOLEAN
?condition: grouped_condition | ungrouped_condition
?ungrouped_condition: operation | basic_condition
?grouped_condition: "(" _WHITESPACE? condition _WHITESPACE? ")"
?basic_condition: comparison_simple_value | comparison_list_value | truthy_operand
?operation: operation_not | operation_and | operation_or
operation_and: condition _WHITESPACE "and" _WHITESPACE condition
operation_or: condition _WHITESPACE "or" _WHITESPACE condition
operation_not: "not" ( ( _WHITESPACE ungrouped_condition ) | ( _WHITESPACE? grouped_condition ) )
truthy_operand: IDENTIFIER
comparison_simple_value: IDENTIFIER _WHITESPACE ( /not/ _WHITESPACE )? /eq|ne|gt|lt|ge|le|contains/ _WHITESPACE SIMPLEVALUE
comparison_list_value: IDENTIFIER _WHITESPACE ( /not/ _WHITESPACE )? /contains|in/ _WHITESPACE listvalue
listvalue: "(" _WHITESPACE? SIMPLEVALUE ( _WHITESPACE? "," _WHITESPACE? SIMPLEVALUE )* _WHITESPACE? ")"
// command: set
commandset: "<ppp:set" _WHITESPACE IDENTIFIER [ _WHITESPACE? /evaluate/ ] [ _WHITESPACE? /add/ ] _WHITESPACE? ">" content "<ppp:/set>"
commandset: "<ppp:set" _WHITESPACE IDENTIFIER [ commandsetmodifiers ] _WHITESPACE? ">" content "<ppp:/set>"
commandsetmodifiers: (_WHITESPACE /evaluate|ifundefined|add/ )+
// command: echo
commandecho: "<ppp:echo" _WHITESPACE IDENTIFIER _WHITESPACE? ">" [ content "<ppp:/echo>" ]
// variable set
variableset.2: "${" _WHITESPACE? IDENTIFIER _WHITESPACE? [ /\+/ ] "=" [ /!/ ] varvalue "}"
variableset.2: "${" _WHITESPACE? IDENTIFIER [ variablesetmodifiers ] _WHITESPACE? "=" [ /!/ ] varvalue "}"
variablesetmodifiers: /[+?!]+/
// variable use
variableuse.2: "${" _WHITESPACE? IDENTIFIER _WHITESPACE? [ ":" varvalue ] "}"
+239 -151
View File
@@ -1,4 +1,3 @@
from functools import reduce
import logging
import math
import os
@@ -7,7 +6,7 @@ import textwrap
import time
from collections import namedtuple
from enum import Enum
from typing import Callable, Optional
from typing import Any, Callable, Optional
import lark
import lark.parsers
@@ -52,18 +51,27 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
stop = "stop"
DEFAULT_STN_SEPARATOR = ", "
DEFAULT_PONY_SUBSTRINGS = ",".join(["pony", "pny", "pdxl"])
DEFAULT_VARIANTS_DEFINITIONS = "pony(sdxl)=pony,pny,pdxl\nillustrious(sdxl)=illustrious,illust,ilxl"
DEFAULT_CHOICE_SEPARATOR = ", "
WILDCARD_WARNING = '(WARNING TEXT "INVALID WILDCARD" IN BRIGHT RED:1.5)\nBREAK '
WILDCARD_STOP = "INVALID WILDCARD! {0}\nBREAK "
UNPROCESSED_STOP = "UNPROCESSED CONSTRUCTS!\nBREAK "
SUPPORTED_MODELS = [
"sd1",
"sd2",
"sdxl",
"sd3",
"flux",
"auraflow",
]
def __init__(
self,
logger: logging.Logger,
interrupt: Optional[Callable],
env_info: dict[str, any],
options: Optional[dict[str, any]] = None,
env_info: dict[str, Any],
options: Optional[dict[str, Any]] = None,
grammar_content: Optional[str] = None,
wildcards_obj: PPPWildcards = None,
):
@@ -80,16 +88,32 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
"""
self.logger = logger
self.rng = np.random.default_rng() # gets seeded on each process prompt call
self.the_interrupt = interrupt
self.interrupt_callback = interrupt
self.options = options
self.env_info = env_info
self.wildcard_obj = wildcards_obj
# General options
self.debug_level = DEBUG_LEVEL(options.get("debug_level", DEBUG_LEVEL.none.value))
self.pony_substrings = list(
x.strip() for x in (str(options.get("pony_substrings", self.DEFAULT_PONY_SUBSTRINGS))).split(",")
)
variants_definitions_option = str(options.get("variants_definitions", self.DEFAULT_VARIANTS_DEFINITIONS))
self.variants_definitions = {}
if variants_definitions_option:
lines = variants_definitions_option.splitlines()
for line in lines:
if "=" in line:
model_tag, elements = line.split("=", 1)
model_name, model_type = re.match(r"(\w+)(?:\((\w+)\))?", model_tag).groups()
if model_type is not None and model_type not in self.SUPPORTED_MODELS:
self.logger.warning(
f"Unsupported model type '{model_type}' in definition for variant '{model_name}'."
)
elif model_name in self.SUPPORTED_MODELS:
self.logger.warning(f"Invalid model name in definition for variant '{model_name}'.")
else:
self.variants_definitions[model_name.strip()] = (
model_type or "",
[element.strip() for element in elements.split(",")],
)
# Wildcards options
self.wil_process_wildcards = options.get("process_wildcards", True)
self.wil_keep_choices_order = options.get("keep_choices_order", False)
@@ -148,12 +172,13 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
)
self.__init_sysvars()
self.user_variables = {}
self.echoed_variables = {}
def interrupt(self):
if self.the_interrupt is not None:
self.the_interrupt()
if self.interrupt_callback is not None:
self.interrupt_callback()
def formatOutput(self, text: str) -> str:
def format_output(self, text: str) -> str:
"""
Formats the output text by encoding it using unicode_escape and decoding it using utf-8.
@@ -165,7 +190,7 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
"""
return text.encode("unicode_escape").decode("utf-8")
def isComfyUI(self) -> bool:
def is_comfy_ui(self) -> bool:
"""
Checks if the current environment is ComfyUI.
@@ -179,36 +204,37 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
Initializes the system variables.
"""
self.system_variables = {}
sdchecks = {
"sd1": self.env_info.get("is_sd1", False),
"sd2": self.env_info.get("is_sd2", False),
"sdxl": self.env_info.get("is_sdxl", False),
"sd3": self.env_info.get("is_sd3", False),
"flux": self.env_info.get("is_flux", False),
"auraflow": self.env_info.get("is_auraflow", False),
"": True,
}
sdchecks = {x: self.env_info.get("is_" + x, False) for x in self.SUPPORTED_MODELS}
sdchecks.update({"": True})
self.system_variables["_model"] = [k for k, v in sdchecks.items() if v][0]
self.system_variables["_sd"] = self.system_variables["_model"] # deprecated
model_filename = self.env_info.get("model_filename", "")
is_pony = any(s in model_filename.lower() for s in self.pony_substrings)
is_ssd = self.env_info.get("is_ssd", False)
self.system_variables["_sdfullname"] = model_filename # deprecated
self.system_variables["_modelfullname"] = model_filename
self.system_variables["_sdname"] = os.path.basename(model_filename) # deprecated
self.system_variables["_modelname"] = os.path.basename(model_filename)
self.system_variables["_modelclass"] = self.env_info.get("model_class", "")
self.system_variables["_is_sd1"] = sdchecks["sd1"]
self.system_variables["_is_sd2"] = sdchecks["sd2"]
self.system_variables["_is_sdxl"] = sdchecks["sdxl"]
is_models = {
model_name: (model_type_and_substrings[0] == "" or sdchecks.get(model_type_and_substrings[0], False))
and any(s in model_filename.lower() for s in model_type_and_substrings[1])
for model_name, model_type_and_substrings in self.variants_definitions.items()
if model_name not in self.SUPPORTED_MODELS
}
self.system_variables.update({"_is_" + x: y for x, y in is_models.items()})
for x in sdchecks.keys():
if x != "":
self.system_variables["_is_" + x] = sdchecks[x]
self.system_variables["_is_pure_" + x] = sdchecks[x] and not any(is_models.values())
self.system_variables["_is_variant_" + x] = sdchecks[x] and any(is_models.values())
# special cases
self.system_variables["_is_sd"] = sdchecks["sd1"] or sdchecks["sd2"] or sdchecks["sdxl"] or sdchecks["sd3"]
is_ssd = self.env_info.get("is_ssd", False)
self.system_variables["_is_ssd"] = is_ssd
self.system_variables["_is_sdxl_no_ssd"] = sdchecks["sdxl"] and not is_ssd
self.system_variables["_is_pony"] = sdchecks["sdxl"] and is_pony
self.system_variables["_is_sdxl_no_pony"] = sdchecks["sdxl"] and not is_pony
self.system_variables["_is_sd3"] = sdchecks["sd3"]
self.system_variables["_is_sd"] = sdchecks["sd1"] or sdchecks["sd2"] or sdchecks["sdxl"] or sdchecks["sd3"]
self.system_variables["_is_flux"] = sdchecks["flux"]
self.system_variables["_is_auraflow"] = sdchecks["auraflow"]
# backcompatibility (but the modern one to use would be _is_pure_sdxl)
self.system_variables["_is_sdxl_no_pony"] = sdchecks["sdxl"] and not self.system_variables.get(
"_is_pony", False
)
def __add_to_insertion_points(
self, negative_prompt: str, add_at_insertion_point: list[str], insertion_at: list[tuple[int, int]]
@@ -398,6 +424,8 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
tuple: A tuple containing the processed prompt and negative prompt.
"""
self.user_variables = {}
self.echoed_variables = {}
all_variables = {**self.system_variables}
# Process prompt
p_processor = self.TreeProcessor(self)
@@ -409,16 +437,21 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
n_parsed = self.parse_prompt("negative prompt", negative_prompt, self.parser_complete)
negative_prompt = n_processor.start_visit("negative prompt", n_parsed, True)
var_keys = set(self.user_variables.keys()).union(set(self.echoed_variables.keys()))
all_variables.update(
{k: self.echoed_variables.get(k, p_processor.get_final_user_variable(k)) for k in var_keys}
)
# Insertions in the negative prompt
if self.debug_level == DEBUG_LEVEL.full:
self.logger.debug(self.formatOutput(f"New negative additions: {p_processor.add_at}"))
self.logger.debug(self.formatOutput(f"New negative indexes: {n_processor.insertion_at}"))
self.logger.debug(self.format_output(f"New negative additions: {p_processor.add_at}"))
self.logger.debug(self.format_output(f"New negative indexes: {n_processor.insertion_at}"))
negative_prompt = self.__add_to_insertion_points(
negative_prompt, p_processor.add_at["insertion_point"], n_processor.insertion_at
)
if len(p_processor.add_at["start"]) > 0:
if p_processor.add_at["start"]:
negative_prompt = self.__add_to_start(negative_prompt, p_processor.add_at["start"])
if len(p_processor.add_at["end"]) > 0:
if p_processor.add_at["end"]:
negative_prompt = self.__add_to_end(negative_prompt, p_processor.add_at["end"])
# Clean up
@@ -426,8 +459,8 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
negative_prompt = self.__cleanup(negative_prompt)
# Check for wildcards not processed
foundP = len(p_processor.detectedWildcards) > 0
foundNP = len(n_processor.detectedWildcards) > 0
foundP = bool(p_processor.detectedWildcards)
foundNP = bool(n_processor.detectedWildcards)
if foundP or foundNP:
if self.wil_ifwildcards == self.IFWILDCARDS_CHOICES.stop:
self.logger.error("Found unprocessed wildcards!")
@@ -436,9 +469,9 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
ppwl = ", ".join(p_processor.detectedWildcards)
npwl = ", ".join(n_processor.detectedWildcards)
if foundP:
self.logger.error(self.formatOutput(f"In the positive prompt: {ppwl}"))
self.logger.error(self.format_output(f"In the positive prompt: {ppwl}"))
if foundNP:
self.logger.error(self.formatOutput(f"In the negative prompt: {npwl}"))
self.logger.error(self.format_output(f"In the negative prompt: {npwl}"))
if self.wil_ifwildcards == self.IFWILDCARDS_CHOICES.warn:
prompt = self.WILDCARD_WARNING + prompt
elif self.wil_ifwildcards == self.IFWILDCARDS_CHOICES.stop:
@@ -448,7 +481,15 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
if foundNP:
negative_prompt = self.WILDCARD_STOP.format(npwl) + negative_prompt
self.interrupt()
return prompt, negative_prompt
# Check for special character sequences that should not be in the result
compound_prompt = prompt + "\n" + negative_prompt
found_sequences = re.findall(r"::|\$\$|\$\{|[{}]", compound_prompt)
if found_sequences:
self.logger.warning(
f"""Found probably invalid character sequences on the result ({', '.join(map(lambda x: '"' + x + '"', set(found_sequences)))}). Something might be wrong!"""
)
return prompt, negative_prompt, all_variables
def process_prompt(
self,
@@ -465,8 +506,9 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
seed (int): The seed.
Returns:
tuple: A tuple containing the processed prompt and negative prompt.
tuple: A tuple containing the processed prompt, negative prompt and all the prompt variables.
"""
all_variables = {}
try:
if seed == -1:
seed = np.random.randint(0, 2**32, dtype=np.int64)
@@ -477,26 +519,28 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
if self.debug_level != DEBUG_LEVEL.none:
self.logger.info(f"System variables: {self.system_variables}")
self.logger.info(f"Input seed: {seed}")
self.logger.info(self.formatOutput(f"Input prompt: {prompt}"))
self.logger.info(self.formatOutput(f"Input negative_prompt: {negative_prompt}"))
self.logger.info(self.format_output(f"Input prompt: {prompt}"))
self.logger.info(self.format_output(f"Input negative_prompt: {negative_prompt}"))
t1 = time.time()
prompt, negative_prompt = self.__processprompts(prompt, negative_prompt)
prompt, negative_prompt, all_variables = self.__processprompts(prompt, negative_prompt)
t2 = time.time()
if self.debug_level != DEBUG_LEVEL.none:
self.logger.info(self.formatOutput(f"Result prompt: {prompt}"))
self.logger.info(self.formatOutput(f"Result negative_prompt: {negative_prompt}"))
self.logger.info(self.format_output(f"Result prompt: {prompt}"))
self.logger.info(self.format_output(f"Result negative_prompt: {negative_prompt}"))
self.logger.info(f"Process prompt pair time: {t2 - t1:.3f} seconds")
# if self.debug_level != DEBUG_LEVEL.none:
# self.logger.debug(f"Wildcards memory usage: {self.wildcard_obj.__sizeof__()}")
# Check for constructs not processed due to parsing problems
fullcontent: str = prompt + negative_prompt
if fullcontent.find("<ppp:") >= 0:
self.logger.error("Found unprocessed constructs in prompt or negative prompt! Stopping the generation.")
prompt = self.UNPROCESSED_STOP + prompt
self.interrupt()
return prompt, negative_prompt
return prompt, negative_prompt, all_variables
except Exception as e: # pylint: disable=broad-exception-caught
self.logger.exception(e)
return original_prompt, original_negative_prompt
return original_prompt, original_negative_prompt, all_variables
def parse_prompt(self, prompt_description: str, prompt: str, parser: lark.Lark, raise_parsing_error: bool = False):
"""
@@ -514,7 +558,7 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
t1 = time.time()
try:
if self.debug_level == DEBUG_LEVEL.full:
self.logger.debug(self.formatOutput(f"Parsing {prompt_description}: '{prompt}'"))
self.logger.debug(self.format_output(f"Parsing {prompt_description}: '{prompt}'"))
parsed_prompt = parser.parse(prompt)
# we store the contents so we can use them later even if the meta position is not valid anymore
for n in parsed_prompt.iter_subtrees():
@@ -526,7 +570,7 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
except lark.exceptions.UnexpectedInput:
if raise_parsing_error:
raise
self.logger.exception(self.formatOutput(f"Parsing failed on prompt!: {prompt}"))
self.logger.exception(self.format_output(f"Parsing failed on prompt!: {prompt}"))
t2 = time.time()
if self.debug_level == DEBUG_LEVEL.full:
self.logger.debug("Tree:\n" + textwrap.indent(re.sub(r"\n$", "", parsed_prompt.pretty()), " "))
@@ -671,6 +715,9 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
self.result += v
return v
def get_final_user_variable(self, name: str) -> str:
return self.__get_user_variable_value(name, True, False)
def __set_user_variable_value(self, name: str, value: str):
"""
Set the value of a user variable.
@@ -707,10 +754,10 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
if output != "":
output = f" >> '{output}'"
self.__ppp.logger.debug(
self.__ppp.formatOutput(f"TreeProcessor.{construct} {info}({duration:.3f} seconds){output}")
self.__ppp.format_output(f"TreeProcessor.{construct} {info}({duration:.3f} seconds){output}")
)
def __eval_condition(self, cond_var: str, cond_comp: str, cond_value: str | list[str]) -> bool:
def __eval_basiccondition(self, cond_var: str, cond_comp: str, cond_value: str | list[str]) -> bool:
"""
Evaluate a condition based on the given variable, comparison, and value.
@@ -722,10 +769,16 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
Returns:
bool: The result of the condition evaluation.
"""
var_value = self.__ppp.system_variables.get(cond_var, self.__get_user_variable_value(cond_var))
if var_value is None:
var_value = ""
self.__ppp.logger.warning(f"Unknown variable {cond_var}")
if cond_var.startswith("_"): # system variable
var_value = self.__ppp.system_variables.get(cond_var, None)
if var_value is None:
var_value = ""
self.__ppp.logger.warning(f"Unknown system variable {cond_var}")
else: # user variable
var_value = self.__get_user_variable_value(cond_var)
if var_value is None:
var_value = ""
self.__ppp.logger.warning(f"Unknown user variable {cond_var}")
if isinstance(var_value, str):
var_value = var_value.lower()
if isinstance(cond_value, list):
@@ -751,7 +804,7 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
(
c[1:-1].lower()
if c.startswith('"') or c.startswith("'")
else True if c.lower() == "true" else False if c.lower() == "false" else int(c)
else True if c.lower() == "true" else False if c.lower() == "false" or c == "" else int(c)
)
for c in cond_value
)
@@ -762,7 +815,7 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
if isinstance(c, str)
else (
True
if isinstance(c, bool) and var_value != "false" and var_value is not False
if isinstance(c, bool) and var_value != "false" and var_value != "" and var_value is not False
else (
False
if isinstance(c, bool) and (var_value != "true" or var_value is False)
@@ -775,7 +828,7 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
break
return result
def __evaluate_if(self, condition: lark.Tree) -> bool:
def __eval_condition(self, condition: lark.Tree) -> bool:
"""
Evaluate an if condition based on the given condition tree.
@@ -785,45 +838,54 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
Returns:
bool: The result of the if condition evaluation.
"""
individualcondition: lark.Tree = condition.children[0]
# we get the name of the variable and check for a preceding not
invert = False
first = individualcondition.children[0].value # it should be a Token
if first == "not":
invert = True
cond_var = individualcondition.children[1].value # it should be a Token
poscomp = 2
else:
cond_var = first
# self.__ppp.logger.debug(f"__eval_condition {condition.data}")
if condition.data == "operation_and":
cond_result = True
for c in condition.children:
cond_result = cond_result and self.__eval_condition(c)
if not cond_result:
break
elif condition.data == "operation_or":
cond_result = False
for c in condition.children:
cond_result = cond_result or self.__eval_condition(c)
if cond_result:
break
elif condition.data == "operation_not":
cond_result = not self.__eval_condition(condition.children[0])
else: # truthy_operand / comparison_simple_value / comparison_list_value
# we get the name of the variable
cond_var = condition.children[0].value # it should be a Token
poscomp = 1
if poscomp >= len(individualcondition.children):
# no condition, just a variable
cond_comp = "truthy"
cond_value = "true"
else:
# we get the comparison (with possible not) and the value
cond_comp = individualcondition.children[poscomp].value # it should be a Token
if cond_comp == "not":
invert = not invert
invert = False
if poscomp >= len(condition.children):
# no condition, just a variable
cond_comp = "truthy"
cond_value = "true"
else:
# we get the comparison (with possible not) and the value
cond_comp = condition.children[poscomp].value # it should be a Token
if cond_comp == "not":
invert = not invert
poscomp += 1
cond_comp = condition.children[poscomp].value # it should be a Token
poscomp += 1
cond_comp = individualcondition.children[poscomp].value # it should be a Token
poscomp += 1
cond_value_node = individualcondition.children[poscomp]
cond_value = (
list(v.value for v in cond_value_node.children)
if isinstance(cond_value_node, (lark.Tree, list))
else cond_value_node.value if isinstance(cond_value_node, lark.Token) else cond_value_node
)
cond_result = self.__eval_condition(cond_var, cond_comp, cond_value)
if invert:
cond_result = not cond_result
cond_value_node = condition.children[poscomp]
cond_value = (
list(v.value for v in cond_value_node.children)
if isinstance(cond_value_node, (lark.Tree, list))
else cond_value_node.value if isinstance(cond_value_node, lark.Token) else cond_value_node
)
cond_result = self.__eval_basiccondition(cond_var, cond_comp, cond_value)
if invert:
cond_result = not cond_result
return cond_result
def promptcomp(self, tree: lark.Tree):
"""
Process a prompt composition construct in the tree.
"""
# if self.__ppp.isComfyUI():
# if self.__ppp.is_comfy_ui():
# self.__ppp.logger.warning("Prompt composition is not supported in ComfyUI.")
start_result = self.result
t1 = time.time()
@@ -852,7 +914,7 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
"""
Process a scheduling construct in the tree and add it to the accumulated shell.
"""
# if self.__ppp.isComfyUI():
# if self.__ppp.is_comfy_ui():
# self.__ppp.logger.warning("Prompt scheduling is not supported in ComfyUI.")
start_result = self.result
t1 = time.time()
@@ -888,7 +950,7 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
"""
Process an alternation construct in the tree and add it to the accumulated shell.
"""
# if self.__ppp.isComfyUI():
# if self.__ppp.is_comfy_ui():
# self.__ppp.logger.warning("Prompt alternation is not supported in ComfyUI.")
start_result = self.result
t1 = time.time()
@@ -944,7 +1006,7 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
weight = math.floor(weight * 100) / 100 # we round to 2 decimals
weight_str = f"{weight:.2f}".rstrip("0").rstrip(".")
self.__shell.append(self.AccumulatedShell("at", weight))
if weight == 0.9 and not self.__ppp.isComfyUI():
if weight == 0.9 and not self.__ppp.is_comfy_ui():
starttag = "["
self.result += starttag
self.__visit(current_tree)
@@ -1017,8 +1079,7 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
self,
command: str,
variable: str,
immediateevaluation: str | None,
adding: str | None,
modifiers: lark.Tree | None,
content: lark.Tree | None,
):
"""
@@ -1033,7 +1094,8 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
info = variable
value_description = self.__get_original_node_content(content, None)
value = content
if adding is not None:
modifiers_str: list[str] = [m.value for m in modifiers.children] if modifiers is not None else []
if any(item in modifiers_str for item in ["+", "add"]):
info += f" += '{value_description}'"
raw_oldvalue = self.__ppp.user_variables.get(variable, None)
if raw_oldvalue is None:
@@ -1051,19 +1113,28 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
[raw_oldvalue, value],
# Meta should be {"content": raw_oldvalue.meta.content + value.meta.content},
)
elif any(item in modifiers_str for item in ["?", "ifundefined"]):
info += f" ?= '{value_description}'"
raw_oldvalue = self.__ppp.user_variables.get(variable, None)
if raw_oldvalue is None:
newvalue = value
else:
info += " (not set)"
newvalue = None
else:
newvalue = value
if immediateevaluation is not None:
newvalue = self.__visit(newvalue, False, True)
info += " =! "
else:
info += " = "
self.__set_user_variable_value(variable, newvalue)
currentvalue = self.__get_user_variable_value(variable, False)
if currentvalue is None:
info += "not evaluated yet"
else:
info += f"'{currentvalue}'"
if newvalue is not None:
if any(item in modifiers_str for item in ["!", "evaluate"]):
newvalue = self.__visit(newvalue, False, True)
info += " =! "
else:
info += " = "
self.__set_user_variable_value(variable, newvalue)
currentvalue = self.__get_user_variable_value(variable, False)
if currentvalue is None:
info += "not evaluated yet"
else:
info += f"'{currentvalue}'"
t2 = time.time()
self.__debug_end(command, start_result, t2 - t1, info)
@@ -1071,13 +1142,18 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
"""
Process a DP set variable command in the tree and add it to the dictionary of variables.
"""
self.__varset("variableset", tree.children[0], tree.children[2], tree.children[1], tree.children[3])
modifiers = tree.children[1] or lark.Tree(lark.Token("RULE", "variablesetmodifiers"), [])
immediate = tree.children[2]
if immediate is not None:
modifiers.children = modifiers.children.copy()
modifiers.children.append(immediate)
self.__varset("variableset", str(tree.children[0]), modifiers, tree.children[3])
def commandset(self, tree: lark.Tree):
"""
Process a set command in the tree and add it to the dictionary of variables.
"""
self.__varset("commandset", tree.children[0], tree.children[1], tree.children[2], tree.children[3])
self.__varset("commandset", str(tree.children[0]), tree.children[1], tree.children[2])
def __varecho(self, command: str, variable: str, default: lark.Tree | None):
"""
@@ -1090,7 +1166,9 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
value = self.__get_user_variable_value(variable, True, True)
if value is None:
if default is not None:
self.result += self.__visit(default, False, True)
v = self.__visit(default, False, True)
self.__ppp.echoed_variables[variable] = v
self.result += v
else:
self.__ppp.logger.warning(f"Unknown variable {variable}")
t2 = time.time()
@@ -1103,13 +1181,13 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
"""
Process a DP use variable command in the tree.
"""
self.__varecho("variableuse", tree.children[0], tree.children[1])
self.__varecho("variableuse", str(tree.children[0]), tree.children[1])
def commandecho(self, tree: lark.Tree):
"""
Process an echo command in the tree.
"""
self.__varecho("commandecho", tree.children[0], tree.children[1])
self.__varecho("commandecho", str(tree.children[0]), tree.children[1])
def commandif(self, tree: lark.Tree):
"""
@@ -1123,7 +1201,7 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
# has a condition
condition = n.children[0]
c = self.__get_original_node_content(condition, f"condition {i}")
if self.__evaluate_if(condition):
if self.__eval_condition(condition):
self.__visit(content)
t2 = time.time()
self.__debug_end("commandif", start_result, t2 - t1, c)
@@ -1147,13 +1225,13 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
t2 = time.time()
self.__debug_end("extranetworktag", start_result, t2 - t1)
def __get_choices(
def __get_choices_internal(
self,
options: dict | None,
choice_values: list[dict],
filter_specifier: Optional[list[list[str]]] = None,
wildcard_key: str = None,
) -> str:
) -> tuple[str, list[str], str, str]:
"""
Select choices based on the options.
@@ -1164,7 +1242,7 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
wildcard_key (str): The wildcard key if it is a wildcard.
Returns:
str: The selected choice.
tuple: A tuple containing the prefix, selected choices, separator and suffix
"""
if options is None:
options = {}
@@ -1181,7 +1259,7 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
msg = f"wildcard '{wildcard_key}'" if wildcard_key else "choices"
self.__ppp.logger.warning(f"Unsupported sampler '{sampler}' in {msg} options!")
self.__ppp.interrupt()
return ""
return ("", [], separator, "")
if filter_specifier is not None:
filtered_choice_values = []
for i, c in enumerate(choice_values):
@@ -1201,15 +1279,13 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
break
if passes:
filtered_choice_values.append(c)
if len(filtered_choice_values) == 0:
if not filtered_choice_values:
self.__ppp.logger.warning(
f"Wildcard filter specifier '{','.join(['+'.join(y for y in x) for x in filter_specifier])}' found no matches in choices for wildcard '{wildcard_key}'!"
)
else:
filtered_choice_values = choice_values.copy()
if len(filtered_choice_values) == 0:
num_choices = 0
else:
if filtered_choice_values:
if from_value < 0:
from_value = 1
elif from_value > len(filtered_choice_values):
@@ -1223,11 +1299,13 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
if from_value < to_value
else from_value
)
else:
num_choices = 0
if num_choices < 2:
repeating = False
if self.__ppp.debug_level == DEBUG_LEVEL.full:
self.__ppp.logger.debug(
self.__ppp.formatOutput(
self.__ppp.format_output(
f"Selecting {'repeating ' if repeating else ''}{num_choices} choice"
+ (f"s and separating with '{separator}'" if num_choices > 1 else "")
)
@@ -1241,8 +1319,8 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
for i, c in enumerate(filtered_choice_values):
c["choice_index"] = i # we index them to later sort the results
weight = float(c.get("weight", 1.0))
theif = c.get("if", None)
if weight > 0 and (theif is None or self.__evaluate_if(theif)):
condition = c.get("if", None)
if weight > 0 and (condition is None or self.__eval_condition(condition)):
available_choices.append(c)
weights.append(weight)
included_choices += 1
@@ -1290,8 +1368,18 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
suffix = " " + suffix
# remove comments
results = [re.sub(r"\s*#[^\n]*(?:\n|$)", "", r, flags=re.DOTALL) for r in selected_choices_text]
return prefix + separator.join(results) + suffix
return ""
return (prefix, results, separator, suffix)
return ("", "", separator, "")
def __get_choices(
self,
options: dict | None,
choice_values: list[dict],
filter_specifier: Optional[list[list[str]]] = None,
wildcard_key: str = None,
) -> str:
r = self.__get_choices_internal(options, choice_values, filter_specifier, wildcard_key)
return r[0] + r[2].join(r[1]) + r[3]
def __convert_choices_options(self, options: Optional[lark.Tree]) -> dict:
"""
@@ -1305,12 +1393,12 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
"""
if options is None:
return None
the_options = {}
options_dict = {}
if len(options.children) == 1:
the_options["sampler"] = options.children[0] if options.children[0] is not None else "~"
options_dict["sampler"] = options.children[0] if options.children[0] is not None else "~"
else:
the_options["sampler"] = options.children[0].children[0] if options.children[0] is not None else "~"
the_options["repeating"] = (
options_dict["sampler"] = options.children[0].children[0] if options.children[0] is not None else "~"
options_dict["repeating"] = (
options.children[1].children[0] == "r" if options.children[1] is not None else False
)
if len(options.children) == 4:
@@ -1321,16 +1409,16 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
ifrom = 2
ito = 3
isep = 4
the_options["from"] = (
options_dict["from"] = (
int(options.children[ifrom].children[0]) if options.children[ifrom] is not None else 1
)
the_options["to"] = int(options.children[ito].children[0]) if options.children[ito] is not None else 1
the_options["separator"] = (
options_dict["to"] = int(options.children[ito].children[0]) if options.children[ito] is not None else 1
options_dict["separator"] = (
self.__visit(options.children[isep], False, True)
if options.children[isep] is not None
else self.__ppp.wil_choice_separator
)
return the_options
return options_dict
def __convert_choice(self, choice: lark.Tree) -> dict:
"""
@@ -1342,17 +1430,17 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
Returns:
dict: The converted choice.
"""
the_choice = {}
choice_dict = {}
c_label_obj = choice.children[0]
the_choice["labels"] = (
choice_dict["labels"] = (
[x.value.lower() for x in c_label_obj.children[1:-1]] # should be a token
if c_label_obj is not None
else []
)
the_choice["weight"] = float(choice.children[1].children[0]) if choice.children[1] is not None else 1.0
the_choice["if"] = choice.children[2].children[0] if choice.children[2] is not None else None
the_choice["content"] = choice.children[3]
return the_choice
choice_dict["weight"] = float(choice.children[1].children[0]) if choice.children[1] is not None else 1.0
choice_dict["if"] = choice.children[2].children[0] if choice.children[2] is not None else None
choice_dict["content"] = choice.children[3]
return choice_dict
def __check_wildcard_initialization(self, wildcard: PPPWildcard):
"""
@@ -1414,15 +1502,15 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
for cv in wildcard.unprocessed_choices[n:]:
if isinstance(cv, dict):
if self.__ppp.wildcard_obj.is_dict_choice_options(cv):
theif = cv.get("if", None)
if theif is not None and isinstance(theif, str):
condition = cv.get("if", None)
if condition is not None and isinstance(condition, str):
try:
cv["if"] = self.__ppp.parse_prompt(
"condition", theif, self.__ppp.parser_condition, True
"condition", condition, self.__ppp.parser_condition, True
)
except lark.exceptions.UnexpectedInput as e:
self.__ppp.logger.warning(
f"Error parsing condition '{theif}' in wildcard '{wildcard.key}'! : {e.__class__.__name__}"
f"Error parsing condition '{condition}' in wildcard '{wildcard.key}'! : {e.__class__.__name__}"
)
cv["if"] = None
content = cv.get("content", cv.get("text", None))
@@ -1477,13 +1565,13 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
if self.__ppp.debug_level == DEBUG_LEVEL.full:
self.__ppp.logger.debug(f"Processing wildcard: {wildcard_key}")
selected_wildcards = self.__ppp.wildcard_obj.get_wildcards(wildcard_key)
if len(selected_wildcards) == 0:
if not selected_wildcards:
self.detectedWildcards.append(wc)
self.result += wc
t2 = time.time()
self.__debug_end("wildcard", start_result, t2 - t1, wc)
return
filter_specifier = None
filter_specifier: list[int | str] = None
filter_object = tree.children[2]
if filter_object is not None:
if (
@@ -1508,7 +1596,7 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
if (
len(selected_wildcards) > 1
and filter_specifier is not None
and any(x.isdecimal() for x in reduce(lambda x, y: x + y, filter_specifier))
and any(x.isdecimal() for x in filter_specifier)
):
self.__ppp.logger.warning(
f"Using a globbing wildcard '{wildcard_key}' with positional index filters is not recommended!"
@@ -1632,7 +1720,7 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
self.__already_processed.append(content)
if self.__ppp.debug_level == DEBUG_LEVEL.full:
self.__ppp.logger.debug(
self.__ppp.formatOutput(f"Adding content at position {position}: {content}")
self.__ppp.format_output(f"Adding content at position {position}: {content}")
)
if position == "e":
self.add_at["end"].append(content)
@@ -1642,6 +1730,6 @@ class PromptPostProcessor: # pylint: disable=too-few-public-methods,too-many-in
else: # position == "s" or invalid
self.add_at["start"].append(content)
else:
self.__ppp.logger.warning(self.__ppp.formatOutput(f"Ignoring repeated content: {content}"))
self.__ppp.logger.warning(self.__ppp.format_output(f"Ignoring repeated content: {content}"))
t2 = time.time()
self.__debug_end("start", "", t2 - t1)
+8 -1
View File
@@ -1,15 +1,20 @@
from collections import OrderedDict
from logging import Logger
from typing import Tuple
from ppp_logging import DEBUG_LEVEL
class PPPLRUCache:
ProcessInput = Tuple[int, int, str, str] # (seed, wildcards_hash, positive_prompt, negative_prompt)
ProcessResult = Tuple[str, str] # (positive_prompt, negative_prompt)
def __init__(self, capacity: int):
def __init__(self, capacity: int, logger: Logger = None, debug_level: DEBUG_LEVEL = DEBUG_LEVEL.none):
self.cache = OrderedDict()
self.capacity = capacity
self._logger = logger
self._debug_level = debug_level
def get(self, key: ProcessInput) -> ProcessResult:
if key not in self.cache:
@@ -22,3 +27,5 @@ class PPPLRUCache:
self.cache.move_to_end(key)
if len(self.cache) > self.capacity:
self.cache.popitem(last=False)
# if self._logger is not None and self._debug_level != DEBUG_LEVEL.none:
# self._logger.debug(f"Cache size: {self.cache.__sizeof__()}")
+79 -13
View File
@@ -15,6 +15,9 @@ if __name__ == "__main__":
class PromptPostProcessorComfyUINode:
"""
Node for processing prompts.
"""
logger = None
@@ -95,12 +98,13 @@ class PromptPostProcessorComfyUINode:
"forceInput": False,
},
),
"pony_substrings": (
"variants_definitions": (
"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",
"default": PromptPostProcessor.DEFAULT_VARIANTS_DEFINITIONS,
"multiline": True,
"placeholder": "",
"tooltip": "Definitions for variant models to be recognized based on strings found in the full filename. Format for each line is: 'name(kind)=comma separated list of substrings (case insensitive)' with kind being one of the base model types or not specified",
"defaultInput": False,
"forceInput": False,
},
@@ -314,10 +318,12 @@ class PromptPostProcessorComfyUINode:
RETURN_TYPES = (
"STRING",
"STRING",
"PPP_DICT",
)
RETURN_NAMES = (
"pos_prompt",
"neg_prompt",
"variables",
)
FUNCTION = "process"
@@ -333,7 +339,7 @@ class PromptPostProcessorComfyUINode:
neg_prompt,
seed,
debug_level, # pylint: disable=unused-argument
pony_substrings,
variants_definitions,
wc_process_wildcards,
wc_wildcards_folders,
wc_if_wildcards,
@@ -354,14 +360,16 @@ class PromptPostProcessorComfyUINode:
remove_extranetwork_tags,
):
if wc_process_wildcards:
return float("NaN") # since we can't detect changes in wildcards we assume they are always changed when enabled
return float(
"NaN"
) # since we can't detect changes in wildcards we assume they are always changed when enabled
new_run = { # everything except debug_level
"model": model,
"modelname": modelname,
"pos_prompt": pos_prompt,
"neg_prompt": neg_prompt,
"seed": seed,
"pony_substrings": pony_substrings,
"variants_definitions": variants_definitions,
"process_wildcards": wc_process_wildcards,
"wildcards_folders": wc_wildcards_folders,
"if_wildcards": wc_if_wildcards,
@@ -392,7 +400,7 @@ class PromptPostProcessorComfyUINode:
neg_prompt,
seed,
debug_level,
pony_substrings,
variants_definitions,
wc_process_wildcards,
wc_wildcards_folders,
wc_if_wildcards,
@@ -431,11 +439,11 @@ class PromptPostProcessorComfyUINode:
),
"is_ssd": modelclass in ("SSD1B",),
"is_sd3": modelclass in ("SD3",),
"is_flux": modelclass in ("Flux",),
"is_flux": modelclass in ("Flux", "FluxInpaint", "FluxSchnell"),
"is_auraflow": modelclass in ("AuraFlow",),
}
# SVD_img2vid, SVD3D_u, SVD3_p, Stable_Zero123, SD_X4Upscaler,
# Stable_Cascade_C, Stable_Cascade_B, StableAudio
# Also supported: SVD_img2vid, SVD3D_u, SVD3_p, Stable_Zero123, SD_X4Upscaler,
# Stable_Cascade_C, Stable_Cascade_B, StableAudio, HunyuanDiT, HunyuanDiT1, GenmoMochi, LTXV
if wc_wildcards_folders == "":
wc_wildcards_folders = ",".join(folder_paths.get_folder_paths("wildcards") or [])
@@ -446,9 +454,11 @@ class PromptPostProcessorComfyUINode:
for f in wc_wildcards_folders.split(",")
if f.strip() != ""
]
if variants_definitions != "" and not "=" in variants_definitions: # mainly to warn about the old format
raise ValueError("Invalid variants_definitions format")
options = {
"debug_level": debug_level,
"pony_substrings": pony_substrings,
"variants_definitions": variants_definitions,
"process_wildcards": wc_process_wildcards,
"if_wildcards": wc_if_wildcards,
"choice_separator": wc_choice_separator,
@@ -471,11 +481,67 @@ class PromptPostProcessorComfyUINode:
ppp = PromptPostProcessor(
self.logger, self.interrupt, env_info, options, self.grammar_content, self.wildcards_obj
)
pos_prompt, neg_prompt = ppp.process_prompt(pos_prompt, neg_prompt, seed if seed is not None else 1)
pos_prompt, neg_prompt, variables = ppp.process_prompt(pos_prompt, neg_prompt, seed if seed is not None else 1)
return (
pos_prompt,
neg_prompt,
variables,
)
def interrupt(self):
nodes.interrupt_processing(True)
class PromptPostProcessorSelectVariableComfyUINode:
"""
Node for selecting a variable from a dictionary.
"""
def __init__(self):
pass
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"variables": (
"PPP_DICT",
{
"forceInput": True,
},
),
},
"optional": {
"name": (
"STRING",
{
"placeholder": "variable name",
"multiline": False,
"default": "",
"dynamicPrompts": False,
"defaultInput": False,
"forceInput": False,
},
),
},
}
RETURN_TYPES = ("STRING",)
RETURN_NAMES = ("value",)
FUNCTION = "select"
CATEGORY = "ACB"
def select(
self,
variables: dict[str, str],
name: str,
):
value = ""
if variables:
if name == "":
value = "\n".join(f"{k}: {v}" for k, v in variables.items())
elif name in variables:
value = variables[name]
return (value,)
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

+64 -43
View File
@@ -51,6 +51,15 @@ class PPPWildcard:
t = (self.key, deep_freeze(self.unprocessed_choices))
return hash(t)
def __sizeof__(self):
return (
self.key.__sizeof__()
+ self.file.__sizeof__()
+ self.unprocessed_choices.__sizeof__()
+ self.choices.__sizeof__()
+ self.options.__sizeof__()
)
class PPPWildcards:
"""
@@ -72,6 +81,9 @@ class PPPWildcards:
def __hash__(self) -> int:
return hash(deep_freeze(self.wildcards))
def __sizeof__(self):
return self.wildcards.__sizeof__() + self.__wildcards_folders.__sizeof__() + self.__wildcard_files.__sizeof__()
def refresh_wildcards(self, debug_level: DEBUG_LEVEL, wildcards_folders: Optional[list[str]]):
"""
Initialize the wildcards.
@@ -229,51 +241,60 @@ class PPPWildcards:
Returns:
list: list of choices
"""
choices = None
if obj is not None:
if isinstance(obj, (str, dict)):
choices = [obj]
elif isinstance(obj, (int, float, bool)):
choices = [str(obj)]
elif isinstance(obj, list) and len(obj) > 0:
choices = []
for i, c in enumerate(obj):
invalid_choice = False
if isinstance(c, str):
choice = c
elif isinstance(c, (int, float, bool)):
choice = str(c)
elif isinstance(c, list):
# we create an anonymous wildcard
choice = self.__create_anonymous_wildcard(full_path, key_parts, i, c)
elif isinstance(c, dict):
if self.is_dict_choices_options(c) or self.is_dict_choice_options(c):
# we assume it is a choice or wildcard parameters in object format
choice = c
choice_content = choice.get("content", choice.get("text", None))
if choice_content is not None and isinstance(choice_content, list):
# we create an anonymous wildcard
choice["content"] = self.__create_anonymous_wildcard(
full_path, key_parts, i, choice_content
)
if "text" in choice:
del choice["text"]
elif len(c) == 1:
# we assume it is an anonymous wildcard with options
firstkey = list(c.keys())[0]
choice = self.__create_anonymous_wildcard(full_path, key_parts, i, c[firstkey], firstkey)
else:
invalid_choice = True
else:
invalid_choice = True
if invalid_choice:
self.__logger.warning(
f"Invalid choice {i+1} in wildcard '{'/'.join(key_parts)}' in file '{full_path}'!"
)
else:
choices.append(choice)
if obj is None:
return None
if isinstance(obj, (str, dict)):
return [obj]
if isinstance(obj, (int, float, bool)):
return [str(obj)]
if not isinstance(obj, list) or len(obj) == 0:
self.__logger.warning(f"Invalid format in wildcard '{'/'.join(key_parts)}' in file '{full_path}'!")
return None
choices = []
for i, c in enumerate(obj):
if isinstance(c, (str, int, float, bool)):
choices.append(str(c))
elif isinstance(c, list):
# we create an anonymous wildcard
choices.append(self.__create_anonymous_wildcard(full_path, key_parts, i, c))
elif isinstance(c, dict):
choices.append(self.__process_dict_choice(c, full_path, key_parts, i))
else:
self.__logger.warning(
f"Invalid choice {i+1} in wildcard '{'/'.join(key_parts)}' in file '{full_path}'!"
)
return choices
def __process_dict_choice(self, c: dict, full_path: str, key_parts: list[str], i: int) -> dict:
"""
Process a dictionary choice.
Args:
c (dict): The dictionary choice.
full_path (str): The path to the file.
key_parts (list[str]): The parts of the key.
i (int): The index of the choice.
Returns:
dict: The processed choice.
"""
if self.is_dict_choices_options(c) or self.is_dict_choice_options(c):
# we assume it is a choice or wildcard parameters in object format
choice = c
choice_content = choice.get("content", choice.get("text", None))
if choice_content is not None and isinstance(choice_content, list):
# we create an anonymous wildcard
choice["content"] = self.__create_anonymous_wildcard(full_path, key_parts, i, choice_content)
if "text" in choice:
del choice["text"]
return choice
if len(c) == 1:
# we assume it is an anonymous wildcard with options
firstkey = list(c.keys())[0]
return self.__create_anonymous_wildcard(full_path, key_parts, i, c[firstkey], firstkey)
self.__logger.warning(f"Invalid choice {i+1} in wildcard '{'/'.join(key_parts)}' in file '{full_path}'!")
return None
def __create_anonymous_wildcard(self, full_path, key_parts, i, content, options=None):
"""
Create an anonymous wildcard.
+4 -4
View File
@@ -1,9 +1,9 @@
[project]
name = "sd-webui-prompt-postprocessor"
description = "Stable Diffusion WebUI & ComfyUI extension to post-process the prompt, including sending content from the prompt to the negative prompt and wildcards."
version = "2.8.0"
license = {file = "LICENSE.txt"}
dependencies = ["lark"]
version = "2.9.0"
license = { file = "LICENSE.txt" }
dependencies = ["lark", "numpy", "pyyaml"]
[project.urls]
Repository = "https://github.com/acorderob/sd-webui-prompt-postprocessor"
@@ -12,4 +12,4 @@ Repository = "https://github.com/acorderob/sd-webui-prompt-postprocessor"
[tool.comfy]
PublisherId = "acorderob"
DisplayName = "sd-webui-prompt-postprocessor"
Icon = ""
Icon = "ppp_icon.png"
+2
View File
@@ -1 +1,3 @@
lark
numpy
pyyaml
+156 -50
View File
@@ -37,6 +37,16 @@ class PromptPostProcessorA1111Script(scripts.Script):
__on_ui_settings(): Callback function for UI settings.
"""
instance_count = 0
@classmethod
def increment_instance_count(cls):
cls.instance_count += 1
@classmethod
def get_instance_count(cls):
return cls.instance_count
def __init__(self):
"""
Initializes the PromptPostProcessor object.
@@ -49,16 +59,19 @@ class PromptPostProcessorA1111Script(scripts.Script):
Returns:
None
"""
self.increment_instance_count()
lf = PromptPostProcessorLogFactory()
self.name = PromptPostProcessor.NAME
self.ppp_logger = lf.log
self.ppp_debug_level = DEBUG_LEVEL(getattr(opts, "ppp_gen_debug_level", DEBUG_LEVEL.none.value))
self.lru_cache = PPPLRUCache(1000)
self.lru_cache = PPPLRUCache(1000, logger=self.ppp_logger, debug_level=self.ppp_debug_level)
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)
self.ppp_logger.info(f"{PromptPostProcessor.NAME} {PromptPostProcessor.VERSION} initialized")
i = self.get_instance_count()
if i == 1: # some UIs create multiple instances
self.ppp_logger.info(f"{PromptPostProcessor.NAME} {PromptPostProcessor.VERSION} initialized")
def title(self):
"""
@@ -86,48 +99,67 @@ class PromptPostProcessorA1111Script(scripts.Script):
force_equal_seeds = gr.Checkbox(
label="Force equal seeds",
info="Force all image seeds and variation seeds to be equal to the first one, disabling the default autoincrease.",
default=False,
value=False,
# show_label=True,
elem_id="ppp_force_equal_seeds",
)
gr.HTML(
"""<br><div>Unlink the seed to use the specified one for the prompts instead of the image seed.
This seed will only change for each image in the batch if the value is -1 or 'variable seed' is checked.</div>
<div>Seeds are only used for the wildcards and choice constructs.</div>"""
gr.HTML("<br>")
gr.Markdown(
"""
Unlink the seed to use the specified one for the prompts instead of the image seed.
* A seed of -1 and "Incremental seed" checked will use a random seed for the first prompt and consecutive values for the rest. This is the same as when you use -1 for the image seed.
* A seed of -1 and "Incremental seed" unchecked will use a random seed for each prompt.
* Any other seed value and "Incremental seed" checked will use the specified seed for the first prompt and consecutive values for the rest.
* Any other seed value and "Incremental seed" unchecked will use the specified seed for all the prompts.
Seeds are only used for the wildcards and choice constructs.
"""
)
unlink_seed = gr.Checkbox(
label="Unlink seed",
default=False,
# show_label=True,
elem_id="ppp_unlink_seed",
)
seed = gr.Number(
label="Seed",
default=-1,
precision=0,
# minimum=-1,
# maximum=2**32 - 1,
# step=1,
# show_label=True,
min_width=100,
elem_id="ppp_seed",
)
variable_seed = gr.Checkbox(
label="Variable seed",
default=False,
# show_label=True,
elem_id="ppp_variable_seed",
)
return [force_equal_seeds, unlink_seed, seed, variable_seed]
gr.HTML("<br>")
with gr.Row(equal_height=True):
unlink_seed = gr.Checkbox(
label="Unlink seed",
value=False,
# show_label=True,
elem_id="ppp_unlink_seed",
)
seed = gr.Number(
label="Prompt seed",
value=-1,
precision=0,
# minimum=-1,
# maximum=2**32 - 1,
# step=1,
# show_label=True,
min_width=100,
elem_id="ppp_seed",
)
incremental_seed = gr.Checkbox(
label="Incremental seed (only applies to batches)",
value=False,
# show_label=True,
elem_id="ppp_incremental_seed",
)
return [force_equal_seeds, unlink_seed, seed, incremental_seed]
def process(
self, p: StableDiffusionProcessing, input_force_equal_seeds, input_unlink_seed, input_seed, input_variable_seed
self,
p: StableDiffusionProcessing,
input_force_equal_seeds,
input_unlink_seed,
input_seed,
input_incremental_seed,
): # pylint: disable=arguments-differ
"""
Processes the prompts and applies post-processing operations.
Args:
p (StableDiffusionProcessing): The StableDiffusionProcessing object containing the prompts.
input_force_equal_seeds (bool): Flag indicating whether to force equal seeds.
input_unlink_seed (bool): Flag indicating whether to unlink the seed.
input_seed (int): The seed value.
input_incremental_seed (bool): Flag indicating whether to use incremental seed.
Returns:
None
@@ -135,20 +167,53 @@ class PromptPostProcessorA1111Script(scripts.Script):
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
init_images = getattr(p, "init_images", [None]) or [None]
is_i2i = bool(init_images[0])
self.ppp_debug_level = DEBUG_LEVEL(getattr(opts, "ppp_gen_debug_level", DEBUG_LEVEL.none.value))
do_i2i = getattr(opts, "ppp_gen_doi2i", False)
if is_i2i and not do_i2i:
if self.ppp_debug_level != DEBUG_LEVEL.none:
self.ppp_logger.info("Not processing the prompt for i2i")
return
if self.ppp_debug_level != DEBUG_LEVEL.none:
self.ppp_logger.info(f"Post-processing prompts ({'i2i' if is_i2i else 't2i'})")
p.extra_generation_params.update(
{
"PPP force equal seeds": input_force_equal_seeds,
"PPP unlink seed": input_unlink_seed,
"PPP prompt seed": input_seed,
"PPP incremental seed": input_incremental_seed,
}
)
app_names = {
"sdnext": "SD.Next",
"forge": "Forge",
"reforge": "reForge",
"a1111": "A1111 (or compatible)",
}
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"
else (
"reforge"
if hasattr(p.sd_model, "forge_objects")
else ("sdnext" if hasattr(p.sd_model, "is_sdxl") and not hasattr(p.sd_model, "is_ssd") else "a1111")
)
)
if self.ppp_debug_level != DEBUG_LEVEL.none:
self.ppp_logger.info(f"Post-processing prompts ({'i2i' if is_i2i else 't2i'}) running on {app_names[app]}")
models_supported = {x: True for x in PromptPostProcessor.SUPPORTED_MODELS}
if app == "sdnext":
models_supported["ssd"] = False
elif app == "forge":
models_supported["ssd"] = False
models_supported["auraflow"] = False
elif app == "reforge":
models_supported["flux"] = False
models_supported["auraflow"] = False
else: # assume A1111 compatible
models_supported["flux"] = False
models_supported["auraflow"] = False
env_info = {
"app": app,
"models_path": models_path,
@@ -173,15 +238,28 @@ class PromptPostProcessorA1111Script(scripts.Script):
env_info["is_sd3"] = p.sd_model.__class__.__name__ == "StableDiffusion3Pipeline"
env_info["is_flux"] = p.sd_model.__class__.__name__ == "FluxPipeline"
env_info["is_auraflow"] = p.sd_model.__class__.__name__ == "AuraFlowPipeline"
# also supports 'Latent Consistency Model': LatentConsistencyModelPipeline', 'PixArt-Alpha': 'PixArtAlphaPipeline', 'UniDiffuser': 'UniDiffuserPipeline', 'Wuerstchen': 'WuerstchenCombinedPipeline', 'Kandinsky 2.1': 'KandinskyPipeline', 'Kandinsky 2.2': 'KandinskyV22Pipeline', 'Kandinsky 3': 'Kandinsky3Pipeline', 'DeepFloyd IF': 'IFPipeline', 'Custom Diffusers Pipeline': 'DiffusionPipeline', 'InstaFlow': 'StableDiffusionPipeline', 'SegMoE': 'StableDiffusionPipeline', 'Kolors': 'KolorsPipeline', 'AuraFlow': 'AuraFlowPipeline', 'CogView': 'CogView3PlusPipeline'
elif app == "forge":
# from repositories\huggingface_guess\huggingface_guess\model_list.py
env_info["model_class"] = p.sd_model.model_config.__class__.__name__
env_info["is_sd1"] = getattr(p.sd_model, "is_sd1", False)
env_info["is_sd2"] = getattr(p.sd_model, "is_sd2", False)
env_info["is_sdxl"] = getattr(p.sd_model, "is_sdxl", False)
env_info["is_ssd"] = False # ?
env_info["is_sd3"] = getattr(
p.sd_model, "is_sd3", False
) # p.sd_model.model_config.__class__.__name__ == "SD3" # not actually supported?
env_info["is_flux"] = p.sd_model.model_config.__class__.__name__ in ("Flux", "FluxSchnell")
env_info["is_auraflow"] = False # p.sd_model.model_config.__class__.__name__ == "AuraFlow" # not supported
elif app == "reforge":
env_info["model_class"] = p.sd_model.__class__.__name__
env_info["is_sd1"] = getattr(p.sd_model, "is_sd1", False)
env_info["is_sd2"] = getattr(p.sd_model, "is_sd2", False)
env_info["is_sdxl"] = getattr(p.sd_model, "is_sdxl", False)
env_info["is_ssd"] = getattr(p.sd_model, "is_ssd", False)
env_info["is_sd3"] = getattr(p.sd_model, "is_sd3", False)
env_info["is_flux"] = p.sd_model.model_config.__class__.__name__ == "Flux"
env_info["is_auraflow"] = False # p.sd_model.model_config.__class__.__name__ == "AuraFlow"
env_info["is_flux"] = False
env_info["is_auraflow"] = False
else: # assume A1111 compatible (p.sd_model.__class__.__name__=="DiffusionEngine")
env_info["model_class"] = p.sd_model.__class__.__name__
env_info["is_sd1"] = getattr(p.sd_model, "is_sd1", False)
@@ -201,7 +279,9 @@ class PromptPostProcessorA1111Script(scripts.Script):
]
options = {
"debug_level": getattr(opts, "ppp_gen_debug_level", DEBUG_LEVEL.none.value),
"pony_substrings": getattr(opts, "ppp_gen_ponysubstrings", PromptPostProcessor.DEFAULT_PONY_SUBSTRINGS),
"variants_definitions": getattr(
opts, "ppp_gen_variantsdefinitions", PromptPostProcessor.DEFAULT_VARIANTS_DEFINITIONS
),
"process_wildcards": getattr(opts, "ppp_wil_processwildcards", True),
"if_wildcards": getattr(opts, "ppp_wil_ifwildcards", PromptPostProcessor.IFWILDCARDS_CHOICES.ignore.value),
"choice_separator": getattr(opts, "ppp_wil_choice_separator", PromptPostProcessor.DEFAULT_CHOICE_SEPARATOR),
@@ -240,10 +320,11 @@ class PromptPostProcessorA1111Script(scripts.Script):
if self.ppp_debug_level != DEBUG_LEVEL.none:
self.ppp_logger.info("Using unlinked seed")
num_seeds = len(getattr(p, "all_seeds", []))
if input_seed == -1:
if input_incremental_seed:
first_seed = np.random.randint(0, 2**32, dtype=np.int64) if input_seed == -1 else input_seed
calculated_seeds = [first_seed + i for i in range(num_seeds)]
elif input_seed == -1:
calculated_seeds = np.random.randint(0, 2**32, size=num_seeds, dtype=np.int64)
elif input_variable_seed:
calculated_seeds = [input_seed + i for i in range(num_seeds)]
else:
calculated_seeds = [input_seed for _ in range(num_seeds)]
else:
@@ -261,31 +342,51 @@ class PromptPostProcessorA1111Script(scripts.Script):
else:
calculated_seeds = seeds
# initialize extra generation parameters
extra_params = {}
# adds regular prompts
rpr = getattr(p, "all_prompts", None)
rnr = getattr(p, "all_negative_prompts", None)
rpr: list[str] = getattr(p, "all_prompts", None)
rnr: list[str] = getattr(p, "all_negative_prompts", None)
if rpr is not None and rnr is not None:
extra_params.update(
{
"PPP original prompts": rpr.copy(),
"PPP original negative prompts": rnr.copy(),
}
)
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
rph = getattr(p, "all_hr_prompts", None)
rnh = getattr(p, "all_hr_negative_prompts", None)
rph: list[str] = getattr(p, "all_hr_prompts", None)
rnh: list[str] = getattr(p, "all_hr_negative_prompts", None)
if rph is not None and rnh is not None:
extra_params.update(
{
"PPP original HR prompts": rph.copy(),
"PPP original HR negative prompts": rnh.copy(),
}
)
prompts_list += [
("hiresfix", seed, prompt, negative_prompt)
for seed, prompt, negative_prompt in zip(calculated_seeds, rph, rnh)
if (seed, prompt, negative_prompt) not in prompts_list
]
# fill extra generation parameters only if not already present
for k, v in extra_params.items():
if p.extra_generation_params.get(k) is None:
p.extra_generation_params[k] = v
# 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, hash(self.wildcards_obj), prompt, negative_prompt)) is None:
posp, negp = ppp.process_prompt(prompt, negative_prompt, seed)
posp, negp, _ = ppp.process_prompt(prompt, negative_prompt, seed)
self.lru_cache.put((seed, hash(self.wildcards_obj), prompt, negative_prompt), (posp, negp))
# adds also the result so i2i doesn't process it unnecessarily
self.lru_cache.put((seed, hash(self.wildcards_obj), posp, negp), (posp, negp))
@@ -378,10 +479,15 @@ def on_ui_settings():
),
)
shared.opts.add_option(
key="ppp_gen_ponysubstrings",
key="ppp_gen_variantsdefinitions",
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)",
PromptPostProcessor.DEFAULT_VARIANTS_DEFINITIONS,
label="Definitions for variant models",
comment_after="Recognized based on strings found in the full filename. Format for each line is: 'name(kind)=comma separated list of substrings (case insensitive)' with kind being one of the base model types ("
+ ",".join(PromptPostProcessor.SUPPORTED_MODELS)
+ ") or not specified.",
component=gr.Textbox,
component_args={"lines": 7},
section=section,
),
)
+117 -3
View File
@@ -29,7 +29,7 @@ class TestPromptPostProcessor(unittest.TestCase):
self.__ppp_logger.setLevel(logging.DEBUG)
self.__defopts = {
"debug_level": DEBUG_LEVEL.full.value,
"pony_substrings": PromptPostProcessor.DEFAULT_PONY_SUBSTRINGS,
"variants_definitions": PromptPostProcessor.DEFAULT_VARIANTS_DEFINITIONS,
"process_wildcards": True,
"if_wildcards": PromptPostProcessor.IFWILDCARDS_CHOICES.ignore.value,
"choice_separator": ", ",
@@ -141,7 +141,7 @@ class TestPromptPostProcessor(unittest.TestCase):
the_obj = ppp or self.__defppp
out = expected_output_prompts if isinstance(expected_output_prompts, list) else [expected_output_prompts]
for eo in out:
result_prompt, result_negative_prompt = the_obj.process_prompt(
result_prompt, result_negative_prompt, _ = the_obj.process_prompt(
input_prompts.prompt,
input_prompts.negative_prompt,
seed,
@@ -355,7 +355,7 @@ class TestPromptPostProcessor(unittest.TestCase):
def test_cmd_if_nested(self): # nested if command
self.__process(
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 <ppp:if _sd eq 'sd1'>SD1<ppp:else><ppp:if _is_pony>PONY<ppp:else>SD2<ppp:/if><ppp:/if><ppp:if _is_sdxl_no_pony>NOPONY<ppp:/if><ppp:if _is_pure_sdxl>NOPONY<ppp:/if>", ""
),
PromptPair("this is PONY", ""),
ppp=PromptPostProcessor(
@@ -392,6 +392,48 @@ class TestPromptPostProcessor(unittest.TestCase):
PromptPair("OK OK OK", ""),
)
def test_cmd_set_if_complex_conditions_1(self): # complex conditions (or)
self.__process(
PromptPair("<ppp:set v1>true<ppp:/set><ppp:set v2>false<ppp:/set>this test is <ppp:if v1 or v2>OK<ppp:else>not OK<ppp:/if>", ""),
PromptPair("this test is OK", ""),
)
def test_cmd_set_if_complex_conditions_2(self): # complex conditions (and)
self.__process(
PromptPair("<ppp:set v1>true<ppp:/set><ppp:set v2>true<ppp:/set>this test is <ppp:if v1 and v2>OK<ppp:else>not OK<ppp:/if>", ""),
PromptPair("this test is OK", ""),
)
def test_cmd_set_if_complex_conditions_3(self): # complex conditions (not)
self.__process(
PromptPair("<ppp:set v1>false<ppp:/set>this test is <ppp:if not v1>OK<ppp:else>not OK<ppp:/if>", ""),
PromptPair("this test is OK", ""),
)
def test_cmd_set_if_complex_conditions_4(self): # complex conditions (not, precedence)
self.__process(
PromptPair("<ppp:set v1>true<ppp:/set><ppp:set v2>false<ppp:/set>this test is <ppp:if not (v1 and v2)>OK<ppp:else>not OK<ppp:/if>", ""),
PromptPair("this test is OK", ""),
)
def test_cmd_set_if_complex_conditions_5(self): # complex conditions (not, precedence, comparison)
self.__process(
PromptPair("<ppp:set v1>1<ppp:/set><ppp:set v2>false<ppp:/set>this test is <ppp:if not(v1 eq '1' and v2)>OK<ppp:else>not OK<ppp:/if>", ""),
PromptPair("this test is OK", ""),
)
def test_cmd_set_if_complex_conditions_6(self): # complex conditions
self.__process(
PromptPair("<ppp:set v1>1<ppp:/set><ppp:set v2>2<ppp:/set><ppp:set v3>3<ppp:/set>this test is <ppp:if v1 eq '1' and v2 eq '2' and v3 eq '3'>OK<ppp:else>not OK<ppp:/if>", ""),
PromptPair("this test is OK", ""),
)
def test_cmd_set_if_complex_conditions_7(self): # complex conditions
self.__process(
PromptPair("<ppp:set v1>1<ppp:/set><ppp:set v2>2<ppp:/set><ppp:set v3>3<ppp:/set>this test is <ppp:if v1 eq '1' and v2 not eq '2' or v3 eq '3'>OK<ppp:else>not OK<ppp:/if>", ""),
PromptPair("this test is OK", ""),
)
def test_cmd_set_if2(self): # set and more complex if commands
self.__process(
PromptPair(
@@ -442,6 +484,42 @@ class TestPromptPostProcessor(unittest.TestCase):
ppp=self.__nocupppp,
)
def test_cmd_set_ifundefined_if(self): # set, ifundefined and if commands
self.__process(
PromptPair(
"<ppp:set v ifundefined>value<ppp:/set>this test is <ppp:if v eq 'value'>OK<ppp:else>not OK<ppp:/if>",
"",
),
PromptPair("this test is OK", ""),
)
def test_cmd_set_ifundefined_if_2(self): # set, ifundefined and if commands
self.__process(
PromptPair(
"<ppp:set v>value<ppp:/set><ppp:set v ifundefined>value2<ppp:/set>this test is <ppp:if v eq 'value'>OK<ppp:else>not OK<ppp:/if>",
"",
),
PromptPair("this test is OK", ""),
)
def test_cmd_set_ifundefined_DP_if(self): # set, ifundefined (DP format) and if commands
self.__process(
PromptPair(
"${v?=value}this test is <ppp:if v eq 'value'>OK<ppp:else>not OK<ppp:/if>",
"",
),
PromptPair("this test is OK", ""),
)
def test_cmd_set_ifundefined_DP_if_2(self): # set, ifundefined (DP format) and if commands
self.__process(
PromptPair(
"${v=!value}${v?=!value2}this test is <ppp:if v eq 'value'>OK<ppp:else>not OK<ppp:/if>",
"",
),
PromptPair("this test is OK", ""),
)
# Choices tests
def test_ch_choices(self): # simple choices with weights
@@ -686,6 +764,20 @@ class TestPromptPostProcessor(unittest.TestCase):
ppp=self.__nocupppp,
)
def test_wc_test2_yaml(self): # simple yaml wildcard
self.__process(
PromptPair("the choice is: __testwc/test2__", ""),
PromptPair("the choice is: 2", ""),
ppp=self.__nocupppp,
)
def test_wc_test3_yaml(self): # simple yaml wildcard
self.__process(
PromptPair("the choice is: __testwc/test3__", ""),
PromptPair("the choice is: one choice", ""),
ppp=self.__nocupppp,
)
def test_wc_wildcard_filter_index(self): # wildcard with positional index filter
self.__process(
PromptPair("the choice is: __yaml/wildcard2'2'__", ""),
@@ -824,6 +916,28 @@ class TestPromptPostProcessor(unittest.TestCase):
ppp=self.__nocupppp,
)
# Model variants tests
def test_variants(self):
self.__process(
PromptPair("<ppp:if _is_test1>test1<ppp:/if><ppp:if _is_test2>test2<ppp:/if><ppp:if _is_test3>test3<ppp:/if><ppp:if _is_test4>test4<ppp:/if>", ""),
PromptPair("test1test2", ""),
ppp=PromptPostProcessor(
self.__ppp_logger,
self.__interrupt,
{
**self.__def_env_info,
"model_filename": "./webui/models/Stable-diffusion/testmodel.safetensors",
},
{
**self.__defopts,
"variants_definitions": "test1(sdxl)=testmodel\ntest2=testmodel\ntest3(sd1)=testmodel\ntest4(invalid)=testmodel\nsdxl()=testmodel",
},
self.__grammar_content,
self.__wildcards_obj,
),
)
# ComfyUI tests
def test_comfyui_attention(self): # attention conversion