Compare commits

..
13 Commits
Author SHA1 Message Date
Antonio Cordero Balcazar bfc47d0d53 * A1111: add prompts to metadata only if they change 2025-05-03 12:07:18 +02:00
Antonio Cordero Balcazar b837f38422 * if_wildcards option defaults now to stop.
* ComfyUI: new wildcards_input.
* removed json import since yaml can read them.
* more models detected in ComfyUI.
* ComfyUI: example workflow.
2025-05-03 12:07:18 +02:00
Antonio Cordero Balcazar 5e3c8cbc44 * ComfyUI: updates to the ComfyUI registry workflow.
* A1111: option to add original prompts to metadata.
* fixed some bogus import error shenanigans.
* warn when a model name detects multiple variants.
2025-04-11 17:50:26 +02:00
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
Antonio Cordero Balcazar 3810320f6e * Simpler versioning
* Cache now takes wildcard definitions into consideration.
* ComfyUI: node is not cached if wildcard processing is active.
* Add a space with prefix and suffix if needed.
* Checks requirements before installing them.
2024-10-27 16:05:55 +01:00
Antonio Cordero Balcazar c87c24924e * Support anonymous wildcards. 2024-10-19 16:05:21 +02:00
Antonio Cordero Balcazar e9092b877c * Improved wildcard definition support. 2024-10-19 12:45:48 +02:00
Antonio Cordero Balcazar a18e5e659c * Improved processing of wildcards and choices.
* Support for prefix and suffix in wildcard options and object format of the options in json/yaml files.
2024-10-13 12:57:25 +02:00
Antonio Cordero Balcazar 93f0102897 * Fix a bug with the variable echo command.
* Fix a bug obtaining random seed.
* Reading version number from pyproject file.
* A1111: Support for forcing the same seed/variation seed to all images in a batch.
* A1111: Support for unlinking the seed from the image seed and specifying one for the prompt.
* ComfyUI: Warning on use of invalid constructs. Related settings kept but default to False.
2024-10-11 18:13:57 +02:00
22 changed files with 1970 additions and 860 deletions
+5 -3
View File
@@ -8,17 +8,19 @@ on:
paths: paths:
- "pyproject.toml" - "pyproject.toml"
permissions:
issues: write
jobs: jobs:
publish-node: publish-node:
name: Publish Custom Node to registry name: Publish Custom Node to registry
runs-on: ubuntu-latest runs-on: ubuntu-latest
# if this is a forked repository. Skipping the workflow. if: ${{ github.repository_owner == 'acorderob' }}
if: github.event.repository.fork == false
steps: steps:
- name: Check out code - name: Check out code
uses: actions/checkout@v4 uses: actions/checkout@v4
- name: Publish Custom Node - name: Publish Custom Node
uses: Comfy-Org/publish-node-action@main uses: Comfy-Org/publish-node-action@v1
with: with:
## Add your own personal access token to your Github Repository secrets and reference it here. ## Add your own personal access token to your Github Repository secrets and reference it here.
personal_access_token: ${{ secrets.REGISTRY_ACCESS_TOKEN }} personal_access_token: ${{ secrets.REGISTRY_ACCESS_TOKEN }}
+2 -1
View File
@@ -1,5 +1,6 @@
{ {
"default": true, "default": true,
"MD013": false, "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 // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0", "version": "0.2.0",
"configurations": [ "configurations": [
{
"name": "Python Debugger: Attach using Process Id",
"type": "debugpy",
"request": "attach",
"processId": "${command:pickProcess}"
},
{ {
"name": "Tests", "name": "Tests",
"type": "debugpy", "type": "debugpy",
+20 -339
View File
@@ -3,9 +3,9 @@
The Prompt PostProcessor (PPP), formerly known as "sd-webui-sendtonegative", is an extension designed to process the prompt, possibly after other extensions have modified it. This extension is compatible with: The Prompt PostProcessor (PPP), formerly known as "sd-webui-sendtonegative", is an extension designed to process the prompt, possibly after other extensions have modified it. This extension is compatible with:
* [AUTOMATIC1111 Stable Diffusion WebUI](https://github.com/AUTOMATIC1111/stable-diffusion-webui) * [AUTOMATIC1111 Stable Diffusion WebUI](https://github.com/AUTOMATIC1111/stable-diffusion-webui)
* [SD.Next](https://github.com/vladmandic/automatic).
* [Forge](https://github.com/lllyasviel/stable-diffusion-webui-forge) * [Forge](https://github.com/lllyasviel/stable-diffusion-webui-forge)
* [reForge](https://github.com/Panchovix/stable-diffusion-webui-reForge) * [reForge](https://github.com/Panchovix/stable-diffusion-webui-reForge)
* [SD.Next](https://github.com/vladmandic/automatic)
* ...and probably other forks * ...and probably other forks
* [ComfyUI](https://github.com/comfyanonymous/ComfyUI) * [ComfyUI](https://github.com/comfyanonymous/ComfyUI)
@@ -17,372 +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. * Process wildcards. Compatible with Dynamic Prompts formats. Can also detect invalid wildcards and act as you choose.
* Clean up the prompt and negative prompt. * Clean up the prompt and negative prompt.
Note: 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". 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: 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)` * **Attention**: `[prompt] (prompt) (prompt:weight)`
* **Alternation**: `\[prompt1|prompt2|...\]` * **Alternation**: `[prompt1|prompt2|...]`
* **Scheduling**: `\[prompt1:prompt2:step\]` * **Scheduling**: `[prompt1:prompt2:step]`
* **Extra networks**: `\<kind:model...\>` * **Extra networks**: `<kind:model...>`
* **BREAK**: `prompt1 BREAK prompt2` * **BREAK**: `prompt1 BREAK prompt2`
* **Composable Diffusion**: `prompt1:weight1 AND prompt2:weight2` * **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.
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. 3. It does not create *AND/BREAK* constructs when moving content to the negative prompt.
## Installation ## Installation
On A1111 compatible webuis: On *A1111* compatible webuis:
1. Go to Extensions > Install from URL 1. Go to Extensions > Install from URL
2. Paste <https://github.com/acorderob/sd-webui-prompt-postprocessor> in the URL for extension's git repository text field 2. Paste <https://github.com/acorderob/sd-webui-prompt-postprocessor> in the URL for extension's git repository text field
3. Click the Install button 3. Click the Install button
4. Restart the webui 4. Restart the webui
On ComfyUI: On *SD.Next* I recommend you disable the native wildcard processing.
On *ComfyUI*:
1. Go to Manager > Custom Nodes Manager 1. Go to Manager > Custom Nodes Manager
2. Install through ComfyUI Manager 2. Search for "Prompt PostProcessor" and install or click Install via Git URL and enter <https://github.com/acorderob/sd-webui-prompt-postprocessor>
3. Click Install via Git URL and enter <https://github.com/acorderob/sd-webui-prompt-postprocessor> 3. Restart
4. Restart
## Usage ## Usage
### Commands See the [syntax documentation](docs/SYNTAX.md).
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.
The filter can be used to filter specific choices from the wildcard. The filtering works before applying the choice conditions (if any). The surrounding quotes can be single or double. The filter is a comma separated list of an integer (positional choice index) or choice label. You can also compound them with "+". That is, the comma separated items act as an OR and the "+" inside them as an AND. Using labels can simplify the definitions of complex wildcards where you want to have direct access to specific choices on occasion (you don't need to create wildcards for each individual choice). There are some additional formats when using filters. You can specify "^wildcard" as a filter to use the filter of a previous wildcard in the chain. You can start the filter (regular or inherited) with "#" and it will not be applied to the current wildcard choices, but the filter will remain in memory to use by other descendant wildcards. You use "#" and "^" when you want to pass a filter to inner wildcards (see the test files).
The variable value only applies during the evaluation of the selected choices and is discarded afterward (the variable keeps its original value if there was one).
These are examples of formats you can use to insert a wildcard:
```text
__path/wildcard__ # select 1 choice
__path/wildcard'0'__ # select the first choice
__path/wildcard'label'__ # select the choices with label "label"
__path/wildcard'0,label1,label2'__ # select the first choice and those with labels "label1" or "label2"
__path/wildcard'0,label1+label2'__ # select the first choice and those with both labels "label1" and "label2"
__3$$path/wildcard__ # select 3 choices
__2-3$$path/wildcard__ # select 2 to 3 choices
__r2-3$$path/wildcard__ # select 2 to 3 choices allowing repetition
__2-3$$ / $$path/wildcard__ # select 2 to 3 choices with separator " / "
__path/wildcard(var=value)__ # select 1 choice using the specified variable value in the evaluation.
```
#### Wildcard definitions
A wildcard definition can be:
* A txt file. The wildcard name will be the relative path of the file, without the extension. Each line will be a choice. Lines starting with "#" or empty are ignored.
* An array or string inside a json or yaml file. The wildcard name includes the relative folder path of the file (without the name or extension) but also the path of the object inside the file.
In a choice, the content after a "#" is ignored.
If the first choice follows the format of wildcard parameters, it will be used as default parameters for that wildcard (see examples in the tests folder). The choices of the wildcard follow the same format as in the choices construct, or the object format of **Dynamic Prompts** (only in structured files). If using the object format for a choice you can use a new "if" property for the condition in addition to the standard "weight" and "text"/"content".
Wildcards can contain just one choice. In json and yaml formats this allows the use of a string value for the keys, rather than an array.
#### Detection of remaining wildcards
This extension should run after any other wildcard extensions, so if you don't use the internal wildcards processing, any remaining wildcards present in the prompt or negative_prompt at this point must be invalid. Usually you might not notice this problem until you check the image metadata, so this option gives you some ways to detect and treat the problem.
### 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.
## Configuration ## Configuration
### ComfyUI specific inputs See the [configuration documentation](docs/CONFIG.md).
* **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.
### Content removal settings
* **Remove extra network tags**: removes all extra network tags.
## License ## License
+8 -12
View File
@@ -10,19 +10,15 @@ import os
sys.path.append(os.path.dirname(os.path.abspath(__file__))) 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_CLASS_MAPPINGS = {
"ACBPromptPostProcessor": PromptPostProcessorComfyUINode,
NODE_DISPLAY_NAME_MAPPINGS = {"ACBPromptPostProcessor": "ACB Prompt Post Processor"} "ACBPPPSelectVariable": PromptPostProcessorSelectVariableComfyUINode,
}
MANIFEST = { NODE_DISPLAY_NAME_MAPPINGS = {
"name": "ACB Prompt Post Processor", "ACBPromptPostProcessor": "ACB Prompt Post Processor",
"version": PromptPostProcessorComfyUINode.VERSION, "ACBPPPSelectVariable": "ACB PPP Select Variable",
"author": "ACB",
"project": "https://github.com/acorderob/sd-webui-prompt-postprocessor",
"description": "Node for processing prompts",
"license": "MIT",
} }
__all__ = ["NODE_CLASS_MAPPINGS", "NODE_DISPLAY_NAME_MAPPINGS"] __all__ = ["NODE_CLASS_MAPPINGS", "NODE_DISPLAY_NAME_MAPPINGS"]
+80
View File
@@ -0,0 +1,80 @@
# 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.
* **wc_wildcards_input**: Wildcards definitions (in yaml or json format). Direct input added to the ones found in the wildcards folders. Allows wildcards to be included in the workflow.
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*).
* **Add original prompts to metadata**: adds original prompts to the metadata if they have changed (*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+/ _WHITESPACE: /\s+/
STRING: /("(?!"").*?(?<!\\)(\\\\)*?"|'(?!'').*?(?<!\\)(\\\\)*?')/i STRING: /("(?!"").*?(?<!\\)(\\\\)*?"|'(?!'').*?(?<!\\)(\\\\)*?')/i
BOOLEAN: /true|false/i BOOLEAN: /true|false/i
INDEX: INT | IDENTIFIER
WILDCARD_NAME: /(?:(?!__|\$\$|[('"])\S)+/ WILDCARD_NAME: /(?:(?!__|\$\$|[('"])\S)+/
INDEX: INT | IDENTIFIER
IDENTIFIER: CNAME
SIMPLEVALUE: STRING | NUMBER | BOOLEAN
// plain text and weights // plain text and weights
?plain: /((?!__|\bAND\b|\${)[^\\()\[\]:<>${]|\\.)+/s // exclude only the starting ones ?plain: /((?!__|\bAND\b|\${)[^\\()\[\]:<>${]|\\.)+/s // exclude only the starting ones
@@ -58,22 +60,29 @@ commandif_else: "<ppp:else" _WHITESPACE? ">" ifvalue
ifvalue: content ifvalue: content
// conditions // conditions
condition: conditionsimplevalue | conditionlistvalue | conditionnocomparison ?condition: grouped_condition | ungrouped_condition
conditionnocomparison: ( /not/ _WHITESPACE )? IDENTIFIER ?ungrouped_condition: operation | basic_condition
conditionsimplevalue: ( /not/ _WHITESPACE )? IDENTIFIER _WHITESPACE ( /not/ _WHITESPACE )? /eq|ne|gt|lt|ge|le|contains/ _WHITESPACE SIMPLEVALUE ?grouped_condition: "(" _WHITESPACE? condition _WHITESPACE? ")"
conditionlistvalue: ( /not/ _WHITESPACE )? IDENTIFIER _WHITESPACE ( /not/ _WHITESPACE )? /contains|in/ _WHITESPACE listvalue ?basic_condition: comparison_simple_value | comparison_list_value | truthy_operand
IDENTIFIER: CNAME ?operation: operation_not | operation_and | operation_or
SIMPLEVALUE: STRING | NUMBER | BOOLEAN 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? ")" listvalue: "(" _WHITESPACE? SIMPLEVALUE ( _WHITESPACE? "," _WHITESPACE? SIMPLEVALUE )* _WHITESPACE? ")"
// command: set // 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 // command: echo
commandecho: "<ppp:echo" _WHITESPACE IDENTIFIER _WHITESPACE? ">" [ content "<ppp:/echo>" ] commandecho: "<ppp:echo" _WHITESPACE IDENTIFIER _WHITESPACE? ">" [ content "<ppp:/echo>" ]
// variable set // variable set
variableset.2: "${" _WHITESPACE? IDENTIFIER _WHITESPACE? [ /\+/ ] "=" [ /!/ ] varvalue "}" variableset.2: "${" _WHITESPACE? IDENTIFIER [ variablesetmodifiers ] _WHITESPACE? "=" [ /!/ ] varvalue "}"
variablesetmodifiers: /[+?!]+/
// variable use // variable use
variableuse.2: "${" _WHITESPACE? IDENTIFIER _WHITESPACE? [ ":" varvalue ] "}" variableuse.2: "${" _WHITESPACE? IDENTIFIER _WHITESPACE? [ ":" varvalue ] "}"
+10 -2
View File
@@ -1,5 +1,13 @@
import os import os
import launch
requirements_filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), "requirements.txt") requirements_filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), "requirements.txt")
launch.run_pip(f'install -r "{requirements_filename}"', "requirements for Prompt Post-Processor")
try:
from modules.launch_utils import requirements_met, run_pip # A1111
if not requirements_met(requirements_filename):
run_pip(f'install -r "{requirements_filename}"', "requirements for Prompt Post-Processor")
except ImportError:
import launch
launch.run_pip(f'install -r "{requirements_filename}"', "requirements for Prompt Post-Processor")
+585 -241
View File
File diff suppressed because it is too large Load Diff
+10 -3
View File
@@ -1,15 +1,20 @@
from collections import OrderedDict from collections import OrderedDict
from logging import Logger
from typing import Tuple from typing import Tuple
from ppp_logging import DEBUG_LEVEL # pylint: disable=import-error
class PPPLRUCache: class PPPLRUCache:
ProcessInput = Tuple[int, str, str] ProcessInput = Tuple[int, int, str, str] # (seed, wildcards_hash, positive_prompt, negative_prompt)
ProcessResult = Tuple[str, str] 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.cache = OrderedDict()
self.capacity = capacity self.capacity = capacity
self._logger = logger
self._debug_level = debug_level
def get(self, key: ProcessInput) -> ProcessResult: def get(self, key: ProcessInput) -> ProcessResult:
if key not in self.cache: if key not in self.cache:
@@ -22,3 +27,5 @@ class PPPLRUCache:
self.cache.move_to_end(key) self.cache.move_to_end(key)
if len(self.cache) > self.capacity: if len(self.cache) > self.capacity:
self.cache.popitem(last=False) 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__()}")
+116 -69
View File
@@ -1,5 +1,3 @@
# pylint: disable=missing-module-docstring, missing-class-docstring, missing-function-docstring, invalid-name
import os import os
# pylint: disable=import-error # pylint: disable=import-error
@@ -15,8 +13,9 @@ if __name__ == "__main__":
class PromptPostProcessorComfyUINode: class PromptPostProcessorComfyUINode:
"""
VERSION = PromptPostProcessor.VERSION Node for processing prompts.
"""
logger = None logger = None
@@ -27,6 +26,7 @@ class PromptPostProcessorComfyUINode:
with open(grammar_filename, "r", encoding="utf-8") as file: with open(grammar_filename, "r", encoding="utf-8") as file:
self.grammar_content = file.read() self.grammar_content = file.read()
self.wildcards_obj = PPPWildcards(lf.log) self.wildcards_obj = PPPWildcards(lf.log)
self.logger.info(f"{PromptPostProcessor.NAME} {PromptPostProcessor.VERSION} initialized")
class SmartType(str): class SmartType(str):
def __ne__(self, other): def __ne__(self, other):
@@ -46,8 +46,6 @@ class PromptPostProcessorComfyUINode:
"multiline": True, "multiline": True,
"default": "", "default": "",
"dynamicPrompts": False, "dynamicPrompts": False,
"defaultInput": True,
"forceInput": False,
}, },
), ),
"neg_prompt": ( "neg_prompt": (
@@ -56,8 +54,6 @@ class PromptPostProcessorComfyUINode:
"multiline": True, "multiline": True,
"default": "", "default": "",
"dynamicPrompts": False, "dynamicPrompts": False,
"defaultInput": True,
"forceInput": False,
}, },
), ),
}, },
@@ -75,16 +71,13 @@ class PromptPostProcessorComfyUINode:
{ {
"default": "", "default": "",
"placeholder": "full path of the model", "placeholder": "full path of the model",
"defaultInput": True, "dynamicPrompts": False,
"forceInput": False,
}, },
), ),
"seed": ( "seed": (
"INT", "INT",
{ {
"default": -1, "default": -1,
"defaultInput": True,
"forceInput": False,
}, },
), ),
"debug_level": ( "debug_level": (
@@ -92,18 +85,16 @@ class PromptPostProcessorComfyUINode:
{ {
"default": DEBUG_LEVEL.minimal.value, "default": DEBUG_LEVEL.minimal.value,
"tooltip": "Debug level", "tooltip": "Debug level",
"defaultInput": False,
"forceInput": False,
}, },
), ),
"pony_substrings": ( "variants_definitions": (
"STRING", "STRING",
{ {
"default": PromptPostProcessor.DEFAULT_PONY_SUBSTRINGS, "default": PromptPostProcessor.DEFAULT_VARIANTS_DEFINITIONS,
"placeholder": "comma separated list", "multiline": True,
"tooltip": "Comma separated list of substrings to look for in the modelname to determine if the model is a pony model", "placeholder": "",
"defaultInput": False, "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",
"forceInput": False, "dynamicPrompts": False,
}, },
), ),
"wc_process_wildcards": ( "wc_process_wildcards": (
@@ -113,8 +104,6 @@ class PromptPostProcessorComfyUINode:
"tooltip": "Process wildcards in the prompt", "tooltip": "Process wildcards in the prompt",
"label_on": "Yes", "label_on": "Yes",
"label_off": "No", "label_off": "No",
"defaultInput": False,
"forceInput": False,
}, },
), ),
"wc_wildcards_folders": ( "wc_wildcards_folders": (
@@ -122,17 +111,24 @@ class PromptPostProcessorComfyUINode:
{ {
"default": "", "default": "",
"tooltip": "Comma separated list of wildcards folders", "tooltip": "Comma separated list of wildcards folders",
"defaultInput": False, "dynamicPrompts": False,
"forceInput": False, },
),
"wc_wildcards_input": (
"STRING",
{
"default": "",
"multiline": True,
"placeholder": "wildcards definitions",
"tooltip": "Wildcards definitions in yaml/json format",
"dynamicPrompts": False,
}, },
), ),
"wc_if_wildcards": ( "wc_if_wildcards": (
[e.value for e in PromptPostProcessor.IFWILDCARDS_CHOICES], [e.value for e in PromptPostProcessor.IFWILDCARDS_CHOICES],
{ {
"default": PromptPostProcessor.IFWILDCARDS_CHOICES.ignore.value, "default": PromptPostProcessor.IFWILDCARDS_CHOICES.stop.value,
"tooltip": "How to handle invalid wildcards in the prompt", "tooltip": "How to handle invalid wildcards in the prompt",
"defaultInput": False,
"forceInput": False,
}, },
), ),
"wc_choice_separator": ( "wc_choice_separator": (
@@ -140,8 +136,7 @@ class PromptPostProcessorComfyUINode:
{ {
"default": PromptPostProcessor.DEFAULT_CHOICE_SEPARATOR, "default": PromptPostProcessor.DEFAULT_CHOICE_SEPARATOR,
"tooltip": "Default separator for selected choices", "tooltip": "Default separator for selected choices",
"defaultInput": False, "dynamicPrompts": False,
"forceInput": False,
}, },
), ),
"wc_keep_choices_order": ( "wc_keep_choices_order": (
@@ -151,8 +146,6 @@ class PromptPostProcessorComfyUINode:
"tooltip": "Keep the order of the choices in the prompt", "tooltip": "Keep the order of the choices in the prompt",
"label_on": "Yes", "label_on": "Yes",
"label_off": "No", "label_off": "No",
"defaultInput": False,
"forceInput": False,
}, },
), ),
"stn_separator": ( "stn_separator": (
@@ -160,8 +153,7 @@ class PromptPostProcessorComfyUINode:
{ {
"default": PromptPostProcessor.DEFAULT_STN_SEPARATOR, "default": PromptPostProcessor.DEFAULT_STN_SEPARATOR,
"tooltip": "Separator for the content added to the negative prompt", "tooltip": "Separator for the content added to the negative prompt",
"defaultInput": False, "dynamicPrompts": False,
"forceInput": False,
}, },
), ),
"stn_ignore_repeats": ( "stn_ignore_repeats": (
@@ -171,8 +163,6 @@ class PromptPostProcessorComfyUINode:
"tooltip": "Ignore repeated content added to the negative prompt", "tooltip": "Ignore repeated content added to the negative prompt",
"label_on": "Yes", "label_on": "Yes",
"label_off": "No", "label_off": "No",
"defaultInput": False,
"forceInput": False,
}, },
), ),
"cleanup_extra_spaces": ( "cleanup_extra_spaces": (
@@ -182,8 +172,6 @@ class PromptPostProcessorComfyUINode:
"tooltip": "Remove extra spaces", "tooltip": "Remove extra spaces",
"label_on": "Yes", "label_on": "Yes",
"label_off": "No", "label_off": "No",
"defaultInput": False,
"forceInput": False,
}, },
), ),
"cleanup_empty_constructs": ( "cleanup_empty_constructs": (
@@ -193,8 +181,6 @@ class PromptPostProcessorComfyUINode:
"tooltip": "Remove empty constructs", "tooltip": "Remove empty constructs",
"label_on": "Yes", "label_on": "Yes",
"label_off": "No", "label_off": "No",
"defaultInput": False,
"forceInput": False,
}, },
), ),
"cleanup_extra_separators": ( "cleanup_extra_separators": (
@@ -204,8 +190,6 @@ class PromptPostProcessorComfyUINode:
"tooltip": "Remove extra separators", "tooltip": "Remove extra separators",
"label_on": "Yes", "label_on": "Yes",
"label_off": "No", "label_off": "No",
"defaultInput": False,
"forceInput": False,
}, },
), ),
"cleanup_extra_separators2": ( "cleanup_extra_separators2": (
@@ -215,19 +199,15 @@ class PromptPostProcessorComfyUINode:
"tooltip": "Remove extra separators (additional cases)", "tooltip": "Remove extra separators (additional cases)",
"label_on": "Yes", "label_on": "Yes",
"label_off": "No", "label_off": "No",
"defaultInput": False,
"forceInput": False,
}, },
), ),
"cleanup_breaks": ( "cleanup_breaks": (
"BOOLEAN", "BOOLEAN",
{ {
"default": True, "default": False,
"tooltip": "Cleanup around BREAKs", "tooltip": "Cleanup around BREAKs",
"label_on": "Yes", "label_on": "Yes",
"label_off": "No", "label_off": "No",
"defaultInput": False,
"forceInput": False,
}, },
), ),
"cleanup_breaks_eol": ( "cleanup_breaks_eol": (
@@ -237,19 +217,15 @@ class PromptPostProcessorComfyUINode:
"tooltip": "Set BREAKs in their own line", "tooltip": "Set BREAKs in their own line",
"label_on": "Yes", "label_on": "Yes",
"label_off": "No", "label_off": "No",
"defaultInput": False,
"forceInput": False,
}, },
), ),
"cleanup_ands": ( "cleanup_ands": (
"BOOLEAN", "BOOLEAN",
{ {
"default": True, "default": False,
"tooltip": "Cleanup around ANDs", "tooltip": "Cleanup around ANDs",
"label_on": "Yes", "label_on": "Yes",
"label_off": "No", "label_off": "No",
"defaultInput": False,
"forceInput": False,
}, },
), ),
"cleanup_ands_eol": ( "cleanup_ands_eol": (
@@ -259,8 +235,6 @@ class PromptPostProcessorComfyUINode:
"tooltip": "Set ANDs in their own line", "tooltip": "Set ANDs in their own line",
"label_on": "Yes", "label_on": "Yes",
"label_off": "No", "label_off": "No",
"defaultInput": False,
"forceInput": False,
}, },
), ),
"cleanup_extranetwork_tags": ( "cleanup_extranetwork_tags": (
@@ -270,8 +244,6 @@ class PromptPostProcessorComfyUINode:
"tooltip": "Clean up around extra network tags", "tooltip": "Clean up around extra network tags",
"label_on": "Yes", "label_on": "Yes",
"label_off": "No", "label_off": "No",
"defaultInput": False,
"forceInput": False,
}, },
), ),
"cleanup_merge_attention": ( "cleanup_merge_attention": (
@@ -281,8 +253,6 @@ class PromptPostProcessorComfyUINode:
"tooltip": "Merge nested attention constructs", "tooltip": "Merge nested attention constructs",
"label_on": "Yes", "label_on": "Yes",
"label_off": "No", "label_off": "No",
"defaultInput": False,
"forceInput": False,
}, },
), ),
"remove_extranetwork_tags": ( "remove_extranetwork_tags": (
@@ -292,8 +262,6 @@ class PromptPostProcessorComfyUINode:
"tooltip": "Remove extra network tags", "tooltip": "Remove extra network tags",
"label_on": "Yes", "label_on": "Yes",
"label_off": "No", "label_off": "No",
"defaultInput": False,
"forceInput": False,
}, },
), ),
}, },
@@ -315,10 +283,12 @@ class PromptPostProcessorComfyUINode:
RETURN_TYPES = ( RETURN_TYPES = (
"STRING", "STRING",
"STRING", "STRING",
"PPP_DICT",
) )
RETURN_NAMES = ( RETURN_NAMES = (
"pos_prompt", "pos_prompt",
"neg_prompt", "neg_prompt",
"variables",
) )
FUNCTION = "process" FUNCTION = "process"
@@ -334,9 +304,10 @@ class PromptPostProcessorComfyUINode:
neg_prompt, neg_prompt,
seed, seed,
debug_level, # pylint: disable=unused-argument debug_level, # pylint: disable=unused-argument
pony_substrings, variants_definitions,
wc_process_wildcards, wc_process_wildcards,
wc_wildcards_folders, wc_wildcards_folders,
wc_wildcards_input,
wc_if_wildcards, wc_if_wildcards,
wc_choice_separator, wc_choice_separator,
wc_keep_choices_order, wc_keep_choices_order,
@@ -354,15 +325,20 @@ class PromptPostProcessorComfyUINode:
cleanup_merge_attention, cleanup_merge_attention,
remove_extranetwork_tags, 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
new_run = { # everything except debug_level new_run = { # everything except debug_level
"model": model, "model": model,
"modelname": modelname, "modelname": modelname,
"pos_prompt": pos_prompt, "pos_prompt": pos_prompt,
"neg_prompt": neg_prompt, "neg_prompt": neg_prompt,
"seed": seed, "seed": seed,
"pony_substrings": pony_substrings, "variants_definitions": variants_definitions,
"process_wildcards": wc_process_wildcards, "process_wildcards": wc_process_wildcards,
"wildcards_folders": wc_wildcards_folders, "wildcards_folders": wc_wildcards_folders,
"wildcards_input": wc_wildcards_input,
"if_wildcards": wc_if_wildcards, "if_wildcards": wc_if_wildcards,
"choice_separator": wc_choice_separator, "choice_separator": wc_choice_separator,
"keep_choices_order": wc_keep_choices_order, "keep_choices_order": wc_keep_choices_order,
@@ -391,9 +367,10 @@ class PromptPostProcessorComfyUINode:
neg_prompt, neg_prompt,
seed, seed,
debug_level, debug_level,
pony_substrings, variants_definitions,
wc_process_wildcards, wc_process_wildcards,
wc_wildcards_folders, wc_wildcards_folders,
wc_wildcards_input,
wc_if_wildcards, wc_if_wildcards,
wc_choice_separator, wc_choice_separator,
wc_keep_choices_order, wc_keep_choices_order,
@@ -418,23 +395,33 @@ class PromptPostProcessorComfyUINode:
self.logger.warning("Model class is not provided. System variables might not be properly set.") self.logger.warning("Model class is not provided. System variables might not be properly set.")
if modelname == "": if modelname == "":
self.logger.warning("Modelname is not provided. System variables will not be properly set.") self.logger.warning("Modelname is not provided. System variables will not be properly set.")
# model class values in ComfyUI\comfy\supported_models.py
env_info = { env_info = {
"app": "comfyui", "app": "comfyui",
"models_path": folder_paths.models_dir, "models_path": folder_paths.models_dir,
"model_filename": modelname or "", # path is relative to checkpoints folder "model_filename": modelname or "", # path is relative to checkpoints folder
"model_class": modelclass, "model_class": modelclass,
"is_sd1": modelclass in ("SD15", "SD15_instructpix2pix"), "is_sd1": modelclass in ("SD15", "SD15_instructpix2pix"),
"is_sd2": modelclass in ("SD20", "SD21UnclipL", "SD21UnclipH"), "is_sd2": modelclass in ("SD20", "SD21UnclipL", "SD21UnclipH", "LotusD"),
"is_sdxl": ( "is_sdxl": (
modelclass in ("SDXL", "SDXLRefiner", "SDXL_instructpix2pix", "Segmind_Vega", "KOALA_700M", "KOALA_1B") modelclass in ("SDXL", "SDXLRefiner", "SDXL_instructpix2pix", "Segmind_Vega", "KOALA_700M", "KOALA_1B")
), ),
"is_ssd": modelclass in ("SSD1B",), "is_ssd": modelclass in ("SSD1B",),
"is_sd3": modelclass in ("SD3",), "is_sd3": modelclass in ("SD3",),
"is_flux": modelclass in ("Flux",), "is_flux": modelclass in ("Flux", "FluxInpaint", "FluxSchnell"),
"is_auraflow": modelclass in ("AuraFlow",), "is_auraflow": modelclass in ("AuraFlow",),
"is_pixart": modelclass in ("PixArtAlpha", "PixArtSigma"),
"is_lumina2": modelclass in ("Lumina2",),
"is_ltxv": modelclass in ("LTXV",),
"is_cosmos": modelclass in ("CosmosT2V", "CosmosI2V"),
"is_genmomochi": modelclass in ("GenmoMochi",),
"is_hunyuan": modelclass in ("HunyuanDiT", "HunyuanDiT1"),
"is_hunyuanvideo": modelclass in ("HunyuanVideo", "HunyuanVideoI2V", "HunyuanVideoSkyreelsI2V"),
"is_hunyuan3d": modelclass in ("Hunyuan3Dv2", "Hunyuan3Dv2mini"),
"is_wanvideo": modelclass in ("WAN21_T2V", "WAN21_I2V", "WAN21_FunControl2V"),
"is_hidream": modelclass in ("HiDream",),
} }
# SVD_img2vid, SVD3D_u, SVD3_p, Stable_Zero123, SD_X4Upscaler, # Also supported: SVD_img2vid, SVD3D_u, SVD3_p, Stable_Zero123, SD_X4Upscaler, Stable_Cascade_C, Stable_Cascade_B, StableAudio
# Stable_Cascade_C, Stable_Cascade_B, StableAudio
if wc_wildcards_folders == "": if wc_wildcards_folders == "":
wc_wildcards_folders = ",".join(folder_paths.get_folder_paths("wildcards") or []) wc_wildcards_folders = ",".join(folder_paths.get_folder_paths("wildcards") or [])
@@ -445,9 +432,11 @@ class PromptPostProcessorComfyUINode:
for f in wc_wildcards_folders.split(",") for f in wc_wildcards_folders.split(",")
if f.strip() != "" 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 = { options = {
"debug_level": debug_level, "debug_level": debug_level,
"pony_substrings": pony_substrings, "variants_definitions": variants_definitions,
"process_wildcards": wc_process_wildcards, "process_wildcards": wc_process_wildcards,
"if_wildcards": wc_if_wildcards, "if_wildcards": wc_if_wildcards,
"choice_separator": wc_choice_separator, "choice_separator": wc_choice_separator,
@@ -466,15 +455,73 @@ class PromptPostProcessorComfyUINode:
"cleanup_merge_attention": cleanup_merge_attention, "cleanup_merge_attention": cleanup_merge_attention,
"remove_extranetwork_tags": remove_extranetwork_tags, "remove_extranetwork_tags": remove_extranetwork_tags,
} }
self.wildcards_obj.refresh_wildcards(debug_level, wildcards_folders if options["process_wildcards"] else None) self.wildcards_obj.refresh_wildcards(
debug_level,
wildcards_folders if options["process_wildcards"] else None,
wc_wildcards_input,
)
ppp = PromptPostProcessor( ppp = PromptPostProcessor(
self.logger, self.interrupt, env_info, options, self.grammar_content, self.wildcards_obj self.logger, self.interrupt, env_info, options, self.grammar_content, self.wildcards_obj
) )
pos_prompt, neg_prompt = ppp.process_prompt(pos_prompt, neg_prompt, seed if seed is not None else 1) pos_prompt, neg_prompt, variables = ppp.process_prompt(pos_prompt, neg_prompt, seed if seed is not None else 1)
return ( return (
pos_prompt, pos_prompt,
neg_prompt, neg_prompt,
variables,
) )
def interrupt(self): def interrupt(self):
nodes.interrupt_processing(True) 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,
},
),
},
}
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

+324 -100
View File
@@ -1,60 +1,131 @@
import fnmatch import fnmatch
import os import os
import json
from typing import Optional from typing import Optional
import logging
import yaml import yaml
from ppp_logging import DEBUG_LEVEL from ppp_logging import DEBUG_LEVEL # pylint: disable=import-error
def deep_freeze(obj):
"""
Deep freeze an object.
Args:
obj (object): The object to freeze.
Returns:
object: The frozen object.
"""
if isinstance(obj, dict):
return tuple((k, deep_freeze(v)) for k, v in sorted(obj.items()))
if isinstance(obj, list):
return tuple(deep_freeze(i) for i in obj)
if isinstance(obj, set):
return tuple(deep_freeze(i) for i in sorted(obj))
return obj
class PPPWildcard: class PPPWildcard:
"""
A wildcard object.
Attributes:
key (str): The key of the wildcard.
file (str): The path to the file where the wildcard is defined.
unprocessed_choices (list[str]): The unprocessed choices of the wildcard.
choices (list[dict]): The processed choices of the wildcard.
options (dict): The options of the wildcard.
"""
def __init__(self, fullpath: str, key: str, choices: list[str]): def __init__(self, fullpath: str, key: str, choices: list[str]):
self.key: str = key self.key: str = key
self.file: str = fullpath self.file: str = fullpath
self.choices: list[str] = choices self.unprocessed_choices: list[str] = choices
self.choices_obj: list[object] = None self.choices: list[dict] = None
self.options_obj: object = None self.options: dict = None
def __hash__(self) -> int:
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: class PPPWildcards:
"""
A class to manage wildcards.
Attributes:
wildcards (dict[str, PPPWildcard]): The wildcards.
"""
DEFAULT_WILDCARDS_FOLDER = "wildcards" DEFAULT_WILDCARDS_FOLDER = "wildcards"
LOCALINPUT_FILENAME = "#INPUT"
def __init__(self, logger): def __init__(self, logger):
self.logger = logger self.__logger: logging.Logger = logger
self.debug_level = DEBUG_LEVEL.none self.__debug_level = DEBUG_LEVEL.none
self.wildcards_folders = [] self.__wildcards_folders = []
self.__wildcard_files = {}
self.wildcards: dict[str, PPPWildcard] = {} self.wildcards: dict[str, PPPWildcard] = {}
self.wildcard_files = {}
def refresh_wildcards(self, debug_level: DEBUG_LEVEL, wildcards_folders: Optional[list[str]]): 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]], wildcards_input: str = None
):
""" """
Initialize the wildcards. Initialize the wildcards.
""" """
self.debug_level = debug_level self.__debug_level = debug_level
self.wildcards_folders = wildcards_folders self.__wildcards_folders = wildcards_folders or []
if wildcards_folders is not None: # if self.debug_level != DEBUG_LEVEL.none:
# if self.debug_level != DEBUG_LEVEL.none: # self.logger.info("Refreshing wildcards...")
# self.logger.info("Initializing wildcards...") # t1 = time.time()
# t1 = time.time() for fullpath in list(self.__wildcard_files.keys()):
for fullpath in list(self.wildcard_files.keys()): if fullpath != self.LOCALINPUT_FILENAME:
path = os.path.dirname(fullpath) path = os.path.dirname(fullpath)
if not os.path.exists(fullpath) or not any( if not os.path.exists(fullpath) or not any(
os.path.commonpath([path, folder]) == folder for folder in self.wildcards_folders os.path.commonpath([path, folder]) == folder for folder in self.__wildcards_folders
): ):
self.__remove_wildcards_from_file(fullpath) self.__remove_wildcards_from_path(fullpath)
for f in self.wildcards_folders: elif wildcards_input is None:
self.__get_wildcards_in_directory(f, f) self.__remove_wildcards_from_path(fullpath)
# t2 = time.time() if wildcards_folders is not None or wildcards_input is not None:
# if self.debug_level != DEBUG_LEVEL.none: if wildcards_folders is not None:
# self.logger.info(f"Wildcards init time: {t2 - t1:.3f} seconds") for f in self.__wildcards_folders:
self.__get_wildcards_in_directory(f, f)
if wildcards_input is not None:
self.__get_wildcards_in_input(wildcards_input)
else: else:
self.wildcards_folders = []
self.wildcards = {} self.wildcards = {}
self.wildcard_files = {} self.__wildcard_files = {}
# t2 = time.time()
# if self.debug_level != DEBUG_LEVEL.none:
# self.logger.info(f"Wildcards refresh time: {t2 - t1:.3f} seconds")
def get_wildcards(self, key: str) -> list[PPPWildcard]: def get_wildcards(self, key: str) -> list[PPPWildcard]:
"""
Get all wildcards that match a key.
Args:
key (str): The key to match.
Returns:
list: A list of all wildcards that match the key.
"""
keys = sorted(fnmatch.filter(self.wildcards.keys(), key)) keys = sorted(fnmatch.filter(self.wildcards.keys(), key))
return [self.wildcards[k] for k in keys] return [self.wildcards[k] for k in keys]
@@ -96,7 +167,7 @@ class PPPWildcards:
return None return None
return current_dict return current_dict
def __remove_wildcards_from_file(self, full_path: str, debug=True): def __remove_wildcards_from_path(self, full_path: str, debug=True):
""" """
Clear all wildcards in a file. Clear all wildcards in a file.
@@ -104,11 +175,14 @@ class PPPWildcards:
full_path (str): The path to the file. full_path (str): The path to the file.
debug (bool): Whether to print debug messages or not. debug (bool): Whether to print debug messages or not.
""" """
last_modified_cached = self.wildcard_files.get(full_path, None) last_modified_cached = self.__wildcard_files.get(full_path, None) # a time or a hash
if debug and last_modified_cached is not None and self.debug_level != DEBUG_LEVEL.none: if debug and last_modified_cached is not None and self.__debug_level != DEBUG_LEVEL.none:
self.logger.debug(f"Removing wildcards from file: {full_path}") if full_path == self.LOCALINPUT_FILENAME:
if full_path in self.wildcard_files.keys(): self.__logger.debug("Removing wildcards from input")
del self.wildcard_files[full_path] else:
self.__logger.debug(f"Removing wildcards from file: {full_path}")
if full_path in self.__wildcard_files.keys():
del self.__wildcard_files[full_path]
for key in list(self.wildcards.keys()): for key in list(self.wildcards.keys()):
if self.wildcards[key].file == full_path: if self.wildcards[key].file == full_path:
del self.wildcards[key] del self.wildcards[key]
@@ -122,87 +196,237 @@ class PPPWildcards:
full_path (str): The path to the file. full_path (str): The path to the file.
""" """
last_modified = os.path.getmtime(full_path) last_modified = os.path.getmtime(full_path)
last_modified_cached = self.wildcard_files.get(full_path, None) last_modified_cached = self.__wildcard_files.get(full_path, None)
if last_modified_cached is not None and last_modified == self.wildcard_files[full_path]: if last_modified_cached is not None and last_modified == self.__wildcard_files[full_path]:
return return
filename = os.path.basename(full_path) filename = os.path.basename(full_path)
name, extension = os.path.splitext(filename) _, extension = os.path.splitext(filename)
if extension not in (".txt", ".json", ".yaml", ".yml"): if extension not in (".txt", ".json", ".yaml", ".yml"):
return return
self.__remove_wildcards_from_file(full_path, False) self.__remove_wildcards_from_path(full_path, False)
if last_modified_cached is not None and self.debug_level != DEBUG_LEVEL.none: if last_modified_cached is not None and self.__debug_level != DEBUG_LEVEL.none:
self.logger.debug(f"Updating wildcards from file: {full_path}") self.__logger.debug(f"Updating wildcards from file: {full_path}")
relfolders = os.path.relpath(os.path.dirname(full_path), base)
if relfolders == ".":
relfolders = ""
elif relfolders != "":
relfolders += "/"
if extension == ".txt": if extension == ".txt":
self.__get_wildcards_in_text_file(full_path, relfolders, name) self.__get_wildcards_in_text_file(full_path, base)
elif extension in (".json", ".yaml", ".yml"): elif extension in (".json", ".yaml", ".yml"):
self.__get_wildcards_in_structured_file(full_path, relfolders, extension) self.__get_wildcards_in_structured_file(full_path, base)
self.wildcard_files[full_path] = last_modified self.__wildcard_files[full_path] = last_modified
def __get_wildcards_in_structured_file(self, full_path, relfolders, extension): def __get_wildcards_in_input(self, wildcards_input: str):
with open(full_path, "r", encoding="utf-8") as file: """
if extension == ".json": Get all wildcards in the string.
content = json.loads(file.read())
Args:
wildcards_input (str): The input string containing wildcards in json or yaml format.
"""
new_h = hash(wildcards_input)
h = self.__wildcard_files.get(self.LOCALINPUT_FILENAME, None)
if h == new_h:
return
self.__remove_wildcards_from_path(self.LOCALINPUT_FILENAME, False)
if h is not None and self.__debug_level != DEBUG_LEVEL.none:
self.__logger.debug("Updating wildcards from input")
wildcards_input = wildcards_input.strip()
if wildcards_input != "":
try:
content = yaml.safe_load(wildcards_input)
except yaml.YAMLError as e:
self.__logger.warning(f"Invalid format for input wildcards: {e}")
return
if content is not None:
self.__add_wildcard(content, self.LOCALINPUT_FILENAME, [self.LOCALINPUT_FILENAME])
self.__wildcard_files[self.LOCALINPUT_FILENAME] = new_h
def is_dict_choices_options(self, d: dict) -> bool:
"""
Check if a dictionary is a valid choices options dictionary.
Args:
d (dict): The dictionary to check.
Returns:
bool: Whether the dictionary is a valid choices options dictionary or not.
"""
return all(
k in ["sampler", "repeating", "count", "from", "to", "prefix", "suffix", "separator"] for k in d.keys()
)
def is_dict_choice_options(self, d: dict) -> bool:
"""
Check if a dictionary is a valid choice options dictionary.
Args:
d (dict): The dictionary to check.
Returns:
bool: Whether the dictionary is a valid choice options dictionary or not.
"""
return all(k in ["labels", "weight", "if", "content", "text"] for k in d.keys())
def __get_choices(self, obj: object, full_path: str, key_parts: list[str]) -> list:
"""
We process the choices in the object and return them as a list.
Args:
obj (object): the value of a wildcard
full_path (str): path to the file where the wildcard is defined
key_parts (list[str]): parts of the key for the wildcard
Returns:
list: list of choices
"""
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: else:
content = yaml.safe_load(file) self.__logger.warning(
keys = self.__get_keys_in_dict(content) f"Invalid choice {i+1} in wildcard '{'/'.join(key_parts)}' in file '{full_path}'!"
for key in keys:
fullkey = f"{relfolders}{key}"
if self.wildcards.get(fullkey, None) is not None:
self.logger.warning(
f"Duplicate wildcard '{fullkey}' in file '{full_path}' and '{self.wildcards[fullkey].file}'!"
) )
else: return choices
obj = self.__get_nested(content, key)
if obj is not None:
if isinstance(obj, str):
choices = [obj]
elif isinstance(obj, (int, float, bool)):
choices = [str(obj)]
elif isinstance(obj, list) and len(obj) > 0:
choices = []
for c in obj:
if isinstance(c, str):
choices.append(c)
elif isinstance(c, dict): # we convert the dict to a string
d = ""
if "weight" in c.keys():
d += str(c["weight"])
if "if" in c.keys():
d += f" if {c['if']}"
if d != "":
d += "::"
if "text" in c.keys():
d += c["text"]
elif "content" in c.keys():
d += c["content"]
choices.append(d)
else:
obj = None
if obj is None:
self.logger.warning(f"Invalid wildcard '{fullkey}' in file '{full_path}'!")
else:
self.wildcards[fullkey] = PPPWildcard(full_path, fullkey, choices)
def __get_wildcards_in_text_file(self, full_path, relfolders, name): 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.
Args:
full_path (str): The path to the file that contains it.
key_parts (list[str]): The parts of the key.
i (int): The index of the wildcard.
content (object): The content of the wildcard.
options (str): The options for the choice where the wildcard is defined.
Returns:
str: The resulting value for the choice.
"""
new_parts = key_parts + [f"#ANON_{i}"]
self.__add_wildcard(content, full_path, new_parts)
value = f"__{'/'.join(new_parts)}__"
if options is not None:
value = f"{options}::{value}"
return value
def __add_wildcard(self, content: object, full_path: str, external_key_parts: list[str]):
"""
Add a wildcard to the wildcards dictionary.
Args:
content (object): The content of the wildcard.
full_path (str): The path to the file that contains it.
external_key_parts (list[str]): The parts of the key.
"""
key_parts = external_key_parts.copy()
if isinstance(content, dict):
key_parts.pop()
keys = self.__get_keys_in_dict(content)
for key in keys:
tmp_key_parts = key_parts.copy()
tmp_key_parts.extend(key.split("/"))
fullkey = "/".join(tmp_key_parts)
if self.wildcards.get(fullkey, None) is not None:
self.__logger.warning(
f"Duplicate wildcard '{fullkey}' in file '{full_path}' and '{self.wildcards[fullkey].file}'!"
)
else:
obj = self.__get_nested(content, key)
choices = self.__get_choices(obj, full_path, tmp_key_parts)
if choices is None:
self.__logger.warning(f"Invalid wildcard '{fullkey}' in file '{full_path}'!")
else:
self.wildcards[fullkey] = PPPWildcard(full_path, fullkey, choices)
return
if isinstance(content, str):
content = [content]
elif isinstance(content, (int, float, bool)):
content = [str(content)]
if not isinstance(content, list):
self.__logger.warning(f"Invalid wildcard in file '{full_path}'!")
return
fullkey = "/".join(key_parts)
if self.wildcards.get(fullkey, None) is not None:
self.__logger.warning(
f"Duplicate wildcard '{fullkey}' in file '{full_path}' and '{self.wildcards[fullkey].file}'!"
)
else:
choices = self.__get_choices(content, full_path, key_parts)
if choices is None:
self.__logger.warning(f"Invalid wildcard '{fullkey}' in file '{full_path}'!")
else:
self.wildcards[fullkey] = PPPWildcard(full_path, fullkey, choices)
def __get_wildcards_in_structured_file(self, full_path, base):
"""
Get all wildcards in a structured file.
Args:
full_path (str): The path to the file.
base (str): The base path for the wildcards.
"""
external_key: str = os.path.relpath(os.path.splitext(full_path)[0], base)
external_key_parts = external_key.split(os.sep)
with open(full_path, "r", encoding="utf-8") as file:
content = yaml.safe_load(file)
self.__add_wildcard(content, full_path, external_key_parts)
def __get_wildcards_in_text_file(self, full_path, base):
"""
Get all wildcards in a text file.
Args:
full_path (str): The path to the file.
base (str): The base path for the wildcards.
"""
external_key: str = os.path.relpath(os.path.splitext(full_path)[0], base)
external_key_parts = external_key.split(os.sep)
with open(full_path, "r", encoding="utf-8") as file: with open(full_path, "r", encoding="utf-8") as file:
text_content = map(lambda x: x.strip("\n\r"), file.readlines()) text_content = map(lambda x: x.strip("\n\r"), file.readlines())
text_content = list(filter(lambda x: x.strip() != "" and not x.strip().startswith("#"), text_content)) text_content = list(filter(lambda x: x.strip() != "" and not x.strip().startswith("#"), text_content))
text_content = [x.split("#")[0].rstrip() if len(x.split("#")) > 1 else x for x in text_content] text_content = [x.split("#")[0].rstrip() if len(x.split("#")) > 1 else x for x in text_content]
fullkey = f"{relfolders}{name}" self.__add_wildcard(text_content, full_path, external_key_parts)
if self.wildcards.get(fullkey, None) is not None:
self.logger.warning(
f"Duplicate wildcard '{fullkey}' in file '{full_path}' and '{self.wildcards[fullkey].file}'!"
)
else:
if len(text_content) == 0:
self.logger.warning(f"Invalid wildcard in file '{full_path}'!")
else:
self.wildcards[fullkey] = PPPWildcard(full_path, fullkey, text_content)
def __get_wildcards_in_directory(self, base: str, directory: str): def __get_wildcards_in_directory(self, base: str, directory: str):
""" """
@@ -213,7 +437,7 @@ class PPPWildcards:
directory (str): The path to the directory. directory (str): The path to the directory.
""" """
if not os.path.exists(directory): if not os.path.exists(directory):
self.logger.warning(f"Wildcard directory '{directory}' does not exist!") self.__logger.warning(f"Wildcard directory '{directory}' does not exist!")
return return
for filename in os.listdir(directory): for filename in os.listdir(directory):
full_path = os.path.abspath(os.path.join(directory, filename)) full_path = os.path.abspath(os.path.join(directory, filename))
+4 -4
View File
@@ -1,9 +1,9 @@
[project] [project]
name = "sd-webui-prompt-postprocessor" 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." 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.7.0" version = "2.10.0"
license = {file = "LICENSE.txt"} license = { file = "LICENSE.txt" }
dependencies = ["lark"] dependencies = ["lark", "numpy", "pyyaml"]
[project.urls] [project.urls]
Repository = "https://github.com/acorderob/sd-webui-prompt-postprocessor" Repository = "https://github.com/acorderob/sd-webui-prompt-postprocessor"
@@ -12,4 +12,4 @@ Repository = "https://github.com/acorderob/sd-webui-prompt-postprocessor"
[tool.comfy] [tool.comfy]
PublisherId = "acorderob" PublisherId = "acorderob"
DisplayName = "sd-webui-prompt-postprocessor" DisplayName = "sd-webui-prompt-postprocessor"
Icon = "" Icon = "ppp_icon.png"
+2
View File
@@ -1 +1,3 @@
lark lark
numpy
pyyaml
+251 -51
View File
@@ -4,19 +4,19 @@ if __name__ == "__main__":
import sys import sys
import os import os
import time import time
import numpy as np
sys.path.append(os.path.join(sys.path[0], "..")) sys.path.append(os.path.join(os.path.realpath(__file__), "..")) # base path for the extension
from modules import scripts, shared, script_callbacks # pylint: disable=import-error
from modules import scripts, shared, script_callbacks from modules.processing import StableDiffusionProcessing # pylint: disable=import-error
from modules.processing import StableDiffusionProcessing from modules.shared import opts # pylint: disable=import-error
from modules.shared import opts from modules.paths import models_path # pylint: disable=import-error
from modules.paths import models_path import gradio as gr # pylint: disable=import-error
import gradio as gr from ppp import PromptPostProcessor # pylint: disable=import-error
from ppp import PromptPostProcessor from ppp_logging import DEBUG_LEVEL, PromptPostProcessorLogFactory # pylint: disable=import-error
from ppp_logging import DEBUG_LEVEL, PromptPostProcessorLogFactory from ppp_cache import PPPLRUCache # pylint: disable=import-error
from ppp_cache import PPPLRUCache from ppp_wildcards import PPPWildcards # pylint: disable=import-error
from ppp_wildcards import PPPWildcards
class PromptPostProcessorA1111Script(scripts.Script): class PromptPostProcessorA1111Script(scripts.Script):
@@ -36,6 +36,16 @@ class PromptPostProcessorA1111Script(scripts.Script):
__on_ui_settings(): Callback function for UI settings. __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): def __init__(self):
""" """
Initializes the PromptPostProcessor object. Initializes the PromptPostProcessor object.
@@ -48,15 +58,19 @@ class PromptPostProcessorA1111Script(scripts.Script):
Returns: Returns:
None None
""" """
self.increment_instance_count()
lf = PromptPostProcessorLogFactory() lf = PromptPostProcessorLogFactory()
self.name = PromptPostProcessor.NAME self.name = PromptPostProcessor.NAME
self.ppp_logger = lf.log self.ppp_logger = lf.log
self.ppp_debug_level = DEBUG_LEVEL(getattr(opts, "ppp_gen_debug_level", DEBUG_LEVEL.none.value)) 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") grammar_filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), "../grammar.lark")
with open(grammar_filename, "r", encoding="utf-8") as file: with open(grammar_filename, "r", encoding="utf-8") as file:
self.grammar_content = file.read() self.grammar_content = file.read()
self.wildcards_obj = PPPWildcards(lf.log) self.wildcards_obj = PPPWildcards(lf.log)
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): def title(self):
""" """
@@ -67,7 +81,7 @@ class PromptPostProcessorA1111Script(scripts.Script):
""" """
return PromptPostProcessor.NAME return PromptPostProcessor.NAME
def show(self, is_img2img): def show(self, is_img2img): # pylint: disable=unused-argument
""" """
Determines whether the script should be shown based on the kind of processing. Determines whether the script should be shown based on the kind of processing.
@@ -79,12 +93,72 @@ class PromptPostProcessorA1111Script(scripts.Script):
""" """
return scripts.AlwaysVisible return scripts.AlwaysVisible
def process(self, p: StableDiffusionProcessing, *args, **kwargs): # pylint: disable=unused-argument def ui(self, is_img2img): # pylint: disable=unused-argument
with gr.Accordion(PromptPostProcessor.NAME, open=False):
force_equal_seeds = gr.Checkbox(
label="Force equal seeds",
info="Force all image seeds and variation seeds to be equal to the first one, disabling the default autoincrease.",
value=False,
# show_label=True,
elem_id="ppp_force_equal_seeds",
)
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.
"""
)
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_incremental_seed,
): # pylint: disable=arguments-differ
""" """
Processes the prompts and applies post-processing operations. Processes the prompts and applies post-processing operations.
Args: Args:
p (StableDiffusionProcessing): The StableDiffusionProcessing object containing the prompts. 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: Returns:
None None
@@ -92,20 +166,54 @@ class PromptPostProcessorA1111Script(scripts.Script):
t1 = time.time() t1 = time.time()
if getattr(opts, "prompt_attention", "") == "Compel parser": if getattr(opts, "prompt_attention", "") == "Compel parser":
self.ppp_logger.warning("Compel parser is not supported!") 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)) self.ppp_debug_level = DEBUG_LEVEL(getattr(opts, "ppp_gen_debug_level", DEBUG_LEVEL.none.value))
do_i2i = getattr(opts, "ppp_gen_doi2i", False) do_i2i = getattr(opts, "ppp_gen_doi2i", False)
add_prompts = getattr(opts, "ppp_gen_addpromptstometadata", True)
if is_i2i and not do_i2i: if is_i2i and not do_i2i:
if self.ppp_debug_level != DEBUG_LEVEL.none: if self.ppp_debug_level != DEBUG_LEVEL.none:
self.ppp_logger.info("Not processing the prompt for i2i") self.ppp_logger.info("Not processing the prompt for i2i")
return 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 = ( app = (
"forge" "forge"
if hasattr(p.sd_model, "model_config") 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 = { env_info = {
"app": app, "app": app,
"models_path": models_path, "models_path": models_path,
@@ -118,6 +226,16 @@ class PromptPostProcessorA1111Script(scripts.Script):
"is_sd3": False, # Stable Diffusion 3 "is_sd3": False, # Stable Diffusion 3
"is_flux": False, # Flux "is_flux": False, # Flux
"is_auraflow": False, # AuraFlow "is_auraflow": False, # AuraFlow
"is_pixart": False, # PixArt
"is_lumina2": False, # Lumina2
"is_ltxv": False, # LTXV
"is_cosmos": False, # Cosmos
"is_genmomochi": False, # GenmoMochi
"is_hunyuan": False, # Hunyuan
"is_hunyuanvideo": False, # HunyuanVideo
"is_hunyuan3d": False, # Hunyuan3D
"is_wanvideo": False, # WanVideo
"is_hidream": False, # HiDream
} }
if app == "sdnext": if app == "sdnext":
# cannot differentiate SD1 and SD2, we set True to both # cannot differentiate SD1 and SD2, we set True to both
@@ -130,15 +248,28 @@ class PromptPostProcessorA1111Script(scripts.Script):
env_info["is_sd3"] = p.sd_model.__class__.__name__ == "StableDiffusion3Pipeline" env_info["is_sd3"] = p.sd_model.__class__.__name__ == "StableDiffusion3Pipeline"
env_info["is_flux"] = p.sd_model.__class__.__name__ == "FluxPipeline" env_info["is_flux"] = p.sd_model.__class__.__name__ == "FluxPipeline"
env_info["is_auraflow"] = p.sd_model.__class__.__name__ == "AuraFlowPipeline" 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": 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["model_class"] = p.sd_model.model_config.__class__.__name__
env_info["is_sd1"] = getattr(p.sd_model, "is_sd1", False) 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_sd2"] = getattr(p.sd_model, "is_sd2", False)
env_info["is_sdxl"] = getattr(p.sd_model, "is_sdxl", False) env_info["is_sdxl"] = getattr(p.sd_model, "is_sdxl", False)
env_info["is_ssd"] = 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_sd3"] = getattr(p.sd_model, "is_sd3", False)
env_info["is_flux"] = p.sd_model.model_config.__class__.__name__ == "Flux" env_info["is_flux"] = False
env_info["is_auraflow"] = False # p.sd_model.model_config.__class__.__name__ == "AuraFlow" env_info["is_auraflow"] = False
else: # assume A1111 compatible (p.sd_model.__class__.__name__=="DiffusionEngine") else: # assume A1111 compatible (p.sd_model.__class__.__name__=="DiffusionEngine")
env_info["model_class"] = p.sd_model.__class__.__name__ env_info["model_class"] = p.sd_model.__class__.__name__
env_info["is_sd1"] = getattr(p.sd_model, "is_sd1", False) env_info["is_sd1"] = getattr(p.sd_model, "is_sd1", False)
@@ -158,9 +289,11 @@ class PromptPostProcessorA1111Script(scripts.Script):
] ]
options = { options = {
"debug_level": getattr(opts, "ppp_gen_debug_level", DEBUG_LEVEL.none.value), "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), "process_wildcards": getattr(opts, "ppp_wil_processwildcards", True),
"if_wildcards": getattr(opts, "ppp_wil_ifwildcards", PromptPostProcessor.IFWILDCARDS_CHOICES.ignore.value), "if_wildcards": getattr(opts, "ppp_wil_ifwildcards", PromptPostProcessor.IFWILDCARDS_CHOICES.stop.value),
"choice_separator": getattr(opts, "ppp_wil_choice_separator", PromptPostProcessor.DEFAULT_CHOICE_SEPARATOR), "choice_separator": getattr(opts, "ppp_wil_choice_separator", PromptPostProcessor.DEFAULT_CHOICE_SEPARATOR),
"keep_choices_order": getattr(opts, "ppp_wil_keep_choices_order", False), "keep_choices_order": getattr(opts, "ppp_wil_keep_choices_order", False),
"stn_separator": getattr(opts, "ppp_stn_separator", PromptPostProcessor.DEFAULT_STN_SEPARATOR), "stn_separator": getattr(opts, "ppp_stn_separator", PromptPostProcessor.DEFAULT_STN_SEPARATOR),
@@ -185,23 +318,46 @@ class PromptPostProcessorA1111Script(scripts.Script):
) )
prompts_list = [] prompts_list = []
seeds = getattr(p, "all_seeds", []) if input_force_equal_seeds:
subseeds = getattr(p, "all_subseeds", []) if self.ppp_debug_level != DEBUG_LEVEL.none:
subseed_strength = getattr(p, "subseed_strength", 0.0) self.ppp_logger.info("Forcing equal seeds")
if subseed_strength > 0: seeds = getattr(p, "all_seeds", [])
calculated_seeds = [ subseeds = getattr(p, "all_subseeds", [])
int(subseed * subseed_strength + seed * (1 - subseed_strength)) p.all_seeds = [seeds[0] for _ in seeds]
for seed, subseed in zip(seeds, subseeds) p.all_subseeds = [subseeds[0] for _ in subseeds]
]
if input_unlink_seed:
if self.ppp_debug_level != DEBUG_LEVEL.none:
self.ppp_logger.info("Using unlinked seed")
num_seeds = len(getattr(p, "all_seeds", []))
if input_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)
else:
calculated_seeds = [input_seed for _ in range(num_seeds)]
else: else:
calculated_seeds = seeds seeds = getattr(p, "all_seeds", [])
if len(set(calculated_seeds)) < len(calculated_seeds): subseeds = getattr(p, "all_subseeds", [])
self.ppp_logger.info("Adjusting seeds because some are equal.") subseed_strength = getattr(p, "subseed_strength", 0.0)
calculated_seeds = [seed + i for i, seed in enumerate(calculated_seeds)] if subseed_strength > 0:
calculated_seeds = [
int(subseed * subseed_strength + seed * (1 - subseed_strength))
for seed, subseed in zip(seeds, subseeds)
]
# if len(set(calculated_seeds)) < len(calculated_seeds):
# self.ppp_logger.info("Adjusting seeds because some are equal.")
# calculated_seeds = [seed + i for i, seed in enumerate(calculated_seeds)]
else:
calculated_seeds = seeds
# initialize extra generation parameters
extra_params = {}
# adds regular prompts # adds regular prompts
rpr = getattr(p, "all_prompts", None) rpr: list[str] = getattr(p, "all_prompts", None)
rnr = getattr(p, "all_negative_prompts", None) rnr: list[str] = getattr(p, "all_negative_prompts", None)
if rpr is not None and rnr is not None: if rpr is not None and rnr is not None:
prompts_list += [ prompts_list += [
("regular", seed, prompt, negative_prompt) ("regular", seed, prompt, negative_prompt)
@@ -209,8 +365,8 @@ class PromptPostProcessorA1111Script(scripts.Script):
if (seed, prompt, negative_prompt) not in prompts_list if (seed, prompt, negative_prompt) not in prompts_list
] ]
# make it compatible with A1111 hires fix # make it compatible with A1111 hires fix
rph = getattr(p, "all_hr_prompts", None) rph: list[str] = getattr(p, "all_hr_prompts", None)
rnh = getattr(p, "all_hr_negative_prompts", None) rnh: list[str] = getattr(p, "all_hr_negative_prompts", None)
if rph is not None and rnh is not None: if rph is not None and rnh is not None:
prompts_list += [ prompts_list += [
("hiresfix", seed, prompt, negative_prompt) ("hiresfix", seed, prompt, negative_prompt)
@@ -222,27 +378,58 @@ class PromptPostProcessorA1111Script(scripts.Script):
for i, (prompttype, seed, prompt, negative_prompt) in enumerate(prompts_list): for i, (prompttype, seed, prompt, negative_prompt) in enumerate(prompts_list):
if self.ppp_debug_level != DEBUG_LEVEL.none: if self.ppp_debug_level != DEBUG_LEVEL.none:
self.ppp_logger.info(f"processing prompts[{i+1}] ({prompttype})") self.ppp_logger.info(f"processing prompts[{i+1}] ({prompttype})")
if self.lru_cache.get((seed, prompt, negative_prompt)) is None: if self.lru_cache.get((seed, hash(self.wildcards_obj), prompt, negative_prompt)) is None:
pp, np = ppp.process_prompt(prompt, negative_prompt, seed) posp, negp, _ = ppp.process_prompt(prompt, negative_prompt, seed)
self.lru_cache.put((seed, prompt, negative_prompt), (pp, np)) 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 # adds also the result so i2i doesn't process it unnecessarily
self.lru_cache.put((seed, pp, np), (pp, np)) self.lru_cache.put((seed, hash(self.wildcards_obj), posp, negp), (posp, negp))
elif self.ppp_debug_level != DEBUG_LEVEL.none: elif self.ppp_debug_level != DEBUG_LEVEL.none:
self.ppp_logger.info("result already in cache") self.ppp_logger.info("result already in cache")
# updates the prompts # updates the prompts
if rpr is not None and rnr is not None: if rpr is not None and rnr is not None:
rpr_changes = False
rnr_changes = False
rpr_copy = rpr.copy()
rnr_copy = rnr.copy()
for i, (seed, prompt, negative_prompt) in enumerate(zip(calculated_seeds, rpr, rnr)): for i, (seed, prompt, negative_prompt) in enumerate(zip(calculated_seeds, rpr, rnr)):
found = self.lru_cache.get((seed, prompt, negative_prompt)) found = self.lru_cache.get((seed, hash(self.wildcards_obj), prompt, negative_prompt))
if found is not None: if found is not None:
if rpr[i].strip() != found[0].strip():
rpr_changes = True
if rnr[i].strip() != found[1].strip():
rnr_changes = True
rpr[i] = found[0] rpr[i] = found[0]
rnr[i] = found[1] rnr[i] = found[1]
if add_prompts:
if rpr_changes:
extra_params["PPP original prompts"] = rpr_copy
if rnr_changes:
extra_params["PPP original negative prompts"] = rnr_copy
if rph is not None and rnh is not None: if rph is not None and rnh is not None:
rph_changes = False
rnh_changes = False
rph_copy = rph.copy()
rnh_copy = rnh.copy()
for i, (seed, prompt, negative_prompt) in enumerate(zip(calculated_seeds, rph, rnh)): for i, (seed, prompt, negative_prompt) in enumerate(zip(calculated_seeds, rph, rnh)):
found = self.lru_cache.get((seed, prompt, negative_prompt)) found = self.lru_cache.get((seed, hash(self.wildcards_obj), prompt, negative_prompt))
if found is not None: if found is not None:
if rph[i].strip() != found[0].strip():
rph_changes = True
if rnh[i].strip() != found[1].strip():
rnh_changes = True
rph[i] = found[0] rph[i] = found[0]
rnh[i] = found[1] rnh[i] = found[1]
if add_prompts:
if rph_changes:
extra_params["PPP original HR prompts"] = rph_copy
if rnh_changes:
extra_params["PPP original HR negative prompts"] = rnh_copy
# 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
t2 = time.time() t2 = time.time()
if self.ppp_debug_level != DEBUG_LEVEL.none: if self.ppp_debug_level != DEBUG_LEVEL.none:
@@ -316,10 +503,15 @@ def on_ui_settings():
), ),
) )
shared.opts.add_option( shared.opts.add_option(
key="ppp_gen_ponysubstrings", key="ppp_gen_variantsdefinitions",
info=shared.OptionInfo( info=shared.OptionInfo(
PromptPostProcessor.DEFAULT_PONY_SUBSTRINGS, PromptPostProcessor.DEFAULT_VARIANTS_DEFINITIONS,
label="Comma separated list of substrings to look for in the model full filename to flag it as Pony (case insensitive)", 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, section=section,
), ),
) )
@@ -332,11 +524,19 @@ def on_ui_settings():
section=section, section=section,
), ),
) )
shared.opts.add_option(
key="ppp_gen_addpromptstometadata",
info=shared.OptionInfo(
True,
label="Add original prompts to metadata (if they change)",
section=section,
),
)
# wildcard settings # wildcard settings
shared.opts.add_option( shared.opts.add_option(
key="ppp_wil_sep", key="ppp_wil_sep",
info=new_html_title('<br><h2>Wildcard settings</h2>'), info=new_html_title("<br><h2>Wildcard settings</h2>"),
) )
shared.opts.add_option( shared.opts.add_option(
key="ppp_wil_processwildcards", key="ppp_wil_processwildcards",
@@ -395,7 +595,7 @@ def on_ui_settings():
# content removal settings # content removal settings
shared.opts.add_option( shared.opts.add_option(
key="ppp_rem_sep", key="ppp_rem_sep",
info=new_html_title('<br><h2>Content removal settings</h2>'), info=new_html_title("<br><h2>Content removal settings</h2>"),
) )
shared.opts.add_option( shared.opts.add_option(
key="ppp_rem_removeextranetworktags", key="ppp_rem_removeextranetworktags",
@@ -409,7 +609,7 @@ def on_ui_settings():
# send to negative settings # send to negative settings
shared.opts.add_option( shared.opts.add_option(
key="ppp_stn_sep", key="ppp_stn_sep",
info=new_html_title('<br><h2>Send to Negative settings</h2>'), info=new_html_title("<br><h2>Send to Negative settings</h2>"),
) )
shared.opts.add_option( shared.opts.add_option(
key="ppp_stn_separator", key="ppp_stn_separator",
@@ -430,7 +630,7 @@ def on_ui_settings():
# clean-up settings # clean-up settings
shared.opts.add_option( shared.opts.add_option(
key="ppp_cup_sep", key="ppp_cup_sep",
info=new_html_title('<br><h2>Clean-up settings</h2>'), info=new_html_title("<br><h2>Clean-up settings</h2>"),
) )
shared.opts.add_option( shared.opts.add_option(
key="ppp_cup_emptyconstructs", key="ppp_cup_emptyconstructs",
+212 -23
View File
@@ -4,12 +4,11 @@ import unittest
import sys import sys
import os import os
from ppp_wildcards import PPPWildcards sys.path.append(os.path.join(os.path.realpath(__file__), "..")) # base path for the extension
sys.path.append(os.path.join(sys.path[0], "..")) from ppp_wildcards import PPPWildcards # pylint: disable=import-error
from ppp import PromptPostProcessor # pylint: disable=import-error
from ppp import PromptPostProcessor from ppp_logging import DEBUG_LEVEL, PromptPostProcessorLogFactory # pylint: disable=import-error
from ppp_logging import DEBUG_LEVEL, PromptPostProcessorLogFactory
PromptPair = namedtuple("PromptPair", ["prompt", "negative_prompt"], defaults=["", ""]) PromptPair = namedtuple("PromptPair", ["prompt", "negative_prompt"], defaults=["", ""])
@@ -29,7 +28,7 @@ class TestPromptPostProcessor(unittest.TestCase):
self.__ppp_logger.setLevel(logging.DEBUG) self.__ppp_logger.setLevel(logging.DEBUG)
self.__defopts = { self.__defopts = {
"debug_level": DEBUG_LEVEL.full.value, "debug_level": DEBUG_LEVEL.full.value,
"pony_substrings": PromptPostProcessor.DEFAULT_PONY_SUBSTRINGS, "variants_definitions": PromptPostProcessor.DEFAULT_VARIANTS_DEFINITIONS,
"process_wildcards": True, "process_wildcards": True,
"if_wildcards": PromptPostProcessor.IFWILDCARDS_CHOICES.ignore.value, "if_wildcards": PromptPostProcessor.IFWILDCARDS_CHOICES.ignore.value,
"choice_separator": ", ", "choice_separator": ", ",
@@ -69,6 +68,13 @@ class TestPromptPostProcessor(unittest.TestCase):
os.path.abspath(os.path.join(os.path.dirname(__file__), "wildcards")), os.path.abspath(os.path.join(os.path.dirname(__file__), "wildcards")),
os.path.abspath(os.path.join(os.path.dirname(__file__), "wildcards2")), os.path.abspath(os.path.join(os.path.dirname(__file__), "wildcards2")),
], ],
"""
yaml_input:
wildcardI:
- choice1
- choice2
- choice3
"""
) )
grammar_filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), "../grammar.lark") grammar_filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), "../grammar.lark")
with open(grammar_filename, "r", encoding="utf-8") as file: with open(grammar_filename, "r", encoding="utf-8") as file:
@@ -101,6 +107,18 @@ class TestPromptPostProcessor(unittest.TestCase):
self.__grammar_content, self.__grammar_content,
self.__wildcards_obj, self.__wildcards_obj,
) )
self.__comfyuippp = PromptPostProcessor(
self.__ppp_logger,
self.__interrupt,
{
**self.__def_env_info,
"app": "comfyui",
"model_class": "SDXL",
},
self.__defopts,
self.__grammar_content,
self.__wildcards_obj,
)
def __interrupt(self): def __interrupt(self):
self.__interrupted = True self.__interrupted = True
@@ -129,7 +147,7 @@ class TestPromptPostProcessor(unittest.TestCase):
the_obj = ppp or self.__defppp the_obj = ppp or self.__defppp
out = expected_output_prompts if isinstance(expected_output_prompts, list) else [expected_output_prompts] out = expected_output_prompts if isinstance(expected_output_prompts, list) else [expected_output_prompts]
for eo in out: 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.prompt,
input_prompts.negative_prompt, input_prompts.negative_prompt,
seed, seed,
@@ -343,7 +361,8 @@ class TestPromptPostProcessor(unittest.TestCase):
def test_cmd_if_nested(self): # nested if command def test_cmd_if_nested(self): # nested if command
self.__process( self.__process(
PromptPair( 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", ""), PromptPair("this is PONY", ""),
ppp=PromptPostProcessor( ppp=PromptPostProcessor(
@@ -380,6 +399,66 @@ class TestPromptPostProcessor(unittest.TestCase):
PromptPair("OK OK OK", ""), 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 def test_cmd_set_if2(self): # set and more complex if commands
self.__process( self.__process(
PromptPair( PromptPair(
@@ -430,6 +509,42 @@ class TestPromptPostProcessor(unittest.TestCase):
ppp=self.__nocupppp, 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 # Choices tests
def test_ch_choices(self): # simple choices with weights def test_ch_choices(self): # simple choices with weights
@@ -575,8 +690,8 @@ class TestPromptPostProcessor(unittest.TestCase):
self.__process( self.__process(
PromptPair("__bad_wildcard__", "{option1|option2}"), PromptPair("__bad_wildcard__", "{option1|option2}"),
PromptPair( PromptPair(
PromptPostProcessor.WILDCARD_STOP + "__bad_wildcard__", PromptPostProcessor.WILDCARD_STOP.format("__bad_wildcard__") + "__bad_wildcard__",
PromptPostProcessor.WILDCARD_STOP + "{option1|option2}", "{option1|option2}",
), ),
ppp=PromptPostProcessor( ppp=PromptPostProcessor(
self.__ppp_logger, self.__ppp_logger,
@@ -593,6 +708,24 @@ class TestPromptPostProcessor(unittest.TestCase):
interrupted=True, interrupted=True,
) )
def test_wcinvar_warn(self): # wildcards in var with warn option
self.__process(
PromptPair("${v=__bad_wildcard__}${v}", ""),
PromptPair(PromptPostProcessor.WILDCARD_WARNING + "__bad_wildcard__", ""),
ppp=PromptPostProcessor(
self.__ppp_logger,
self.__interrupt,
self.__def_env_info,
{
**self.__defopts,
"process_wildcards": False,
"if_wildcards": PromptPostProcessor.IFWILDCARDS_CHOICES.warn.value,
},
self.__grammar_content,
self.__wildcards_obj,
),
)
def test_wc_wildcard1a_text(self): # simple text wildcard def test_wc_wildcard1a_text(self): # simple text wildcard
self.__process( self.__process(
PromptPair("the choices are: __text/wildcard1__", ""), PromptPair("the choices are: __text/wildcard1__", ""),
@@ -656,6 +789,20 @@ class TestPromptPostProcessor(unittest.TestCase):
ppp=self.__nocupppp, 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 def test_wc_wildcard_filter_index(self): # wildcard with positional index filter
self.__process( self.__process(
PromptPair("the choice is: __yaml/wildcard2'2'__", ""), PromptPair("the choice is: __yaml/wildcard2'2'__", ""),
@@ -780,6 +927,61 @@ class TestPromptPostProcessor(unittest.TestCase):
ppp=self.__nocupppp, ppp=self.__nocupppp,
) )
def test_wc_wildcardPS_yaml(self): # yaml wildcard with object formatted choices and options and prefix and suffix
self.__process(
PromptPair("the choices are: __yaml/wildcardPS__", ""),
PromptPair("the choices are: prefix-choice2/choice3-suffix", ""),
ppp=self.__nocupppp,
)
def test_wc_anonymouswildcard_yaml(self): # yaml anonymous wildcard
self.__process(
PromptPair("the choices are: __yaml/anonwildcards__", ""),
PromptPair("the choices are: six", ""),
ppp=self.__nocupppp,
)
def test_wc_wildcard_input(self): # simple yaml wildcard input
self.__process(
PromptPair("the choices are: __yaml_input/wildcardI__", ""),
PromptPair("the choices are: choice2", ""),
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
self.__process(
PromptPair("(test1) (test2:1.5) [test3] [(test4)]", ""),
PromptPair("(test1) (test2:1.5) (test3:0.9) (test4:0.99)", ""),
ppp=self.__comfyuippp,
)
# def test_mix(self): # def test_mix(self):
# self.__process( # self.__process(
# PromptPair( # PromptPair(
@@ -793,19 +995,6 @@ class TestPromptPostProcessor(unittest.TestCase):
# ppp=self.__nocupppp, # ppp=self.__nocupppp,
# ) # )
# def test_real(self):
# self.__wildcards_obj.refresh_wildcards(
# DEBUG_LEVEL.full,
# ["D:\\AI\\SD\\_configuraciones\\acb-wildcards\\wildcards"],
# )
# self.__process(
# PromptPair(
# "${separator=()}, __quality/high__ __misc/sep__, photograph of a __character__",
# "__negatives/ng_generic__",
# ),
# PromptPair("", ""),
# )
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()
+36 -3
View File
@@ -8,7 +8,7 @@ yaml:
- ~r2-3$$- - ~r2-3$$-
- "'label1,label2'4::choice1" - "'label1,label2'4::choice1"
- "3:: choice2 " - "3:: choice2 "
- "'label1,label3'2::choice3" - { labels: ["label1", "label3"], weight: 2, content: choice3 }
- 5 if _is_sd1::choice4 - 5 if _is_sd1::choice4
wildcard2bis: wildcard2bis:
@@ -28,9 +28,42 @@ yaml:
- { weight: 2, text: choice1 } - { weight: 2, text: choice1 }
- { weight: 3, content: choice2 } - { weight: 3, content: choice2 }
- { text: choice3 } - { text: choice3 }
- { weight: 4, if: "_is_ssd", text: choice4 } - { weight: 4, if: _is_ssd, text: choice4 }
wildcard7: wildcard7:
- <lora:test1:1> - <lora:test1:1>
- <lora:test2:1> - <lora:test2:1>
- <lora:test3:1> - <lora:test3:1>
wildcardPS:
- {
sampler: "~",
repeating: false,
count: 2,
prefix: "prefix-",
suffix: "-suffix",
separator: "/",
}
- { weight: 3, text: choice1 }
- { weight: 2, text: choice2 }
- { weight: 1, text: choice3 }
more_nested:
even_more_nested: # this would be __yaml/more_nested/even_more_nested__
- one
- two
anonwildcards:
- one
- two
- # choice without options and anonymous wildcard
- three
- four
- 3 if _is_sdxl: # choice with options and anonymous wildcard
- five
- six
- { weight: 1, text: [seven, eight] } # anonymous wildcard used in a choice in object format
- # choice without options and anonymous wildcard with parameters
- { count: 2, prefix: "#" }
- nine
- ten
+3
View File
@@ -0,0 +1,3 @@
- one
- 2
- three
+1
View File
@@ -0,0 +1 @@
one choice
File diff suppressed because one or more lines are too long