Compare commits

..
11 Commits
Author SHA1 Message Date
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
Antonio Cordero Balcazar 0b03e1773f * Added pyproject.toml to publish to ComfyUI registry.
* ComfyUI: optional modelname and model, and allowed sending a classname string instead of a model (#12)
2024-10-05 11:56:26 +02:00
Antonio Cordero Balcazar 830bd4f785 Add Github Action for Publishing to Comfy Registry (#14)
This PR adds a Github Action (publish-node-action) that will publish an
updated version of your custom node to the
[registry](https://comfyregistry.org/) whenever the `pyproject.toml`
file changes.
2024-10-05 11:03:28 +02:00
snomiao b019c5604b chore(publish): Add Github Action for Publishing to Comfy Registry 2024-10-01 16:01:06 +00:00
Antonio Cordero Balcazar 42f7fc65cc * Fixed "dictionary changed size during iteration" error.
* Support for choice labels and their use for filtering wildcard choices.
* New system variable _modelclass and renaming of _sd->_model, _sdname->_modelname and _sdfullname->_modelfullname.
* Support for auraflow models with _is_auraflow.
* Option to "join attention" when sending to negative replaced and generalized as "merge attention".
* Improved processing of extranetwork tags.
2024-09-29 22:51:39 +02:00
Antonio Cordero Balcazar 956a47adbc * Fix process wildcards setting. 2024-09-05 19:26:54 +02:00
16 changed files with 1544 additions and 516 deletions
+24
View File
@@ -0,0 +1,24 @@
name: Publish to Comfy registry
on:
workflow_dispatch:
push:
branches:
- main
- master
paths:
- "pyproject.toml"
jobs:
publish-node:
name: Publish Custom Node to registry
runs-on: ubuntu-latest
# if this is a forked repository. Skipping the workflow.
if: github.event.repository.fork == false
steps:
- name: Check out code
uses: actions/checkout@v4
- name: Publish Custom Node
uses: Comfy-Org/publish-node-action@main
with:
## Add your own personal access token to your Github Repository secrets and reference it here.
personal_access_token: ${{ secrets.REGISTRY_ACCESS_TOKEN }}
+90 -24
View File
@@ -1,11 +1,11 @@
# Prompt Postprocessor for Stable Diffusion WebUI and ComfyUI
# Prompt PostProcessor for Stable Diffusion WebUI and ComfyUI
The Prompt Postprocessor, formerly known as "sd-webui-sendtonegative", is an extension designed to process the prompt, possibly after other extensions have modified it. This extension is compatible with:
The Prompt PostProcessor (PPP), formerly known as "sd-webui-sendtonegative", is an extension designed to process the prompt, possibly after other extensions have modified it. This extension is compatible with:
* [AUTOMATIC1111 Stable Diffusion WebUI](https://github.com/AUTOMATIC1111/stable-diffusion-webui)
* [SD.Next](https://github.com/vladmandic/automatic).
* [Forge](https://github.com/lllyasviel/stable-diffusion-webui-forge)
* [reForge](https://github.com/Panchovix/stable-diffusion-webui-reForge)
* [SD.Next](https://github.com/vladmandic/automatic).
* ...and probably other forks
* [ComfyUI](https://github.com/comfyanonymous/ComfyUI)
@@ -19,6 +19,8 @@ Currently this extension has these functions:
Note: when used in an A1111 compatible webui, the extension must be loaded after any other extension that modifies the prompt (like another wildcards extension). Usually extensions load by their folder name in alphanumeric order, so if the extensions are not loading in the correct order just rename this extension's folder so the ordering works out. When in doubt, just rename this extension's folder with a "z" in front (for example) so that it is the last one to load, or manually set such folder name when installing it.
If the extension runs before others, like Dynamic Prompts, and the "Process wildcards" is enabled, the wildcards will be processed by PPP and those extensions will not get them. If you disable processing the wildcards, and intend another extension to process them, you should keep the "What to do with remaining wildcards?" option as "ignore".
Notes:
1. Other than its own commands, it only recognizes regular A1111 prompt formats. So:
@@ -31,7 +33,11 @@ Notes:
* **Composable Diffusion**: `prompt1:weight1 AND prompt2:weight2`
In SD.Next that means only the *A1111* or *Full* parsers. It will warn you if you use the *Compel* parser.
2. It recognizes wildcards in the *\_\_wildcard\_\_* and *{choice|choice}* formats (and anything that [Dynamic Prompts](https://github.com/adieyal/sd-dynamic-prompts) supports).
Does not recognize tokenizer separators like "TE2:" and "TE3:", so sending to negative prompt from those sections of the prompt will not add them in the corresponding section of the negative prompt.
ComfyUI only supports natively the attention using parenthesis, so the ones with the braces will be converted. The other constructs are not natively supported but some custom nodes implement them.
2. It recognizes wildcards in the *\_\_wildcard\_\_* and *{choice|choice}* formats (and almost everything that [Dynamic Prompts](https://github.com/adieyal/sd-dynamic-prompts) supports).
3. It does not create *AND/BREAK* constructs when moving content to the negative prompt.
## Installation
@@ -43,6 +49,8 @@ On A1111 compatible webuis:
3. Click the Install button
4. Restart the webui
On SD.Next I recommend you disable the native wildcard processing.
On ComfyUI:
1. Go to Manager > Custom Nodes Manager
@@ -90,8 +98,9 @@ The construct parameters can be written with the following options (all are opti
The choice options are as follows:
* "**n**": weight of the choice (default 1)
* "**if condition**": filters out the choice if the condition is false (this is an extension to the Dynamic Prompts syntax). Same conditions as in the `if` command.
* "**'identifiers'**": comma separated labels for the choice (optional, quotes can be single or double). Only makes sense inside a wildcard definition. Can be used when specifying the wildcard to select this specific choice. It's case insensitive.
* "**n**": weight of the choice (optional, default 1).
* "**if condition**": filters out the choice if the condition is false (optional; this is an extension to the Dynamic Prompts syntax). Same conditions as in the `if` command.
* "**::**": end of choice options
Whitespace is allowed between parameters.
@@ -109,33 +118,71 @@ These are examples of formats you can use to insert a choice construct:
Notes:
* The Dynamic Prompts format `{2$$__flavours__}` does not work as expected. It will only output one value. You can write is as `{r2$$__flavours__}` to get two values, but they may repeat since the evaluation of the wildcard is independent of the choices selection.
* Whitespace in the choices is not ignored like in Dynamic Prompts, but will be cleaned up if the appropiate settings are checked.
* Whitespace in the choices is not ignored like in Dynamic Prompts, but will be cleaned up if the appropriate settings are checked.
### Wildcards
The generic format is:
```text
__parameters$$path/to/wildcard(var=value)__
__parameters$$wildcard'filter'(var=value)__
```
The parameters and the setting of a variable are optional. The parameters follow the same format as for the choices. The variable value only applies during the evaluation of the selected choices and is discarded afterward (the variable keeps its original value if there was one).
The parameters, the filter, and the setting of a variable are optional. The parameters follow the same format as for the choices.
In the wildcard definition (which supports the text, json and yaml formats), if the first choice follows the format of these parameters, it will be used as default parameters for that wildcard (see examples in the tests folder). The choices of the wildcard follow the same format as in the choices construct. If using the object format for a choice you can use a new "if" property for the condition in addition to the standard "weight" and "text"/"content".
The wildcard identifier can contain globbing formatting, to read multiple wildcards and merge their choices. Note that if there are no parameters specified, the globbing will use the ones from the first wildcard that matches and have parameters (sorted by keys), so if you don't want that you might want to specify them. Also note that, unlike with Dynamic Prompts, the wildcard name has to be specified with its full path (unless you use globbing).
Wildcards can contain just one choice. In json and yaml formats this allows the use of a string value for the keys, rather than an array.
The filter can be used to filter specific choices from the wildcard. The filtering works before applying the choice conditions (if any). The surrounding quotes can be single or double. The filter is a comma separated list of an integer (positional choice index; zero-based) or choice label. You can also compound them with "+". That is, the comma separated items act as an OR and the "+" inside them as an AND. Using labels can simplify the definitions of complex wildcards where you want to have direct access to specific choices on occasion (you don't need to create wildcards for each individual choice). There are some additional formats when using filters. You can specify "^wildcard" as a filter to use the filter of a previous wildcard in the chain. You can start the filter (regular or inherited) with "#" and it will not be applied to the current wildcard choices, but the filter will remain in memory to use by other descendant wildcards. You use "#" and "^" when you want to pass a filter to inner wildcards (see the test files).
The variable value only applies during the evaluation of the selected choices and is discarded afterward (the variable keeps its original value if there was one).
These are examples of formats you can use to insert a wildcard:
```text
__path/wildcard__ # select 1 choice
__3$$path/wildcard__ # select 3 choices
__2-3$$path/wildcard__ # select 2 to 3 choices
__r2-3$$path/wildcard__ # select 2 to 3 choices allowing repetition
__2-3$$ / $$path/wildcard__ # select 2 to 3 choices with separator " / "
__path/wildcard(var=value)__ # select 1 choice using the specified variable value in the evaluation.
__path/wildcard__ # select 1 choice
__path/wildcard'0'__ # select the first choice
__path/wildcard'label'__ # select the choices with label "label"
__path/wildcard'0,label1,label2'__ # select the first choice and those with labels "label1" or "label2"
__path/wildcard'0,label1+label2'__ # select the first choice and those with both labels "label1" and "label2"
__3$$path/wildcard__ # select 3 choices
__2-3$$path/wildcard__ # select 2 to 3 choices
__r2-3$$path/wildcard__ # select 2 to 3 choices allowing repetition
__2-3$$ / $$path/wildcard__ # select 2 to 3 choices with separator " / "
__path/wildcard(var=value)__ # select 1 choice using the specified variable value in the evaluation.
```
#### Wildcard definitions
A wildcard definition can be:
* A txt file. The wildcard name will be the relative path of the file, without the extension. Each line will be a choice. Lines starting with "#" or empty are ignored. Doesn't support nesting.
* An array or scalar value inside a json or yaml file. The wildcard name includes the relative folder path of the file, without the extension, but also the path of the value inside the file (if there is one). If the file contains a dictionary, the filename part is not used for the wildcard name. Supports nesting by having dictionaries inside dictionaries.
The best format is a yaml file with a dictionary of wildcards inside. An editor supporting yaml syntax is recommended.
In a choice, the content after a "#" is ignored.
If the first choice follows the format of wildcard parameters, it will be used as default parameters for that wildcard (see examples in the tests folder). The choices of the wildcard follow the same format as in the choices construct, or the object format of **Dynamic Prompts** (only in structured files). If using the object format for a choice you can use a new "if" property for the condition, and the "labels" property (an array of strings) in addition to the standard "weight" and "text"/"content".
```yaml
{ labels: ["some_label"], weight: 2, if: "_is_pony", content: "the text" } # "text" property can be used instead of "content"
```
Wildcard parameters in a json/yaml file can also be in object format, and support two additional properties, prefix and suffix:
```yaml
{ sampler: "~", repeating: false, count: 2, prefix: "prefix-", suffix: "-suffix", separator: "/" }
{ sampler: "~", repeating: false, from: 2, to: 3, prefix: "prefix-", suffix: "-suffix", separator: "/" }
```
The prefix and suffix are added to the result along with the selected choices and separators. They can contain other constructs, but the separator can't.
It is recommended to use the object format for the wildcard parameters and for choices with complex options.
Wildcards can contain just one choice. In json and yaml formats this allows the use of a string value for the keys, rather than an array.
A choice inside a wildcard can also be a list or a dictionary of one element containing a list. These are considered anonymous wildcards. With a list it will be an anonymous wildcard with no choice options, and with a dictionary the key will be the options for the choice containing the anonymous wildcard and the value the choices of the anonymous wildcard. Anonymous wildcards can help formatting complex choice values that are used in only one place and thus creating a regular wildcard is not necessary. See test.yaml for examples.
#### Detection of remaining wildcards
This extension should run after any other wildcard extensions, so if you don't use the internal wildcards processing, any remaining wildcards present in the prompt or negative_prompt at this point must be invalid. Usually you might not notice this problem until you check the image metadata, so this option gives you some ways to detect and treat the problem.
@@ -161,7 +208,7 @@ The Dynamic Prompts format also works:
```text
${var=value}
${var=!value} # immmediate evaluation
${var=!value} # immediate evaluation
```
If also supports the addition as an extension of the Dynamic Prompts format:
@@ -217,9 +264,10 @@ For a list of values the allowed operations are `contains`, `in` and the value o
The variable can be one set with the `set` or `add` commands or you can use internal variables like these (names starting with an underscore are reserved):
* `_sd` : the loaded model version (`"sd1"`, `"sd2"`, `"sdxl"`)
* `_sdname` : the loaded model filename (without path)
* `_sdfullname`: the loaded model filename (with path)
* `_model` : the loaded model identifier (`"sd1"`, `"sd2"`, `"sdxl"`, `"sd3"`, `"flux"`, `"auraflow"`). `_sd` also works but is deprecated.
* `_modelname` : the loaded model filename (without path). `_sdname` also works but is deprecated.
* `_modelfullname`: the loaded model filename (with path). `_sdfullname` also works but is deprecated.
* `_modelclass`: the class used for the model. Note that this is dependent on the webui. In A1111 all SD versions use the same class. Can be used for new models that are not supported yet with the `_is_*` variables.
* `_is_sd`: true if the loaded model version is any version of SD
* `_is_sd1`: true if the loaded model version is SD 1.x
* `_is_sd2`: true if the loaded model version is SD 2.x
@@ -230,6 +278,7 @@ The variable can be one set with the `set` or `add` commands or you can use inte
* `_is_sdxl_no_pony`: true if the loaded model version is SDXL and not a Pony model.
* `_is_sd3`: true if the loaded model version is SD 3.x
* `_is_flux`: true if the loaded model is Flux
* `_is_auraflow`: true if the loaded model is AuraFlow
Any `elif`s (there can be multiple) and the `else` are optional.
@@ -296,8 +345,8 @@ The content of the negative commands is not processed and is copied as-is to the
They will be translated to the negative prompt. For example:
* `(red<ppp:stn>square<ppp:/stn>:1.5)` will end up as `(square:1.5)` in the negative prompt
* `(red[<ppp:stn>square<ppp:/stn>]:1.5)` will end up as `(square:1.35)` in the negative prompt (weight=1.5*0.9)
* However `(red<ppp:stn>[square]<ppp:/stn>:1.5)` will end up as `([square]:1.5)` in the negative prompt. The content of the negative tag is copied as is, and not joined with the surrounding modifier.
* `(red[<ppp:stn>square<ppp:/stn>]:1.5)` will end up as `(square:1.35)` in the negative prompt (weight=1.5*0.9) if the merge attention option is enabled or `([square]:1.5)` otherwise.
* However `(red<ppp:stn>[square]<ppp:/stn>:1.5)` will end up as `([square]:1.5)` in the negative prompt. The content of the negative tag is copied as is, and is not merged with the surrounding modifier because the insertions happen after the attention merging.
#### Prompt editing constructs (alternation and scheduling)
@@ -310,6 +359,21 @@ This should still work as intended, and the only negative point i see is the unn
## Configuration
### A1111 (and compatible UIs) UI options
* **Force equal seeds**: Changes the image seeds and variation seeds to be equal to the first of the batch. This allows using the same values for all the images in a batch.
* **Unlink seed**: Uses the specified seed for the prompt generation instead of the one from the image.
* **Seed**: The seed to use for the prompt generation. If -1 a random one will be used for each image in the batch. This seed is only used for wildcards and choices.
* **Variable seed**: If the seed is not -1 you can use this to increase it for the other images in the batch.
### ComfyUI specific inputs
* **model**: Connect here the MODEL or a string with the model class name used by ComfyUI. Needed for the model kind system variables.
* **modelname**: Name of the model. Needed for the model name system variables and detection of pony (this also requieres for the model to be SDXL).
* **seed**: Connect here the seed used. By default it is -1 (random).
* **pos_prompt**: Connect here the prompt text, or fill it as a widget.
* **neg_prompt**: Connect here the negative prompt text, or fill it as a widget.
### General settings
* **Debug level**: what to write to the console. Note: in SD.Next debug messages only show if you launch it with the --debug argument.
@@ -332,7 +396,6 @@ This should still work as intended, and the only negative point i see is the unn
* **Separator used when adding to the negative prompt**: you can specify the separator used when adding to the negative prompt (by default it's ", ").
* **Ignore repeated content**: it ignores repeated content to avoid repetitions in the negative prompt.
* **Join attention modifiers (weights) when possible**: it joins attention modifiers when possible (joins into one, multipliying their values).
### Clean up settings
@@ -344,8 +407,11 @@ This should still work as intended, and the only negative point i see is the unn
* **Clean up around ANDs**: removes consecutive ANDs and unnecessary commas and space around them.
* **Use EOL instead of Space before ANDs**: add a newline before ANDs.
* **Clean up around extra network tags**: removes spaces around them.
* **Merge attention modifiers (weights) when possible**: it merges attention modifiers when possible (merges into one, multiplying their values). Only merges individually nested modifiers.
* **Remove extra spaces**: removes other unnecessary spaces.
Please note that ComfyUI does not support the BREAK and AND constructs, but the related settings are kept in that UI.
### Content removal settings
* **Remove extra network tags**: removes all extra network tags.
-10
View File
@@ -13,16 +13,6 @@ sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from .ppp_comfyui import PromptPostProcessorComfyUINode
NODE_CLASS_MAPPINGS = {"ACBPromptPostProcessor": PromptPostProcessorComfyUINode}
NODE_DISPLAY_NAME_MAPPINGS = {"ACBPromptPostProcessor": "ACB Prompt Post Processor"}
MANIFEST = {
"name": "ACB Prompt Post Processor",
"version": PromptPostProcessorComfyUINode.VERSION,
"author": "ACB",
"project": "https://github.com/acorderob/sd-webui-prompt-postprocessor",
"description": "Node for processing prompts",
"license": "MIT",
}
__all__ = ["NODE_CLASS_MAPPINGS", "NODE_DISPLAY_NAME_MAPPINGS"]
+37 -30
View File
@@ -1,8 +1,10 @@
%import common (LETTER, DIGIT, INT, CNAME, SIGNED_NUMBER, NUMBER)
_WHITESPACE: /\s+/
STRING: /("(?!"").*?(?<!\\)(\\\\)*?"|'(?!'').*?(?<!\\)(\\\\)*?')/i
BOOLEAN: /true|false/i
_WHITESPACE: /\s+/
STRING: /("(?!"").*?(?<!\\)(\\\\)*?"|'(?!'').*?(?<!\\)(\\\\)*?')/i
BOOLEAN: /true|false/i
INDEX: INT | IDENTIFIER
WILDCARD_NAME: /(?:(?!__|\$\$|[('"])\S)+/
// plain text and weights
?plain: /((?!__|\bAND\b|\${)[^\\()\[\]:<>${]|\\.)+/s // exclude only the starting ones
@@ -19,33 +21,33 @@ BOOLEAN: /true|false/i
start: promptcomp | content
// prompt composition with AND
promptcomp.4: promptcomppart ([":" numpar] (/\bAND\b/ promptcomppart [":" numpar])+)+
promptcomp.4: promptcomppart ( [ ":" numpar ] ( /\bAND\b/ promptcomppart [ ":" numpar ] )+ )+
promptcomppart: content
// simple prompts
?content.2: (old_content | new_content | plain | specialchars)*
?content_choice.2: (old_content | new_content | plain_choice | specialchars_choice)*
?content_var.2: (old_content | new_content | plain_var | specialchars_var)*
?content_negtag.2: (old_content | new_content_negtag | plain | specialchars_negtag)*
?content_alternate.2: (old_content | new_content | plain_alternate | specialchars_alternate)*
?old_content.2: (emphasized | deemphasized | scheduled | alternate | extranetworktag)+
?new_content.3: (variableset | variableuse | commandstn | commandstni | commandset | commandecho | commandif | wildcard | choices)+
?new_content_negtag.3: (variableset | variableuse | commandset | commandecho | commandif | wildcard | choices)+
?content.2: ( old_content | new_content | plain | specialchars )*
?inside_content.2: ( new_content | plain | specialchars )*
?content_choice.2: ( old_content | new_content | plain_choice | specialchars_choice )*
?content_var.2: ( old_content | new_content | plain_var | specialchars_var )*
?content_negtag.2: ( old_content | new_content_negtag | plain | specialchars_negtag )*
?content_alternate.2: ( old_content | new_content | plain_alternate | specialchars_alternate )*
?old_content.2: ( attention | scheduled | alternate | extranetworktag )+
?new_content.3: ( variableset | variableuse | commandstn | commandstni | commandset | commandecho | commandif | wildcard | choices )+
?new_content_negtag.3: ( variableset | variableuse | commandset | commandecho | commandif | wildcard | choices )+
// attention modifiers
emphasized: "(" content [":" numpar] ")"
deemphasized: "[" content "]"
attention: ( "(" content [ ":" numpar ] ")" ) | ( "[" content "]" )
// prompt scheduling and alternation
alternate: "[" alternateoption ("|" alternateoption)+ "]"
alternate: "[" alternateoption ( "|" alternateoption )+ "]"
alternateoption: content_alternate
scheduled: "[" [content ":"] content ":" numpar "]"
scheduled: "[" [ content ":" ] content ":" numpar "]"
// extra network tags
extranetworktag: "<" /(?!ppp:)[^>]+/ ">"
extranetworktag: "<" /(?!ppp:)\w+:/ inside_content ">"
// command: stn (send to negative)
commandstn: "<ppp:stn" [_WHITESPACE /s|e|p\d/] _WHITESPACE? ">" content_negtag "<ppp:/stn>"
commandstn: "<ppp:stn" [ _WHITESPACE /s|e|p\d/ ] _WHITESPACE? ">" content_negtag "<ppp:/stn>"
commandstni: "<ppp:stn" _WHITESPACE /i\d/ _WHITESPACE? ">"
// command: if
@@ -57,12 +59,12 @@ ifvalue: content
// conditions
condition: conditionsimplevalue | conditionlistvalue | conditionnocomparison
conditionnocomparison: (/not/ _WHITESPACE)? IDENTIFIER
conditionsimplevalue: (/not/ _WHITESPACE)? IDENTIFIER _WHITESPACE (/not/ _WHITESPACE)? /eq|ne|gt|lt|ge|le|contains/ _WHITESPACE SIMPLEVALUE
conditionlistvalue: (/not/ _WHITESPACE)? IDENTIFIER _WHITESPACE (/not/ _WHITESPACE)? /contains|in/ _WHITESPACE listvalue
conditionnocomparison: ( /not/ _WHITESPACE )? IDENTIFIER
conditionsimplevalue: ( /not/ _WHITESPACE )? IDENTIFIER _WHITESPACE ( /not/ _WHITESPACE )? /eq|ne|gt|lt|ge|le|contains/ _WHITESPACE SIMPLEVALUE
conditionlistvalue: ( /not/ _WHITESPACE )? IDENTIFIER _WHITESPACE ( /not/ _WHITESPACE )? /contains|in/ _WHITESPACE listvalue
IDENTIFIER: CNAME
SIMPLEVALUE: STRING | INT | BOOLEAN
listvalue: "(" _WHITESPACE? SIMPLEVALUE (_WHITESPACE? "," _WHITESPACE? SIMPLEVALUE)* _WHITESPACE? ")"
SIMPLEVALUE: STRING | NUMBER | BOOLEAN
listvalue: "(" _WHITESPACE? SIMPLEVALUE ( _WHITESPACE? "," _WHITESPACE? SIMPLEVALUE )* _WHITESPACE? ")"
// command: set
commandset: "<ppp:set" _WHITESPACE IDENTIFIER [ _WHITESPACE? /evaluate/ ] [ _WHITESPACE? /add/ ] _WHITESPACE? ">" content "<ppp:/set>"
@@ -71,20 +73,23 @@ commandset: "<ppp:set" _WHITESPACE IDENTIFIER [ _WHITESPACE? /evaluate/ ] [ _WHI
commandecho: "<ppp:echo" _WHITESPACE IDENTIFIER _WHITESPACE? ">" [ content "<ppp:/echo>" ]
// variable set
variableset.2: "${" _WHITESPACE? IDENTIFIER _WHITESPACE? [/\+/] "=" [/!/] varvalue "}"
variableset.2: "${" _WHITESPACE? IDENTIFIER _WHITESPACE? [ /\+/ ] "=" [ /!/ ] varvalue "}"
// variable use
variableuse.2: "${" _WHITESPACE? IDENTIFIER _WHITESPACE? [":" varvalue] "}"
variableuse.2: "${" _WHITESPACE? IDENTIFIER _WHITESPACE? [ ":" varvalue ] "}"
varvalue: content_var
// wildcards
wildcard.2: "__" [choicesoptions_sampler | (choicesoptions _WHITESPACE? "$$")] /(?:(?!__|\$\$|\()\S)+/ [ wildcard_var ] "__"
wildcard_var: "(" _WHITESPACE? IDENTIFIER _WHITESPACE? "=" varvalue ")"
wildcard.2: "__" [ choicesoptions_sampler | ( choicesoptions _WHITESPACE? "$$" ) ] WILDCARD_NAME [ wc_filter ] [ wildcardvar ] "__"
wc_filter: /["']/ ( [ /#/ ] wc_filter_or | ( /#?\^/ WILDCARD_NAME ) ) /["']/
wc_filter_or: wc_filter_and ( _WHITESPACE? "," _WHITESPACE? wc_filter_and )*
wc_filter_and: INDEX ( _WHITESPACE? "+" _WHITESPACE? INDEX )*
wildcardvar: "(" _WHITESPACE? IDENTIFIER _WHITESPACE? "=" varvalue ")"
// choices
choices.2: "{" [choicesoptions_sampler | (choicesoptions _WHITESPACE? "$$")] choice ("|" choice)* "}"
choices.2: "{" [ choicesoptions_sampler | ( choicesoptions _WHITESPACE? "$$" ) ] choice ( "|" choice )* "}"
choicesoptions: [choicesoptions_sampler] [_WHITESPACE? choicesoptions_rep] (([_WHITESPACE? choicesoptions_from] "-" [_WHITESPACE? choicesoptions_to]) | [_WHITESPACE? choicesoptions_num] ) [_WHITESPACE? choicesoptions_sep]
choicesoptions: [ choicesoptions_sampler ] [ _WHITESPACE? choicesoptions_rep ] ( ( [ _WHITESPACE? choicesoptions_from ] "-" [ _WHITESPACE? choicesoptions_to ] ) | [ _WHITESPACE? choicesoptions_num ] ) [ _WHITESPACE? choicesoptions_sep ]
choicesoptions_sampler: /[~@]/ // ~ for random, @ for cyclical
choicesoptions_rep: /r/
choicesoptions_num: INT
@@ -92,7 +97,9 @@ choicesoptions_from: INT
choicesoptions_to: INT
choicesoptions_sep: "$$" plain
choice: [[_WHITESPACE? choiceweight] [_WHITESPACE? choiceif] _WHITESPACE? "::"] choicevalue
choice: [ [ _WHITESPACE? choicelabels ] [ _WHITESPACE? choiceweight ] [ _WHITESPACE? choiceif ] _WHITESPACE? "::" ] choicevalue
choicelabels: /["']/ IDENTIFIER ( _WHITESPACE? "," _WHITESPACE? IDENTIFIER )* /["']/
choiceweight: NUMBER
choiceif: "if" _WHITESPACE condition
choicevalue: content_choice
+10 -2
View File
@@ -1,5 +1,13 @@
import os
import launch
requirements_filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), "requirements.txt")
launch.run_pip(f'install -r "{requirements_filename}"', "requirements for Prompt Post-Processor")
try:
from modules.launch_utils import requirements_met, run_pip # A1111
if not requirements_met(requirements_filename):
run_pip(f'install -r "{requirements_filename}"', "requirements for Prompt Post-Processor")
except ImportError:
import launch
launch.run_pip(f'install -r "{requirements_filename}"', "requirements for Prompt Post-Processor")
+581 -223
View File
File diff suppressed because it is too large Load Diff
+2 -2
View File
@@ -4,8 +4,8 @@ from typing import Tuple
class PPPLRUCache:
ProcessInput = Tuple[int, str, str]
ProcessResult = Tuple[str, str]
ProcessInput = Tuple[int, int, str, str] # (seed, wildcards_hash, positive_prompt, negative_prompt)
ProcessResult = Tuple[str, str] # (positive_prompt, negative_prompt)
def __init__(self, capacity: int):
self.cache = OrderedDict()
+124 -49
View File
@@ -16,8 +16,6 @@ if __name__ == "__main__":
class PromptPostProcessorComfyUINode:
VERSION = PromptPostProcessor.VERSION
logger = None
def __init__(self):
@@ -27,30 +25,28 @@ class PromptPostProcessorComfyUINode:
with open(grammar_filename, "r", encoding="utf-8") as file:
self.grammar_content = file.read()
self.wildcards_obj = PPPWildcards(lf.log)
self.logger.info(f"{PromptPostProcessor.NAME} {PromptPostProcessor.VERSION} initialized")
class SmartType(str):
def __ne__(self, other):
if self == "*" or other == "*":
return False
selfset = set(self.split(","))
otherset = set(other.split(","))
return not otherset.issubset(selfset)
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"model": (
"MODEL",
{
"forceInput": True,
},
),
"modelname": (
"STRING",
{
"default": "",
"forceInput": True,
},
),
"pos_prompt": (
"STRING",
{
"multiline": True,
"default": "",
"forceInput": True,
"dynamicPrompts": False,
"defaultInput": True,
"forceInput": False,
},
),
"neg_prompt": (
@@ -58,15 +54,35 @@ class PromptPostProcessorComfyUINode:
{
"multiline": True,
"default": "",
"forceInput": True,
"dynamicPrompts": False,
"defaultInput": True,
"forceInput": False,
},
),
},
"optional": {
"model": (
cls.SmartType("MODEL,STRING"),
{
"default": "",
"placeholder": "internal model class name",
"forceInput": True,
},
),
"modelname": (
"STRING",
{
"default": "",
"placeholder": "full path of the model",
"defaultInput": True,
"forceInput": False,
},
),
"seed": (
"INT",
{
"default": -1,
"defaultInput": True,
"forceInput": False,
},
),
@@ -75,6 +91,8 @@ class PromptPostProcessorComfyUINode:
{
"default": DEBUG_LEVEL.minimal.value,
"tooltip": "Debug level",
"defaultInput": False,
"forceInput": False,
},
),
"pony_substrings": (
@@ -83,6 +101,8 @@ class PromptPostProcessorComfyUINode:
"default": PromptPostProcessor.DEFAULT_PONY_SUBSTRINGS,
"placeholder": "comma separated list",
"tooltip": "Comma separated list of substrings to look for in the modelname to determine if the model is a pony model",
"defaultInput": False,
"forceInput": False,
},
),
"wc_process_wildcards": (
@@ -92,6 +112,8 @@ class PromptPostProcessorComfyUINode:
"tooltip": "Process wildcards in the prompt",
"label_on": "Yes",
"label_off": "No",
"defaultInput": False,
"forceInput": False,
},
),
"wc_wildcards_folders": (
@@ -99,6 +121,8 @@ class PromptPostProcessorComfyUINode:
{
"default": "",
"tooltip": "Comma separated list of wildcards folders",
"defaultInput": False,
"forceInput": False,
},
),
"wc_if_wildcards": (
@@ -106,6 +130,8 @@ class PromptPostProcessorComfyUINode:
{
"default": PromptPostProcessor.IFWILDCARDS_CHOICES.ignore.value,
"tooltip": "How to handle invalid wildcards in the prompt",
"defaultInput": False,
"forceInput": False,
},
),
"wc_choice_separator": (
@@ -113,6 +139,8 @@ class PromptPostProcessorComfyUINode:
{
"default": PromptPostProcessor.DEFAULT_CHOICE_SEPARATOR,
"tooltip": "Default separator for selected choices",
"defaultInput": False,
"forceInput": False,
},
),
"wc_keep_choices_order": (
@@ -122,6 +150,8 @@ class PromptPostProcessorComfyUINode:
"tooltip": "Keep the order of the choices in the prompt",
"label_on": "Yes",
"label_off": "No",
"defaultInput": False,
"forceInput": False,
},
),
"stn_separator": (
@@ -129,6 +159,8 @@ class PromptPostProcessorComfyUINode:
{
"default": PromptPostProcessor.DEFAULT_STN_SEPARATOR,
"tooltip": "Separator for the content added to the negative prompt",
"defaultInput": False,
"forceInput": False,
},
),
"stn_ignore_repeats": (
@@ -138,15 +170,8 @@ class PromptPostProcessorComfyUINode:
"tooltip": "Ignore repeated content added to the negative prompt",
"label_on": "Yes",
"label_off": "No",
},
),
"stn_join_attention": (
"BOOLEAN",
{
"default": True,
"tooltip": "Merge attention in the content added to the negative prompt",
"label_on": "Yes",
"label_off": "No",
"defaultInput": False,
"forceInput": False,
},
),
"cleanup_extra_spaces": (
@@ -156,6 +181,8 @@ class PromptPostProcessorComfyUINode:
"tooltip": "Remove extra spaces",
"label_on": "Yes",
"label_off": "No",
"defaultInput": False,
"forceInput": False,
},
),
"cleanup_empty_constructs": (
@@ -165,6 +192,8 @@ class PromptPostProcessorComfyUINode:
"tooltip": "Remove empty constructs",
"label_on": "Yes",
"label_off": "No",
"defaultInput": False,
"forceInput": False,
},
),
"cleanup_extra_separators": (
@@ -174,6 +203,8 @@ class PromptPostProcessorComfyUINode:
"tooltip": "Remove extra separators",
"label_on": "Yes",
"label_off": "No",
"defaultInput": False,
"forceInput": False,
},
),
"cleanup_extra_separators2": (
@@ -183,15 +214,19 @@ class PromptPostProcessorComfyUINode:
"tooltip": "Remove extra separators (additional cases)",
"label_on": "Yes",
"label_off": "No",
"defaultInput": False,
"forceInput": False,
},
),
"cleanup_breaks": (
"BOOLEAN",
{
"default": True,
"default": False,
"tooltip": "Cleanup around BREAKs",
"label_on": "Yes",
"label_off": "No",
"defaultInput": False,
"forceInput": False,
},
),
"cleanup_breaks_eol": (
@@ -201,15 +236,19 @@ class PromptPostProcessorComfyUINode:
"tooltip": "Set BREAKs in their own line",
"label_on": "Yes",
"label_off": "No",
"defaultInput": False,
"forceInput": False,
},
),
"cleanup_ands": (
"BOOLEAN",
{
"default": True,
"default": False,
"tooltip": "Cleanup around ANDs",
"label_on": "Yes",
"label_off": "No",
"defaultInput": False,
"forceInput": False,
},
),
"cleanup_ands_eol": (
@@ -219,6 +258,8 @@ class PromptPostProcessorComfyUINode:
"tooltip": "Set ANDs in their own line",
"label_on": "Yes",
"label_off": "No",
"defaultInput": False,
"forceInput": False,
},
),
"cleanup_extranetwork_tags": (
@@ -228,6 +269,19 @@ class PromptPostProcessorComfyUINode:
"tooltip": "Clean up around extra network tags",
"label_on": "Yes",
"label_off": "No",
"defaultInput": False,
"forceInput": False,
},
),
"cleanup_merge_attention": (
"BOOLEAN",
{
"default": True,
"tooltip": "Merge nested attention constructs",
"label_on": "Yes",
"label_off": "No",
"defaultInput": False,
"forceInput": False,
},
),
"remove_extranetwork_tags": (
@@ -237,11 +291,26 @@ class PromptPostProcessorComfyUINode:
"tooltip": "Remove extra network tags",
"label_on": "Yes",
"label_off": "No",
"defaultInput": False,
"forceInput": False,
},
),
},
}
@classmethod
def VALIDATE_INPUTS(cls, input_types: dict[str, str]):
it = cls.INPUT_TYPES()
expected = {
k: cls.SmartType("COMBO,STRING") if isinstance(v[0], list) else v[0] # we allow string for combos
for k, v in {**it["required"], **it["optional"]}.items()
}
for input_name, input_type in input_types.items():
t = expected[input_name]
if input_type != t:
return f"Invalid type for input '{input_name}': {input_type} (expected {t})"
return True
RETURN_TYPES = (
"STRING",
"STRING",
@@ -272,7 +341,6 @@ class PromptPostProcessorComfyUINode:
wc_keep_choices_order,
stn_separator,
stn_ignore_repeats,
stn_join_attention,
cleanup_extra_spaces,
cleanup_empty_constructs,
cleanup_extra_separators,
@@ -282,9 +350,12 @@ class PromptPostProcessorComfyUINode:
cleanup_ands,
cleanup_ands_eol,
cleanup_extranetwork_tags,
cleanup_merge_attention,
remove_extranetwork_tags,
):
new_run = {
if wc_process_wildcards:
return float("NaN") # since we can't detect changes in wildcards we assume they are always changed when enabled
new_run = { # everything except debug_level
"model": model,
"modelname": modelname,
"pos_prompt": pos_prompt,
@@ -298,7 +369,6 @@ class PromptPostProcessorComfyUINode:
"keep_choices_order": wc_keep_choices_order,
"stn_separator": stn_separator,
"stn_ignore_repeats": stn_ignore_repeats,
"stn_join_attention": stn_join_attention,
"cleanup_extra_spaces": cleanup_extra_spaces,
"cleanup_empty_constructs": cleanup_empty_constructs,
"cleanup_extra_separators": cleanup_extra_separators,
@@ -308,6 +378,7 @@ class PromptPostProcessorComfyUINode:
"cleanup_ands": cleanup_ands,
"cleanup_ands_eol": cleanup_ands_eol,
"cleanup_extranetwork_tags": cleanup_extranetwork_tags,
"cleanup_merge_attention": cleanup_merge_attention,
"remove_extranetwork_tags": remove_extranetwork_tags,
}
return new_run.__hash__
@@ -329,7 +400,6 @@ class PromptPostProcessorComfyUINode:
wc_keep_choices_order,
stn_separator,
stn_ignore_repeats,
stn_join_attention,
cleanup_extra_spaces,
cleanup_empty_constructs,
cleanup_extra_separators,
@@ -339,25 +409,30 @@ class PromptPostProcessorComfyUINode:
cleanup_ands,
cleanup_ands_eol,
cleanup_extranetwork_tags,
cleanup_merge_attention,
remove_extranetwork_tags,
):
model_info = {
modelclass = (
model.model.model_config.__class__.__name__ if model is not None and not isinstance(model, str) else model
) or ""
if modelclass == "":
self.logger.warning("Model class is not provided. System variables might not be properly set.")
if modelname == "":
self.logger.warning("Modelname is not provided. System variables will not be properly set.")
env_info = {
"app": "comfyui",
"models_path": folder_paths.models_dir,
"model_filename": modelname, # path is relative to checkpoints folder
"is_sd1": model.model.model_config.__class__.__name__ in ("SD15", "SD15_instructpix2pix"),
"is_sd2": model.model.model_config.__class__.__name__ in ("SD20", "SD21UnclipL", "SD21UnclipH"),
"is_sdxl": model.model.model_config.__class__.__name__
in (
"SDXL",
"SDXLRefiner",
"SDXL_instructpix2pix",
"Segmind_Vega",
"KOALA_700M",
"KOALA_1B",
"model_filename": modelname or "", # path is relative to checkpoints folder
"model_class": modelclass,
"is_sd1": modelclass in ("SD15", "SD15_instructpix2pix"),
"is_sd2": modelclass in ("SD20", "SD21UnclipL", "SD21UnclipH"),
"is_sdxl": (
modelclass in ("SDXL", "SDXLRefiner", "SDXL_instructpix2pix", "Segmind_Vega", "KOALA_700M", "KOALA_1B")
),
"is_ssd": model.model.model_config.__class__.__name__ in ("SSD1B"),
"is_sd3": model.model.model_config.__class__.__name__ in ("SD3"),
"is_flux": model.model.model_config.__class__.__name__ in ("Flux"),
"is_ssd": modelclass in ("SSD1B",),
"is_sd3": modelclass in ("SD3",),
"is_flux": modelclass in ("Flux",),
"is_auraflow": modelclass in ("AuraFlow",),
}
# SVD_img2vid, SVD3D_u, SVD3_p, Stable_Zero123, SD_X4Upscaler,
# Stable_Cascade_C, Stable_Cascade_B, StableAudio
@@ -380,7 +455,6 @@ class PromptPostProcessorComfyUINode:
"keep_choices_order": wc_keep_choices_order,
"stn_separator": stn_separator,
"stn_ignore_repeats": stn_ignore_repeats,
"stn_join_attention": stn_join_attention,
"cleanup_extra_spaces": cleanup_extra_spaces,
"cleanup_empty_constructs": cleanup_empty_constructs,
"cleanup_extra_separators": cleanup_extra_separators,
@@ -390,11 +464,12 @@ class PromptPostProcessorComfyUINode:
"cleanup_ands": cleanup_ands,
"cleanup_ands_eol": cleanup_ands_eol,
"cleanup_extranetwork_tags": cleanup_extranetwork_tags,
"cleanup_merge_attention": cleanup_merge_attention,
"remove_extranetwork_tags": remove_extranetwork_tags,
}
self.wildcards_obj.refresh_wildcards(debug_level, wildcards_folders if options["process_wildcards"] else None)
ppp = PromptPostProcessor(
self.logger, self.interrupt, model_info, options, self.grammar_content, self.wildcards_obj
self.logger, self.interrupt, env_info, options, self.grammar_content, self.wildcards_obj
)
pos_prompt, neg_prompt = ppp.process_prompt(pos_prompt, neg_prompt, seed if seed is not None else 1)
return (
+273 -84
View File
@@ -1,47 +1,115 @@
import fnmatch
import os
import json
from typing import Optional
import logging
import yaml
from ppp_logging import DEBUG_LEVEL
def deep_freeze(obj):
"""
Deep freeze an object.
Args:
obj (object): The object to freeze.
Returns:
object: The frozen object.
"""
if isinstance(obj, dict):
return tuple((k, deep_freeze(v)) for k, v in sorted(obj.items()))
elif isinstance(obj, list):
return tuple(deep_freeze(i) for i in obj)
elif isinstance(obj, set):
return tuple(deep_freeze(i) for i in sorted(obj))
else:
return obj
class PPPWildcard:
"""
A wildcard object.
Attributes:
key (str): The key of the wildcard.
file (str): The path to the file where the wildcard is defined.
unprocessed_choices (list[str]): The unprocessed choices of the wildcard.
choices (list[dict]): The processed choices of the wildcard.
options (dict): The options of the wildcard.
"""
def __init__(self, fullpath: str, key: str, choices: list[str]):
self.key: str = key
self.file: str = fullpath
self.unprocessed_choices: list[str] = choices
self.choices: list[dict] = None
self.options: dict = None
def __hash__(self) -> int:
t = (self.key, deep_freeze(self.unprocessed_choices))
return hash(t)
class PPPWildcards:
"""
A class to manage wildcards.
Attributes:
wildcards (dict[str, PPPWildcard]): The wildcards.
"""
DEFAULT_WILDCARDS_FOLDER = "wildcards"
def __init__(self, logger):
self.logger = logger
self.debug_level = DEBUG_LEVEL.none
self.wildcards_folders = []
self.wildcards = {}
self.wildcard_files = {}
self.__logger: logging.Logger = logger
self.__debug_level = DEBUG_LEVEL.none
self.__wildcards_folders = []
self.__wildcard_files = {}
self.wildcards: dict[str, PPPWildcard] = {}
def __hash__(self) -> int:
return hash(deep_freeze(self.wildcards))
def refresh_wildcards(self, debug_level: DEBUG_LEVEL, wildcards_folders: Optional[list[str]]):
"""
Initialize the wildcards.
"""
self.debug_level = debug_level
self.wildcards_folders = wildcards_folders
self.__debug_level = debug_level
self.__wildcards_folders = wildcards_folders
if wildcards_folders is not None:
# if self.debug_level != DEBUG_LEVEL.none:
# self.logger.info("Initializing wildcards...")
# t1 = time.time()
for fullpath in self.wildcard_files.keys():
for fullpath in list(self.__wildcard_files.keys()):
path = os.path.dirname(fullpath)
if not os.path.exists(fullpath) or not any(
os.path.commonpath([path, folder]) == folder for folder in self.wildcards_folders
os.path.commonpath([path, folder]) == folder for folder in self.__wildcards_folders
):
self.__remove_wildcards_from_file(fullpath)
for f in self.wildcards_folders:
for f in self.__wildcards_folders:
self.__get_wildcards_in_directory(f, f)
# t2 = time.time()
# if self.debug_level != DEBUG_LEVEL.none:
# self.logger.info(f"Wildcards init time: {t2 - t1:.3f} seconds")
else:
self.wildcards_folders = []
self.__wildcards_folders = []
self.wildcards = {}
self.wildcard_files = {}
self.__wildcard_files = {}
def get_wildcards(self, key: str) -> list[PPPWildcard]:
"""
Get all wildcards that match a key.
Args:
key (str): The key to match.
Returns:
list: A list of all wildcards that match the key.
"""
keys = sorted(fnmatch.filter(self.wildcards.keys(), key))
return [self.wildcards[k] for k in keys]
def __get_keys_in_dict(self, dictionary: dict, prefix="") -> list[str]:
"""
@@ -81,20 +149,21 @@ class PPPWildcards:
return None
return current_dict
def __remove_wildcards_from_file(self, full_path: str):
def __remove_wildcards_from_file(self, full_path: str, debug=True):
"""
Clear all wildcards in a file.
Args:
full_path (str): The path to the file.
debug (bool): Whether to print debug messages or not.
"""
last_modified_cached = self.wildcard_files.get(full_path, None)
if last_modified_cached is not None and self.debug_level != DEBUG_LEVEL.none:
self.logger.debug(f"Removing wildcards from file: {full_path}")
if full_path in self.wildcard_files.keys():
del self.wildcard_files[full_path]
last_modified_cached = self.__wildcard_files.get(full_path, None)
if debug and last_modified_cached is not None and self.__debug_level != DEBUG_LEVEL.none:
self.__logger.debug(f"Removing wildcards from file: {full_path}")
if full_path in self.__wildcard_files.keys():
del self.__wildcard_files[full_path]
for key in list(self.wildcards.keys()):
if self.wildcards[key]["file"] == full_path:
if self.wildcards[key].file == full_path:
del self.wildcards[key]
def __get_wildcards_in_file(self, base, full_path: str):
@@ -106,87 +175,207 @@ class PPPWildcards:
full_path (str): The path to the file.
"""
last_modified = os.path.getmtime(full_path)
last_modified_cached = self.wildcard_files.get(full_path, None)
if last_modified_cached is not None and last_modified == self.wildcard_files[full_path]:
last_modified_cached = self.__wildcard_files.get(full_path, None)
if last_modified_cached is not None and last_modified == self.__wildcard_files[full_path]:
return
filename = os.path.basename(full_path)
name, extension = os.path.splitext(filename)
_, extension = os.path.splitext(filename)
if extension not in (".txt", ".json", ".yaml", ".yml"):
return
self.__remove_wildcards_from_file(full_path)
if last_modified_cached is not None and self.debug_level != DEBUG_LEVEL.none:
self.logger.debug(f"Updating wildcards from file: {full_path}")
relfolders = os.path.relpath(os.path.dirname(full_path), base)
if relfolders == ".":
relfolders = ""
elif relfolders != "":
relfolders += "/"
self.__remove_wildcards_from_file(full_path, False)
if last_modified_cached is not None and self.__debug_level != DEBUG_LEVEL.none:
self.__logger.debug(f"Updating wildcards from file: {full_path}")
if extension == ".txt":
self.__get_wildcards_in_text_file(full_path, name, relfolders)
self.__get_wildcards_in_text_file(full_path, base)
elif extension in (".json", ".yaml", ".yml"):
self.__get_wildcards_in_structured_file(full_path, extension, relfolders)
self.wildcard_files[full_path] = last_modified
self.__get_wildcards_in_structured_file(full_path, base)
self.__wildcard_files[full_path] = last_modified
def __get_wildcards_in_structured_file(self, full_path, extension, relfolders):
def is_dict_choices_options(self, d: dict) -> bool:
"""
Check if a dictionary is a valid choices options dictionary.
Args:
d (dict): The dictionary to check.
Returns:
bool: Whether the dictionary is a valid choices options dictionary or not.
"""
return all(
k in ["sampler", "repeating", "count", "from", "to", "prefix", "suffix", "separator"] for k in d.keys()
)
def is_dict_choice_options(self, d: dict) -> bool:
"""
Check if a dictionary is a valid choice options dictionary.
Args:
d (dict): The dictionary to check.
Returns:
bool: Whether the dictionary is a valid choice options dictionary or not.
"""
return all(k in ["labels", "weight", "if", "content", "text"] for k in d.keys())
def __get_choices(self, obj: object, full_path: str, key_parts: list[str]) -> list:
"""
We process the choices in the object and return them as a list.
Args:
obj (object): the value of a wildcard
full_path (str): path to the file where the wildcard is defined
key_parts (list[str]): parts of the key for the wildcard
Returns:
list: list of choices
"""
choices = None
if obj is not None:
if isinstance(obj, (str, dict)):
choices = [obj]
elif isinstance(obj, (int, float, bool)):
choices = [str(obj)]
elif isinstance(obj, list) and len(obj) > 0:
choices = []
for i, c in enumerate(obj):
invalid_choice = False
if isinstance(c, str):
choice = c
elif isinstance(c, (int, float, bool)):
choice = str(c)
elif isinstance(c, list):
# we create an anonymous wildcard
choice = self.__create_anonymous_wildcard(full_path, key_parts, i, c)
elif isinstance(c, dict):
if self.is_dict_choices_options(c) or self.is_dict_choice_options(c):
# we assume it is a choice or wildcard parameters in object format
choice = c
choice_content = choice.get("content", choice.get("text", None))
if choice_content is not None and isinstance(choice_content, list):
# we create an anonymous wildcard
choice["content"] = self.__create_anonymous_wildcard(
full_path, key_parts, i, choice_content
)
if "text" in choice:
del choice["text"]
elif len(c) == 1:
# we assume it is an anonymous wildcard with options
firstkey = list(c.keys())[0]
choice = self.__create_anonymous_wildcard(full_path, key_parts, i, c[firstkey], firstkey)
else:
invalid_choice = True
else:
invalid_choice = True
if invalid_choice:
self.__logger.warning(
f"Invalid choice {i+1} in wildcard '{'/'.join(key_parts)}' in file '{full_path}'!"
)
else:
choices.append(choice)
return choices
def __create_anonymous_wildcard(self, full_path, key_parts, i, content, options=None):
"""
Create an anonymous wildcard.
Args:
full_path (str): The path to the file that contains it.
key_parts (list[str]): The parts of the key.
i (int): The index of the wildcard.
content (object): The content of the wildcard.
options (str): The options for the choice where the wildcard is defined.
Returns:
str: The resulting value for the choice.
"""
new_parts = key_parts + [f"#ANON_{i}"]
self.__add_wildcard(content, full_path, new_parts)
value = f"__{'/'.join(new_parts)}__"
if options is not None:
value = f"{options}::{value}"
return value
def __add_wildcard(self, content: object, full_path: str, external_key_parts: list[str]):
"""
Add a wildcard to the wildcards dictionary.
Args:
content (object): The content of the wildcard.
full_path (str): The path to the file that contains it.
external_key_parts (list[str]): The parts of the key.
"""
key_parts = external_key_parts.copy()
if isinstance(content, dict):
key_parts.pop()
keys = self.__get_keys_in_dict(content)
for key in keys:
tmp_key_parts = key_parts.copy()
tmp_key_parts.extend(key.split("/"))
fullkey = "/".join(tmp_key_parts)
if self.wildcards.get(fullkey, None) is not None:
self.__logger.warning(
f"Duplicate wildcard '{fullkey}' in file '{full_path}' and '{self.wildcards[fullkey].file}'!"
)
else:
obj = self.__get_nested(content, key)
choices = self.__get_choices(obj, full_path, tmp_key_parts)
if choices is None:
self.__logger.warning(f"Invalid wildcard '{fullkey}' in file '{full_path}'!")
else:
self.wildcards[fullkey] = PPPWildcard(full_path, fullkey, choices)
return
if isinstance(content, str):
content = [content]
elif isinstance(content, (int, float, bool)):
content = [str(content)]
if not isinstance(content, list):
self.__logger.warning(f"Invalid wildcard in file '{full_path}'!")
return
fullkey = "/".join(key_parts)
if self.wildcards.get(fullkey, None) is not None:
self.__logger.warning(
f"Duplicate wildcard '{fullkey}' in file '{full_path}' and '{self.wildcards[fullkey].file}'!"
)
else:
choices = self.__get_choices(content, full_path, key_parts)
if choices is None:
self.__logger.warning(f"Invalid wildcard '{fullkey}' in file '{full_path}'!")
else:
self.wildcards[fullkey] = PPPWildcard(full_path, fullkey, choices)
def __get_wildcards_in_structured_file(self, full_path, base):
"""
Get all wildcards in a structured file.
Args:
full_path (str): The path to the file.
base (str): The base path for the wildcards.
"""
external_key: str = os.path.relpath(os.path.splitext(full_path)[0], base)
external_key_parts = external_key.split(os.sep)
_, extension = os.path.splitext(full_path)
with open(full_path, "r", encoding="utf-8") as file:
if extension == ".json":
content = json.loads(file.read())
else:
content = yaml.safe_load(file)
keys = self.__get_keys_in_dict(content)
for key in keys:
fullkey = f"{relfolders}{key}"
if self.wildcards.get(fullkey) is not None:
self.logger.warning(
f"Duplicate wildcard '{fullkey}' in file '{full_path}' and '{self.wildcards[fullkey]['file']}'!"
)
else:
obj = self.__get_nested(content, key)
if obj is not None:
if isinstance(obj, str):
choices = [obj]
elif isinstance(obj, (int, float, bool)):
choices = [str(obj)]
elif isinstance(obj, list) and len(obj) > 0:
choices = []
for c in obj:
if isinstance(c, str):
choices.append(c)
elif isinstance(c, dict): # we convert the dict to a string
d = ""
if "weight" in c.keys():
d += str(c["weight"])
if "if" in c.keys():
d += f" if {c['if']}"
if d != "":
d += "::"
if "text" in c.keys():
d += c["text"]
elif "content" in c.keys():
d += c["content"]
choices.append(d)
else:
obj = None
if obj is None:
self.logger.warning(f"Invalid wildcard '{fullkey}' in file '{full_path}'!")
else:
self.wildcards[fullkey] = {"file": full_path, "choices": choices}
self.__add_wildcard(content, full_path, external_key_parts)
def __get_wildcards_in_text_file(self, full_path, name, relfolders):
def __get_wildcards_in_text_file(self, full_path, base):
"""
Get all wildcards in a text file.
Args:
full_path (str): The path to the file.
base (str): The base path for the wildcards.
"""
external_key: str = os.path.relpath(os.path.splitext(full_path)[0], base)
external_key_parts = external_key.split(os.sep)
with open(full_path, "r", encoding="utf-8") as file:
text_content = map(lambda x: x.strip("\n\r"), file.readlines())
text_content = list(filter(lambda x: x.strip() != "" and not x.strip().startswith("#"), text_content))
text_content = [x.split("#")[0].rstrip() if len(x.split("#")) > 1 else x for x in text_content]
fullkey = f"{relfolders}{name}"
if self.wildcards.get(fullkey) is not None:
self.logger.warning(
f"Duplicate wildcard '{fullkey}' in file '{full_path}' and '{self.wildcards[fullkey]['file']}'!"
)
else:
if len(text_content) == 0:
self.logger.warning(f"Invalid wildcard in file '{full_path}'!")
else:
self.wildcards[fullkey] = {"file": full_path, "choices": text_content}
self.__add_wildcard(text_content, full_path, external_key_parts)
def __get_wildcards_in_directory(self, base: str, directory: str):
"""
@@ -197,7 +386,7 @@ class PPPWildcards:
directory (str): The path to the directory.
"""
if not os.path.exists(directory):
self.logger.warning(f"Wildcard directory '{directory}' does not exist!")
self.__logger.warning(f"Wildcard directory '{directory}' does not exist!")
return
for filename in os.listdir(directory):
full_path = os.path.abspath(os.path.join(directory, filename))
+15
View File
@@ -0,0 +1,15 @@
[project]
name = "sd-webui-prompt-postprocessor"
description = "Stable Diffusion WebUI & ComfyUI extension to post-process the prompt, including sending content from the prompt to the negative prompt and wildcards."
version = "2.8.1"
license = {file = "LICENSE.txt"}
dependencies = ["lark"]
[project.urls]
Repository = "https://github.com/acorderob/sd-webui-prompt-postprocessor"
# Used by Comfy Registry https://comfyregistry.org
[tool.comfy]
PublisherId = "acorderob"
DisplayName = "sd-webui-prompt-postprocessor"
Icon = ""
+2
View File
@@ -1 +1,3 @@
lark
numpy
pyyaml
+133 -61
View File
@@ -4,6 +4,7 @@ if __name__ == "__main__":
import sys
import os
import time
import numpy as np
sys.path.append(os.path.join(sys.path[0], ".."))
@@ -57,6 +58,7 @@ class PromptPostProcessorA1111Script(scripts.Script):
with open(grammar_filename, "r", encoding="utf-8") as file:
self.grammar_content = file.read()
self.wildcards_obj = PPPWildcards(lf.log)
self.ppp_logger.info(f"{PromptPostProcessor.NAME} {PromptPostProcessor.VERSION} initialized")
def title(self):
"""
@@ -79,7 +81,48 @@ class PromptPostProcessorA1111Script(scripts.Script):
"""
return scripts.AlwaysVisible
def process(self, p: StableDiffusionProcessing, *args, **kwargs): # pylint: disable=unused-argument
def ui(self, is_img2img):
with gr.Accordion(PromptPostProcessor.NAME, open=False):
force_equal_seeds = gr.Checkbox(
label="Force equal seeds",
info="Force all image seeds and variation seeds to be equal to the first one, disabling the default autoincrease.",
default=False,
# show_label=True,
elem_id="ppp_force_equal_seeds",
)
gr.HTML(
"""<br><div>Unlink the seed to use the specified one for the prompts instead of the image seed.
This seed will only change for each image in the batch if the value is -1 or 'variable seed' is checked.</div>
<div>Seeds are only used for the wildcards and choice constructs.</div>"""
)
unlink_seed = gr.Checkbox(
label="Unlink seed",
default=False,
# show_label=True,
elem_id="ppp_unlink_seed",
)
seed = gr.Number(
label="Seed",
default=-1,
precision=0,
# minimum=-1,
# maximum=2**32 - 1,
# step=1,
# show_label=True,
min_width=100,
elem_id="ppp_seed",
)
variable_seed = gr.Checkbox(
label="Variable seed",
default=False,
# show_label=True,
elem_id="ppp_variable_seed",
)
return [force_equal_seeds, unlink_seed, seed, variable_seed]
def process(
self, p: StableDiffusionProcessing, input_force_equal_seeds, input_unlink_seed, input_seed, input_variable_seed
): # pylint: disable=arguments-differ
"""
Processes the prompts and applies post-processing operations.
@@ -92,7 +135,8 @@ class PromptPostProcessorA1111Script(scripts.Script):
t1 = time.time()
if getattr(opts, "prompt_attention", "") == "Compel parser":
self.ppp_logger.warning("Compel parser is not supported!")
is_i2i = getattr(p, "init_images", [None])[0] is not None
init_images = getattr(p, "init_images", [None]) or [None]
is_i2i = bool(init_images[0])
self.ppp_debug_level = DEBUG_LEVEL(getattr(opts, "ppp_gen_debug_level", DEBUG_LEVEL.none.value))
do_i2i = getattr(opts, "ppp_gen_doi2i", False)
if is_i2i and not do_i2i:
@@ -101,44 +145,53 @@ class PromptPostProcessorA1111Script(scripts.Script):
return
if self.ppp_debug_level != DEBUG_LEVEL.none:
self.ppp_logger.info(f"Post-processing prompts ({'i2i' if is_i2i else 't2i'})")
model_info = {
app = (
"forge"
if hasattr(p.sd_model, "model_config")
else "sdnext" if hasattr(p.sd_model, "is_sdxl") and not hasattr(p.sd_model, "is_ssd") else "a1111"
)
env_info = {
"app": app,
"models_path": models_path,
"model_filename": getattr(p.sd_model.sd_checkpoint_info, "filename", ""), # path is absolute
"model_filename": getattr(p.sd_model.sd_checkpoint_info, "filename", ""),
"model_class": "",
"is_sd1": False, # Stable Diffusion 1
"is_sd2": False, # Stable Diffusion 2
"is_sdxl": False, # Stable Diffusion XL
"is_ssd": False, # Segmind Stable Diffusion 1B
"is_sd3": False, # Stable Diffusion 3
"is_flux": False, # Flux
"is_auraflow": False, # AuraFlow
}
app = (
"forge"
if hasattr(p.sd_model, "model_config")
else "sdnext" if hasattr(p.sd_model, "is_sdxl") and not hasattr(p.sd_model, "is_ssd") else "a1111"
)
if app == "sdnext":
# cannot differenciate SD1 and SD2, we set True to both
# cannot differentiate SD1 and SD2, we set True to both
# LatentDiffusion is for the original backend, StableDiffusionPipeline is for the diffusers backend
model_info["is_sd1"] = p.sd_model.__class__.__name__ in ("LatentDiffusion", "StableDiffusionPipeline")
model_info["is_sd2"] = p.sd_model.__class__.__name__ in ("LatentDiffusion", "StableDiffusionPipeline")
model_info["is_sdxl"] = p.sd_model.__class__.__name__ == "StableDiffusionXLPipeline"
model_info["is_ssd"] = False # ?
model_info["is_sd3"] = p.sd_model.__class__.__name__ == "StableDiffusion3Pipeline"
model_info["is_flux"] = False
env_info["model_class"] = p.sd_model.__class__.__name__
env_info["is_sd1"] = p.sd_model.__class__.__name__ in ("LatentDiffusion", "StableDiffusionPipeline")
env_info["is_sd2"] = p.sd_model.__class__.__name__ in ("LatentDiffusion", "StableDiffusionPipeline")
env_info["is_sdxl"] = p.sd_model.__class__.__name__ == "StableDiffusionXLPipeline"
env_info["is_ssd"] = False # ?
env_info["is_sd3"] = p.sd_model.__class__.__name__ == "StableDiffusion3Pipeline"
env_info["is_flux"] = p.sd_model.__class__.__name__ == "FluxPipeline"
env_info["is_auraflow"] = p.sd_model.__class__.__name__ == "AuraFlowPipeline"
elif app == "forge":
model_info["is_sd1"] = getattr(p.sd_model, "is_sd1", False)
model_info["is_sd2"] = getattr(p.sd_model, "is_sd2", False)
model_info["is_sdxl"] = getattr(p.sd_model, "is_sdxl", False)
model_info["is_ssd"] = False # ?
model_info["is_sd3"] = getattr(p.sd_model, "is_sd3", False)
model_info["is_flux"] = p.sd_model.model_config.__class__.__name__ == "Flux"
env_info["model_class"] = p.sd_model.model_config.__class__.__name__
env_info["is_sd1"] = getattr(p.sd_model, "is_sd1", False)
env_info["is_sd2"] = getattr(p.sd_model, "is_sd2", False)
env_info["is_sdxl"] = getattr(p.sd_model, "is_sdxl", False)
env_info["is_ssd"] = False # ?
env_info["is_sd3"] = getattr(p.sd_model, "is_sd3", False)
env_info["is_flux"] = p.sd_model.model_config.__class__.__name__ == "Flux"
env_info["is_auraflow"] = False # p.sd_model.model_config.__class__.__name__ == "AuraFlow"
else: # assume A1111 compatible (p.sd_model.__class__.__name__=="DiffusionEngine")
model_info["is_sd1"] = getattr(p.sd_model, "is_sd1", False)
model_info["is_sd2"] = getattr(p.sd_model, "is_sd2", False)
model_info["is_sdxl"] = getattr(p.sd_model, "is_sdxl", False)
model_info["is_ssd"] = getattr(p.sd_model, "is_ssd", False)
model_info["is_sd3"] = getattr(p.sd_model, "is_sd3", False)
model_info["is_flux"] = False
env_info["model_class"] = p.sd_model.__class__.__name__
env_info["is_sd1"] = getattr(p.sd_model, "is_sd1", False)
env_info["is_sd2"] = getattr(p.sd_model, "is_sd2", False)
env_info["is_sdxl"] = getattr(p.sd_model, "is_sdxl", False)
env_info["is_ssd"] = getattr(p.sd_model, "is_ssd", False)
env_info["is_sd3"] = getattr(p.sd_model, "is_sd3", False)
env_info["is_flux"] = False
env_info["is_auraflow"] = False
wc_wildcards_folders = getattr(opts, "ppp_wil_wildcardsfolders", "")
if wc_wildcards_folders == "":
wc_wildcards_folders = os.getenv("WILDCARD_DIR", PPPWildcards.DEFAULT_WILDCARDS_FOLDER)
@@ -150,13 +203,12 @@ class PromptPostProcessorA1111Script(scripts.Script):
options = {
"debug_level": getattr(opts, "ppp_gen_debug_level", DEBUG_LEVEL.none.value),
"pony_substrings": getattr(opts, "ppp_gen_ponysubstrings", PromptPostProcessor.DEFAULT_PONY_SUBSTRINGS),
"process_wildcards": getattr(opts, "ppp_wil_process_wildcards", True),
"process_wildcards": getattr(opts, "ppp_wil_processwildcards", True),
"if_wildcards": getattr(opts, "ppp_wil_ifwildcards", PromptPostProcessor.IFWILDCARDS_CHOICES.ignore.value),
"choice_separator": getattr(opts, "ppp_wil_choice_separator", PromptPostProcessor.DEFAULT_CHOICE_SEPARATOR),
"keep_choices_order": getattr(opts, "ppp_wil_keep_choices_order", False),
"stn_separator": getattr(opts, "ppp_stn_separator", PromptPostProcessor.DEFAULT_STN_SEPARATOR),
"stn_ignore_repeats": getattr(opts, "ppp_stn_ignorerepeats", True),
"stn_join_attention": getattr(opts, "ppp_stn_joinattention", True),
"cleanup_extra_spaces": getattr(opts, "ppp_cup_extraspaces", True),
"cleanup_empty_constructs": getattr(opts, "ppp_cup_emptyconstructs", True),
"cleanup_extra_separators": getattr(opts, "ppp_cup_extraseparators", True),
@@ -166,29 +218,49 @@ class PromptPostProcessorA1111Script(scripts.Script):
"cleanup_ands": getattr(opts, "ppp_cup_ands", True),
"cleanup_ands_eol": getattr(opts, "ppp_cup_ands_eol", False),
"cleanup_extranetwork_tags": getattr(opts, "ppp_cup_extranetworktags", False),
"cleanup_merge_attention": getattr(opts, "ppp_cup_mergeattention", True),
"remove_extranetwork_tags": getattr(opts, "ppp_rem_removeextranetworktags", False),
}
self.wildcards_obj.refresh_wildcards(
self.ppp_debug_level, wildcards_folders if options["process_wildcards"] else None
)
ppp = PromptPostProcessor(
self.ppp_logger, self.ppp_interrupt, model_info, options, self.grammar_content, self.wildcards_obj
self.ppp_logger, self.ppp_interrupt, env_info, options, self.grammar_content, self.wildcards_obj
)
prompts_list = []
seeds = getattr(p, "all_seeds", [])
subseeds = getattr(p, "all_subseeds", [])
subseed_strength = getattr(p, "subseed_strength", 0.0)
if subseed_strength > 0:
calculated_seeds = [
int(subseed * subseed_strength + seed * (1 - subseed_strength))
for seed, subseed in zip(seeds, subseeds)
]
if input_force_equal_seeds:
if self.ppp_debug_level != DEBUG_LEVEL.none:
self.ppp_logger.info("Forcing equal seeds")
seeds = getattr(p, "all_seeds", [])
subseeds = getattr(p, "all_subseeds", [])
p.all_seeds = [seeds[0] for _ in seeds]
p.all_subseeds = [subseeds[0] for _ in subseeds]
if input_unlink_seed:
if self.ppp_debug_level != DEBUG_LEVEL.none:
self.ppp_logger.info("Using unlinked seed")
num_seeds = len(getattr(p, "all_seeds", []))
if input_seed == -1:
calculated_seeds = np.random.randint(0, 2**32, size=num_seeds, dtype=np.int64)
elif input_variable_seed:
calculated_seeds = [input_seed + i for i in range(num_seeds)]
else:
calculated_seeds = [input_seed for _ in range(num_seeds)]
else:
calculated_seeds = seeds
if len(set(calculated_seeds)) < len(calculated_seeds):
self.ppp_logger.info("Adjusting seeds because some are equal.")
calculated_seeds = [seed + i for i, seed in enumerate(calculated_seeds)]
seeds = getattr(p, "all_seeds", [])
subseeds = getattr(p, "all_subseeds", [])
subseed_strength = getattr(p, "subseed_strength", 0.0)
if subseed_strength > 0:
calculated_seeds = [
int(subseed * subseed_strength + seed * (1 - subseed_strength))
for seed, subseed in zip(seeds, subseeds)
]
# if len(set(calculated_seeds)) < len(calculated_seeds):
# self.ppp_logger.info("Adjusting seeds because some are equal.")
# calculated_seeds = [seed + i for i, seed in enumerate(calculated_seeds)]
else:
calculated_seeds = seeds
# adds regular prompts
rpr = getattr(p, "all_prompts", None)
@@ -213,24 +285,24 @@ class PromptPostProcessorA1111Script(scripts.Script):
for i, (prompttype, seed, prompt, negative_prompt) in enumerate(prompts_list):
if self.ppp_debug_level != DEBUG_LEVEL.none:
self.ppp_logger.info(f"processing prompts[{i+1}] ({prompttype})")
if self.lru_cache.get((seed, prompt, negative_prompt)) is None:
pp, np = ppp.process_prompt(prompt, negative_prompt, seed)
self.lru_cache.put((seed, prompt, negative_prompt), (pp, np))
if self.lru_cache.get((seed, hash(self.wildcards_obj), prompt, negative_prompt)) is None:
posp, negp = ppp.process_prompt(prompt, negative_prompt, seed)
self.lru_cache.put((seed, hash(self.wildcards_obj), prompt, negative_prompt), (posp, negp))
# adds also the result so i2i doesn't process it unnecessarily
self.lru_cache.put((seed, pp, np), (pp, np))
self.lru_cache.put((seed, hash(self.wildcards_obj), posp, negp), (posp, negp))
elif self.ppp_debug_level != DEBUG_LEVEL.none:
self.ppp_logger.info("result already in cache")
# updates the prompts
if rpr is not None and rnr is not None:
for i, (seed, prompt, negative_prompt) in enumerate(zip(calculated_seeds, rpr, rnr)):
found = self.lru_cache.get((seed, prompt, negative_prompt))
found = self.lru_cache.get((seed, hash(self.wildcards_obj), prompt, negative_prompt))
if found is not None:
rpr[i] = found[0]
rnr[i] = found[1]
if rph is not None and rnh is not None:
for i, (seed, prompt, negative_prompt) in enumerate(zip(calculated_seeds, rph, rnh)):
found = self.lru_cache.get((seed, prompt, negative_prompt))
found = self.lru_cache.get((seed, hash(self.wildcards_obj), prompt, negative_prompt))
if found is not None:
rph[i] = found[0]
rnh[i] = found[1]
@@ -327,7 +399,7 @@ def on_ui_settings():
# wildcard settings
shared.opts.add_option(
key="ppp_wil_sep",
info=new_html_title('<br><h2>Wildcard settings</h2>'),
info=new_html_title("<br><h2>Wildcard settings</h2>"),
)
shared.opts.add_option(
key="ppp_wil_processwildcards",
@@ -386,7 +458,7 @@ def on_ui_settings():
# content removal settings
shared.opts.add_option(
key="ppp_rem_sep",
info=new_html_title('<br><h2>Content removal settings</h2>'),
info=new_html_title("<br><h2>Content removal settings</h2>"),
)
shared.opts.add_option(
key="ppp_rem_removeextranetworktags",
@@ -400,7 +472,7 @@ def on_ui_settings():
# send to negative settings
shared.opts.add_option(
key="ppp_stn_sep",
info=new_html_title('<br><h2>Send to Negative settings</h2>'),
info=new_html_title("<br><h2>Send to Negative settings</h2>"),
)
shared.opts.add_option(
key="ppp_stn_separator",
@@ -418,18 +490,10 @@ def on_ui_settings():
section=section,
),
)
shared.opts.add_option(
key="ppp_stn_joinattention",
info=shared.OptionInfo(
True,
label="Join attention modifiers (weights) when possible",
section=section,
),
)
# clean-up settings
shared.opts.add_option(
key="ppp_cup_sep",
info=new_html_title('<br><h2>Clean-up settings</h2>'),
info=new_html_title("<br><h2>Clean-up settings</h2>"),
)
shared.opts.add_option(
key="ppp_cup_emptyconstructs",
@@ -503,6 +567,14 @@ def on_ui_settings():
section=section,
),
)
shared.opts.add_option(
key="ppp_cup_mergeattention",
info=shared.OptionInfo(
True,
label="Merge attention modifiers (weights) when possible",
section=section,
),
)
# Remove old settings
# for name in ["ppp_gen_ifwildcards", "ppp_ifwildcards", "ppp_gen_debug", "ppp_stn_doi2i", "ppp_cup_doi2i"]:
+202 -28
View File
@@ -36,7 +36,6 @@ class TestPromptPostProcessor(unittest.TestCase):
"keep_choices_order": False,
"stn_separator": ", ",
"stn_ignore_repeats": True,
"stn_join_attention": True,
"cleanup_empty_constructs": True,
"cleanup_extra_separators": True,
"cleanup_extra_separators2": True,
@@ -46,15 +45,19 @@ class TestPromptPostProcessor(unittest.TestCase):
"cleanup_ands": True,
"cleanup_ands_eol": False,
"cleanup_extranetwork_tags": True,
"cleanup_merge_attention": True,
"remove_extranetwork_tags": False,
}
self.__def_model_info = {
self.__def_env_info = {
"app": "tests",
"is_sd1": False,
"is_sd2": False,
"is_sdxl": True,
"is_ssd": False,
"is_sd3": False,
"is_flux": False,
"is_auraflow": False,
"model_class": "DiffusionEngine",
"models_path": "./webui/models",
"model_filename": "./webui/models/Stable-diffusion/testmodel.safetensors",
}
@@ -73,7 +76,7 @@ class TestPromptPostProcessor(unittest.TestCase):
self.__defppp = PromptPostProcessor(
self.__ppp_logger,
self.__interrupt,
self.__def_model_info,
self.__def_env_info,
self.__defopts,
self.__grammar_content,
self.__wildcards_obj,
@@ -81,7 +84,7 @@ class TestPromptPostProcessor(unittest.TestCase):
self.__nocupppp = PromptPostProcessor(
self.__ppp_logger,
self.__interrupt,
self.__def_model_info,
self.__def_env_info,
{
**self.__defopts,
"cleanup_empty_constructs": False,
@@ -93,10 +96,23 @@ class TestPromptPostProcessor(unittest.TestCase):
"cleanup_ands": False,
"cleanup_ands_eol": False,
"cleanup_extranetwork_tags": False,
"cleanup_merge_attention": False,
},
self.__grammar_content,
self.__wildcards_obj,
)
self.__comfyuippp = PromptPostProcessor(
self.__ppp_logger,
self.__interrupt,
{
**self.__def_env_info,
"app": "comfyui",
"model_class": "SDXL",
},
self.__defopts,
self.__grammar_content,
self.__wildcards_obj,
)
def __interrupt(self):
self.__interrupted = True
@@ -167,7 +183,7 @@ class TestPromptPostProcessor(unittest.TestCase):
),
PromptPair(
" (()), flowers , , ",
"red, (pink:1.21), normal quality, mauve, yellow, bad quality, green, worse quality, purple, blue",
"red, ((pink)), normal quality, mauve, yellow, bad quality, green, worse quality, purple, blue",
),
ppp=self.__nocupppp,
)
@@ -179,7 +195,7 @@ class TestPromptPostProcessor(unittest.TestCase):
"normal quality",
),
PromptPair(
"this is a ((test) (test:2.0):1.5) (red:1.5)", "[neg1], ([square]:1.5), normal quality, (neg2:1.65)"
"this is a ((test) (test:2):1.5) (red:1.5)", "[neg1], ([square]:1.5), normal quality, (neg2:1.65)"
),
)
@@ -190,7 +206,7 @@ class TestPromptPostProcessor(unittest.TestCase):
"normal quality",
),
PromptPair(
"this is a (([complex|simple|regular] test)(test:2.0):1.5)",
"this is a (([complex|simple|regular] test)(test:2):1.5)",
"([neg1||]:1.65), ([|neg2|]:1.65), ([||neg3]:1.65), normal quality",
),
)
@@ -202,7 +218,7 @@ class TestPromptPostProcessor(unittest.TestCase):
"normal quality",
),
PromptPair(
"this is a (([complex[one|two||three|four]|simple|regular] test)(test:2.0):1.5)",
"this is a (([complex[one|two||three|four]|simple|regular] test)(test:2):1.5)",
"([neg1||]:1.65), ([[|neg12|||]||]:1.65), ([[||||(neg14)]||]:1.65), ([|neg2|]:1.65), ([||neg3]:1.65), normal quality",
),
)
@@ -220,7 +236,7 @@ class TestPromptPostProcessor(unittest.TestCase):
"normal quality, <ppp:stn i0>",
),
PromptPair(
"this \\(is\\): a (([complex|simple|regular] test)(test:2.0):1.5)\nBREAK with [abc:def:5]:0.5 AND loratrigger <lora:xxx:1> AND hypernettrigger <hypernet:yyy>:0.3",
"this \\(is\\): a (([complex|simple|regular] test)(test:2):1.5)\nBREAK with [abc:def:5]:0.5 AND loratrigger <lora:xxx:1> AND hypernettrigger <hypernet:yyy>:0.3",
"[neg5], ([|neg6|]:1.65), (neg1:1.65), [neg4::5], normal quality, [neg2(neg3:1.6):5]",
),
)
@@ -232,17 +248,30 @@ class TestPromptPostProcessor(unittest.TestCase):
"normal quality, <ppp:stn i0>",
),
PromptPair(
"this \\(is\\): a (([complex|simple|regular] test)(test:2.0):1.5)\nBREAK with [abc:def:5]:0.5 AND loratrigger <lora:xxx:1> AND hypernettrigger <hypernet:yyy>:0.3",
"this \\(is\\): a (([complex|simple|regular] test)(test:2):1.5)\nBREAK with [abc:def:5]:0.5 AND loratrigger <lora:xxx:1> AND hypernettrigger <hypernet:yyy>:0.3",
"[neg5], ([|neg6|]:1.65), (neg1:1.65), [neg4::5], normal quality, [neg2(neg3:1.6):5]",
),
)
def test_stn_inside_alternation_recursive_2(self): # negtag inside alternation (recursive alternation)
self.__process(
PromptPair(
"[pos1<ppp:stn>neg1<ppp:/stn>[pos11|pos12<ppp:stn>neg12<ppp:/stn>||pos14|pos15<ppp:stn>neg15<ppp:/stn>]|pos2<ppp:stn>neg2<ppp:/stn>|pos3<ppp:stn>neg3<ppp:/stn>]",
"",
),
PromptPair(
"[pos1[pos11|pos12||pos14|pos15]|pos2|pos3]",
"[neg1||], [[|neg12|||]||], [[||||neg15]||], [|neg2|], [||neg3]",
# "[neg1[|neg12|||neg15]|neg2|neg3]", # expected output if the constructs were unified
),
)
# Cleanup tests
def test_cl_simple(self): # simple cleanup
self.__process(
PromptPair(" this is a ((test ), , , (), , [] ( , test ,:2.0):1.5) (red:1.5) ", " normal quality "),
PromptPair("this is a ((test), (test,:2.0):1.5) (red:1.5)", "normal quality"),
PromptPair("this is a ((test), (test,:2):1.5) (red:1.5)", "normal quality"),
)
def test_cl_complex(self): # complex cleanup
@@ -252,32 +281,48 @@ class TestPromptPostProcessor(unittest.TestCase):
" [:hands, feet, :0.15]normal quality ",
),
PromptPair(
"this is BREAKABLE a ((test)) AND(<lora:test> ANDERSON (test:2.0):1.5) :o BREAK (red:1.5)",
"this is BREAKABLE a (test:1.21) AND(<lora:test> ANDERSON (test:2):1.5) :o BREAK (red:1.5)",
"[:hands, feet, :0.15]normal quality",
),
)
def test_cl_removenetworktags(self): # remove network tags
self.__process(
PromptPair("this is a <lora:test> test", ""),
PromptPair("this is a <lora:test:1> test__yaml/wildcard7__", ""),
PromptPair("this is a test", ""),
ppp=PromptPostProcessor(
self.__ppp_logger,
self.__interrupt,
self.__def_model_info,
self.__def_env_info,
{**self.__defopts, "remove_extranetwork_tags": True},
self.__grammar_content,
self.__wildcards_obj,
),
)
def test_cl_dontremoveseparatorsoneol(self): # dont remove separators on eol
def test_cl_dontremoveseparatorsoneol(self): # don't remove separators on eol
self.__process(
PromptPair("this is a test,\nsecond line", ""),
PromptPair("this is a test,\nsecond line", ""),
ppp=PromptPostProcessor(
self.__ppp_logger,
self.__interrupt,
self.__def_model_info,
self.__def_env_info,
{**self.__defopts, "cleanup_extra_separators2": False},
self.__grammar_content,
self.__wildcards_obj,
),
)
def test_cl_mergeattention(self): # merge attention
self.__process(
PromptPair(
"this is (a test:1.5) of (attention (merging:1.2)) where ((this)) ((is joined:1.2)) and ([this too]:1.3)",
"",
),
PromptPair(
"this is (a test:1.5) of (attention (merging:1.2)) where (this:1.21) (is joined:1.32) and (this too:1.17)",
"",
),
)
@@ -290,7 +335,7 @@ class TestPromptPostProcessor(unittest.TestCase):
"normal quality, <ppp:stn i0>",
),
PromptPair(
"this \\(is\\): a (([complex|simple|regular] test)(test:2.0):1.5)\nBREAK with [abc:def:5]:0.5 AND loratrigger <lora:xxx:1> AND hypernettrigger <hypernet:yyy>:0.3",
"this \\(is\\): a (([complex|simple|regular] test)(test:2):1.5)\nBREAK with [abc:def:5]:0.5 AND loratrigger <lora:xxx:1> AND hypernettrigger <hypernet:yyy>:0.3",
"[neg5], ([|neg6|]:1.65), (neg1:1.65), [neg4::5], normal quality, [neg2(neg3:1.6):5]",
),
)
@@ -302,7 +347,7 @@ class TestPromptPostProcessor(unittest.TestCase):
"normal quality",
),
PromptPair(
"this \\(is\\): a (([complex|simple|regular] test)(test:2.0):1.5)\nBREAK :0.5 AND hypernettrigger <hypernet:yyy>:0.3",
"this \\(is\\): a (([complex|simple|regular] test)(test:2):1.5)\nBREAK :0.5 AND hypernettrigger <hypernet:yyy>:0.3",
"normal quality",
),
)
@@ -317,10 +362,12 @@ class TestPromptPostProcessor(unittest.TestCase):
self.__ppp_logger,
self.__interrupt,
{
**self.__def_model_info,
**self.__def_env_info,
"model_filename": "./webui/models/Stable-diffusion/ponymodel.safetensors",
},
self.__defopts,
self.__grammar_content,
self.__wildcards_obj,
),
)
@@ -453,6 +500,27 @@ class TestPromptPostProcessor(unittest.TestCase):
ppp=self.__nocupppp,
)
def test_ch_choicesinsidelora(self): # simple choices inside a lora
self.__process(
PromptPair("<lora:test1:1><lora:test2:{0.2|0.5|0.7|1}>", ""),
PromptPair("<lora:test1:1><lora:test2:0.7>", ""),
ppp=self.__nocupppp,
)
def test_ch_removelorawithchoices(self): # remove lora with choices inside
self.__process(
PromptPair("<lora:test1:1><lora:test2:{0.2|0.5|0.7|1}>", ""),
PromptPair("", ""),
ppp=PromptPostProcessor(
self.__ppp_logger,
self.__interrupt,
self.__def_env_info,
{**self.__defopts, "remove_extranetwork_tags": True},
self.__grammar_content,
self.__wildcards_obj,
),
)
# Wildcards tests
def test_wc_ignore(self): # wildcards with ignore option
@@ -462,12 +530,14 @@ class TestPromptPostProcessor(unittest.TestCase):
ppp=PromptPostProcessor(
self.__ppp_logger,
self.__interrupt,
self.__def_model_info,
self.__def_env_info,
{
**self.__defopts,
"process_wildcards": False,
"if_wildcards": PromptPostProcessor.IFWILDCARDS_CHOICES.ignore.value,
},
self.__grammar_content,
self.__wildcards_obj,
),
)
@@ -478,18 +548,20 @@ class TestPromptPostProcessor(unittest.TestCase):
"normal quality, <ppp:stn i0> {option1|option2}",
),
PromptPair(
"this is: a (([complex|simple|regular] test)(test:2.0):1.5)\nBREAK with [abc:def:5]<lora:xxx:1>",
"this is: a (([complex|simple|regular] test)(test:2):1.5)\nBREAK with [abc:def:5]<lora:xxx:1>",
"[neg5], ([|neg6|]:1.65), (neg1:1.65), [neg4::5], normal quality, [neg2(neg3:1.6):5]",
),
ppp=PromptPostProcessor(
self.__ppp_logger,
self.__interrupt,
self.__def_model_info,
self.__def_env_info,
{
**self.__defopts,
"process_wildcards": False,
"if_wildcards": PromptPostProcessor.IFWILDCARDS_CHOICES.remove.value,
},
self.__grammar_content,
self.__wildcards_obj,
),
)
@@ -500,12 +572,14 @@ class TestPromptPostProcessor(unittest.TestCase):
ppp=PromptPostProcessor(
self.__ppp_logger,
self.__interrupt,
self.__def_model_info,
self.__def_env_info,
{
**self.__defopts,
"process_wildcards": False,
"if_wildcards": PromptPostProcessor.IFWILDCARDS_CHOICES.warn.value,
},
self.__grammar_content,
self.__wildcards_obj,
),
)
@@ -513,22 +587,42 @@ class TestPromptPostProcessor(unittest.TestCase):
self.__process(
PromptPair("__bad_wildcard__", "{option1|option2}"),
PromptPair(
PromptPostProcessor.WILDCARD_STOP + "__bad_wildcard__",
PromptPostProcessor.WILDCARD_STOP + "{option1|option2}",
PromptPostProcessor.WILDCARD_STOP.format("__bad_wildcard__") + "__bad_wildcard__",
"{option1|option2}",
),
ppp=PromptPostProcessor(
self.__ppp_logger,
self.__interrupt,
self.__def_model_info,
self.__def_env_info,
{
**self.__defopts,
"process_wildcards": False,
"if_wildcards": PromptPostProcessor.IFWILDCARDS_CHOICES.stop.value,
},
self.__grammar_content,
self.__wildcards_obj,
),
interrupted=True,
)
def test_wcinvar_warn(self): # wildcards in var with warn option
self.__process(
PromptPair("${v=__bad_wildcard__}${v}", ""),
PromptPair(PromptPostProcessor.WILDCARD_WARNING + "__bad_wildcard__", ""),
ppp=PromptPostProcessor(
self.__ppp_logger,
self.__interrupt,
self.__def_env_info,
{
**self.__defopts,
"process_wildcards": False,
"if_wildcards": PromptPostProcessor.IFWILDCARDS_CHOICES.warn.value,
},
self.__grammar_content,
self.__wildcards_obj,
),
)
def test_wc_wildcard1a_text(self): # simple text wildcard
self.__process(
PromptPair("the choices are: __text/wildcard1__", ""),
@@ -592,6 +686,62 @@ class TestPromptPostProcessor(unittest.TestCase):
ppp=self.__nocupppp,
)
def test_wc_wildcard_filter_index(self): # wildcard with positional index filter
self.__process(
PromptPair("the choice is: __yaml/wildcard2'2'__", ""),
PromptPair("the choice is: choice3-choice3", ""),
ppp=self.__nocupppp,
)
def test_wc_wildcard_filter_label(self): # wildcard with label filter
self.__process(
PromptPair("the choice is: __yaml/wildcard2'label1'__", ""),
PromptPair("the choice is: choice3-choice1", ""),
ppp=self.__nocupppp,
)
def test_wc_wildcard_filter_label2(self): # wildcard with label filter in multiple choices
self.__process(
PromptPair("the choice is: __yaml/wildcard2'label2'__", ""),
PromptPair("the choice is: choice1-choice1", ""),
ppp=self.__nocupppp,
)
def test_wc_wildcard_filter_label3(self): # wildcard with multiple label filter
self.__process(
PromptPair("the choice is: __yaml/wildcard2'label1,label2'__", ""),
PromptPair("the choice is: choice3-choice1", ""),
ppp=self.__nocupppp,
)
def test_wc_wildcard_filter_indexlabel(self): # wildcard with mixed index and label filter
self.__process(
PromptPair("the choice is: __yaml/wildcard2'2,label2'__", ""),
PromptPair("the choice is: choice3-choice1", ""),
ppp=self.__nocupppp,
)
def test_wc_wildcard_filter_compound(self): # wildcard with compound filter
self.__process(
PromptPair("the choice is: __yaml/wildcard2'label1+label3'__", ""),
PromptPair("the choice is: choice3-choice3", ""),
ppp=self.__nocupppp,
)
def test_wc_wildcard_filter_compound2(self): # wildcard with inherited compound filter
self.__process(
PromptPair("the choice is: __yaml/wildcard2bis'#label1+label3'__", ""),
PromptPair("the choice is: choice3bis", ""),
ppp=self.__nocupppp,
)
def test_wc_wildcard_filter_compound3(self): # wildcard with doubly inherited compound filter
self.__process(
PromptPair("the choice is: __yaml/wildcard2bisbis'#label1+label3'__", ""),
PromptPair("the choice is: choice3bisbis", ""),
ppp=self.__nocupppp,
)
def test_wc_nested_wildcard_text(self): # nested text wildcard with repeating multiple choices
self.__process(
PromptPair("the choices are: __r3$$-$$text/wildcard3__", ""),
@@ -648,8 +798,8 @@ class TestPromptPostProcessor(unittest.TestCase):
def test_wc_wildcard_globbing(self): # wildcard with globbing
self.__process(
PromptPair("the choices are: __yaml/wildcard[12]__, __yaml/wildcard*__", ""),
PromptPair("the choices are: choice3-choice2, choice3-choice1- choice2 ", ""),
PromptPair("the choices are: __yaml/wildcard[12]__, __yaml/wildcard?__", ""),
PromptPair("the choices are: choice3-choice2, <lora:test2:1>- choice2 -choice3", ""),
ppp=self.__nocupppp,
)
@@ -660,6 +810,30 @@ class TestPromptPostProcessor(unittest.TestCase):
ppp=self.__nocupppp,
)
def test_wc_wildcardPS_yaml(self): # yaml wildcard with object formatted choices and options and prefix and suffix
self.__process(
PromptPair("the choices are: __yaml/wildcardPS__", ""),
PromptPair("the choices are: prefix-choice2/choice3-suffix", ""),
ppp=self.__nocupppp,
)
def test_wc_anonymouswildcard_yaml(self): # yaml anonymous wildcard
self.__process(
PromptPair("the choices are: __yaml/anonwildcards__", ""),
PromptPair("the choices are: six", ""),
ppp=self.__nocupppp,
)
# ComfyUI tests
def test_comfyui_attention(self): # attention conversion
self.__process(
PromptPair("(test1) (test2:1.5) [test3] [(test4)]", ""),
PromptPair("(test1) (test2:1.5) (test3:0.9) (test4:0.99)", ""),
ppp=self.__comfyuippp,
)
# def test_mix(self):
# self.__process(
# PromptPair(
+47 -3
View File
@@ -6,11 +6,17 @@ yaml:
wildcard2:
- ~r2-3$$-
- 4::choice1
- "'label1,label2'4::choice1"
- "3:: choice2 "
- 2::choice3
- { labels: ["label1", "label3"], weight: 2, content: choice3 }
- 5 if _is_sd1::choice4
wildcard2bis:
- __1$$yaml/wildcard2'^yaml/wildcard2bis'__bis
wildcard2bisbis:
- __1$$yaml/wildcard2bis'#^yaml/wildcard2bisbis'__bis
wildcard3:
- __2$$,$$yaml/wildcard2__
@@ -22,4 +28,42 @@ yaml:
- { weight: 2, text: choice1 }
- { weight: 3, content: choice2 }
- { text: choice3 }
- { weight: 4, if: "_is_ssd", text: choice4 }
- { weight: 4, if: _is_ssd, text: choice4 }
wildcard7:
- <lora:test1:1>
- <lora:test2:1>
- <lora:test3:1>
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