Compare commits

...
65 Commits
Author SHA1 Message Date
Antonio Cordero Balcazar 83e88e41e9 * Fix in container processing. 2026-05-07 21:50:28 +02:00
Antonio Cordero BalcazarandCopilot db68347bcd * Support for attention merge at the wildcard/choice boundary.
Co-authored-by: Copilot <copilot@github.com>
2026-05-05 16:29:21 +02:00
Antonio Cordero Balcazar b03a522ed4 * Added container property to wildcard definitions. 2026-05-05 16:29:21 +02:00
Antonio Cordero Balcazar fcf577ea50 * Wildcard and ENmapping folder list support specific files. 2026-05-05 16:29:21 +02:00
Antonio Cordero Balcazar c43b601ee0 * Improved test logging. 2026-05-05 16:29:21 +02:00
Antonio Cordero Balcazar ccafbb7e0a * Options as system variables
* Additional tests
2026-05-05 16:29:21 +02:00
Antonio Cordero Balcazar a8914c5f29 * Update version number. 2026-05-05 16:29:03 +02:00
Antonio Cordero Balcazar b4ddd74414 * Fixed AND in wildcard filters. #34
* Added support for specifying a variable with the whole wildcard filter.
2026-05-05 09:23:33 +02:00
Antonio Cordero Balcazar f1a4e2fc62 * Better logging.
* Refactored tests.
2026-05-03 17:06:57 +02:00
Antonio Cordero Balcazar 73996b1609 * Doc fix. 2026-05-03 00:13:59 +02:00
Antonio Cordero BalcazarandCopilot 0970afb018 * Support for the cyclical sampler ("@").
* Support for attention merging wheen sending to negative prompt.

Co-authored-by: Copilot <copilot@github.com>
2026-05-02 17:18:44 +02:00
Antonio Cordero BalcazarandCopilot 8e38265b04 * Improved documentation.
Co-authored-by: Copilot <copilot@github.com>
2026-05-02 14:31:56 +02:00
Antonio Cordero BalcazarandCopilot 04119b169e * Improved combinatorial limit warning.
* Added combinatorial shuffle.
* A1111: Improved combinatorial with hiresfix.
* Some refactoring.
* Update of AI test instructions.

Co-authored-by: Copilot <copilot@github.com>
2026-05-02 13:40:06 +02:00
Antonio Cordero Balcazar 37bf9f38d5 * Fix in commandext.
* Improved tests.
2026-05-02 09:25:33 +02:00
Antonio Cordero BalcazarandCopilot a31070b986 * Added combinatorial generation.
* Updated pylint settings.
* Fixed a bug with wildcard or extranetwork mappings folders in a different drive than the extension.

Co-authored-by: Copilot <copilot@github.com>
2026-05-01 23:02:57 +02:00
Antonio Cordero Balcazar ad5cad30ff * Process positive and negative prompts together. 2026-04-30 21:09:39 +02:00
Antonio Cordero BalcazarandCopilot b2a5c38f30 * Unify variables in their own object.
Co-authored-by: Copilot <copilot@github.com>
2026-04-30 11:32:05 +02:00
Antonio Cordero Balcazar ea93669a82 * Support comparisons between floats and ints. 2026-04-30 10:18:14 +02:00
Antonio Cordero BalcazarandCopilot 9de5d69272 * Change format for separator specifier in array vars.
Co-authored-by: Copilot <copilot@github.com>
2026-04-29 16:59:40 +02:00
Antonio Cordero BalcazarandCopilot 563c071a65 * Support for reading array variable length.
* Support for additional operators: any_in and contains_any.
* Support for starred initialization in the set command.
* New option to set strict operations for some operators.
* Fixes detecting integers and added float support.
* Fixed echoing system variables.
* A1111: fixed prompt processing when there are more than 500 prompts.

Co-authored-by: Copilot <copilot@github.com>
2026-04-29 11:55:07 +02:00
Antonio Cordero BalcazarandCopilot aa4ea000b5 * Use pydantic for the configuration checks.
* Improved variable tests and documentation.

Co-authored-by: Copilot <copilot@github.com>
2026-04-27 13:31:53 +02:00
Antonio Cordero Balcazar c4582b30cd * Merged tests for variables and commands in the same file.
* Added support for array variables.
* Support for exception logging.
2026-04-26 17:32:15 +02:00
Antonio Cordero Balcazar 1fc60e55d8 * Added formatted parameter to log function for conditional message formatting.
* Added a new test for comparing two variables with the `lt` command.
2026-04-13 23:21:08 +02:00
Antonio Cordero Balcazar fdd2f5ba51 * Added result checks for special character sequences and unmatched parentheses/brackets. 2026-04-09 16:47:55 +02:00
Antonio Cordero Balcazar 62939f4416 * Refactor logging.
* Update option names in PromptPostProcessor.
2026-04-09 10:39:04 +02:00
Antonio Cordero Balcazar 932bce32f5 * Massive refactoring to decouple classes and improve configuration and state handling.
* Wildcard descriptions used in concat node.
2026-04-07 22:55:59 +02:00
Antonio Cordero Balcazar b1a01cb71c * Added description to wildcard definition options.
* Refactoring of grammar and token reading.
2026-04-07 11:53:02 +02:00
Antonio Cordero Balcazar 6e732e6e65 * Updated reforge detection classes for multiple models in ppp_config.yaml.defaults 2026-04-06 13:59:38 +02:00
Antonio Cordero Balcazar 12d5d40440 * Added Wildcard Concat node for selecting and concatenating wildcards.
* Refactor functions to resolve wildcard and extra-network mapping folder paths.
2026-04-06 13:32:35 +02:00
Antonio Cordero Balcazar b460565605 * Support for ranges in wildcard positional filters
* Some Copilot instructions.
2026-04-06 10:44:25 +02:00
Antonio Cordero Balcazar 4176559166 * Improved preprocessing of grammar file.
* Added cookbook.
* Support for variables in wildcard filters.
* Fix support for empty echoed variables.
2026-04-05 21:36:51 +02:00
Antonio Cordero Balcazar 47375db1ba * Escape quotes on log messages
* Separated tests in multiple files.
* Added pydantic validation for configuration.
* Improved backup/restore on visit of nodes.
* Echoing of variables with default no longer visits the default when not used (and no longer creates variables that were not actually used).
2026-04-04 19:04:46 +02:00
Antonio Cordero Balcazar 6ceb23b84b * Moved known models and their detection to the configuration file.
* Updated `ppp_config.yaml.defaults` to enhance documentation and structure for model detection and configuration.
* Improved validation of configuration files.
* Added a new `tests` application in `SUPPORTED_APPS` for testing purposes.
* Support for variables in list of values comparisons.
2026-04-03 15:53:10 +02:00
Antonio Cordero Balcazar acdf337354 * Added minimum value constraint for seed input in PromptPostProcessorComfyUINode. 2026-03-28 16:39:29 +01:00
Antonio Cordero Balcazar 78631d4adc * Support for more base models.
* Enhanced the processing of "BREAK" and "AND" constructs to support replacing with a comma. Set both to comma as default in ComfyUI.
* Added a new command `setwcdeffilter` to set default filters for a wildcard.
* Fixed bug with echoed variables.
* ComfyUI: Individual help documentation for the nodes.
* Can now test for output variables.
* Improved documentation.
2026-03-28 16:39:29 +01:00
Antonio Cordero Balcazar dc8b4b6631 * Added option to cleanup output variables. 2026-03-28 16:39:29 +01:00
Antonio Cordero Balcazar 87d12ca53f * Added new configuration options in ppp_config.yaml.defaults (and ppp_config.yaml) for host applications and models.
* Implemented handling for attention, scheduling, alternation, AND, and BREAK operators in the new configuration file.
* Moved variant definitions to the new configuration file.
* Improved tests speed by creating the needed PPP object in the process method.
2026-03-28 16:38:53 +01:00
Antonio Cordero Balcazar 39d6008f6f * Update version to 3.0.0 because of breaking changes for ComfyUI nodes.
* Refactor PromptPostProcessor class to use the same default values for options
* Add new nodes for the different option groups
2026-03-28 16:34:12 +01:00
Antonio Cordero Balcazar 1b781cb8b8 Allow variables in RHS of if expressions (#28)
# Pull Request

## Description

Allows variables in the RHS of expressions (e.g. `<ppp:if tagsSeen
contains tag>`) for more complex logic

## Type of change

Please delete options that are not relevant.

- [x] New feature (non-breaking change which adds functionality)
- [x] This change requires a documentation update

## How Has This Been Tested?

Added additional tests to the existing test suite.

## Checklist

- [x] My code follows the style guidelines of this project
- [x] I have performed a self-review of my own code
- [x] I have commented my code, particularly in hard-to-understand areas
- [x] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [x] Any dependent changes have been merged and published in downstream
modules
- [x] I have checked my code and corrected any misspellings
2026-03-28 16:24:42 +01:00
Antonio Cordero Balcazar 9cc10cd3f6 Merge branch 'main' into feature/var-vs-var-comparison 2026-03-28 16:09:08 +01:00
Antonio Cordero Balcazar 66422490eb support warn mode when var value cannot be converted to integer (#29)
# Pull Request

## Description

When a variable cannot be converted to integer (because it is undefined, or it is a string that cannot convert) then either warn or fail with proper error message (instead of current behavior which just sort of crashes with no useful information).

`<ppp:if undefinedVar gt 1>` or `{var=hello}<ppp:if var gt 1>` will no longer crash but will warn or stop.


## Type of change

Please delete options that are not relevant.

- [x] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [x] This change requires a documentation update

## How Has This Been Tested?

Additional unit tests added to the test suite.

## Checklist

- [x] My code follows the style guidelines of this project
- [x] I have performed a self-review of my own code
- [x] I have commented my code, particularly in hard-to-understand areas
- [x] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my feature works
- [x] New and existing unit tests pass locally with my changes
- [x] Any dependent changes have been merged and published in downstream modules
- [x] I have checked my code and corrected any misspellings
2026-03-28 15:51:29 +01:00
Brandon Wallace 1fdf71c76e fix: add warn_or_stop for undefined variables in __resolve_cond_value
Align RHS variable resolution with LHS behavior in __eval_basiccondition
so undefined system/user variables honor the on_warning setting.
2026-03-19 23:59:41 -05:00
Brandon Wallace f444ce0233 feat(ppp): add variable-vs-variable comparison support in conditions
Allow bare identifiers as the RHS of comparisons in ppp:if conditions
(e.g., `<ppp:if var1 contains var2>`). The grammar now accepts IDENTIFIER
as an alternative to SIMPLEVALUE, and __eval_basiccondition resolves
bare identifier values as variable references before comparison.
2026-03-19 23:41:06 -05:00
Brandon Wallace d7216d5f93 test: add failing tests for PPP variable-vs-variable comparison support
Add 4 TDD tests for the var-vs-var comparison feature (S1-TT2):
- test_cmd_if_var_vs_var_eq
- test_cmd_if_var_vs_var_ne
- test_cmd_if_var_vs_var_contains
- test_cmd_if_var_vs_var_not_contains

All tests fail because the grammar only accepts SIMPLEVALUE on RHS,
not IDENTIFIER. Implementation in S1-T2 will make them pass.
2026-03-19 23:41:06 -05:00
Brandon Wallace cad7a52ed0 fix: handle NaN/undefined variable in integer comparisons
Wrap int(var_value) in __eval_basiccondition() with try/except to
prevent ValueError crash when a variable is undefined, empty, or
contains non-numeric text. In warn mode the comparison evaluates to
false; in stop mode a PPPInterrupt is raised with a descriptive message.
2026-03-19 01:28:24 -05:00
Brandon Wallace 3409909b05 test: add failing tests for NaN/undefined variable integer comparison bug
Add 5 tests covering the ValueError crash in __eval_basiccondition()
when int() is called on empty/non-numeric variable values during
integer comparisons. Tests verify expected behavior for warn mode
(comparison evaluates to false) and stop mode (proper PPPInterrupt).
2026-03-19 01:07:13 -05:00
Antonio Cordero Balcazar fcad5e195c * Fix loading widget values in recent ComfyUI versions. 2026-03-15 22:19:47 +01:00
Antonio Cordero Balcazar 84d31ef072 * 🤦 2025-12-09 20:12:47 +01:00
Antonio Cordero Balcazar e83c836200 * Wildcard identifiers cannot start with underscore.
* Fix cleaning constructs with only whitespace.
* Logging supports saving to file (used optionally in tests).
* Fix en refresh wildcards.
2025-12-08 20:04:23 +01:00
Antonio Cordero Balcazar 4bd8a4d71a * implemented proper support for variable echoing inside wildcard names. 2025-12-08 13:11:43 +01:00
Antonio Cordero Balcazar b2e30a6b02 * Commands without content can be ended with "/>" instead of a full "<ppp:/command>". This applies to "stn i", "echo" and "ext" commands.
* Fix and better logging on evaluating final user variables.
* Variables can be used inside a wildcard identifier, as in __wildcard${x:1}__.
2025-12-08 11:53:13 +01:00
Antonio Cordero Balcazar 22c5c77904 * Choice commands: include wildcard choices allowing composability in wildcards.
* Detection of circular references in wildcards.
* Cache for extranetwork mappings.
2025-10-05 16:08:27 +02:00
Antonio Cordero Balcazar db43d409f8 * removed unnecessary processing of variables.
* error control when reading wildcard or mapping files, and second chance for files with incorrect encoding.
* fix processing inside extranetwork tag.
2025-09-27 13:22:19 +02:00
Antonio Cordero Balcazar 8769c21ee5 * new "optional" parameter for choices/wildcard.
* added "$$" to end of parameters as first choice in a wildcard, to avoid confusion with a choice that could be considered parameters.
* additional fixes in the parser.
2025-09-06 18:14:00 +02:00
Antonio Cordero Balcazar 54a20117ce * New extranetworks command to group the triggers with the extranetwork.
* Mapping of extranetworks through the new command. This allows you to use a "virtual" extranetwork that is converted to a real one depending on conditions, like the model kind/variant.
* Fixed a parsing bug with the if command.
* Some refactoring and additional checks.
2025-09-01 16:50:46 +02:00
Antonio Cordero Balcazar fe61e54f41 * Selection of prompt parser based on heuristics to improve parsing time on simpler cases.
* Improved timing logging.
* Refactor application support handling.
* Enhanced regex patterns in `ppp.py` for wildcard and choice detection to ensure proper escaping.
* Included icon source files.
2025-08-08 14:05:34 +02:00
Antonio Cordero Balcazar c03339a618 * Better processing of interruptions using an exception.
* New option on_warning to stop on invalid content.
* New option cleanup_extra_separators_include_eol to choose whether to also remove adjacent EOLs when removing extra separators or not. Default is not to do it. In previous versions it was forced.
* Refactoring of the cleanup method.
* Only simplify attention modifiers when the merge attention option is set.
* A1111: avoid logging about HR prompts and avoid saving them in extra metadata when not different.
* A1111: consider options and environment info in the caching of results.
* Some refactorings.
2025-05-31 12:08:57 +02:00
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
61 changed files with 13987 additions and 3426 deletions
@@ -0,0 +1,37 @@
---
description: "Use when writing, editing, or reviewing Python code. Enforces Python 3.10 compatibility — avoid syntax and stdlib features introduced in 3.11 or later."
applyTo: "**/*.py"
---
# Python 3.10 Compatibility
All Python code must be compatible with Python 3.10. Do not use language features or standard-library additions introduced in 3.11 or later.
## Forbidden (3.11+)
| Avoid | Use instead |
|-------|-------------|
| `tomllib` (stdlib) | `tomli` (third-party) or conditional `try/except ImportError` |
| `typing.Self` | `TypeVar("Self", bound="ClassName")` or `from __future__ import annotations` |
| `typing.Never` | `typing.NoReturn` |
| `typing.TypeVarTuple`, `typing.Unpack` | `typing_extensions` backports |
| `StrEnum` | `class MyEnum(str, enum.Enum)` |
| `except*` / `ExceptionGroup` | Not available; raise/catch normally |
| `asyncio.TaskGroup`, `asyncio.timeout()` | `asyncio.gather()` / `asyncio.wait_for()` |
## Forbidden (3.12+)
| Avoid | Use instead |
|-------|-------------|
| `type X = ...` (type alias statement) | `X: TypeAlias = ...` with `typing.TypeAlias` |
| `def f[T](...)` / `class C[T]` generic syntax | `TypeVar` + `Generic[T]` |
| `@typing.override` | Omit or use comment |
| `itertools.batched()` | Manual chunking or `more-itertools` |
## Safe to use (available in 3.10)
- `match`/`case` structural pattern matching
- `X | Y` union type syntax in annotations (e.g., `int | None`)
- `typing.TypeAlias`, `typing.ParamSpec`
- `list[int]`, `dict[str, int]` — built-in generic aliases
- `zip(..., strict=True)`
- `str.removeprefix()` / `str.removesuffix()`
+129
View File
@@ -0,0 +1,129 @@
---
description: "Use when writing, editing, or reviewing test files. Enforces unittest patterns used in this project: TestCase inheritance, base class setup, process() helper, and naming conventions."
applyTo: "tests/**/*.py"
---
# Test Conventions
All tests use the standard `unittest` module. Do **not** use pytest-specific features (fixtures, `@pytest.mark`, `conftest.py`, etc.).
## File & Class Naming
- One test file per concern, named `tests_{category}.py`
- One test class per file inheriting from `TestPromptPostProcessorBase`:
```python
import unittest
from tests.base_tests import TestPromptPostProcessorBase
class TestMyFeature(TestPromptPostProcessorBase):
def setUp(self):
super().setUp(enable_file_logging=False)
```
## Test Method Naming
```
test_{2-3 letter category prefix}_{short_description}
```
Examples: `test_cl_simple`, `test_ch_choices`, `test_wc_ignore`
## Running a Test Case via `self.process()`
Use the `process()` helper from the base class — never instantiate `PromptPostProcessor` directly in test methods unless there is a need to pass specific options not covered by the default setup or the "nocup" or "nostrict" options.
```python
def test_cl_simple(self):
"""simple cleanup"""
self.process(
InputTuple(
"input prompt", # positive prompt
""), # negative prompt
OutputTuple(
"expected output", # expected positive prompt
"", # expected negative prompt
{} # expected variables (optional)
),
)
def test_cl_combinatorial(self):
"""simple cleanup"""
self.process(
InputTuple(
"input prompt", # positive prompt
""), # negative prompt
[
OutputTuple(
"expected output", # expected positive prompt
"", # expected negative prompt
{} # expected variables (optional)
),
OutputTuple(
"expected output", # expected positive prompt
"", # expected negative prompt
{} # expected variables (optional)
),
],
combinatorial=True,
)
```
### `process()` Signature (key parameters)
| Parameter | Type | Notes |
|-----------|------|-------|
| `input_prompts` | `InputTuple` | Prompts input |
| `expected_output` | `OutputTuple \| list[OutputTuple]` | Single or multiple valid outputs |
| `seed` | `int` | Optional, defaults to fixed seed |
| `ppp` | `PromptPostProcessor \| str \| None` | Supported values `"nocup"`, `"nostrict"` or a specific instance |
| `interrupted` | `bool` | Expected interrupt flag |
| `combinatorial` | `bool` | Whether to run a combinatorial generation. If a specific ppp instance is used then it is ignored |
## Assertions
Use `assertEqual` or similar methods with a descriptive message string:
```python
self.assertEqual(result, expected, "Descriptive failure message")
self.assertIn(key, container, f"Key '{key}' not found in output")
```
Do not use bare `assert` statements.
## Default Options & Environment
Override `self.defopts` or `self.def_env_info` to pass non-default options — do not hardcode option dicts from scratch:
```python
def test_cl_custom(self):
"""cleanup with custom separator"""
self.process(
InputTuple("a, , b", ""),
OutputTuple("a | b", ""),
ppp=PromptPostProcessor(
self.ppp_logger,
self.def_env_info,
replace(
self.defopts,
keep_choices_order=True,
cup_do_cleanup=False,
do_combinatorial=True,
),
self.grammar_content,
self.interrupt,
self.wildcards_obj,
self.extranetwork_maps_obj,
),
)
```
## Entry Point
Every test file must start with:
```python
if __name__ == "__main__":
raise SystemExit("This script must not be run directly")
```
+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 }}
+7
View File
@@ -1,5 +1,12 @@
**/__pycache__ **/__pycache__
.venv
venv
.vscode/**/* .vscode/**/*
!.vscode/settings.json !.vscode/settings.json
!.vscode/launch.json !.vscode/launch.json
logs
tests/tests_local.py
tests/local_wildcards
tests/logs
+5 -1
View File
@@ -1,5 +1,9 @@
{ {
"default": true, "default": true,
"MD013": false, "MD013": false,
"MD024": false "MD024": false,
"MD033": false,
"MD060": {
"style": "any"
}
} }
-12
View File
@@ -59,16 +59,6 @@ single-line-class-stmt=no
single-line-if-stmt=no single-line-if-stmt=no
[IMPORTS] [IMPORTS]
allow-any-import-level=
allow-reexport-from-package=no
allow-wildcard-with-all=no
deprecated-modules=
ext-import-graph=
import-graph=
int-import-graph=
known-standard-library=
known-third-party=enchant
preferred-modules=
[LOGGING] [LOGGING]
logging-format-style=new logging-format-style=new
@@ -94,9 +84,7 @@ disable=raw-checker-failed,
missing-module-docstring, missing-module-docstring,
missing-class-docstring, missing-class-docstring,
logging-fstring-interpolation, logging-fstring-interpolation,
import-outside-toplevel,
consider-iterating-dictionary, consider-iterating-dictionary,
wrong-import-position,
unnecessary-lambda, unnecessary-lambda,
consider-using-dict-items, consider-using-dict-items,
dangerous-default-value, dangerous-default-value,
+1
View File
@@ -0,0 +1 @@
3.10.11
+10 -3
View File
@@ -5,12 +5,19 @@
"version": "0.2.0", "version": "0.2.0",
"configurations": [ "configurations": [
{ {
"name": "Tests", "name": "Python Debugger: Attach using Process Id",
"type": "debugpy",
"request": "attach",
"processId": "${command:pickProcess}"
},
{
"name": "Python: Debug Tests (Testing Panel)",
"type": "debugpy", "type": "debugpy",
"request": "launch", "request": "launch",
"program": "tests/tests.py", "purpose": ["debug-test"],
"console": "integratedTerminal", "console": "integratedTerminal",
"justMyCode": true "justMyCode": false,
"cwd": "${workspaceFolder}"
} }
] ]
} }
+8 -1
View File
@@ -3,7 +3,7 @@
"python.testing.unittestArgs": [ "python.testing.unittestArgs": [
"-v", "-v",
"-s", "-s",
"./tests", ".",
"-p", "-p",
"test*.py" "test*.py"
], ],
@@ -12,5 +12,12 @@
"python.analysis.typeCheckingMode": "off", "python.analysis.typeCheckingMode": "off",
"black-formatter.args": [ "black-formatter.args": [
"--line-length=120" "--line-length=120"
],
"python-envs.pythonProjects": [
{
"path": ".",
"envManager": "ms-python.python:venv",
"packageManager": "ms-python.python:pip"
}
] ]
} }
+43 -379
View File
@@ -1,420 +1,84 @@
# Prompt PostProcessor for Stable Diffusion WebUI and ComfyUI # Prompt PostProcessor for Stable Diffusion WebUI and ComfyUI
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 (or PPP, formerly known as "sd-webui-sendtonegative"), is an extension designed to process the prompt in several ways. This extension is compatible with:
* [AUTOMATIC1111 Stable Diffusion WebUI](https://github.com/AUTOMATIC1111/stable-diffusion-webui) * [ComfyUI](https://github.com/comfyanonymous/ComfyUI)
* [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). * [AUTOMATIC1111 Stable Diffusion WebUI](https://github.com/AUTOMATIC1111/stable-diffusion-webui)
* ...and probably other forks * ...and probably other forks
* [ComfyUI](https://github.com/comfyanonymous/ComfyUI)
Currently this extension has these functions: These are some features:
* Sending parts of the prompt to the negative prompt. This allows for useful tricks when using wildcards since you can add negative content from choices made in the positive prompt. * Process wildcards. Compatible with the Dynamic Prompts extension.
* Sending parts of the prompt to the negative prompt (the original function of the extension). This allows for useful tricks when using wildcards since you can add negative content from choices made in the positive prompt.
* Set and modify local variables. * Set and modify local variables.
* Filter content based on the loaded SD model or a variable. * Define model variants (pony, illustrious, ...).
* Process wildcards. Compatible with Dynamic Prompts formats. Can also detect invalid wildcards and act as you choose. * Filter content based on the loaded SD model/variant or a variable.
* Clean up the prompt and negative prompt. * Map extranetworks (LoRAs) depending on conditions (like the loaded model variant). This allows you to add "virtual" LoRAs to the prompt that will be translated to the correct one.
* Clean up the prompt of unnecessary separators or spaces.
* Combinatorial mode.
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" option is enabled, the wildcards will be processed by PPP and those extensions will not get them. If you disable processing the wildcards, and you 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)` * **Extra networks**: `<kind:model...>`
* **Alternation**: `\[prompt1|prompt2|...\]` * **Attention**: `[prompt] (prompt) (prompt:weight)`
* **Scheduling**: `\[prompt1:prompt2:step\]` * **Alternation**: `[prompt1|prompt2|...]`
* **Extra networks**: `\<kind:model...\>` * **Scheduling**: `[prompt1:prompt2:step]`
* **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. *ComfyUI* only supports natively the attention using parentheses, so the ones with the brackets will be converted. The other constructs are not natively supported but some custom nodes implement them. For example:
[ComfyUI_smZNodes](https://github.com/shiimizu/ComfyUI_smZNodes)
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. I also recommend a node to add LoRAs from the prompt, like:
[LoRA Tag Loader for ComfyUI](https://github.com/badjeff/comfyui_lora_tag_loader)
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. 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 almost everything that [Dynamic Prompts](https://github.com/adieyal/sd-dynamic-prompts) supports).
Does not recognize tokenizer separators like `TE2:` and `TE3:`, so sending to negative prompt from those sections of the prompt will not add them in the corresponding section of the negative prompt.
2. It recognizes wildcards in the `__wildcard__` and {choice|choice} formats (and almost everything that [Dynamic Prompts](https://github.com/adieyal/sd-dynamic-prompts) supports).
3. It does not create *AND/BREAK* constructs when moving content to the negative prompt. 3. It does not create *AND/BREAK* constructs when moving content to the negative prompt.
## Installation ## Installation
On A1111 compatible webuis: On *ComfyUI*:
1. Go to Manager > Custom Nodes 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. Restart
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 SD.Next I recommend you disable the native wildcard processing. On *SD.Next* I recommend you to disable the native wildcard processing and use the old UI.
On ComfyUI:
1. Go to Manager > Custom Nodes Manager
2. Install through ComfyUI Manager
3. Click Install via Git URL and enter <https://github.com/acorderob/sd-webui-prompt-postprocessor>
4. Restart
## Usage
### Commands
The extension uses a format for its commands similar to an extranetwork, but it has a "ppp:" prefix followed by the command, and then a space and any parameters (if any).
```text
<ppp:command parameters>
```
When a command is associated with any content, it will be between an opening and a closing command:
```text
<ppp:command parameters>content<ppp:/command>
```
For wildcards and choices it uses the formats from the Dynamic Prompts extension, but sometimes with some additional options for more functionality.
### Choices
The generic format is:
```text
{parameters$$opt1::choice1|opt2::choice2|opt3::choice3}
```
Both the construct parameters (up to the '$$') and the individual choice options (up to the '::') are optional.
There is also a format where instead of "parameters$$" you just put the sampler, for compatibility with Dynamic Prompts.
The construct parameters can be written with the following options (all are optional):
* "**~**" or "**@**": sampler (for compatibility with Dynamic Prompts), but only "**~**" (random) is allowed.
* "**r**": means it allows repetition of the choices.
* "**n**" or "**n-m**" or "**n-**" or "**-m**": number or range of choices to select. Allows zero as the start of a range. Default is 1.
* "**$$sep**": separator when multiple choices are selected. Default is set in settings.
* "**$$**": end of the parameters.
The choice options are as follows:
* "**'identifiers'**": comma separated labels for the choice (optional, quotes can be single or double). Only makes sense inside a wildcard definition. Can be used when specifying the wildcard to select this specific choice. It's case insensitive.
* "**n**": weight of the choice (optional, default 1).
* "**if condition**": filters out the choice if the condition is false (optional; this is an extension to the Dynamic Prompts syntax). Same conditions as in the `if` command.
* "**::**": end of choice options
Whitespace is allowed between parameters.
These are examples of formats you can use to insert a choice construct:
```text
{opt1|5::opt2|3::opt3} # select 1 choice, two have weights
{3$$opt1|5 if _is_sd1::opt2|opt3} # select 3 choices, one has a weight and a condition
{2-3$$opt1|opt2|opt3} # select 2 to 3 choices
{r2-3$$opt1|opt2|opt3} # select 2 to 3 choices allowing repetition
{2-3$$ / $$opt1|opt2|opt3} # select 2 to 3 choices with separator " / "
```
Notes:
* The Dynamic Prompts format `{2$$__flavours__}` does not work as expected. It will only output one value. You can write is as `{r2$$__flavours__}` to get two values, but they may repeat since the evaluation of the wildcard is independent of the choices selection.
* Whitespace in the choices is not ignored like in Dynamic Prompts, but will be cleaned up if the appropriate settings are checked.
### Wildcards
The generic format is:
```text
__parameters$$wildcard'filter'(var=value)__
```
The parameters, the filter, and the setting of a variable are optional. The parameters follow the same format as for the choices.
The wildcard identifier can contain globbing formatting, to read multiple wildcards and merge their choices. Note that if there are no parameters specified, the globbing will use the ones from the first wildcard that matches and have parameters (sorted by keys), so if you don't want that you might want to specify them. Also note that, unlike with Dynamic Prompts, the wildcard name has to be specified with its full path (unless you use globbing).
The filter can be used to filter specific choices from the wildcard. The filtering works before applying the choice conditions (if any). The surrounding quotes can be single or double. The filter is a comma separated list of an integer (positional choice index; zero-based) or choice label. You can also compound them with "+". That is, the comma separated items act as an OR and the "+" inside them as an AND. Using labels can simplify the definitions of complex wildcards where you want to have direct access to specific choices on occasion (you don't need to create wildcards for each individual choice). There are some additional formats when using filters. You can specify "^wildcard" as a filter to use the filter of a previous wildcard in the chain. You can start the filter (regular or inherited) with "#" and it will not be applied to the current wildcard choices, but the filter will remain in memory to use by other descendant wildcards. You use "#" and "^" when you want to pass a filter to inner wildcards (see the test files).
The variable value only applies during the evaluation of the selected choices and is discarded afterward (the variable keeps its original value if there was one).
These are examples of formats you can use to insert a wildcard:
```text
__path/wildcard__ # select 1 choice
__path/wildcard'0'__ # select the first choice
__path/wildcard'label'__ # select the choices with label "label"
__path/wildcard'0,label1,label2'__ # select the first choice and those with labels "label1" or "label2"
__path/wildcard'0,label1+label2'__ # select the first choice and those with both labels "label1" and "label2"
__3$$path/wildcard__ # select 3 choices
__2-3$$path/wildcard__ # select 2 to 3 choices
__r2-3$$path/wildcard__ # select 2 to 3 choices allowing repetition
__2-3$$ / $$path/wildcard__ # select 2 to 3 choices with separator " / "
__path/wildcard(var=value)__ # select 1 choice using the specified variable value in the evaluation.
```
#### Wildcard definitions
A wildcard definition can be:
* A txt file. The wildcard name will be the relative path of the file, without the extension. Each line will be a choice. Lines starting with "#" or empty are ignored. Doesn't support nesting.
* An array or scalar value inside a json or yaml file. The wildcard name includes the relative folder path of the file, without the extension, but also the path of the value inside the file (if there is one). If the file contains a dictionary, the filename part is not used for the wildcard name. Supports nesting by having dictionaries inside dictionaries.
The best format is a yaml file with a dictionary of wildcards inside. An editor supporting yaml syntax is recommended.
In a choice, the content after a "#" is ignored.
If the first choice follows the format of wildcard parameters, it will be used as default parameters for that wildcard (see examples in the tests folder). The choices of the wildcard follow the same format as in the choices construct, or the object format of **Dynamic Prompts** (only in structured files). If using the object format for a choice you can use a new "if" property for the condition, and the "labels" property (an array of strings) in addition to the standard "weight" and "text"/"content".
```yaml
{ labels: ["some_label"], weight: 2, if: "_is_pony", content: "the text" } # "text" property can be used instead of "content"
```
Wildcard parameters in a json/yaml file can also be in object format, and support two additional properties, prefix and suffix:
```yaml
{ sampler: "~", repeating: false, count: 2, prefix: "prefix-", suffix: "-suffix", separator: "/" }
{ sampler: "~", repeating: false, from: 2, to: 3, prefix: "prefix-", suffix: "-suffix", separator: "/" }
```
The prefix and suffix are added to the result along with the selected choices and separators. They can contain other constructs, but the separator can't.
It is recommended to use the object format for the wildcard parameters and for choices with complex options.
Wildcards can contain just one choice. In json and yaml formats this allows the use of a string value for the keys, rather than an array.
A choice inside a wildcard can also be a list or a dictionary of one element containing a list. These are considered anonymous wildcards. With a list it will be an anonymous wildcard with no choice options, and with a dictionary the key will be the options for the choice containing the anonymous wildcard and the value the choices of the anonymous wildcard. Anonymous wildcards can help formatting complex choice values that are used in only one place and thus creating a regular wildcard is not necessary. See test.yaml for examples.
#### Detection of remaining wildcards
This extension should run after any other wildcard extensions, so if you don't use the internal wildcards processing, any remaining wildcards present in the prompt or negative_prompt at this point must be invalid. Usually you might not notice this problem until you check the image metadata, so this option gives you some ways to detect and treat the problem.
### Set command
This command sets the value of a variable that can be checked later.
The format is:
```text
<ppp:set varname>value<ppp:/set>
<ppp:set varname evaluate>value<ppp:/set>
<ppp:set varname add>value<ppp:/set>
<ppp:set varname evaluate add>value<ppp:/set>
```
The `evaluate` parameter makes it so the value of the variable is evaluated at this moment, instead of when it is used.
With the `add` parameter the value is added to the current value of the variable. It does not force an immediate evaluation of the old nor the added value.
The Dynamic Prompts format also works:
```text
${var=value}
${var=!value} # immediate evaluation
```
If also supports the addition as an extension of the Dynamic Prompts format:
```text
${var+=value}
${var+=!value}
```
### Echo command
This command prints the value of a variable.
The format is:
```text
<ppp:echo varname>
<ppp:echo varname>default<ppp:/echo>
```
The Dynamic Prompts format is:
```text
${var}
${var:default}
```
### If command
This command allows you to filter content based on conditions.
The full format is:
```text
<ppp:if condition1>content one<ppp:elif condition2>content two<ppp:else>other content<ppp:/if>
```
The *conditionN* compares a variable with a value or a list of values. The allowed formats are:
```text
[not] variable
[not] variable operation value
variable [not] operation value
[not] variable operation (value1,value2...)
variable [not] operation (value1,value2...)
```
When there is no value it will check if the variable is truthy.
For a simple value the allowed operations are `eq`, `ne`, `gt`, `lt`, `ge`, `le`, `contains` and the value can be a quoted string or an integer.
For a list of values the allowed operations are `contains`, `in` and the value of the variable is checked against all the elements of the list until one matches.
The variable can be one set with the `set` or `add` commands or you can use internal variables like these (names starting with an underscore are reserved):
* `_model` : the loaded model identifier (`"sd1"`, `"sd2"`, `"sdxl"`, `"sd3"`, `"flux"`, `"auraflow"`). `_sd` also works but is deprecated.
* `_modelname` : the loaded model filename (without path). `_sdname` also works but is deprecated.
* `_modelfullname`: the loaded model filename (with path). `_sdfullname` also works but is deprecated.
* `_modelclass`: the class used for the model. Note that this is dependent on the webui. In A1111 all SD versions use the same class. Can be used for new models that are not supported yet with the `_is_*` variables.
* `_is_sd`: true if the loaded model version is any version of SD
* `_is_sd1`: true if the loaded model version is SD 1.x
* `_is_sd2`: true if the loaded model version is SD 2.x
* `_is_sdxl`: true if the loaded model version is SDXL (includes Pony models)
* `_is_ssd`: true if the loaded model version is SSD (Segmind Stable Diffusion 1B). Note that for an SSD model `_is_sdxl` will also be true.
* `_is_sdxl_no_ssd`: true if the loaded model version is SDXL and not an SSD model.
* `_is_pony`: true if the loaded model version is SDXL and a Pony model (based on its filename). Note that for a pony model `_is_sdxl` will also be true.
* `_is_sdxl_no_pony`: true if the loaded model version is SDXL and not a Pony model.
* `_is_sd3`: true if the loaded model version is SD 3.x
* `_is_flux`: true if the loaded model is Flux
* `_is_auraflow`: true if the loaded model is AuraFlow
Any `elif`s (there can be multiple) and the `else` are optional.
#### Example
(multiline to be easier to read)
```text
<ppp:if _is_sd1><lora:test_sd1> test sd1x
<ppp:elif _sd_pony><lora:test_pony> test pony
<ppp:elif _sd_sdxl><lora:test_sdxl> test sdxl
<ppp:else>unknown model
<ppp:/if>
```
Only one of the options will end up in the prompt, depending on the loaded model.
### Sending content to the negative prompt
The new format for this command is like this:
```text
<ppp:stn position>content<ppp:/stn>
```
Where position is optional (defaults to the start) and can be:
* **s**: at the start of the negative prompt
* **e**: at the end of the negative prompt
* **pN**: at the position of the insertion point in the negative prompt with N being 0-9
The format of the insertion point to be used in the negative prompt is:
```text
<ppp:stn iN>
```
If the insertion point is not found it inserts at the start.
#### Example
You have a wildcard for hair colors (\_\_haircolors\_\_) with one being strawberry blonde, but you don't want strawberries. So in that option you add a command to add to the negative prompt, like so:
```text
blonde
strawberry blonde <ppp:stn>strawberry<ppp:/stn>
brunette
```
Then, if that option is chosen this extension will process it later and move that part to the negative prompt.
#### Old format
The old format (`<!...!>`) is not supported anymore.
### Notes on negative commands
Positional insertion commands have less priority that start/end commands, so even if they are at the start or end of the negative prompt, they will end up inside any start/end (and default position) commands.
The content of the negative commands is not processed and is copied as-is to the negative prompt. Other modifiers around the commands are processed in the following way.
#### Attention modifiers (weights)
They will be translated to the negative prompt. For example:
* `(red<ppp:stn>square<ppp:/stn>:1.5)` will end up as `(square:1.5)` in the negative prompt
* `(red[<ppp:stn>square<ppp:/stn>]:1.5)` will end up as `(square:1.35)` in the negative prompt (weight=1.5*0.9) if the merge attention option is enabled or `([square]:1.5)` otherwise.
* However `(red<ppp:stn>[square]<ppp:/stn>:1.5)` will end up as `([square]:1.5)` in the negative prompt. The content of the negative tag is copied as is, and is not merged with the surrounding modifier because the insertions happen after the attention merging.
#### Prompt editing constructs (alternation and scheduling)
Negative commands inside such constructs will copy the construct to the negative prompt, but separating its elements. For example:
* **Alternation**: `[red<ppp:stn>square<ppp:/stn>|blue<ppp:stn>circle<ppp:/stn>]` will end up as `[square|], [|circle]` in the negative prompt, instead of `[square|circle]`
* **Scheduling**: `[red<ppp:stn>square<ppp:/stn>:blue<ppp:stn>circle<ppp:/stn>:0.5]` will end up as `[square::0.5], [:circle:0.5]` instead of `[square:circle:0.5]`
This should still work as intended, and the only negative point i see is the unnecessary separators.
## Configuration ## Configuration
### A1111 (and compatible UIs) UI options See the [configuration documentation](docs/CONFIG.md).
* **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. ## Usage
* **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 See the [syntax documentation](docs/SYNTAX.md).
* **model**: Connect here the MODEL or a string with the model class name used by ComfyUI. Needed for the model kind system variables. ## Cookbook
* **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 See the [cookbook](docs/COOKBOOK.md) for interesting usages.
* **Debug level**: what to write to the console. Note: in SD.Next debug messages only show if you launch it with the --debug argument. ## Contributing
* **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 To develop, I suggest doing so with the extension isolated from the UI (you can use a symlink to test it in the UI), and with its own virtual environment (venv or .venv), so the tests work and can be debugged properly.
* **Process wildcards**: you can choose to process them with this extension or use a different one.
* **Wildcards folders**: you can enter multiple folders separated by commas. In ComfyUI you can leave it empty and add a "wildcards" entry in the extra_model_paths.yaml file.
* **What to do with remaining wildcards?**: select what do you want to do with any found wildcards.
* **Ignore**: do not try to detect wildcards.
* **Remove**: detect wildcards and remove them.
* **Add visible warning**: detect wildcards and add a warning text to the prompt, that hopefully produces a noticeable generation.
* **Stop the generation**: detect wildcards and stop the generation.
* **Default separator used when adding multiple choices**: what do you want to use by default to separate multiple choices when the options allow it (by default it's ", ").
* **Keep the order of selected choices**: if checked, a multiple choice construct will return them in the order they are in the construct.
### Send to negative prompt settings
* **Separator used when adding to the negative prompt**: you can specify the separator used when adding to the negative prompt (by default it's ", ").
* **Ignore repeated content**: it ignores repeated content to avoid repetitions in the negative prompt.
### Clean up settings
* **Remove empty constructs**: removes attention/scheduling/alternation constructs when they are invalid.
* **Remove extra separators**: removes unnecessary separators. This applies to the configured separator and regular commas.
* **Remove additional extra separators**: removes unnecessary separators at start or end of lines. This applies to the configured separator and regular commas.
* **Clean up around BREAKs**: removes consecutive BREAKs and unnecessary commas and space around them.
* **Use EOL instead of Space before BREAKs**: add a newline before BREAKs.
* **Clean up around ANDs**: removes consecutive ANDs and unnecessary commas and space around them.
* **Use EOL instead of Space before ANDs**: add a newline before ANDs.
* **Clean up around extra network tags**: removes spaces around them.
* **Merge attention modifiers (weights) when possible**: it merges attention modifiers when possible (merges into one, multiplying their values). Only merges individually nested modifiers.
* **Remove extra spaces**: removes other unnecessary spaces.
Please note that ComfyUI does not support the BREAK and AND constructs, but the related settings are kept in that UI.
### Content removal settings
* **Remove extra network tags**: removes all extra network tags.
## License ## License
@@ -422,4 +86,4 @@ MIT
## Contact ## Contact
If you have any questions or concerns, please leave an issue, or start a thread in the discussions. If you have any questions or concerns, please start a thread in the discussions. For bug reports and feature requests open an issue.
+29 -4
View File
@@ -10,9 +10,34 @@ 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,
PromptPostProcessorWildcardOptionsComfyUINode,
PromptPostProcessorENMappingOptionsComfyUINode,
PromptPostProcessorSTNOptionsComfyUINode,
PromptPostProcessorCleanupOptionsComfyUINode,
PromptPostProcessorSelectVariableComfyUINode,
PromptPostProcessorWildcardConcatComfyUINode,
)
NODE_CLASS_MAPPINGS = {"ACBPromptPostProcessor": PromptPostProcessorComfyUINode} NODE_CLASS_MAPPINGS = {
NODE_DISPLAY_NAME_MAPPINGS = {"ACBPromptPostProcessor": "ACB Prompt Post Processor"} "ACBPromptPostProcessor": PromptPostProcessorComfyUINode,
"ACBPPPWildcardOptions": PromptPostProcessorWildcardOptionsComfyUINode,
"ACBPPPENMappingOptions": PromptPostProcessorENMappingOptionsComfyUINode,
"ACBPPPSendToNegativeOptions": PromptPostProcessorSTNOptionsComfyUINode,
"ACBPPPCleanupOptions": PromptPostProcessorCleanupOptionsComfyUINode,
"ACBPPPSelectVariable": PromptPostProcessorSelectVariableComfyUINode,
"ACBPPPWildcardConcat": PromptPostProcessorWildcardConcatComfyUINode,
}
NODE_DISPLAY_NAME_MAPPINGS = {
"ACBPromptPostProcessor": "ACB Prompt Post Processor",
"ACBPPPWildcardOptions": "ACB PPP Wildcard Options",
"ACBPPPENMappingOptions": "ACB PPP ExtraNetwork Mapping Options",
"ACBPPPSendToNegativeOptions": "ACB PPP Send-To-Negative Options",
"ACBPPPCleanupOptions": "ACB PPP Cleanup Options",
"ACBPPPSelectVariable": "ACB PPP Select Variable",
"ACBPPPWildcardConcat": "ACB PPP Wildcard Concat",
}
WEB_DIRECTORY = "./web"
__all__ = ["NODE_CLASS_MAPPINGS", "NODE_DISPLAY_NAME_MAPPINGS"] __all__ = ["NODE_CLASS_MAPPINGS", "NODE_DISPLAY_NAME_MAPPINGS", "WEB_DIRECTORY"]
+184
View File
@@ -0,0 +1,184 @@
# Prompt PostProcessor configuration
## Configuration file
The extension supports a configuration file `ppp_config.yaml` with some settings that don't usually change.
By default this configuration is read from the file `ppp_config.yaml.defaults` in the extension folder. That file must not be modified. If you want to personalize any settings you should first copy it as `ppp_config.yaml` in the same folder, or in the ComfyUI user folder (preferred, but only possible in ComfyUI). The options in this new file will take precedence over those in the defaults file.
This file contains some options for how the host applications (WebUIs) should act in certain operations, and also define the supported models, including how to detect them and the model variants definitions. Host names are fixed values (those supported by the extension). The defaults file contains comments to explain the available options.
The model variants now support regular expressions instead of a list of strings to detect the variant. If you used a non default value in previous versions you should create a configuration file and add them with the new format. As before, the default file defines variants for *Pony* and *Illustrious* models.
## Important
**Beware of the combinatorial mode with no limits**. Even very few choice/wildcard constructs can cause a *combinatorial explosion*!
The console log can help you determine the number of combinations that it is trying to generate. There will be an **"Estimated combinations"** message that shows an estimate. You can try first with a limit of 1, then check this message in the log. But note that it is a lower bound estimate, and there could be more combinations.
## ComfyUI
### ACB Prompt Post Processor node
The main node that processes the prompt.
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 detection of model variants.
* **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.
* **debug_level**: What to write to the console.
* **on_warnings**: Warn on the console or stop the generation.
* **strict_mode**: Sets the strict mode in comparison operations.
* **process_wildcards**: Activates the wildcard processing.
* **do_cleanup**: Activates the cleanup processing.
* **cleanup_variables**: Do a cleanup of the output variables (depends on do_cleanup).
* **do_combinatorial**: Activates combinatorial mode, where the output are all the combinations of choices/wildcards of the prompt.
* **combinatorial_shuffle**: It shuffles the combinatorial results.
* **combinatorial_limit**: Limit for the number of generated combinations.
* **wc_options**: Connection to a Wildcards options node.
* **stn_options**: Connection to a Send-To-Negative options node.
* **cup_options**: Connection to a Cleanup options node.
* **en_options**: Connection to a ExtraNetworkMapping options node.
The options nodes are optional. If you don't need to change any of the default values then you don't need to use them.
Outputs:
* **pos_prompt**: the resulting positive prompt
* **neg_prompt**: the resulting negative prompt
* **variables**: the dictionary of variables set or echoed.
The outputs are lists, and in combinatorial mode there will be multiple elements that ComfyUI will process sequentially.
### ACB PPP Select Variable node
Lets you extract the variables used from the output (or just one of them). 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 use it as prompt for the head/face detailer.
Inputs:
* **variables**: the variables dictionary from the main node output.
* **name**: optional name of a variable.
Output:
* **value**: the resulting content, either all the variables (one per line, in "name: value" format) or just the content of the chosen one.
### ACB PPP Wildcards Concat node
This node lets you select up to 10 wildcards that will be concatenated with a chosen separator. You can't specify wildcard folders in the node, so use the other available options to set them.
Inputs:
* **previous_prompt**: An optional text that will be prepended to the wildcards. Lets you chain multiple nodes or other string nodes.
* **filter**: a string to filter the identifiers of the wildcards. It matches the start of the identifiers.
* **separator**: a separator string.
* **wildcard_<n>**: the wildcards to concatenate.
Output:
* **prompt**: concatenated result.
### ACB PPP Wildcard Options node
Options for wildcard processing, in case you want to change them from the defaults.
* **folders**: You can enter multiple folders separated by commas. You can leave it empty (the default) and add a `ppp_wildcards` or `wildcards` entry in the **extra_model_paths.yaml** file (recommended).
* **definitions**: 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.
* **if_wildcards**: Select what do you want to do with any found wildcards/choices (when process wildcards is off or after the processing).
* **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.
* **choice_separator**: What do you want to use by default to separate multiple choices when the options allow it (by default it's ', ").
* **keep_choices_order**: If checked, a multiple choice construct will return them in the order they are in the construct.
### ACB PPP Send-To-Negative Options node
Options for sent to negative commands, in case you want to change them from the defaults.
* **separator**: You can specify the separator used when adding to the negative prompt (by default it's ", ").
* **ignore_repeats**: It ignores repeated content to avoid repetitions in the negative prompt.
### ACB PPP Cleanup Options node
Options for cleanup processing, in case you want to change them from the defaults.
* **extra_spaces**: Removes other unnecessary spaces.
* **empty_constructs**: Removes attention/scheduling/alternation constructs when they are invalid.
* **extra_separators**: Removes unnecessary separators. This applies to the configured separator and regular commas.
* **extra_separators_additional**: Removes unnecessary separators at start or end of lines. This applies to the configured separator and regular commas.
* **extra_separators_include_eol**: In the previous two options it also removes EOLs attached to the separators.
* **around_breaks**: Removes consecutive BREAKs and unnecessary commas and space around them.
* **breaks_with_eol**: Add a newline before BREAKs.
* **around_ands**: Removes consecutive ANDs and unnecessary commas and space around them.
* **ands_with_eol**: Add a newline before ANDs.
* **around_extranetwork_tags**: Removes spaces around extra network tags.
* **merge_attention**: It merges attention modifiers when possible (merges into one, multiplying their values). Only merges individually nested attention (even through choice/wildcard boundaries).
* **remove_extranetwork_tags**: Removes all extra network tags.
Please note that *ComfyUI* does not natively support the `BREAK` and `AND` constructs, but the related settings are kept in that UI in case you use a node that supports them and the extension is configured to allow them (see the configuration file below).
### ACB PPP ExtraNetwork Mapping Options node
Options for extranetworks mapping, in case you want to change them from the defaults.
* **folders**: You can enter multiple folders separated by commas. You can leave it empty (the default) and add a `ppp_extranetworkmappings` entry in the **extra_model_paths.yaml** file (recommended).
* **definitions**: Extranetwork Mappings definitions (in yaml format). Direct input added to the ones found in the extranetwork mappings folders. Allows the mappings to be included in the workflow.
## 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.
* **Combinatorial mode**: Generate all possible prompt combinations (from choices and wildcards) and cycle through them to fill the batch.
* **Shuffle combinations**: It shuffles the combinatorial results.
* **Combinations limit**: Maximum number of combinations to generate (0 = no limit). The actual maximum limit is the number of images (batch size * count).
### 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.
* **What to do on invalid content warnings**: Warn on the console or stop the generation. This also affects the use of unknown variables, and integer comparisons with undefined or non-numeric variables: in *warn* mode the comparison evaluates to false, in *stop* mode the generation is stopped with an error.
* **Use strict operators**: Sets strict operations in comparisons.
* **Apply in img2img**: Check if you want to do the processing in img2img processes.
* **Add original prompts to metadata**: Adds original prompts to the metadata if they have changed.
* **Extranetwork Mappings folders**: You can enter multiple folders separated by commas.
### Wildcard settings
* **Process wildcards**: You can choose to process wildcards and choices with this extension or use a different one.
* **Wildcards folders**: You can enter multiple folders separated by commas.
* **What to do with remaining wildcards?**: Select what do you want to do with any found wildcards/choices (when process wildcards is off or after the processing).
* **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.
* **The extra separators options also remove EOLs**: In the previous two options it also removes EOLs attached to the separators.
* **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 extra network tags.
* **Merge attention modifiers (weights) when possible**: It merges attention modifiers when possible (merges into one, multiplying their values). Only merges individually nested attention (even through choice/wildcard boundaries).
* **Remove extra spaces**: Removes other unnecessary spaces.
* **Remove extra network tags**: Removes all extra network tags.
+517
View File
@@ -0,0 +1,517 @@
# Cookbook
This cookbook shows some interesting uses of the features available.
## Wildcard definition
### Grouping
The common use of wildcards is creating multiple of them with simple lists of elements. But this extension supports more interesting ways to group the elements, reducing bloat and improving selection of the wanted elements.
In these examples we assume we want to select colors but we want warm colors to have double weight than cold colors, then have weights for each of them, and we sometimes want to choose only the warm ones, the cold ones, or any of them.
The usual way would be this:
```yaml
colors_warm:
- 3::red
- 2::orange
- 1::yellow
colors_cold:
- 2::blue
- 1::green
```
And use `__colors_warm__` or `__colors_cold__`, or `{2::__colors_warm__|1::__colors_cold__}` to get all of them.
The `1` weights are not necessary, it just makes the intent clearer.
#### Filters
You can add labels to the elements of a wildcard, allowing you to reduce the availability to only a subset of them. This avoids the need to create multiple wildcards of similar elements.
```yaml
colors:
- "'warm'6::red"
- "'warm'4::orange"
- "'warm'2::yellow"
- "'cold'2::blue"
- "'cold'1::green"
```
And use `__colors'warm'__` or `__colors'cold'__`, or just `__colors__` to get all of them.
Labels can be more complex than this. See the syntax document.
#### Anonymous wildcards
You can also group the elements by using anonymous wildcards, which can also be labeled.
```yaml
colors:
- "'warm'2": # this needs double quotes to include the single quotes as part of the options
- 3::red
- 2::orange
- 1::yellow
- "'cold'1":
- 2::blue
- 1::green
```
And use `__colors'warm'__` or `__colors'cold'__`, or just `__colors__` to get all of them.
This produces almost the same effect as before but is easier to read. The only difference is with the weights when you don't filter, because now it first chooses between the warm and the cold options and then the colors inside the chosen one. This allows you to add simpler weights to the two groups and their elements instead of having to calculate the combined weights.
With simple labels this is probably the best method.
#### Wildcard inclusion
Another option, but not as clean, is to include wildcards inside other wildcards.
```yaml
colors_warm:
- 3::red
- 2::orange
- 1::yellow
colors_cold:
- 2::blue
- 1::green
colors:
- "%'warm'2::include colors_warm"
- "%'cold'1::include colors_cold"
```
Then use `__colors_warm__` or `__colors_cold__`, or `__colors'warm'__` or `__colors'cold'__` or `__colors__` to get all of them.
## Prompt building
You can create a complex set of wildcards that build a full prompt, by using the wildcards along with variables with default values. The variables allow you to choose what parts of the prompt you want to change for specific content, or just leave the defaults. Then, inside the UI, you can use styles (with an appropiate styles node if using ComfyUI) to choose which variables to set. This makes it easy to quickly select what do you want to prompt for.
Wildcards:
```yaml
style:
realistic: photograph
oil: oil painting
sketch: pencil sketch
subject:
- "'human'::{man|woman}"
- "'orc,fantasy'::orc"
- "'goblin,fantasy'::goblin"
clothes:
- "'regular'::t-shirt, pants, shoes"
- "'regular'::swimsuit"
- "'regular'::uniform"
- "'regular'::pajamas"
- "'armor'::metal armor"
- "'armor'::leather armor"
action:
- "'passive'::standing"
- "'passive'::sitting"
- "'passive'::lying down"
- "'passive'::crouching"
- "'active'::running"
- "'active'::dancing"
- "'active'::driving"
background:
home:
- in the kitchen
- in the bathroom
- in the living room
nature:
- in a forest
- in a swamp
- in a desert
- in a grotto
- in the beach
building:
- in a cathedral
- in an office
- in a store
- in a castle
- in a dungeon
any: "{__background/home__|__background/nature__|__background/building__}"
character:
- "${style:()}, ${subject:__subject'human'__}, ${clothes:__clothes'regular'__}, ${action:__action'passive'__}, ${background:__background/any__}"
```
Styles file:
```csv
name,prompt,negative_prompt
"Nothing","",""
"Style: realistic","${style=__style/realistic__}",""
"Style: oil","${style=__style/oil__}",""
"Style: sketch","${style=__style/sketch__}",""
"Subject: human","${subject=__subject'human'__}",""
"Subject: orc","${subject=__subject'orc'__}",""
"Subject: goblin","${subject=__subject'goblin'__}",""
"Subject: fantasy","${subject=__subject'fantasy'__}",""
"Subject: any","${subject=__subject__}",""
"Clothes: regular","${clothes=__clothes'regular'__}",""
"Clothes: armor","${clothes=__clothes'armor'__}",""
"Clothes: any","${clothes=__clothes__}",""
"Action: passive","${action=__action'passive'__}",""
"Action: active","${action=__action'active'__}",""
"Action: any","${action=__action__}",""
"Background: home","${background=__background/home__}",""
"Background: nature","${background=__background/nature__}",""
"Background: building","${background=__background/building__}",""
"Background: any","${background=__background/any__}",""
"Character","__character__",""
```
You first select the styles that set the variables that you are interested in changing from the default, and you end with the main wildcard that uses them to build the prompt. Note that the order of the variables don't usually matter because they are only evaluated when echoed to the prompt. This allows you to use variables inside wildcards that are inside other variables, and only at the end they will be evaluated.
Sample style selections:
Sample 1:
- "Style: oil"
- "Subject human"
- "Clothes: regular"
- "Background: home"
- "Character"
This would create this prompt:
`${style=__style/oil__}, ${subject=__subject'human'__}, ${clothes=__clothes'regular'__}, ${background=__background/home__}, __character__`
Sample 2:
- "Subject: fantasy"
- "Clothes: armor"
- "Action: active"
- "Background: nature"
- "Character"
This would create this prompt:
`${subject=__subject'fantasy'__}, ${clothes=__clothes'armor'__}, ${action=__action'active'__}, ${background=__background/nature__}, __character__`
## Using variables for detailer prompts
This only applies to ComfyUI, because the variables cannot be used in the ADetailer prompts of the A1111 UIs.
You can put parts of the prompt to be set into variables, and then extract this variables from the output and use them in the prompts of the detailer nodes.
For example, imagine you have this prompt:
`Cyberpunk woman with ${head:long red hair mohawk, green eyes, and neon makeup} riding a futuristic motorcycle`
That will create a variable `head` with the head description and insert it in that position in the prompt. That variable can then be extracted with the "**ACB PPP Select Variable**" node and used as input for the detailer for the head, thus avoiding prompt clutter that does not apply.
## Conditional content based on model
The `_is_*` system variables let you write a single prompt that adapts automatically to the loaded model. This is especially useful for LoRAs that exist in different versions for different model families.
```text
beautiful woman in a garden
<ppp:if _is_sd1><lora:woman_detail_sd1:0.8> detailed face<ppp:elif _is_sdxl><lora:woman_detail_xl:0.9> detailed face<ppp:elif _is_flux><lora:woman_detail_flux:1.0> detailed face<ppp:/if>
```
Only one branch ends up in the final prompt. You can also nest `if` commands inside wildcards to gate certain choices:
```yaml
poses:
- "'standing'::standing upright"
- "'sitting'::sitting on a bench"
- "'dynamic,sdxl'if _is_sdxl::dynamic action pose"
- "'dynamic,flux'if _is_flux::powerful dynamic pose, motion blur"
```
The last two entries are only available when their respective model is loaded.
## ExtraNetworks mappings
When you have the same LoRA in multiple versions (one per model family), you can create a mapping so the correct one is picked automatically without rewriting your prompt for each model.
**Mapping file** (`enmappings/my_loras.yaml`):
```yaml
lora:
my_character:
- condition: "_is_sd1"
name: "my_character_sd1"
parameters: "0.8"
triggers: ["my_char_trigger"]
- condition: "_is_sdxl"
name: "my_character_xl"
parameters: "0.9"
triggers: ["my_char_trigger", "detailed"]
- condition: "_is_flux"
name: "my_character_flux"
parameters: "1.0"
triggers: ["my_char_trigger"]
```
**Prompt usage:**
```text
a portrait of <ppp:ext $lora my_character>extra trigger<ppp:/ext>
```
The extension picks the mapping whose condition matches the loaded model, builds the correct `<lora:...:...>` tag, prepends the triggers from the mapping, and appends the inline triggers. If no condition matches (e.g. an unsupported model) nothing is added.
You can also specify a weight multiplier directly in the command - if both the command weight and the mapping `parameters` are numbers, they are multiplied:
```text
<ppp:ext $lora my_character 0.75/>
```
## Send-to-negative with attention modifiers
When a send-to-negative command sits inside an attention modifier, the weight is carried over to the negative prompt.
```text
(red apple<ppp:stn>round shape<ppp:/stn>:1.4)
```
Result in the negative: `(round shape:1.4)` - the surrounding weight is applied.
```text
(red apple<ppp:stn>[round shape]<ppp:/stn>:1.4)
(red [apple<ppp:stn>round shape<ppp:/stn>]:1.4)
```
For both cases, the result in the negative is: `(round shape:1.26)` - the weight is merged, if merge attention is enabled.
## Array variables
Arrays let you collect several values and then use them together, which is useful when you want to pick a random set of attributes and later echo them as a formatted list.
**Example - picking random accessories:**
```text
${accessories[]=*__accessories__}
a woman wearing ${accessories[&', ']}
```
The wildcard `__accessories__` is expanded into an array, then echoed as a comma-separated string.
**Example - building a list of a fixed size from separate wildcards:**
```text
${colors[]=*__colors__}
${colors[]+=*__materials__}
abstract composition of ${colors[&' and ']}
```
**Initializing from a literal list:**
```text
${seasons[]=*('spring', 'summer', 'autumn', 'winter')}
seasonal scene: ${seasons[&', ']}
```
## Conditional filtering inside wildcards
You can attach an `if` condition directly to a choice inside a wildcard, making the choice invisible unless the condition is true. This lets a single wildcard file serve all model families without duplication.
```yaml
lora_styles:
- "'painterly'::painterly style, oil paint"
- "'anime'if _is_sd1 or _is_sdxl::anime style, flat shading"
- "'photorealistic'if _is_sdxl or _is_flux::photorealistic, hyper detailed"
- "'concept'if _is_flux::concept art, cinematic lighting"
```
When loaded with SD 1, only `painterly` and `anime` are available. When loaded with Flux, only `painterly`, `photorealistic`, and `concept` are.
You can combine conditions with `and`, `or`, and `not`, and reference any user or system variable:
```yaml
effects:
- "bokeh, shallow depth of field"
- "long exposure, motion blur"
- "'hdr'if _is_sdxl or _is_flux::HDR, high dynamic range"
- "'film'if quality ne 'draft'::film grain, subtle noise"
```
## `setwcdeffilter` for dynamic routing
`setwcdeffilter` sets a default filter on a wildcard before it is referenced anywhere in the prompt. This avoids having to repeat a filter at every call site when you want to pre-select a subset of choices.
A typical use case is a style variable that narrows what a downstream wildcard will pick:
```text
<ppp:setwcdeffilter 'clothing' 'armor'/>
A warrior ${subject} wearing __clothing__
```
Whatever label filter you pass becomes the default for that wildcard for the rest of the current generation.
You can use globbing to apply the filter to a whole family of wildcards:
```text
<ppp:setwcdeffilter 'items/*' 'rare'/>
```
This pre-filters every wildcard under `items/` to only `rare`-labelled choices. To remove a previously set default filter:
```text
<ppp:setwcdeffilter 'items/*'/>
```
A practical workflow combining this with variables and styles:
```csv
name,prompt,negative_prompt
"Gear: armor","<ppp:setwcdeffilter 'clothing' 'armor'/>",""
"Gear: casual","<ppp:setwcdeffilter 'clothing' 'casual'/>",""
"Character","__character__",""
```
Select a "Gear" style first and then "Character" - the wildcard inside `character` will automatically use the pre-filtered `clothing` choices.
## Globbing wildcards
Instead of referencing a single wildcard file, you can use glob patterns to merge choices from multiple files into one pool. This is handy when you split a large collection into smaller themed files.
File layout:
```text
wildcards/
characters/
humans.yaml
orcs.yaml
elves.yaml
```
Each file is a regular YAML wildcard. Reference them all at once:
```text
__characters/*__
```
This merges all choices from `humans.yaml`, `orcs.yaml`, and `elves.yaml` into a single pool and picks one.
Note that the globbing works on the wildcard key, not the file structure. So if those files contain multiple wildcards, all of them will be merged.
You can apply parameters to the glob call just like a regular wildcard:
```text
__2$$ / $$characters/*__
```
Selects 2 choices from the merged pool, separated by " / ".
You can also apply a filter to narrow from within the merged pool:
```text
__characters/*'fantasy'__
```
Only choices labelled `fantasy` across all matched files are eligible.
Note: if no parameters are specified in the glob call, the parameters from the first matching file that defines them (sorted by key) are used. To avoid that, specify parameters explicitly in the call.
## Prefix/suffix or container on wildcard parameters
Using the object format for wildcard parameters you can set a prefix and/or suffix, or a container, that wraps the result. This is cleaner than repeating the wrapper in each choice.
In this example, without prefix/suffix, every choice needs to repeat the attention modifier:
```yaml
qualities:
- "(ultra detailed:1.3)"
- "(highly detailed:1.3)"
- "(intricate details:1.3)"
```
With object-format parameters and a prefix/suffix:
```yaml
qualities:
- { prefix: "(", suffix: ":1.3)" } # parameters line
- "ultra detailed"
- "highly detailed"
- "intricate details"
```
The prefix and suffix are added around the wildcard's result. They can themselves contain constructs.
Another way is with the container property, which does the same in a slightly more flexible way:
```yaml
qualities:
- { container: "(${__choices[]}:1.3)" } # parameters line
- "ultra detailed"
- "highly detailed"
- "intricate details"
```
## `ifundefined` / `?=` for safe defaults
When building multi-style workflows you often want a variable to have a sensible default that can be overridden by an earlier style, but only if it hasn't already been set. The `ifundefined` modifier (`?=`) is designed for this.
```text
${quality?=__qualities__}, ${style?=photograph}, ${subject?=__subject__}
```
If a "Quality: ultra" style already ran `${quality=ultra detailed, 8k}` earlier in the pipeline, the `?=` here does nothing. If no quality style was chosen, it picks a random value from `__qualities__` as the fallback.
This is more robust than relying on the order of styles. But if you need a specific default for different instances use instead the default value when echoing the variable.
**Styles file example:**
```csv
name,prompt,negative_prompt
"Quality: draft","${quality=draft quality}",""
"Quality: high","${quality=ultra detailed, 8k}",""
"Style: photo","${style=photograph}",""
"Style: painting","${style=oil painting}",""
"Character","${quality?=__qualities__}, ${style?=photograph}, __subject__",""
```
If the user selects "Quality: high" and "Character", quality is `ultra detailed, 8k`. If they select only "Character", quality falls back to a random value from `__qualities__`.
You can also use `evaluate ifundefined` (`?=!`) to resolve the wildcard immediately rather than lazily:
```text
${quality?=!__qualities__}
```
## Debugging tips
When something isn't generating as expected, the debug setting is your first tool. Enable it in the extension settings; it will log all system variables at generation time, which tells you exactly what values are available for your conditions.
**Finding the right system variable for an unsupported model:**
If you load a model that isn't matched by any `_is_*` variable, check `_modelclass` in the debug log. Use it directly in a condition while you wait for official support:
```text
<ppp:if _modelclass eq 'StableDiffusionXLPipeline'>content for new model<ppp:/if>
```
**Inspecting variable values mid-prompt:**
Insert a temporary `echo` to see what a variable resolves to:
```text
[DEBUG style=${style}] ${style:photograph}, detailed portrait
```
Remove the bracketed part once you're satisfied.
**Checking which choices are available after filtering:**
If a wildcard seems to produce unexpected results, temporarily use a fixed positional filter to verify specific choices:
```text
__mywildcard'0'__ # always picks the first choice
__mywildcard'1'__ # always picks the second choice
```
This confirms the order and content of choices before relying on label filters.
**Common pitfalls:**
- A `%` choice in a YAML array must be quoted, otherwise YAML treats `%` as invalid syntax.
- Variables set with `${var=value}` are lazy - they are not evaluated until echoed. Use `${var=!__wildcard__}` (with `!`) for immediate evaluation if the value should only be resolved once (as in, you want the same value to be echoed later multiple times).
- Wildcards cannot be used inside extranetwork tags (because some LoRA names contain double underscores). Put the entire `<lora:...>` tag inside a wildcard choice instead, or use the `ext` command.
+496
View File
@@ -0,0 +1,496 @@
# 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.
## Special characters
Remember that to use any special character (like parentheses, brackets or braces) as-is in the prompt (not as part of a construct), you need to escape them with a backslash. Like:
```text
velma \(from scooby doo\)
```
And inside a yaml/json file that backslash must be itself escaped again to be preserved.
```yaml
characters:
- velma \\(from scooby doo\\)
```
## 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):
* "**~**" (random) or "**@**" (cyclical): sampler (for compatibility with *Dynamic Prompts*). The cyclical sampler cycles through all combinations in order across consecutive `process_prompt` calls, resuming where the previous call left off (as long as the input prompt and negative prompt do not change).
* "**r**": means it allows repetition of the choices.
* "**o**": means it is "optional", and no error will be raised if there are no choices to select from.
* "**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.
* "**'description'**": optional description, only valid in wildcard definitions. Used only in the Wildcards Concat node in ComfyUI.
* "**$$sep**": separator when multiple choices are selected. Default is set in settings.
* "**$$**": end of the parameters (not optional if any parameters).
Regarding the "optional" flag, consider this scenario: due to their conditions no choice is available. It will raise an error. If you add the `o` then it will just return an empty string. This is only necessary if all choices have conditions and they could all be false. It is not the same as setting a range starting at 0, because that would be an allowed number of returned choices. If you do this and no choices are available, no error is raised.
The choice options are as follows:
* "**%**": indicates that the content of the choice is a 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 (not optional if any options)
Whitespace is allowed between parameters/options.
The only command available is `include wildcard`, which will include the choices of the specified wildcard in place of this choice. This allows composing choices from multiple wildcards. It also works in the choices of a wildcard, but note that in yaml you cannot start an array element with "%" and you will have to put the full choice in quotes, or use the object format.
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 " / " |
| `{o$$if _is_sd1::choice1\|if _is_sd2::choice2}` | select 1 choice, both have conditions, if none matches it is allowed because we indicate that it is optional |
| `{choice1\|choice2\|%0.5::include path/wildcard}` | select 1 choice from the two specified and the ones inside the path/wildcard wildcard, which will be weighted with half their weights |
Notes:
* The *Dynamic Prompts* format `{2$$__flavours__}` does not work as expected because the wildcard is considered only one possible choice (it will only output one value). You can write it instead as a wildcard with parameters `__2$$flavours__`.
* 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.
Wildcards cannot be used inside an extranetwork tag (because some LoRA names contain double underscores). If you need to choose from multiple LoRAs put the whole extranetwork tag inside a wildcard, or use choices.
### Identifier
* Allowed characters are letters, numbers, underscore (`_`), dash (`-`), dot (`.`), and the path separators (`/` and `\`). It cannot start with an underscore because it would be ambiguous whether it's part of the name or just precedes the wildcard.
* 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).
* You can use variables, with the `${name}`, `${name:default}`, `<ppp:echo name/>` or `<ppp:echo name>default<ppp:/>echo>` formats, to build a dynamic identifier.
### Filter
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/range (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). You can use variables for individual labels (`${v}`) or a single variable for the whole filter (`${v}` or `${a[&',']}` or `${a[&'+']}`).
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).
### Variable
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).
### Examples
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'1-2'__` | select the second or third 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 (*including the final `$$`*), it will be used as default parameters for that wildcard (see examples in the tests folder). Unless the only property used is the wildcard description.
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 the following in addition to the standard `weight` and `text`/`content`:
* `if`: the condition (a string)
* `labels`: list of labels (an array of strings)
* `command`: indicates the content is a command (a boolean)
```yaml
{ command: false, 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 some additional properties, that are not included in the string format:
* `prefix`: content to prefix the list of choices
* `suffix`: content to suffix the list of choices
* `container`: includes the prefix, choices array variable, and suffix
```yaml
{ sampler: "~", repeating: false, optional: false, from: 2, to: 3, description: "test wildcard", container: "prefix-${__choices[&'/']}-suffix" }
{ sampler: "~", repeating: false, optional: false, count: 2, description: "test wildcard", prefix: "prefix-", suffix: "-suffix", separator: "/" }
{ sampler: "~", repeating: false, optional: false, from: 2, to: 3, description: "test wildcard", 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.
The container is a new option that replaces prefix/suffix/separator, and makes use of the recent support for array variables. Its value would be the concatenation of any prefix and/or suffix with the echoing (with the chosen separator) of a temporary `__choices[]` variable that holds the chosen values. This property is preferred over prefix/suffix/separator unless you only need the separator.
It is recommended to use the object format for the wildcard parameters and for choices with complex options.
If your first choice is interpreted as parameters, and you don't need parameters, you can avoid the problem by adding an empty parameters object `{}` as first choice.
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. Use an anonymous wildcard to group options inside a wildcard, and attach a label to it to be able to choose only from that group.
Remember you can use the include command on choices to compose a wildcard from other wildcards' choices.
Note: the files should have UTF-8 encoding. The extension will also try with windows-1252 if that fails.
Wildcard definitions are reloaded automatically on each generation if they change.
### 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 Wildcard Default Filter command
This command can be used to set a default filter for a wildcard, before it is used.
The format is:
| Construct | Meaning |
| --------- | ------- |
| `<ppp:setwcdeffilter 'identifier' 'filter'/>` | Sets a filter |
| `<ppp:setwcdeffilter 'identifier'/>` | Removes the filter |
The wildcard identifier supports globbing. The filter does not allow the `^` or `#` flags.
## 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" |
Set variables are included in the output variables with their last value.
## Echo command
This command prints the value of a variable, or the specified default if it doesn't exist. If the variable does not exist and has no default, depending on the **What to do on invalid content warnings?** setting it will assume an empty value, or result in an error.
The format is:
| Construct |
| --------- |
| `<ppp:echo varname/>` |
| `<ppp:echo varname>default<ppp:/echo>` |
The *Dynamic Prompts* format is:
| Construct |
| --------- |
| `${varname}` |
| `${varname:default}` |
An echoed variable that uses the default because it doesn't have a value will be included in the output variables with the last default value used.
## Array variables
There is support for array variables. They use brackets `[]` to differenciate from regular variables.
They can be initialized in several ways:
| Construct | Meaning |
| --------- | ------- |
| `${var[]=value}` | initialize and set the first value |
| `${var[]=*()}` | initialize an empty array |
| `${var[]=*var2[]}` | initialize an array from another array |
| `${var[]=*__wildcard__}` | initialize an array from a wildcard |
| `${var[]=*('value',var2)}` | initialize an array from a list of values (strings or variables) |
| `${var[]+=}` | add an empty element to the array |
| `${var[]+=value}` | add a value to the array |
| `${var[]+=*var2[]}` | add elements from another array |
| `${var[]+=*__wildcard__}` | add elements from a wildcard |
The star operator `*` is always with inmediate evaluation.
And can be accesed/echoed with:
* Empty brackets mean the whole array (used when initializing or when echoing the whole array)
* An integer inside the brackets means an indexed value. A variable identifier (not an indexed array) can be used to get the integer.
* A hash inside the brackets is used to get the length of the array
* An ampersand followed by a string inside the brackets (with quotes) is used to get the full array joined with a separator.
| Construct | Meaning |
| --------- | ------- |
| `${var[]}` | echo all elements with a default separator |
| `${var[&' / ']}` | echo all elements with a specific separator |
| `${var[n]}` | echo an element from the array |
| `${var[n]:default}` | echo an element with a default |
| `${var[#]}` | echo the length of the array |
## 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` is a boolean expression, which can use `and`, `or`, `not` and grouping with parentheses, and where the simplest expression can be:
| Construct | Meaning |
| --------- | ------- |
| `operand` | check truthyness of the operand, meaning not zero, empty string nor empty array |
| `operand1 [not] operation operand2` | compare the operands |
The operands can be a variable (`variable`, `array[]`, `array[index]`), a quoted string, an integer, a boolean, or a parenthesized list of any of them `('string', 1, variable, false, array[], array[2])`. The allowed operations depend on which operands are used.
When an operand is or contains a variable, it is resolved to the variable's current value before the operation.
Variable values `true` and `false` are considered a boolean, and an all digits value is an integer. Except in substring operations indicated below.
The operation can be preceded by `not` for readability, instead of using it in the front.
The supported operations are: `eq`, `ne`, `gt`, `lt`, `ge`, `le`, `in`, `any_in`, `contains` and `contains_any`. The `any_in` and `contains_any` variants exist because `any` and `contains` consider all elements.
This list shows what they do depending on the kind of operand (R = regular variable, A = array variable).
| Operation | R1 op R2 | A1 op A2 | A1 op R2 | R1 op A2 |
| --------- | -------- | -------- | -------- | -------- |
| `eq` | OK | OK (pairwise) | Error in strict mode, all A1 with R2 otherwise | Error in strict mode, R1 with all A2 otherwise |
| `ne` | OK | OK (pairwise) | Error in strict mode, all A1 with R2 otherwise | Error in strict mode, R1 with all A2 otherwise |
| `gt` | OK | OK (pairwise) | Error in strict mode, all A1 with R2 otherwise | Error in strict mode, R1 with all A2 otherwise |
| `lt` | OK | OK (pairwise) | Error in strict mode, all A1 with R2 otherwise | Error in strict mode, R1 with all A2 otherwise |
| `ge` | OK | OK (pairwise) | Error in strict mode, all A1 with R2 otherwise | Error in strict mode, R1 with all A2 otherwise |
| `le` | OK | OK (pairwise) | Error in strict mode, all A1 with R2 otherwise | Error in strict mode, R1 with all A2 otherwise |
| `in` | OK (substring, R1 in R2) | OK (all A1 in A2) | OK (all substring in A1 in R2) | OK (R1 in A2) |
| `any_in` | Error | OK (any A1 in A2) | OK (any substring in A1 in R2) | Error |
| `contains` | OK (substring, R2 in R1) | OK (all A2 in A1) | OK (R2 in A1) | OK (all substrings A2 in R1) |
| `contains_any` | Error | OK (any A2 in A1) | Error | OK (any substrings A2 in R1) |
When a comparison tries to compare undefined variables or the values have different types (f.e. an integer and a string), the behavior depends on the `on_warning` setting: in `warn` mode the comparison evaluates to false, and in `stop` mode an error is raised. In non strict mode a numeric string literal (with no leading zeros) will be considered an integer.
The variable can be one set with the `set` command (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.
## ExtraNetwork command
This command is a shortcut to add an extranetwork (usually a lora), and its triggers, with conditions. More legible and sometimes shorter than adding regular extranetworks inside if commands.
The full format is:
`<ppp:ext type name [parameters] [if condition]>[triggers]<ppp:/ext>`
`<ppp:ext type name [parameters] [if condition]/>`
The `type` is the kind of extranetwork, like `lora` or `hypernet`.
The `name` is the extranetwork identifier. If it is not a regular identifier (i.e. starts with a number or contains spaces or symbols) it should be inside quotes.
The `parameters` is optional and its format depends on the extranetwork type. With LoRAs or HyperNets it is usually a single weight number, so if the type is one of those and there are no parameters it will default to `1`. If it is not a number it should go inside quotes.
The `condition` uses the same format as in the `if` command, and it is also optional.
The `triggers` are also optional, and can be any content. If there are no triggers the command ending can be omitted.
If the condition passes (or if there is no condition) the extranetwork tag will be built and added to the result along with any triggers.
### Examples
(multiline to be easier to read)
```text
<ppp:ext lora test_sd1 if _is_sd1>test sd1x<ppp:/ext>
<ppp:ext lora test_pony 0.5 if _is_pony>test pony<ppp:/ext>
<ppp:ext lora test_ilxl if _is_illustrious/>
<ppp:ext lora 'test sdxl' '1:0.8' if _is_pure_sdxl>test sdxl<ppp:/ext>
```
Will turn into one of these (or none) depending on the model:
* `<lora:test_sd1:1>test sd1x`
* `<lora:test_pony:0.5>test pony`
* `<lora:test_illustrious:1>`
* `<lora:test sdxl:1:0.8>test sdxl`
### Extranetworks mappings
The extranetwork command supports specifying mappings of extranetworks (like LoRAs), so, for example, a different one can be used depending on the loaded model.
If the type of extranetwork is prefixed with a `$` the command will look for a mapping.
If you have LoRAs that do the same but for different models, create a mapping to group them, configuring there the weight and triggers for each one.
The mappings are configured in yaml files in any of the configured extranetwork mappings folders. The format is like this:
```yaml
extnettype:
mappingname:
- condition: "<a supported condition>"
name: "<name of the extranetwork>"
parameters: "<parameters of the extranetwork>"
triggers: [<list of triggers>]
weight: 1.0
...
```
Used like this:
| Construct | Meaning |
| --------- | ------- |
| `<ppp:ext $lora mappingname/>` | Mapping without additional triggers |
| `<ppp:ext $lora mappingname>inline triggers<ppp:/ext>` | Mapping with additional triggers |
Each mapping can have any number of elements in its list of mappings. There are no mandatory properties for a mapping. The properties mean the following:
* `extnettype`: the kind of extranetwork, for example `lora`.
* `mappingname`: the name you want to give to the mapping, to be referenced in the command.
* `condition`: the condition to check for this mapping to be used (usually it should be one of the `_is_*` variables). If the conditions of multiple mappings evaluate to True, one will be chosen randomly. If the condition is missing it is considered True, to be used in the last mapping to catch as an "else" condition, and will be used if no other mapping applies.
* `name`: name of the real extranetwork. If it is missing no extranetwork tag will be added.
* `parameters`: parameters for the real extranetwork. If it is missing it is assumed "1" for LoRAs and HyperNets. If both this parameter and the parameter in the ext command are numbers they are multiplied for the result. In other case the parameter of the ext command, if it exists, is used.
* `triggers`: list of trigger strings. If it is missing, only the inline triggers in the ext command will be added.
* `weight`: weight for this variant, in case multiple of them apply, to choose one. Default is 1.
See the file in the tests folder as an example.
## 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.
* `(red<ppp:stn>[square]<ppp:/stn>:1.5)` will also end up as `(square:1.35)` in the negative prompt if the merge attention option is enabled, because the content of the negative tag is a single attention construct whose weight is merged with the surrounding modifier.
### 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.
+165 -61
View File
@@ -1,13 +1,18 @@
%import common (LETTER, DIGIT, INT, CNAME, SIGNED_NUMBER, NUMBER) %import common (LETTER, DIGIT, INT, CNAME, SIGNED_NUMBER, NUMBER)
_WHITESPACE: /\s+/ _WHITESPACE: /\s+/
STRING: /("(?!"").*?(?<!\\)(\\\\)*?"|'(?!'').*?(?<!\\)(\\\\)*?')/i STRING: /("(?!"").*?(?<!\\)(\\\\)*?"|'(?!'').*?(?<!\\)(\\\\)*?')/i // string with single or double quotes
BOOLEAN: /true|false/i BOOLEAN: /true|false/i
INDEX: INT | IDENTIFIER WC_NAME_PLAIN_START: /(?:(?!__|\$\$|\$\{|<ppp:|[('"])[a-zA-Z0-9\-.\\\/])+/u // starting characters, excluding end and other inside constructs
WILDCARD_NAME: /(?:(?!__|\$\$|[('"])\S)+/ WC_NAME_PLAIN: /(?:(?!__|\$\$|\$\{|<ppp:|[('"])[a-zA-Z0-9\-.\\\/_#\*\?\[!\]])+/u // continuing characters, including globbing characters and anonymous wildcard mark, excluding end and other inside constructs
INDEX: INT
FILTER: INDEX | IDENTIFIER
IDENTIFIER: CNAME
SIMPLEVALUE: STRING | SIGNED_NUMBER | BOOLEAN
VARNAME: /(?!__)[a-zA-Z_][a-zA-Z0-9_]*/
// plain text and weights // plain text and weights
?plain: /((?!__|\bAND\b|\${)[^\\()\[\]:<>${]|\\.)+/s // exclude only the starting ones ?plain: /((?!__|\bAND\b|\${|\x1d)[^\\()\[\]:<>${]|\\.)+/s // exclude only the starting ones
?plain_choice: /((?!__|\bAND\b|\${|\$\$)[^\\()\[\]:<>${|}~@]|\\.)+/s // add the specific internal choice ones ?plain_choice: /((?!__|\bAND\b|\${|\$\$)[^\\()\[\]:<>${|}~@]|\\.)+/s // add the specific internal choice ones
?plain_alternate: /((?!__|\bAND\b|\${)[^\\()\[\]:<>${|]|\\.)+/s // add the specific internal alternate ones ?plain_alternate: /((?!__|\bAND\b|\${)[^\\()\[\]:<>${|]|\\.)+/s // add the specific internal alternate ones
?plain_var: /((?!__|\bAND\b|\${)[^\\()\[\]:<>${}]|\\.)+/s // add the specific internal var ones ?plain_var: /((?!__|\bAND\b|\${)[^\\()\[\]:<>${}]|\\.)+/s // add the specific internal var ones
@@ -18,22 +23,65 @@ WILDCARD_NAME: /(?:(?!__|\$\$|[('"])\S)+/
?specialchars_var: /[_{()\[\]:<>}]|\$(?![{$])/ // add the internal var ones ?specialchars_var: /[_{()\[\]:<>}]|\$(?![{$])/ // add the internal var ones
?numpar: _WHITESPACE? SIGNED_NUMBER _WHITESPACE? ?numpar: _WHITESPACE? SIGNED_NUMBER _WHITESPACE?
start: promptcomp | content start: prompt negative_sep prompt
?prompt: promptcomp | content
negative_sep: /\x1d/
// prompt composition with AND // prompt composition with AND
promptcomp.4: promptcomppart ( [ ":" numpar ] ( /\bAND\b/ promptcomppart [ ":" numpar ] )+ )+ promptcomp.4: promptcomppart ( [ ":" numpar ] ( /\bAND\b/ promptcomppart [ ":" numpar ] )+ )+
promptcomppart: content promptcomppart: content
// simple prompts // simple prompts
?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 )+ ?old_content.2: ( attention | scheduled | alternate | extranetworktag )+
?new_content.3: ( variableset | variableuse | commandstn | commandstni | commandset | commandecho | commandif | wildcard | choices )+ //#if ALLOW_NEW_CONTENT
?new_content_negtag.3: ( variableset | variableuse | commandset | commandecho | commandif | wildcard | choices )+ ?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 )*
?content_en.2: (new_content_en | plain | specialchars )*
//#if ALLOW_WILDCARDS and ALLOW_CHOICES and ALLOW_COMMVARS
?new_content.3: ( variableset | variableuse | commandstn | commandstni | commandset | commandecho | commandif | commandext | commandsetwcdeffilter | wildcard | choices )+
?new_content_negtag.3: ( variableset | variableuse | commandset | commandecho | commandif | commandext | commandsetwcdeffilter | wildcard | choices )+
?new_content_en.3: ( variableset | variableuse | commandset | commandecho | commandif | choices )+
//#elif ALLOW_WILDCARDS and not ALLOW_CHOICES and ALLOW_COMMVARS
?new_content.3: ( variableset | variableuse | commandstn | commandstni | commandset | commandecho | commandif | commandext | commandsetwcdeffilter | wildcard )+
?new_content_negtag.3: ( variableset | variableuse | commandset | commandecho | commandif | commandext | commandsetwcdeffilter | wildcard )+
?new_content_en.3: ( variableset | variableuse | commandset | commandecho | commandif )+
//#elif not ALLOW_WILDCARDS and ALLOW_CHOICES and ALLOW_COMMVARS
?new_content.3: ( variableset | variableuse | commandstn | commandstni | commandset | commandecho | commandif | commandext | choices )+
?new_content_negtag.3: ( variableset | variableuse | commandset | commandecho | commandif | commandext | choices )+
?new_content_en.3: ( variableset | variableuse | commandset | commandecho | commandif | choices )+
//#elif ALLOW_WILDCARDS and ALLOW_CHOICES and not ALLOW_COMMVARS
?new_content.3: ( wildcard | choices )+
?new_content_negtag.3: ( wildcard | choices )+
?new_content_en.3: ( choices )+
//#elif ALLOW_WILDCARDS and not ALLOW_CHOICES and not ALLOW_COMMVARS
?new_content.3: ( wildcard )+
?new_content_negtag.3: ( wildcard )+
?new_content_en.3: /(?!)./ // never matches
//#elif not ALLOW_WILDCARDS and ALLOW_CHOICES and not ALLOW_COMMVARS
?new_content.3: ( choices )+
?new_content_negtag.3: ( choices )+
?new_content_en.3: ( choices )+
//#elif not ALLOW_WILDCARDS and not ALLOW_CHOICES and ALLOW_COMMVARS
?new_content.3: ( variableset | variableuse | commandstn | commandstni | commandset | commandecho | commandif | commandext )+
?new_content_negtag.3: ( variableset | variableuse | commandset | commandecho | commandif | commandext )+
?new_content_en.3: ( variableset | variableuse | commandset | commandecho | commandif )+
//#else
?new_content.3: ( variableset | variableuse | commandstn | commandstni | commandset | commandecho | commandif | commandext | commandsetwcdeffilter | wildcard | choices )+
?new_content_negtag.3: ( variableset | variableuse | commandset | commandecho | commandif | commandext | commandsetwcdeffilter | wildcard | choices )+
?new_content_en.3: ( variableset | variableuse | commandset | commandecho | commandif | choices )+
//#endif
//#else
?content.2: ( old_content | plain | specialchars )*
?content_choice.2: ( old_content | plain_choice | specialchars_choice )*
?content_var.2: ( old_content | plain_var | specialchars_var )*
?content_negtag.2: ( old_content | plain | specialchars_negtag )*
?content_alternate.2: ( old_content | plain_alternate | specialchars_alternate )*
?content_en.2: (plain | specialchars )*
//#endif
// attention modifiers // attention modifiers
attention: ( "(" content [ ":" numpar ] ")" ) | ( "[" content "]" ) attention: ( "(" content [ ":" numpar ] ")" ) | ( "[" content "]" )
@@ -44,62 +92,118 @@ alternateoption: content_alternate
scheduled: "[" [ content ":" ] content ":" numpar "]" scheduled: "[" [ content ":" ] content ":" numpar "]"
// extra network tags // extra network tags
extranetworktag: "<" /(?!ppp:)\w+:/ inside_content ">" extranetworktag: "<" /(?!ppp:)\w+:/ encontent ">"
?encontent.3: content_en
// command: stn (send to negative) //#if ALLOW_COMMVARS or ALLOW_CHOICES or ALLOW_WILDCARDS
commandstn: "<ppp:stn" [ _WHITESPACE /s|e|p\d/ ] _WHITESPACE? ">" content_negtag "<ppp:/stn>" vardescriptor_get.8: VARNAME [ /\[/ [ SIGNED_NUMBER | IDENTIFIER | separator_descriptor | /#/ ] /\]/ ]
commandstni: "<ppp:stn" _WHITESPACE /i\d/ _WHITESPACE? ">" separator_descriptor: /&/ STRING
vardescriptor_set.9: VARNAME [ /\[/ [ SIGNED_NUMBER | IDENTIFIER ] /\]/ ]
// command: if // conditions
commandif.2: commandif_if commandif_elif* commandif_else? "<ppp:/if>" ?condition: grouped_condition | ungrouped_condition
commandif_if: "<ppp:if" _WHITESPACE condition _WHITESPACE? ">" ifvalue ?ungrouped_condition: operation | basic_condition
commandif_elif: "<ppp:elif" _WHITESPACE condition _WHITESPACE? ">" ifvalue ?grouped_condition.8: "(" _WHITESPACE? condition _WHITESPACE? ")"
commandif_else: "<ppp:else" _WHITESPACE? ">" ifvalue ?basic_condition: comparison | truthy_operand
ifvalue: content ?operation: operation_not | operation_and | operation_or
operation_and: condition _WHITESPACE "and" _WHITESPACE condition
operation_or: condition _WHITESPACE "or" _WHITESPACE condition
operation_not: "not" ( ( _WHITESPACE ungrouped_condition ) | ( _WHITESPACE? grouped_condition ) )
?complexvalue: vardescriptor_get | SIMPLEVALUE
truthy_operand: vardescriptor_get
comparison: ( complexvalue | listvalue ) _WHITESPACE ( /not/ _WHITESPACE )? /eq|ne|lt|gt|le|ge|contains|in|any_in|contains_any/ _WHITESPACE ( complexvalue | listvalue )
listvalue.9: "(" ( _WHITESPACE? complexvalue ( _WHITESPACE? "," _WHITESPACE? complexvalue )* )? _WHITESPACE? ")"
//#endif
// conditions //#if ALLOW_COMMVARS
condition: conditionsimplevalue | conditionlistvalue | conditionnocomparison // command: stn (send to negative)
conditionnocomparison: ( /not/ _WHITESPACE )? IDENTIFIER commandstn: "<ppp:stn" [ _WHITESPACE /s|e|p\d/ ] _WHITESPACE? ">" content_negtag "<ppp:/stn>"
conditionsimplevalue: ( /not/ _WHITESPACE )? IDENTIFIER _WHITESPACE ( /not/ _WHITESPACE )? /eq|ne|gt|lt|ge|le|contains/ _WHITESPACE SIMPLEVALUE commandstni: "<ppp:stn" _WHITESPACE /i\d/ _WHITESPACE? ( "/>" | ">" )
conditionlistvalue: ( /not/ _WHITESPACE )? IDENTIFIER _WHITESPACE ( /not/ _WHITESPACE )? /contains|in/ _WHITESPACE listvalue
IDENTIFIER: CNAME
SIMPLEVALUE: STRING | NUMBER | BOOLEAN
listvalue: "(" _WHITESPACE? SIMPLEVALUE ( _WHITESPACE? "," _WHITESPACE? SIMPLEVALUE )* _WHITESPACE? ")"
// command: set // command: if
commandset: "<ppp:set" _WHITESPACE IDENTIFIER [ _WHITESPACE? /evaluate/ ] [ _WHITESPACE? /add/ ] _WHITESPACE? ">" content "<ppp:/set>" commandif.2: commandif_if commandif_elif* commandif_else? "<ppp:/if>"
commandif_if: "<ppp:if" _WHITESPACE condition _WHITESPACE? ">" ifvalue
commandif_elif: "<ppp:elif" _WHITESPACE condition _WHITESPACE? ">" ifvalue
commandif_else: "<ppp:else" _WHITESPACE? ">" ifvalue
ifvalue.3: content
// command: echo // command: set
commandecho: "<ppp:echo" _WHITESPACE IDENTIFIER _WHITESPACE? ">" [ content "<ppp:/echo>" ] commandset: "<ppp:set" _WHITESPACE vardescriptor_set [ commandsetmodifiers ] _WHITESPACE? ">" ( starredvalue | commandsetcontent ) "<ppp:/set>"
commandsetmodifiers: (_WHITESPACE /evaluate|ifundefined|add/ )+
?commandsetcontent.3: content
// variable set // variable set
variableset.2: "${" _WHITESPACE? IDENTIFIER _WHITESPACE? [ /\+/ ] "=" [ /!/ ] varvalue "}" //#if ALLOW_WILDCARDS
starredvalue.9: "*" ( wildcard | vardescriptor_get | listvalue )
//#else
starredvalue.9: "*" ( vardescriptor_get | listvalue )
//#endif
variableset.2: "${" _WHITESPACE? vardescriptor_set [ variablesetmodifiers ] _WHITESPACE? "=" [ /!/ ] ( starredvalue | varvalue ) "}"
variablesetmodifiers: /[+?!]+/
// variable use // command: echo
variableuse.2: "${" _WHITESPACE? IDENTIFIER _WHITESPACE? [ ":" varvalue ] "}" commandecho: "<ppp:echo" _WHITESPACE vardescriptor_get _WHITESPACE? ( ">" [ commandechodefault "<ppp:/echo>" ] | "/>" )
?commandechodefault.3: content
// variable use
variableuse.2: "${" _WHITESPACE? vardescriptor_get _WHITESPACE? [ ":" varvalue ] "}"
// command: ext
commandext: "<ppp:ext" _WHITESPACE commandexttype _WHITESPACE commandextid [ _WHITESPACE? commandextparams ] [ _WHITESPACE? commandextif ] _WHITESPACE? ( ">" [ commandexttriggers "<ppp:/ext>" ] | "/>" )
commandexttype: [/\$/] IDENTIFIER
?commandextid.2: STRING | CNAME
?commandextparams.2: STRING | SIGNED_NUMBER
?commandextif: "if" _WHITESPACE condition
?commandexttriggers.3: content
//#endif
varvalue: content_var varvalue: content_var
// wildcards //#if ALLOW_CHOICES or ALLOW_WILDCARDS
wildcard.2: "__" [ choicesoptions_sampler | ( choicesoptions _WHITESPACE? "$$" ) ] WILDCARD_NAME [ wc_filter ] [ wildcardvar ] "__" // choices parameters (for choices and wildcards)
wc_filter: /["']/ ( [ /#/ ] wc_filter_or | ( /#?\^/ WILDCARD_NAME ) ) /["']/ choicesoptions: [ choicesoptions_sampler ] [ _WHITESPACE? choicesoptions_flags ] [ _WHITESPACE? choicesoptions_range ] [ _WHITESPACE? choicesoptions_sep ]
wc_filter_or: wc_filter_and ( _WHITESPACE? "," _WHITESPACE? wc_filter_and )* choicesoptions_sampler: /[~@]/ // ~ for random, @ for cyclical
wc_filter_and: INDEX ( _WHITESPACE? "+" _WHITESPACE? INDEX )* choicesoptions_flags: /[ro]{1,2}/ // r for repeating, o for optional
wildcardvar: "(" _WHITESPACE? IDENTIFIER _WHITESPACE? "=" varvalue ")" choicesoptions_range: INT | ( [ INT ] _WHITESPACE? "-" _WHITESPACE? [ INT ] )
choicesoptions_sep: "$$" plain
// choices // choice options
choices.2: "{" [ choicesoptions_sampler | ( choicesoptions _WHITESPACE? "$$" ) ] choice ( "|" choice )* "}" choice: [ [ _WHITESPACE? choiceiscmd ] [ _WHITESPACE? choicelabels ] [ _WHITESPACE? choiceweight ] [ _WHITESPACE? choiceif ] _WHITESPACE? "::" ] choicevalue
choiceiscmd: /%/ // the option text is a special command
choicelabels: /["']/ IDENTIFIER ( _WHITESPACE? "," _WHITESPACE? IDENTIFIER )* /["']/
choiceweight: NUMBER
choiceif: "if" _WHITESPACE condition
choicevalue: content_choice
//#endif
choicesoptions: [ choicesoptions_sampler ] [ _WHITESPACE? choicesoptions_rep ] ( ( [ _WHITESPACE? choicesoptions_from ] "-" [ _WHITESPACE? choicesoptions_to ] ) | [ _WHITESPACE? choicesoptions_num ] ) [ _WHITESPACE? choicesoptions_sep ] //#if ALLOW_CHOICES
choicesoptions_sampler: /[~@]/ // ~ for random, @ for cyclical // choices construct
choicesoptions_rep: /r/ choices.2: "{" [ choicesoptions_sampler | ( choicesoptions _WHITESPACE? "$$" ) ] choice ( "|" choice )* "}"
choicesoptions_num: INT //#endif
choicesoptions_from: INT
choicesoptions_to: INT
choicesoptions_sep: "$$" plain
choice: [ [ _WHITESPACE? choicelabels ] [ _WHITESPACE? choiceweight ] [ _WHITESPACE? choiceif ] _WHITESPACE? "::" ] choicevalue //#if ALLOW_WILDCARDS
choicelabels: /["']/ IDENTIFIER ( _WHITESPACE? "," _WHITESPACE? IDENTIFIER )* /["']/ // wildcard definition options
choiceweight: NUMBER wcdefoptions: [ choicesoptions_sampler ] [ _WHITESPACE? choicesoptions_flags ] [ _WHITESPACE? choicesoptions_range ] [ _WHITESPACE? wcdescription ] [ _WHITESPACE? choicesoptions_sep ]
choiceif: "if" _WHITESPACE condition wcdescription: STRING
choicevalue: content_choice
// wildcards construct
wildcard.2: "__" [ choicesoptions_sampler | ( choicesoptions _WHITESPACE? "$$" ) ] wildcard_name [ wc_filter ] [ wildcardvar ] "__"
wc_filter_nums: IDENTIFIER | INDEX | (INDEX /-/ INDEX)
//#if ALLOW_COMMVARS
wildcard_name.2: ( WC_NAME_PLAIN_START | variableuse | commandecho ) ( WC_NAME_PLAIN | variableuse | commandecho )*
wc_filter_simple: wc_filter_nums | variableuse
//#else
wildcard_name.2: ( WC_NAME_PLAIN_START) WC_NAME_PLAIN*
wc_filter_simple: wc_filter_nums
//#endif
wc_filter: /["']/ ( [ /#/ ] wc_filter_or | ( /#?\^/ wildcard_name ) ) /["']/
wc_filter_or: wc_filter_and ( _WHITESPACE? "," _WHITESPACE? wc_filter_and )*
wc_filter_and: wc_filter_simple ( _WHITESPACE? "+" _WHITESPACE? wc_filter_simple )*
wildcardvar.7: "(" _WHITESPACE? vardescriptor_get _WHITESPACE? "=" varvalue ")"
//#if ALLOW_COMMVARS
// command: setwcdeffilter
commandsetwcdeffilter: "<ppp:setwcdeffilter" _WHITESPACE wc_name_str [_WHITESPACE wc_filter_limited ] _WHITESPACE? "/>"
wc_name_str: /["']/ wildcard_name /["']/
wc_filter_limited: /["']/ wc_filter_or /["']/
//#endif
//#endif
File diff suppressed because one or more lines are too long
Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 20 KiB

+22
View File
@@ -0,0 +1,22 @@
{
"folders": [
{
"name": "sd-webui-prompt-postprocessor",
"path": "."
}
],
"settings": {
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter"
},
"yaml.schemaStore.enable": false,
"cSpell.enabledFiletypes": [
"lark"
],
"powershell.cwd": "sd-webui-prompt-postprocessor"
},
"launch": {
"version": "0.2.0",
"configurations": []
}
}
+852 -1462
View File
File diff suppressed because it is too large Load Diff
+9 -2
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
class PPPLRUCache: class PPPLRUCache:
ProcessInput = Tuple[int, int, str, str] # (seed, wildcards_hash, positive_prompt, negative_prompt) ProcessInput = Tuple[int, int, str, str] # (env_hash, seed, positive_prompt, negative_prompt)
ProcessResult = Tuple[str, str] # (positive_prompt, negative_prompt) ProcessResult = Tuple[str, str] # (positive_prompt, negative_prompt)
def __init__(self, capacity: int): def __init__(self, capacity: int, logger: Logger = None, debug_level: DEBUG_LEVEL = DEBUG_LEVEL.none):
self.cache = OrderedDict() self.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__()}")
+279
View File
@@ -0,0 +1,279 @@
"""Pydantic models for the PPP configuration file structure (ppp_config.yaml)."""
from dataclasses import dataclass, field
from logging import Logger
import re
from enum import Enum
from typing import Literal, Optional
from lark import Lark
from pydantic import BaseModel, ConfigDict, Field, field_validator, model_validator
from ppp_logging import DEBUG_LEVEL
from ppp_wildcards import PPPWildcards
from ppp_enmappings import PPPExtraNetworkMappings
from ppp_variables import VariableRepository
class SUPPORTED_APPS(Enum):
comfyui = "comfyui"
a1111 = "a1111"
forge = "forge"
reforge = "reforge"
sdnext = "sdnext"
tests = "tests" # for testing purposes only, not a real app
SUPPORTED_APPS_NAMES = {
SUPPORTED_APPS.comfyui: "ComfyUI",
SUPPORTED_APPS.sdnext: "SD.Next",
SUPPORTED_APPS.forge: "Forge",
SUPPORTED_APPS.reforge: "reForge",
SUPPORTED_APPS.a1111: "A1111 (or compatible)",
SUPPORTED_APPS.tests: "Tests",
}
class IFWILDCARDS_CHOICES(Enum):
ignore = "ignore"
remove = "remove"
warn = "warn"
stop = "stop"
class ONWARNING_CHOICES(Enum):
warn = "warn"
stop = "stop"
# ------------------- Host configuration -------------------
AttentionOption = Literal["ok", "parentheses", "disable", "remove", "error"]
SchedulingOption = Literal["ok", "before", "after", "first", "remove", "error"]
AlternationOption = Literal["ok", "first", "remove", "error"]
AndOption = Literal["ok", "eol", "comma", "remove", "error"]
BreakOption = Literal["ok", "eol", "comma", "remove", "error"]
class HostConfig(BaseModel):
"""Configuration for a specific host application."""
model_config = ConfigDict(populate_by_name=True, extra="forbid")
attention: AttentionOption = "ok"
scheduling: SchedulingOption = "ok"
alternation: AlternationOption = "ok"
and_: AndOption = Field("ok", alias="and")
break_: BreakOption = Field("ok", alias="break")
# ------------------- Model detection -------------------
class ModelDetectConfig(BaseModel):
"""Detection configuration for a specific host when loading a model."""
model_config = ConfigDict(populate_by_name=True)
class_: Optional[list[str]] = Field(None, alias="class")
property: Optional[str] = None
@model_validator(mode="after")
def check_class_or_property(self) -> "ModelDetectConfig":
if self.class_ is None and self.property is None:
raise ValueError("Either 'class' or 'property' must be specified")
return self
# ------------------- Variant find_in_filename -------------------
class FindInFilenamePattern(BaseModel):
"""A regex pattern with optional flags used to identify a model variant in the filename."""
regex: str
flags: int = 0
@field_validator("flags", mode="before")
@classmethod
def parse_flags(cls, v: object) -> int:
if isinstance(v, int):
return v
if isinstance(v, list):
flag_value = 0
for flag in v:
if not isinstance(flag, str) or not hasattr(re, flag):
raise ValueError(f"Invalid regex flag '{flag}'")
flag_value |= getattr(re, flag)
return flag_value
raise ValueError(f"Expected int or list of flag-name strings, got {type(v).__name__}")
@model_validator(mode="after")
def validate_regex(self) -> "FindInFilenamePattern":
try:
re.compile(self.regex, self.flags)
except re.error as exc:
raise ValueError(f"Invalid regex pattern '{self.regex}': {exc}") from exc
return self
class VariantConfig(BaseModel):
"""Configuration for a specific model variant."""
find_in_filename: list[FindInFilenamePattern]
@field_validator("find_in_filename", mode="before")
@classmethod
def normalize_find_in_filename(cls, v: object) -> list:
"""Normalize str / dict / list input to always be a list of FindInFilenamePattern-compatible dicts."""
if isinstance(v, str):
return [{"regex": v, "flags": re.IGNORECASE}]
if isinstance(v, dict):
return [v]
if isinstance(v, list):
normalized = []
for item in v:
if isinstance(item, str):
normalized.append({"regex": item, "flags": re.IGNORECASE})
elif isinstance(item, dict):
normalized.append(item)
else:
raise ValueError(f"Expected str or dict in 'find_in_filename' list, got {type(item).__name__}")
return normalized
raise ValueError(f"Expected str, dict, or list for 'find_in_filename', got {type(v).__name__}")
# ------------------- Model configuration -------------------
class ModelConfig(BaseModel):
"""Configuration for a supported base model."""
detect: Optional[dict[str, Optional[ModelDetectConfig]]] = None
variants: Optional[dict[str, VariantConfig]] = None
@model_validator(mode="after")
def check_detect_or_variants(self) -> "ModelConfig":
if self.detect is None and self.variants is None:
raise ValueError("At least one of 'detect' or 'variants' must be specified")
return self
# ------------------- Top-level configuration -------------------
class PPPConfig(BaseModel):
"""Top-level PPP configuration structure matching ppp_config.yaml."""
hosts: Optional[dict[str, Optional[HostConfig]]] = None
models: Optional[dict[str, Optional[ModelConfig | None]]] = None
@model_validator(mode="after")
def check_hosts_or_models(self) -> "PPPConfig":
if self.hosts is None and self.models is None:
raise ValueError("At least one of 'hosts' or 'models' must be specified")
return self
# ------------------- State object -------------------
@dataclass(frozen=True)
class PPPStateOptions:
"""Options that can be set for prompt processing."""
debug_level: DEBUG_LEVEL = DEBUG_LEVEL.minimal
on_warning: ONWARNING_CHOICES = ONWARNING_CHOICES.warn
process_wildcards: bool = True
keep_choices_order: bool = True
choice_separator: str = ", "
if_wildcards: IFWILDCARDS_CHOICES = IFWILDCARDS_CHOICES.stop
stn_ignore_repeats: bool = True
stn_separator: str = ", "
cup_do_cleanup: bool = True # whether to do cleanup at all (if False, all other cleanup options are ignored)
cup_cleanup_variables: bool = True
cup_extra_spaces: bool = True
cup_empty_constructs: bool = True
cup_extra_separators: bool = True
cup_extra_separators2: bool = True
cup_extra_separators_include_eol: bool = False
cup_breaks: bool = False
cup_breaks_eol: bool = False
cup_ands: bool = False
cup_ands_eol: bool = False
cup_extranetwork_tags: bool = False
cup_merge_attention: bool = True
cup_remove_extranetwork_tags: bool = False
strict_operators: bool = True
do_combinatorial: bool = False
combinatorial_shuffle: bool = False
combinatorial_limit: int = 100 # 0 = no limit
def __post_init__(self):
if not self.cup_do_cleanup:
object.__setattr__(self, "cup_cleanup_variables", False)
object.__setattr__(self, "cup_extra_spaces", False)
object.__setattr__(self, "cup_empty_constructs", False)
object.__setattr__(self, "cup_extra_separators", False)
object.__setattr__(self, "cup_extra_separators2", False)
object.__setattr__(self, "cup_extra_separators_include_eol", False)
object.__setattr__(self, "cup_breaks", False)
object.__setattr__(self, "cup_breaks_eol", False)
object.__setattr__(self, "cup_ands", False)
object.__setattr__(self, "cup_ands_eol", False)
object.__setattr__(self, "cup_extranetwork_tags", False)
object.__setattr__(self, "cup_merge_attention", False)
object.__setattr__(self, "cup_remove_extranetwork_tags", False)
class CyclicalSamplerState:
"""Maintains the cycling position for '@' choice samplers across process_prompt calls."""
def __init__(self):
self.current_path: list[int] = []
self.last_trace: list[int] = []
self.last_prompt_pair: tuple[str, str] | None = None
def advance(self):
"""Advance to the next combination, cycling back to the start when all are exhausted."""
if not self.last_trace:
self.current_path = []
return
path = list(self.current_path)
while len(path) < len(self.last_trace):
path.append(0)
# Mixed-radix increment: least significant position is last.
for i in range(len(path) - 1, -1, -1):
path[i] += 1
if path[i] < self.last_trace[i]:
break
path[i] = 0
self.current_path = path
def reset(self):
"""Reset the cyclical state to the beginning."""
self.current_path = []
self.last_trace = []
self.last_prompt_pair = None
@dataclass(frozen=True)
class PPPState:
"""State object passed to various PPP components during prompt processing."""
logger: Logger
host_config: HostConfig = field(default_factory=HostConfig)
options: PPPStateOptions = field(default_factory=PPPStateOptions)
variables: VariableRepository = field(default_factory=VariableRepository)
wildcards_obj: PPPWildcards = field(default_factory=PPPWildcards)
extranetwork_mappings_obj: PPPExtraNetworkMappings = field(default_factory=PPPExtraNetworkMappings)
parsers: dict[str, Lark] = field(default_factory=dict)
cyclical_state: CyclicalSamplerState = field(default_factory=CyclicalSamplerState)
class PPPInterrupt(Exception):
"""
Custom exception to handle interruptions in the PromptPostProcessor.
This exception can be raised to stop the processing of prompts.
"""
def __init__(self, message: str = "Processing interrupted.", pos_prefix: str = "", neg_prefix: str = ""):
super().__init__(message)
self.message = message
self.pos_prefix = pos_prefix
self.neg_prefix = neg_prefix
+806 -299
View File
File diff suppressed because it is too large Load Diff
+198
View File
@@ -0,0 +1,198 @@
import ast
import logging
import os
import re
import textwrap
import time
import lark
from ppp_logging import log
from ppp_classes import ONWARNING_CHOICES, PPPInterrupt, PPPState
from ppp_utils import escape_single_quotes, format_output
def parse_prompt(
state: PPPState,
prompt_description: str,
prompt: str,
parser: lark.Lark,
raise_parsing_error: bool = False,
):
"""
Parses a prompt using the specified parser.
Args:
prompt_description (str): The description of the prompt.
prompt (str): The prompt to be parsed.
parser (lark.Lark): The parser to be used.
raise_parsing_error (bool): Whether to raise a parsing error.
Returns:
Tree: The parsed prompt.
"""
t1 = time.monotonic_ns()
parsed_prompt = None
try:
log(
state.logger,
state.options.debug_level,
logging.DEBUG,
f"Parsing {prompt_description}: '{escape_single_quotes(prompt)}'",
)
parsed_prompt = parser.parse(prompt)
# we store the contents so we can use them later even if the meta position is not valid anymore
if isinstance(parsed_prompt, lark.Tree):
for n in parsed_prompt.iter_subtrees():
if isinstance(n, lark.Tree):
if n.meta.empty:
n.meta.content = ""
else:
n.meta.content = prompt[n.meta.start_pos : n.meta.end_pos]
except lark.exceptions.UnexpectedInput:
if raise_parsing_error:
raise
log(
state.logger,
state.options.debug_level,
logging.ERROR,
f"Parsing failed on prompt!: {escape_single_quotes(prompt)}",
)
t2 = time.monotonic_ns()
log(
state.logger,
state.options.debug_level,
logging.DEBUG,
f"Parse {prompt_description} time: {(t2 - t1) / 1_000_000_000:.3f} seconds",
)
if parsed_prompt:
log(
state.logger,
state.options.debug_level,
logging.DEBUG,
"Tree:\n"
+ textwrap.indent(
re.sub(r"\n$", "", (parsed_prompt.pretty() if isinstance(parsed_prompt, lark.Tree) else parsed_prompt)),
" ",
),
formatted=False,
)
return parsed_prompt
def warn_or_stop(state: PPPState, is_negative: bool, message: str, e: Exception = None):
INVALID_CONTENT_STOP = "INVALID CONTENT! {0}\nBREAK "
if state.options.on_warning == ONWARNING_CHOICES.stop:
raise PPPInterrupt(
message,
INVALID_CONTENT_STOP.format(message) if not is_negative else "",
INVALID_CONTENT_STOP.format(message) if is_negative else "",
) from e
log(state.logger, state.options.debug_level, logging.WARNING, format_output(message))
def load_grammar() -> str:
# Process with lark (debug with https://www.lark-parser.org/ide/)
grammar_filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), "grammar.lark")
with open(grammar_filename, "r", encoding="utf-8") as file:
grammar_content = file.read()
return grammar_content
def preprocess_grammar(grammar_content: str, options: dict[str, bool], logger: logging.Logger, debug_level: int) -> str:
"""
Preprocesses the grammar content to handle conditional compilation directives.
Args:
grammar_content (str): The raw grammar content.
options (dict[str,bool]): Options for preprocessing.
logger (logging.Logger): The logger object.
debug_level (int): The debug level for logging.
Returns:
str: The preprocessed grammar content.
"""
lines = grammar_content.split("\n")
result_lines = []
skip_current_block = []
all_blocks_skipped = []
def eval_bool_expr(expr: str, constants: dict[str, bool]) -> bool:
"""
Evaluates a boolean expression using known constants.
Supports: and, or, not, parentheses, and named constants.
Args:
expr (str): The boolean expression to evaluate.
constants (dict[str, bool]): A dictionary of constant values.
Returns:
bool: The result of the evaluated expression.
"""
tree = ast.parse(expr, mode="eval")
def _eval(node) -> bool:
if isinstance(node, ast.Expression):
return _eval(node.body)
if isinstance(node, ast.BoolOp):
if isinstance(node.op, ast.And):
return all(_eval(v) for v in node.values)
if isinstance(node.op, ast.Or):
return any(_eval(v) for v in node.values)
if isinstance(node, ast.UnaryOp) and isinstance(node.op, ast.Not):
return not _eval(node.operand)
if isinstance(node, ast.Name):
return bool(constants[node.id]) # raises KeyError for unknown names
if isinstance(node, ast.Constant) and isinstance(node.value, bool):
return node.value
raise ValueError(f"Unsupported construct: {ast.dump(node)}")
return _eval(tree)
for line in lines:
stripped_line = line.strip()
if stripped_line.startswith("//#if"):
# Extract condition from the #if directive
conditions = stripped_line[5:].strip()
# Evaluate the conditions
skip_current_block.append(not eval_bool_expr(conditions, options))
all_blocks_skipped.append(skip_current_block[-1])
elif stripped_line.startswith("//#elif"):
if not skip_current_block:
log(logger, debug_level, logging.WARNING, "Unmatched //#elif directive found in grammar content.")
elif all_blocks_skipped[-1]:
# Extract condition from the #elif directive
conditions = stripped_line[7:].strip()
# Evaluate the conditions
skip_current_block[-1] = not eval_bool_expr(conditions, options)
if not skip_current_block[-1]:
all_blocks_skipped[-1] = False
else:
skip_current_block[-1] = True
elif stripped_line.startswith("//#else"):
if not skip_current_block:
log(logger, debug_level, logging.WARNING, "Unmatched //#else directive found in grammar content.")
elif all_blocks_skipped[-1]:
skip_current_block[-1] = False
else:
skip_current_block[-1] = True
elif stripped_line.startswith("//#endif"):
if not skip_current_block:
log(logger, debug_level, logging.WARNING, "Unmatched //#endif directive found in grammar content.")
else:
skip_current_block.pop()
all_blocks_skipped.pop()
elif stripped_line.startswith("//#"):
log(
logger,
debug_level,
logging.WARNING,
f"Unrecognized directive found in grammar content: {stripped_line}",
)
elif not any(skip_current_block):
# Include the line if we're not skipping any current block
result_lines.append(stripped_line)
# Check for unclosed blocks at the end
if skip_current_block:
raise PPPInterrupt(
f"Found {len(skip_current_block)} unclosed conditional directive(s) at the end of the grammar file"
)
return "\n".join(result_lines)
+260
View File
@@ -0,0 +1,260 @@
# Prompt Post-Processor configuration file.
# If you want to change any of these options make first a copy of this file as "ppp_config.yaml" and edit that file.
# In ComfyUI the file should be in the user folder.
# This defaults file will be overwritten when updating the extension, but your custom "ppp_config.yaml" should not be affected.
# Options for supported host applications
hosts:
# For each host, define how to handle some operations:
# attention: how to handle attention operators
# - ok: leave as is
# - parentheses: convert to parentheses-based attention
# - disable: remove attention operators
# - remove: remove the whole block, including the text inside
# - error: raise an error
# scheduling: how to handle scheduling operators
# - ok: leave as is
# - before: replace with the first scheduling text
# - after: replace with the second scheduling text
# - first: replace with the first scheduling text if present, otherwise with the second scheduling text
# - remove: remove the whole block, including the text inside
# - error: raise an error
# alternation: how to handle alternation operators
# - ok: leave as is
# - first: replace with the first alternation text
# - remove: remove the whole block, including the text inside
# - error: raise an error
# and: how to handle "AND" operators
# - ok: leave as is
# - eol: replace with end-of-line (newline)
# - comma: replace with comma
# - remove: remove the operator
# - error: raise an error
# break: how to handle "BREAK" operators
# - ok: leave as is
# - eol: replace with end-of-line (newline)
# - comma: replace with comma
# - remove: remove the operator
# - error: raise an error
# Options in the user file override all these options by host (individual options are not inherited and if missing will use defaults in code).
a1111:
attention: ok
scheduling: ok
alternation: ok
and: ok
break: ok
forge:
attention: ok
scheduling: ok
alternation: ok
and: ok
break: ok
reforge:
attention: ok
scheduling: ok
alternation: ok
and: ok
break: ok
sdnext:
attention: ok
scheduling: ok
alternation: ok
and: ok
break: ok
comfyui:
# ComfyUI only supports parentheses-based attention
attention: parentheses
# by default ComfyUI does not support scheduling, alternation, AND or BREAK operators, but you may be able to use them with a custom text encoder node
scheduling: error
alternation: error
and: comma
break: comma
# Supported base models, variants, and options
# Check supported models for each host in:
# A1111:
# Forge: repositories\huggingface_guess\huggingface_guess\model_list.py
# reForge:
# SD.Next:
# ComfyUI:
models:
# We define supported models and how we detect them in each host application (by class or by a known boolean property, or null for not supported).
# We can also define here variants and some options.
# For each model, detect options in the user file replace these ones by host, and variants are fully replaced (so if you want to add to the default ones you need to copy them to your file).
sd1: # Stable Diffusion 1
detect:
a1111: { property: "is_sd1" }
forge: { property: "is_sd1" }
reforge: { property: "is_sd1" }
sdnext: { class: ["LatentDiffusion", "StableDiffusionPipeline"] } # LatentDiffusion is for the original backend, StableDiffusionPipeline is for the diffusers backend; cannot differentiate SD1 and SD2, we set both to True
comfyui: { class: ["SD15", "SD15_instructpix2pix"] }
sd2: # Stable Diffusion 2
detect:
a1111: { property: "is_sd2" }
forge: { property: "is_sd2" }
reforge: { property: "is_sd2" }
sdnext: { class: ["LatentDiffusion", "StableDiffusionPipeline"] } # cannot differentiate SD1 and SD2, we set both to True; LatentDiffusion is for the original backend, StableDiffusionPipeline is for the diffusers backend
comfyui: { class: ["SD20", "SD21UnclipL", "SD21UnclipH", "LotusD"] }
ssd: # Segmind Stable Diffusion 1B
detect:
a1111: { property: "is_ssd" }
forge: null
reforge: { property: "is_ssd" }
sdnext: null
comfyui: { class: ["SSD1B"]}
sdxl: # Stable Diffusion XL
detect:
a1111: { property: "is_sdxl" }
forge: { property: "is_sdxl" }
reforge: { property: "is_sdxl" }
sdnext: { class: ["StableDiffusionXLPipeline"] }
comfyui: { class: ["SDXL", "SDXLRefiner", "SDXL_instructpix2pix", "Segmind_Vega", "KOALA_700M", "KOALA_1B"] }
variants:
# At this level goes the name of the defined variants
# find_in_filename: regex patterns as a str or regex/flags dict or list of them to identify the variant in the model filename.
# If specified as a string, it is considered a regex with the IGNORECASE flag;
# If specified as a dict, it should have "regex" and optional "flags" keys;
# If specified as a list, it should be a list of dicts or strings
pony: { find_in_filename: "(?:pony|pny|pdxl)" }
illustrious: { find_in_filename: { regex: "(?:illustrious|illust|ilxl)", flags: ["IGNORECASE"] } }
sd3: # Stable Diffusion 3
detect:
a1111: { property: "is_sd3" }
forge: { property: "is_sd3", class: ["SD3"] }
reforge: { property: "is_sd3" }
sdnext: { class: ["StableDiffusion3Pipeline"] }
comfyui: { class: ["SD3"] }
flux: # Flux 1
detect:
a1111: null
forge: { class: ["Flux", "FluxSchnell"] }
reforge: { class: ["Flux", "FluxInpaint", "FluxSchnell"] }
sdnext: { class: ["FluxPipeline"] }
comfyui: { class: ["Flux", "FluxInpaint", "FluxSchnell"] }
auraflow: # AuraFlow
detect:
a1111: null
forge: null
reforge: { class: ["AuraFlow"] }
sdnext: { class: ["AuraFlowPipeline"] }
comfyui: { class: ["AuraFlow"] }
pixart: # PixArt
detect:
a1111: null
forge: null
reforge: { class: ["PixArtAlpha", "PixArtSigma"] }
sdnext: { class: ["PixArtAlphaPipeline"] }
comfyui: { class: ["PixArtAlpha", "PixArtSigma"] }
lumina2: # Lumina2 & ZImage
detect:
a1111: null
forge: null
reforge: { class: ["Lumina2", "ZImage"] }
sdnext: null
comfyui: { class: ["Lumina2", "ZImage"] }
ltxv: # LTXV
detect:
a1111: null
forge: null
reforge: null
sdnext: null
comfyui: { class: ["LTXV", "LTXAV"] }
cosmos: # Cosmos
detect:
a1111: null
forge: null
reforge: { class: ["CosmosT2V", "CosmosI2V"] }
sdnext: null
comfyui: { class: ["CosmosT2V", "CosmosI2V"] }
cosmospredict2: # CosmosPredict2
detect:
a1111: null
forge: null
reforge: { class: ["CosmosT2IPredict2", "CosmosI2VPredict2"] }
sdnext: null
comfyui: { class: ["CosmosT2IPredict2", "CosmosI2VPredict2"] }
genmomochi: # GenmoMochi
detect:
a1111: null
forge: null
reforge: { class: ["GenmoMochi"] }
sdnext: null
comfyui: { class: ["GenmoMochi"] }
hunyuan: # Hunyuan
detect:
a1111: null
forge: null
reforge: { class: ["HunyuanDiT", "HunyuanDiT1"] }
sdnext: null
comfyui: { class: ["HunyuanDiT", "HunyuanDiT1"] }
hunyuanvideo: # HunyuanVideo
detect:
a1111: null
forge: null
reforge: { class: ["HunyuanVideo", "HunyuanVideoI2V", "HunyuanVideoSkyreelsI2V", "HunyuanImage21", "HunyuanImage21Refiner", "HunyuanVideo15", "HunyuanVideo15_SR_Distilled"] }
sdnext: null
comfyui: { class: ["HunyuanVideo", "HunyuanVideoI2V", "HunyuanVideoSkyreelsI2V", "HunyuanImage21", "HunyuanImage21Refiner", "HunyuanVideo15", "HunyuanVideo15_SR_Distilled"] }
hunyuan3d: # Hunyuan3D
detect:
a1111: null
forge: null
reforge: { class: ["Hunyuan3Dv2", "Hunyuan3Dv2_1", "Hunyuan3Dv2mini"] }
sdnext: null
comfyui: { class: ["Hunyuan3Dv2", "Hunyuan3Dv2_1", "Hunyuan3Dv2mini"] }
wan: # Wan21 & Wan22
detect:
a1111: null
forge: { class: ["WAN21_T2V", "WAN21_I2V"] }
reforge: { class: ["WAN21_T2V", "WAN21_I2V", "WAN21_FunControl2V", "WAN21_Camera", "WAN22_Camera", "WAN21_Vace", "WAN21_HuMo", "WAN22_S2V", "WAN22_Animate", "WAN22_T2V"] }
sdnext: null
comfyui: { class: ["WAN21_T2V", "WAN21_I2V", "WAN21_FunControl2V", "WAN21_Camera", "WAN22_Camera", "WAN21_Vace", "WAN21_HuMo", "WAN22_S2V", "WAN22_Animate", "WAN22_T2V"] }
hidream: # HiDream
detect:
a1111: null
forge: null
reforge: { class: ["HiDream"] }
sdnext: null
comfyui: { class: ["HiDream"] }
qwenimage: # QwenImage & QwenImageEdit
detect:
a1111: null
forge: { class: ["QwenImage"] }
reforge: null
sdnext: null
comfyui: { class: ["QwenImage"] }
chroma: # Chroma
detect:
a1111: null
forge: { class: ["Chroma"] }
reforge: { class: ["Chroma", "ChromaRadiance"] }
sdnext: null
comfyui: { class: ["Chroma", "ChromaRadiance"] }
omnigen2: # Omnigen 2
detect:
a1111: null
forge: null
reforge: { class: ["Omnigen2"] }
sdnext: null
comfyui: { class: ["Omnigen2"] }
flux2: # Flux 2
detect:
a1111: null
forge: null
reforge: null
sdnext: null
comfyui: { class: ["Flux2"] }
kandinsky5: # Kandinsky 5
detect:
a1111: null
forge: null
reforge: null
sdnext: null
comfyui: { class: ["Kandinsky5", "Kandinsky5Image"] }
anima: # Anima
detect:
a1111: null
forge: null
reforge: null
sdnext: null
comfyui: { class: ["Anima"] }
+320
View File
@@ -0,0 +1,320 @@
import os
from pathlib import Path
from typing import Optional
import logging
import yaml
from ppp_logging import DEBUG_LEVEL, log
from ppp_utils import deep_freeze, escape_single_quotes
class PPPENMappingVariant:
"""
A class to represent a variant of an extra network mapping.
Attributes:
condition (str): The condition for the variant.
name (str): The name of the variant.
parameters (float|str): The parameters for the variant.
triggers (list[str]): The triggers for the variant.
weight (float): The weight for the variant when multiple variants apply.
"""
def __init__(self, condition: str, name: str, parameters: float | str, triggers: list[str], weight: float):
self.condition: str = condition
self.name: str = name
self.parameters: float | str = parameters
self.triggers: list[str] = triggers
self.weight: float = weight
class PPPENMapping:
"""
A extra network mapping object.
Attributes:
kind (str): The kind of the extra network.
name (str): The name of the extra network mapping.
file (str): The path to the file where the extranetwork mapping is defined.
variants (list[PPPENMappingVariant]): The processed variants of the extranetwork mapping.
"""
def __init__(self, fullpath: str, kind: str, name: str, variants: list[dict]):
self.file: str = fullpath
self.kind: str = kind
self.name: str = name
self.variants: list[PPPENMappingVariant] = [
PPPENMappingVariant(
**{**{"condition": None, "name": None, "parameters": None, "triggers": None, "weight": 1.0}, **v}
)
for v in variants
]
def __hash__(self) -> int:
t = (self.kind, self.name, deep_freeze(self.variants))
return hash(t)
def __sizeof__(self):
return self.kind.__sizeof__() + self.name.__sizeof__() + self.file.__sizeof__() + self.variants.__sizeof__()
class PPPExtraNetworkMappings:
"""
A class to manage extra network mappings.
Attributes:
extranetwork_maps (dict[str, PPPENMapping]): The extra network mappings.
"""
DEFAULT_ENMAPPINGS_FOLDER = "extranetworkmappings"
LOCALINPUT_FILENAME = R"//INPUT\\"
def __init__(self, logger=None):
self.__logger: logging.Logger = logger
self.__debug_level = DEBUG_LEVEL.none
self.__enmappings_folders = []
self.__enmappings_files = {}
self.extranetwork_mappings: dict[str, PPPENMapping] = {}
self.cached_mappings = {}
def __hash__(self) -> int:
return hash(deep_freeze(self.extranetwork_mappings))
def __sizeof__(self):
return (
self.extranetwork_mappings.__sizeof__()
+ self.__enmappings_folders.__sizeof__()
+ self.__enmappings_files.__sizeof__()
+ self.cached_mappings.__sizeof__()
)
def refresh_extranetwork_mappings(
self, debug_level: DEBUG_LEVEL, enmappings_folders: Optional[list[str]], enmappings_input: str = None
):
"""
Initialize the extra network mappings.
"""
self.__debug_level = debug_level
self.__enmappings_folders = enmappings_folders or []
# log(self.__logger, self.__debug_level, logging.INFO, "Refreshing extra network mappings...")
# t1 = time.monotonic_ns()
self.cached_mappings = {}
for fullpath in list(self.__enmappings_files.keys()):
if fullpath != self.LOCALINPUT_FILENAME:
path = os.path.dirname(fullpath)
if not os.path.exists(fullpath) or not any(
Path(path).is_relative_to(folder) for folder in self.__enmappings_folders
):
self.__remove_extranetwork_mappings_from_path(fullpath)
elif enmappings_input is None:
self.__remove_extranetwork_mappings_from_path(fullpath)
if enmappings_folders is not None or enmappings_input is not None:
if enmappings_folders is not None:
for f in self.__enmappings_folders:
self.__get_extranetwork_mappings_in_path(f)
if enmappings_input is not None:
self.__get_extranetwork_mappings_in_input(enmappings_input)
else:
self.extranetwork_mappings = {}
self.__enmappings_files = {}
# t2 = time.monotonic_ns()
# log(self.__logger, self.__debug_level, logging.INFO, f"Extra network mappings refresh time: {(t2 - t1) / 1_000_000_000:.3f} seconds")
# def get_extranetwork_mappings(self, key: str) -> list[PPPENMapping]:
# """
# Get all extra network mappings that match a key.
#
# Args:
# key (str): The key to match (kind:name).
#
# Returns:
# list: A list of all extra network mappings that match the key.
# """
# keys = sorted(fnmatch.filter(self.extranetwork_mappings.keys(), key))
# return [self.extranetwork_mappings[k] for k in keys]
def __remove_extranetwork_mappings_from_path(self, full_path: str, debug=True):
"""
Clear all extra network mappings 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.__enmappings_files.get(full_path, None) # a time or a hash
if debug and last_modified_cached is not None:
if full_path == self.LOCALINPUT_FILENAME:
log(self.__logger, self.__debug_level, logging.DEBUG, "Removing extra network mappings from input")
else:
log(
self.__logger,
self.__debug_level,
logging.DEBUG,
f"Removing extra network mappings from file: {full_path}",
)
if full_path in self.__enmappings_files.keys():
del self.__enmappings_files[full_path]
for key in list(self.extranetwork_mappings.keys()):
if self.extranetwork_mappings[key].file == full_path:
del self.extranetwork_mappings[key]
def __get_extranetwork_mappings_in_file(self, full_path: str):
"""
Get all extra network mappings in a file.
Args:
full_path (str): The path to the file.
"""
last_modified = os.path.getmtime(full_path)
last_modified_cached = self.__enmappings_files.get(full_path, None)
if last_modified_cached is not None and last_modified == self.__enmappings_files[full_path]:
return
filename = os.path.basename(full_path)
_, extension = os.path.splitext(filename)
if extension not in (".yaml", ".yml", ".json"):
return
self.__remove_extranetwork_mappings_from_path(full_path, False)
if last_modified_cached is not None:
log(
self.__logger,
self.__debug_level,
logging.DEBUG,
f"Updating extra network mappings from file: {full_path}",
)
self.__get_extranetwork_mappings_in_structured_file(full_path)
self.__enmappings_files[full_path] = last_modified
def __get_extranetwork_mappings_in_input(self, enmappings_input: str):
"""
Get all extra network mappings in the string.
Args:
enmappings_input (str): The input string containing extra network mappings in yaml format.
"""
new_h = hash(enmappings_input)
h = self.__enmappings_files.get(self.LOCALINPUT_FILENAME, None)
if h == new_h:
return
self.__remove_extranetwork_mappings_from_path(self.LOCALINPUT_FILENAME, False)
if h is not None:
log(self.__logger, self.__debug_level, logging.DEBUG, "Updating extra network mappings from input")
enmappings_input = enmappings_input.strip()
if enmappings_input != "":
try:
content = yaml.safe_load(enmappings_input)
except yaml.YAMLError as e:
log(
self.__logger,
self.__debug_level,
logging.WARNING,
f"Invalid format for input extra network mappings: {e}",
)
return
if content is not None:
self.__add_extranetwork_mapping(content, self.LOCALINPUT_FILENAME)
self.__enmappings_files[self.LOCALINPUT_FILENAME] = new_h
def __add_extranetwork_mapping(self, content: dict[str, dict[str, list[dict]]], full_path: str):
"""
Add an extra network mapping to the extra network mappings dictionary.
Args:
content (object): The content of the extra network mapping.
full_path (str): The path to the file that contains it.
"""
if not isinstance(content, dict):
log(
self.__logger,
self.__debug_level,
logging.WARNING,
f"Invalid extra network mapping in file '{escape_single_quotes(full_path)}'!",
)
return
for kind, maps in content.items():
if not isinstance(maps, dict):
log(
self.__logger,
self.__debug_level,
logging.WARNING,
f"Invalid extra network mapping definition for '{escape_single_quotes(kind)}:*' in file '{escape_single_quotes(full_path)}'!",
)
else:
for name, variants in maps.items():
key = f"{kind}:{name}"
if not isinstance(variants, list):
log(
self.__logger,
self.__debug_level,
logging.WARNING,
f"Invalid extra network mapping definition for '{escape_single_quotes(key)}' in file '{escape_single_quotes(full_path)}'!",
)
elif self.extranetwork_mappings.get(key, None) is not None:
log(
self.__logger,
self.__debug_level,
logging.WARNING,
f"Duplicate extra network mapping '{escape_single_quotes(key)}' in file '{escape_single_quotes(full_path)}' and '{escape_single_quotes(self.extranetwork_mappings[key].file)}'!",
)
elif not isinstance(variants, list) or not all(isinstance(v, dict) for v in variants):
log(
self.__logger,
self.__debug_level,
logging.WARNING,
f"Invalid extra network mapping definition for '{escape_single_quotes(key)}' in file '{escape_single_quotes(full_path)}'!",
)
else:
self.extranetwork_mappings[key] = PPPENMapping(full_path, kind, name, variants)
def __get_extranetwork_mappings_in_structured_file(self, full_path):
"""
Get all extra network mappings in a structured file.
Args:
full_path (str): The path to the file.
base (str): The base path for the extra network mappings.
"""
try:
try:
with open(full_path, "r", encoding="utf-8") as file:
content = yaml.safe_load(file)
except: # pylint: disable=bare-except
log(
self.__logger,
self.__debug_level,
logging.WARNING,
f"Could not read file '{escape_single_quotes(full_path)}' with utf-8 encoding, trying windows-1252...",
)
with open(full_path, "r", encoding="windows-1252") as file:
content = yaml.safe_load(file)
self.__add_extranetwork_mapping(content, full_path)
except Exception as e: # pylint: disable=broad-except
log(
self.__logger,
self.__debug_level,
logging.ERROR,
f"Error reading extra network mappings from file '{escape_single_quotes(full_path)}': {e}",
)
def __get_extranetwork_mappings_in_path(self, path: str):
"""
Get all extra network mappings in a path.
Args:
path (str): The path (folder or file).
"""
if not os.path.exists(path):
log(
self.__logger,
self.__debug_level,
logging.WARNING,
f"Extra network mappings path '{escape_single_quotes(path)}' does not exist!",
)
return
if os.path.isfile(path):
self.__get_extranetwork_mappings_in_file(path)
return
for filename in os.listdir(path):
full_path = os.path.abspath(os.path.join(path, filename))
if os.path.basename(full_path).startswith("."):
continue
self.__get_extranetwork_mappings_in_path(full_path)
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

+24 -3
View File
@@ -3,6 +3,8 @@ import logging
import sys import sys
import copy import copy
from ppp_utils import format_output
class DEBUG_LEVEL(Enum): class DEBUG_LEVEL(Enum):
none = "none" none = "none"
@@ -52,14 +54,15 @@ class PromptPostProcessorLogFactory: # pylint: disable=too-few-public-methods
colored_record.levelname = f"{seq}{levelname:8s}{self.COLORS['RESET']}" colored_record.levelname = f"{seq}{levelname:8s}{self.COLORS['RESET']}"
return super().format(colored_record) return super().format(colored_record)
def __init__(self): def __init__(self, filename = None):
""" """
Initializes the PromptPostProcessor class. Initializes the PromptPostProcessor class.
This method sets up the logger for the PromptPostProcessor class and configures its log level and handlers. This method sets up the logger for the PromptPostProcessor class and configures its log level and handlers.
Args: Args:
None filename (str, optional): The name of the file to log to. Defaults to None.
app (SUPPORTED_APPS): The application for which the logger is being created.
Returns: Returns:
None None
@@ -68,8 +71,12 @@ class PromptPostProcessorLogFactory: # pylint: disable=too-few-public-methods
ppplog.propagate = False ppplog.propagate = False
if not ppplog.handlers: if not ppplog.handlers:
handler = logging.StreamHandler(sys.stdout) handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(self.ColoredFormatter("%(asctime)s %(levelname)s %(message)s")) # Used in A1111 / Forge / reForge / ComfyUI, but not in SD.Next handler.setFormatter(self.ColoredFormatter("%(asctime)s %(levelname)s %(message)s"))
ppplog.addHandler(handler) ppplog.addHandler(handler)
if filename is not None:
file_handler = logging.FileHandler(filename, encoding="utf-8")
file_handler.setFormatter(logging.Formatter("%(asctime)s %(levelname)s %(message)s"))
ppplog.addHandler(file_handler)
ppplog.setLevel(logging.DEBUG) ppplog.setLevel(logging.DEBUG)
self.log = PromptPostProcessorLogCustomAdapter(ppplog) self.log = PromptPostProcessorLogCustomAdapter(ppplog)
@@ -90,3 +97,17 @@ class PromptPostProcessorLogCustomAdapter(logging.LoggerAdapter):
tuple: A tuple containing the processed log message and keyword arguments. tuple: A tuple containing the processed log message and keyword arguments.
""" """
return f"[PPP] {msg}", kwargs return f"[PPP] {msg}", kwargs
def log(logger: logging.Logger, debug_level: DEBUG_LEVEL, kind: int, message: str, min_level: DEBUG_LEVEL | None = None, formatted: bool = True, exc_info: bool = False):
if logger:
if min_level is None:
if kind == logging.DEBUG:
min_level = DEBUG_LEVEL.full
elif kind == logging.INFO:
min_level = DEBUG_LEVEL.minimal
else:
min_level = DEBUG_LEVEL.none
i_debug_level = list(DEBUG_LEVEL).index(debug_level)
i_min_level = list(DEBUG_LEVEL).index(min_level)
if i_debug_level >= i_min_level:
logger.log(kind, format_output(message) if formatted else message, exc_info=exc_info)
+2482
View File
File diff suppressed because it is too large Load Diff
+56
View File
@@ -0,0 +1,56 @@
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
def escape_single_quotes(s: str):
"""
Escape single quotes in a string.
Args:
s (str): The string to escape.
Returns:
str: The escaped string.
"""
return s.replace("'", "\\'")
def escape_double_quotes(s: str):
"""
Escape double quotes in a string.
Args:
s (str): The string to escape.
Returns:
str: The escaped string.
"""
return s.replace('"', '\\"')
def format_output(text: str) -> str:
"""
Formats the output text by encoding it using unicode_escape and decoding it using utf-8.
Args:
text (str): The input text to be formatted.
Returns:
str: The formatted output text.
"""
return text.encode("unicode_escape").decode("utf-8")
+117
View File
@@ -0,0 +1,117 @@
from typing import Any
class VariableRepository:
"""
Unified repository for system, user, and echoed prompt variables.
System variables (underscore-prefixed names like ``_model``) are populated
once per processing session and are read-only during prompt evaluation.
User variables are created and mutated by set/echo constructs in the prompt.
Echoed variables record which user variables have already been output and
with what resolved string value.
"""
def __init__(self) -> None:
self._system: dict[str, Any] = {}
self._user: dict[str, Any] = {}
self._echoed: dict[str, str] = {}
def name_is_system(self, name: str) -> bool:
"""Return True if *name* is a system variable (i.e. starts with an underscore)."""
return name.startswith("_")
# ---- System variables ----
def get_system(self, name: str, default: Any = None) -> Any:
"""Return the value of a system variable, or *default* if absent."""
return self._system.get(name, default)
def set_system(self, name: str, value: Any) -> None:
"""Set a system variable."""
if not self.name_is_system(name):
raise ValueError(f"Invalid system variable name '{name}': must start with an underscore")
self._system[name] = value
def update_system(self, mapping: dict[str, Any]) -> None:
"""Bulk-update system variables from *mapping*."""
for name in mapping:
if not self.name_is_system(name):
raise ValueError(f"Invalid system variable name '{name}': must start with an underscore")
self._system.update(mapping)
def clear_system(self) -> None:
"""Remove all system variables."""
self._system.clear()
def get_all_system(self) -> dict[str, Any]:
"""Return a shallow copy of all system variables."""
return self._system.copy()
# ---- User variables ----
def get_user(self, name: str, default: Any = None) -> Any:
"""Return the value of a user variable, or *default* if absent."""
return self._user.get(name, default)
def set_user(self, name: str, value: Any) -> None:
"""Set a user variable."""
if self.name_is_system(name):
raise ValueError(f"Invalid user variable name '{name}': must not start with an underscore")
self._user[name] = value
def delete_user(self, name: str) -> None:
"""Remove a user variable (no-op if it does not exist)."""
self._user.pop(name, None)
def clear_user(self) -> None:
"""Remove all user variables."""
self._user.clear()
# ---- Echoed variables ----
def get_echoed_value(self, name: str, default: str | None = None) -> str | None:
"""Return the echoed string value for *name*, or *default* if not echoed."""
return self._echoed.get(name, default)
def echo(self, name: str, value: str) -> None:
"""Record that *name* was echoed with *value*."""
self._echoed[name] = value
def clear_echoed(self) -> None:
"""Remove all echoed-variable records."""
self._echoed.clear()
# ---- Combined queries ----
def get(self, name: str, default: Any = None) -> Any:
"""
Return the value of a variable, checking system variables first, then user variables.
Args:
name (str): The name of the variable.
default (Any): The value to return if the variable is not found.
"""
if name in self._system:
return self._system.get(name, default)
return self._user.get(name, default)
def all_user_or_echoed_keys(self) -> set[str]:
"""Return the union of user-variable and echoed-variable keys."""
return set(self._user.keys()) | set(self._echoed.keys())
# ---- State backup / restore ----
def backup_user_and_echoed(self) -> tuple[dict[str, Any], dict[str, str]]:
"""Return shallow-copy snapshots of user and echoed variables for rollback."""
return self._user.copy(), self._echoed.copy()
def restore_user_and_echoed(self, backup: tuple[dict[str, Any], dict[str, str]]) -> None:
"""Restore user and echoed variables from a snapshot made by :meth:`backup_user_and_echoed`."""
user_backup, echoed_backup = backup
self._user.clear()
self._user.update(user_backup)
self._echoed.clear()
self._echoed.update(echoed_backup)
+299 -131
View File
@@ -1,31 +1,12 @@
import fnmatch import fnmatch
import os import os
import json from pathlib import Path
from typing import Optional from typing import Optional
import logging import logging
import yaml import yaml
from ppp_logging import DEBUG_LEVEL from ppp_logging import DEBUG_LEVEL, log
from ppp_utils import deep_freeze, escape_single_quotes
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: class PPPWildcard:
@@ -51,6 +32,15 @@ class PPPWildcard:
t = (self.key, deep_freeze(self.unprocessed_choices)) t = (self.key, deep_freeze(self.unprocessed_choices))
return hash(t) 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:
""" """
@@ -61,42 +51,55 @@ class PPPWildcards:
""" """
DEFAULT_WILDCARDS_FOLDER = "wildcards" DEFAULT_WILDCARDS_FOLDER = "wildcards"
LOCALINPUT_FILENAME = R"//INPUT\\"
def __init__(self, logger): def __init__(self, logger=None):
self.__logger: logging.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.__wildcard_files = {}
self.__wildcard_default_filters: dict[str, list[list[str]]] = {}
self.wildcards: dict[str, PPPWildcard] = {} self.wildcards: dict[str, PPPWildcard] = {}
def __hash__(self) -> int: def __hash__(self) -> int:
return hash(deep_freeze(self.wildcards)) return hash(deep_freeze(self.wildcards))
def refresh_wildcards(self, debug_level: DEBUG_LEVEL, wildcards_folders: Optional[list[str]]): 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: # log(self.__logger, self.__debug_level, logging.INFO, "Refreshing wildcards...")
# if self.debug_level != DEBUG_LEVEL.none: # t1 = time.monotonic_ns()
# self.logger.info("Initializing wildcards...") for fullpath in list(self.__wildcard_files.keys()):
# t1 = time.time() if fullpath != self.LOCALINPUT_FILENAME:
for fullpath in list(self.__wildcard_files.keys()):
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 Path(path).is_relative_to(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_path(f if os.path.isdir(f) else os.path.dirname(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.monotonic_ns()
# log(self.__logger, self.__debug_level, logging.INFO, f"Wildcards refresh time: {(t2 - t1) / 1_000_000_000:.3f} seconds")
def get_wildcards(self, key: str) -> list[PPPWildcard]: def get_wildcards(self, key: str) -> list[PPPWildcard]:
""" """
@@ -149,7 +152,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.
@@ -157,9 +160,17 @@ 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:
self.__logger.debug(f"Removing wildcards from file: {full_path}") if full_path == self.LOCALINPUT_FILENAME:
log(self.__logger, self.__debug_level, logging.DEBUG, "Removing from memory wildcards from input")
else:
log(
self.__logger,
self.__debug_level,
logging.DEBUG,
f"Removing from memory wildcards from file: {full_path}",
)
if full_path in self.__wildcard_files.keys(): if full_path in self.__wildcard_files.keys():
del self.__wildcard_files[full_path] del self.__wildcard_files[full_path]
for key in list(self.wildcards.keys()): for key in list(self.wildcards.keys()):
@@ -174,35 +185,87 @@ class PPPWildcards:
base (str): The base path for the wildcards. base (str): The base path for the wildcards.
full_path (str): The path to the file. full_path (str): The path to the file.
""" """
last_modified = os.path.getmtime(full_path) try:
last_modified_cached = self.__wildcard_files.get(full_path, None) last_modified = os.path.getmtime(full_path)
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)
return if last_modified_cached is not None and last_modified == self.__wildcard_files[full_path]:
filename = os.path.basename(full_path) return
_, extension = os.path.splitext(filename) filename = os.path.basename(full_path)
if extension not in (".txt", ".json", ".yaml", ".yml"): _, extension = os.path.splitext(filename)
return if extension not in (".txt", ".json", ".yaml", ".yml"):
self.__remove_wildcards_from_file(full_path, False) return
if last_modified_cached is not None and self.__debug_level != DEBUG_LEVEL.none: self.__remove_wildcards_from_path(full_path, False)
self.__logger.debug(f"Updating wildcards from file: {full_path}") if last_modified_cached is not None:
if extension == ".txt": log(self.__logger, self.__debug_level, logging.DEBUG, f"Updating wildcards from file: {full_path}")
self.__get_wildcards_in_text_file(full_path, base) if extension == ".txt":
elif extension in (".json", ".yaml", ".yml"): self.__get_wildcards_in_text_file(full_path, base)
self.__get_wildcards_in_structured_file(full_path, base) elif extension in (".json", ".yaml", ".yml"):
self.__wildcard_files[full_path] = last_modified self.__get_wildcards_in_structured_file(full_path, base)
self.__wildcard_files[full_path] = last_modified
except Exception as e: # pylint: disable=broad-except
log(
self.__logger,
self.__debug_level,
logging.ERROR,
f"Error reading wildcard file '{escape_single_quotes(full_path)}': {e}",
)
def is_dict_choices_options(self, d: dict) -> bool: def __get_wildcards_in_input(self, wildcards_input: str):
""" """
Check if a dictionary is a valid choices options dictionary. Get all wildcards in the string.
Args:
wildcards_input (str): The input string containing wildcards in json or yaml format.
"""
try:
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:
log(self.__logger, self.__debug_level, logging.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:
log(self.__logger, self.__debug_level, logging.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
except Exception as e: # pylint: disable=broad-except
log(self.__logger, self.__debug_level, logging.ERROR, f"Error reading wildcards input: {e}")
# NOTE wcdef and choice options should not have properties in common
def is_dict_wcdef_options(self, d: dict) -> bool:
"""
Check if a dictionary is a valid wildcard definition options dictionary.
Args: Args:
d (dict): The dictionary to check. d (dict): The dictionary to check.
Returns: Returns:
bool: Whether the dictionary is a valid choices options dictionary or not. bool: Whether the dictionary is a valid wildcard definition options dictionary or not.
""" """
return all( return all(
k in ["sampler", "repeating", "count", "from", "to", "prefix", "suffix", "separator"] for k in d.keys() k
in [
"sampler",
"repeating",
"optional",
"count",
"from",
"to",
"prefix",
"suffix",
"container",
"description",
"separator",
]
for k in d.keys()
) )
def is_dict_choice_options(self, d: dict) -> bool: def is_dict_choice_options(self, d: dict) -> bool:
@@ -215,7 +278,7 @@ class PPPWildcards:
Returns: Returns:
bool: Whether the dictionary is a valid choice options dictionary or not. 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()) return all(k in ["command", "labels", "weight", "if", "content", "text"] for k in d.keys())
def __get_choices(self, obj: object, full_path: str, key_parts: list[str]) -> list: def __get_choices(self, obj: object, full_path: str, key_parts: list[str]) -> list:
""" """
@@ -229,51 +292,75 @@ class PPPWildcards:
Returns: Returns:
list: list of choices list: list of choices
""" """
choices = None if obj is None:
if obj is not None: return None
if isinstance(obj, (str, dict)): if isinstance(obj, (str, dict)):
choices = [obj] return [obj]
elif isinstance(obj, (int, float, bool)): if isinstance(obj, (int, float, bool)):
choices = [str(obj)] return [str(obj)]
elif isinstance(obj, list) and len(obj) > 0: if not isinstance(obj, list) or len(obj) == 0:
choices = [] log(
for i, c in enumerate(obj): self.__logger,
invalid_choice = False self.__debug_level,
if isinstance(c, str): logging.WARNING,
choice = c f"Invalid format in wildcard '{escape_single_quotes('/'.join(key_parts))}' in file '{escape_single_quotes(full_path)}'!",
elif isinstance(c, (int, float, bool)): )
choice = str(c) return None
elif isinstance(c, list): choices = []
# we create an anonymous wildcard for i, c in enumerate(obj):
choice = self.__create_anonymous_wildcard(full_path, key_parts, i, c) if isinstance(c, (str, int, float, bool)):
elif isinstance(c, dict): choices.append(str(c))
if self.is_dict_choices_options(c) or self.is_dict_choice_options(c): elif isinstance(c, list):
# we assume it is a choice or wildcard parameters in object format # we create an anonymous wildcard
choice = c choices.append(self.__create_anonymous_wildcard(full_path, key_parts, i, c))
choice_content = choice.get("content", choice.get("text", None)) elif isinstance(c, dict):
if choice_content is not None and isinstance(choice_content, list): choices.append(self.__process_dict_choice(c, full_path, key_parts, i))
# we create an anonymous wildcard else:
choice["content"] = self.__create_anonymous_wildcard( log(
full_path, key_parts, i, choice_content self.__logger,
) self.__debug_level,
if "text" in choice: logging.WARNING,
del choice["text"] f"Invalid choice {i+1} in wildcard '{escape_single_quotes('/'.join(key_parts))}' in file '{escape_single_quotes(full_path)}'!",
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 return choices
def __process_dict_choice(self, c: dict, full_path: str, key_parts: list[str], i: int) -> dict:
"""
Process a dictionary choice.
Args:
c (dict): The dictionary choice.
full_path (str): The path to the file.
key_parts (list[str]): The parts of the key.
i (int): The index of the choice.
Returns:
dict: The processed choice.
"""
if self.is_dict_wcdef_options(c):
return c
elif self.is_dict_choice_options(c):
# we assume it is a choice 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)
log(
self.__logger,
self.__debug_level,
logging.WARNING,
f"Invalid choice {i+1} in wildcard '{escape_single_quotes('/'.join(key_parts))}' in file '{escape_single_quotes(full_path)}'!",
)
return None
def __create_anonymous_wildcard(self, full_path, key_parts, i, content, options=None): def __create_anonymous_wildcard(self, full_path, key_parts, i, content, options=None):
""" """
Create an anonymous wildcard. Create an anonymous wildcard.
@@ -313,14 +400,29 @@ class PPPWildcards:
tmp_key_parts.extend(key.split("/")) tmp_key_parts.extend(key.split("/"))
fullkey = "/".join(tmp_key_parts) fullkey = "/".join(tmp_key_parts)
if self.wildcards.get(fullkey, None) is not None: if self.wildcards.get(fullkey, None) is not None:
self.__logger.warning( log(
f"Duplicate wildcard '{fullkey}' in file '{full_path}' and '{self.wildcards[fullkey].file}'!" self.__logger,
self.__debug_level,
logging.WARNING,
f"Duplicate wildcard '{escape_single_quotes(fullkey)}' in file '{escape_single_quotes(full_path)}' and '{escape_single_quotes(self.wildcards[fullkey].file)}'!",
) )
else: else:
obj = self.__get_nested(content, key) obj = self.__get_nested(content, key)
choices = self.__get_choices(obj, full_path, tmp_key_parts) choices = self.__get_choices(obj, full_path, tmp_key_parts)
if choices is None: if choices is None:
self.__logger.warning(f"Invalid wildcard '{fullkey}' in file '{full_path}'!") log(
self.__logger,
self.__debug_level,
logging.WARNING,
f"Invalid wildcard '{escape_single_quotes(fullkey)}' in file '{escape_single_quotes(full_path)}'!",
)
elif fullkey.startswith("_"):
log(
self.__logger,
self.__debug_level,
logging.WARNING,
f"Invalid wildcard name '{escape_single_quotes(fullkey)}' in file '{escape_single_quotes(full_path)}'! (cannot start with underscore)",
)
else: else:
self.wildcards[fullkey] = PPPWildcard(full_path, fullkey, choices) self.wildcards[fullkey] = PPPWildcard(full_path, fullkey, choices)
return return
@@ -329,17 +431,37 @@ class PPPWildcards:
elif isinstance(content, (int, float, bool)): elif isinstance(content, (int, float, bool)):
content = [str(content)] content = [str(content)]
if not isinstance(content, list): if not isinstance(content, list):
self.__logger.warning(f"Invalid wildcard in file '{full_path}'!") log(
self.__logger,
self.__debug_level,
logging.WARNING,
f"Invalid wildcard in file '{escape_single_quotes(full_path)}'!",
)
return return
fullkey = "/".join(key_parts) fullkey = "/".join(key_parts)
if self.wildcards.get(fullkey, None) is not None: if self.wildcards.get(fullkey, None) is not None:
self.__logger.warning( log(
f"Duplicate wildcard '{fullkey}' in file '{full_path}' and '{self.wildcards[fullkey].file}'!" self.__logger,
self.__debug_level,
logging.WARNING,
f"Duplicate wildcard '{escape_single_quotes(fullkey)}' in file '{escape_single_quotes(full_path)}' and '{escape_single_quotes(self.wildcards[fullkey].file)}'!",
) )
else: else:
choices = self.__get_choices(content, full_path, key_parts) choices = self.__get_choices(content, full_path, key_parts)
if choices is None: if choices is None:
self.__logger.warning(f"Invalid wildcard '{fullkey}' in file '{full_path}'!") log(
self.__logger,
self.__debug_level,
logging.WARNING,
f"Invalid wildcard '{escape_single_quotes(fullkey)}' in file '{escape_single_quotes(full_path)}'!",
)
elif fullkey.startswith("_"):
log(
self.__logger,
self.__debug_level,
logging.WARNING,
f"Invalid wildcard name '{escape_single_quotes(fullkey)}' in file '{escape_single_quotes(full_path)}'! (cannot start with underscore)",
)
else: else:
self.wildcards[fullkey] = PPPWildcard(full_path, fullkey, choices) self.wildcards[fullkey] = PPPWildcard(full_path, fullkey, choices)
@@ -353,11 +475,17 @@ class PPPWildcards:
""" """
external_key: str = os.path.relpath(os.path.splitext(full_path)[0], base) external_key: str = os.path.relpath(os.path.splitext(full_path)[0], base)
external_key_parts = external_key.split(os.sep) external_key_parts = external_key.split(os.sep)
_, extension = os.path.splitext(full_path) try:
with open(full_path, "r", encoding="utf-8") as file: with open(full_path, "r", encoding="utf-8") as file:
if extension == ".json": content = yaml.safe_load(file)
content = json.loads(file.read()) except: # pylint: disable=bare-except
else: log(
self.__logger,
self.__debug_level,
logging.WARNING,
f"Could not read file '{escape_single_quotes(full_path)}' with utf-8 encoding, trying windows-1252...",
)
with open(full_path, "r", encoding="windows-1252") as file:
content = yaml.safe_load(file) content = yaml.safe_load(file)
self.__add_wildcard(content, full_path, external_key_parts) self.__add_wildcard(content, full_path, external_key_parts)
@@ -371,28 +499,68 @@ class PPPWildcards:
""" """
external_key: str = os.path.relpath(os.path.splitext(full_path)[0], base) external_key: str = os.path.relpath(os.path.splitext(full_path)[0], base)
external_key_parts = external_key.split(os.sep) external_key_parts = external_key.split(os.sep)
with open(full_path, "r", encoding="utf-8") as file: try:
text_content = map(lambda x: x.strip("\n\r"), file.readlines()) with open(full_path, "r", encoding="utf-8") as file:
text_content = map(lambda x: x.strip("\n\r"), file.readlines())
except: # pylint: disable=bare-except
log(
self.__logger,
self.__debug_level,
logging.WARNING,
f"Could not read file '{escape_single_quotes(full_path)}' with utf-8 encoding, trying windows-1252...",
)
with open(full_path, "r", encoding="windows-1252") 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 = 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]
self.__add_wildcard(text_content, full_path, external_key_parts) self.__add_wildcard(text_content, full_path, external_key_parts)
def __get_wildcards_in_directory(self, base: str, directory: str): def __get_wildcards_in_path(self, base: str, path: str):
""" """
Get all wildcards in a directory. Get all wildcards in a path.
Args: Args:
base (str): The base path for the wildcards. base (str): The base path for the wildcards.
directory (str): The path to the directory. path (str): The path (folder or file).
""" """
if not os.path.exists(directory): if not os.path.exists(path):
self.__logger.warning(f"Wildcard directory '{directory}' does not exist!") log(
self.__logger,
self.__debug_level,
logging.WARNING,
f"Wildcard path '{escape_single_quotes(path)}' does not exist!",
)
return return
for filename in os.listdir(directory): if os.path.isfile(path):
full_path = os.path.abspath(os.path.join(directory, filename)) self.__get_wildcards_in_file(base, path)
return
for filename in os.listdir(path):
full_path = os.path.abspath(os.path.join(path, filename))
if os.path.basename(full_path).startswith("."): if os.path.basename(full_path).startswith("."):
continue continue
if os.path.isdir(full_path): self.__get_wildcards_in_path(base, full_path)
self.__get_wildcards_in_directory(base, full_path)
elif os.path.isfile(full_path): def set_wildcard_default_filter(self, wildcard_key: str, filter_options: Optional[list[list[str]]]):
self.__get_wildcards_in_file(base, full_path) """
Set the default filter for a wildcard.
Args:
wildcard_key (str): The key of the wildcard.
filter_options (list[list[str]]): The filter options.
"""
if filter_options is None:
if wildcard_key in self.__wildcard_default_filters:
del self.__wildcard_default_filters[wildcard_key]
else:
self.__wildcard_default_filters[wildcard_key] = filter_options
def get_wildcard_default_filter(self, wildcard_key: str) -> Optional[list[list[str]]]:
"""
Get the default filter for a wildcard.
Args:
wildcard_key (str): The key of the wildcard.
Returns:
Optional[list[list[str]]]: The filter options or None if not set.
"""
return self.__wildcard_default_filters.get(wildcard_key, None)
+8 -5
View File
@@ -1,15 +1,18 @@
[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.8.0" version = "3.1.1"
license = {file = "LICENSE.txt"} license = { file = "LICENSE.txt" }
dependencies = ["lark"] dependencies = ["lark", "numpy", "pyyaml", "pydantic"]
[project.urls] [project.urls]
Repository = "https://github.com/acorderob/sd-webui-prompt-postprocessor" Repository = "https://github.com/acorderob/sd-webui-prompt-postprocessor"
# Used by Comfy Registry https://comfyregistry.org # Used by Comfy Registry https://comfyregistry.org
Documentation = "https://github.com/acorderob/sd-webui-prompt-postprocessor/main/README.md"
"Bug Tracker" = "https://github.com/acorderob/sd-webui-prompt-postprocessor/issues"
[tool.comfy] [tool.comfy]
PublisherId = "acorderob" PublisherId = "acorderob"
DisplayName = "sd-webui-prompt-postprocessor" #DisplayName = "sd-webui-prompt-postprocessor"
Icon = "" DisplayName = "ACB Prompt PostProcessor"
Icon = "https://raw.githubusercontent.com/acorderob/sd-webui-prompt-postprocessor/main/images/prompt-postprocessor-icon.png"
+3
View File
@@ -1 +1,4 @@
lark lark
numpy
pyyaml
pydantic
+430 -189
View File
@@ -1,23 +1,26 @@
if __name__ == "__main__": if __name__ == "__main__":
raise SystemExit("This script must be run from a Stable Diffusion WebUI") raise SystemExit("This script must be run from a Stable Diffusion WebUI")
import logging
import sys import sys
import os import os
import time import time
from pathlib import Path
import numpy as np import numpy as np
sys.path.append(os.path.join(sys.path[0], "..")) sys.path.append(str(Path(__file__).parent)) # base path for the extension
from modules import scripts, shared, script_callbacks # type: ignore
from modules import scripts, shared, script_callbacks from modules.processing import StableDiffusionProcessing # type: ignore
from modules.processing import StableDiffusionProcessing from modules.shared import opts # type: ignore
from modules.shared import opts from modules.paths import models_path # type: ignore
from modules.paths import models_path import gradio as gr # type: ignore
import gradio as gr
from ppp import PromptPostProcessor from ppp import PromptPostProcessor
from ppp_logging import DEBUG_LEVEL, PromptPostProcessorLogFactory from ppp_classes import IFWILDCARDS_CHOICES, ONWARNING_CHOICES, SUPPORTED_APPS, SUPPORTED_APPS_NAMES, PPPStateOptions
from ppp_logging import DEBUG_LEVEL, PromptPostProcessorLogFactory, log
from ppp_cache import PPPLRUCache from ppp_cache import PPPLRUCache
from ppp_wildcards import PPPWildcards from ppp_wildcards import PPPWildcards
from ppp_enmappings import PPPExtraNetworkMappings
class PromptPostProcessorA1111Script(scripts.Script): class PromptPostProcessorA1111Script(scripts.Script):
@@ -37,28 +40,37 @@ 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
return cls.instance_count
@classmethod
def get_instance_count(cls):
return cls.instance_count
def __init__(self): def __init__(self):
""" """
Initializes the PromptPostProcessor object. Initializes the PromptPostProcessor object.
This method adds callbacks for UI settings and initializes the logger.
Parameters: Parameters:
None None
Returns: Returns:
None None
""" """
lf = PromptPostProcessorLogFactory() self.instance_index = self.increment_instance_count()
self.name = PromptPostProcessor.NAME self.name = PromptPostProcessor.NAME
self.ppp_logger = lf.log
self.ppp_debug_level = DEBUG_LEVEL(getattr(opts, "ppp_gen_debug_level", DEBUG_LEVEL.none.value))
self.lru_cache = PPPLRUCache(1000)
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.ppp_logger = None
self.ppp_logger.info(f"{PromptPostProcessor.NAME} {PromptPostProcessor.VERSION} initialized") self.ppp_debug_level = DEBUG_LEVEL.none.value
self.lru_cache = None
self.wildcards_obj = None
self.extranetwork_mappings_obj = None
def title(self): def title(self):
""" """
@@ -69,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.
@@ -81,116 +93,220 @@ class PromptPostProcessorA1111Script(scripts.Script):
""" """
return scripts.AlwaysVisible return scripts.AlwaysVisible
def ui(self, is_img2img): def ui(self, is_img2img): # pylint: disable=unused-argument
with gr.Accordion(PromptPostProcessor.NAME, open=False): with gr.Accordion(PromptPostProcessor.NAME, open=False):
force_equal_seeds = gr.Checkbox( force_equal_seeds = gr.Checkbox(
label="Force equal seeds", label="Force equal seeds",
info="Force all image seeds and variation seeds to be equal to the first one, disabling the default autoincrease.", info="Force all image seeds and variation seeds to be equal to the first one, disabling the default autoincrease.",
default=False, value=False,
# show_label=True, # show_label=True,
elem_id="ppp_force_equal_seeds", elem_id="ppp_force_equal_seeds",
) )
gr.HTML( gr.HTML("<br>")
"""<br><div>Unlink the seed to use the specified one for the prompts instead of the image seed. gr.Markdown("""
This seed will only change for each image in the batch if the value is -1 or 'variable seed' is checked.</div> Unlink the seed to use the specified one for the prompts instead of the image seed.
<div>Seeds are only used for the wildcards and choice constructs.</div>"""
) * 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.
unlink_seed = gr.Checkbox( * A seed of -1 and "Incremental seed" unchecked will use a random seed for each prompt.
label="Unlink seed", * Any other seed value and "Incremental seed" checked will use the specified seed for the first prompt and consecutive values for the rest.
default=False, * Any other seed value and "Incremental seed" unchecked will use the specified seed for all the prompts.
# show_label=True,
elem_id="ppp_unlink_seed", Seeds are only used for the wildcards and choice constructs.
) """)
seed = gr.Number( gr.HTML("<br>")
label="Seed", with gr.Row(equal_height=True):
default=-1, unlink_seed = gr.Checkbox(
precision=0, label="Unlink seed",
# minimum=-1, value=False,
# maximum=2**32 - 1, # show_label=True,
# step=1, elem_id="ppp_unlink_seed",
# show_label=True, )
min_width=100, seed = gr.Number(
elem_id="ppp_seed", label="Prompt seed",
) value=-1,
variable_seed = gr.Checkbox( precision=0,
label="Variable seed", # minimum=-1,
default=False, # maximum=2**32 - 1,
# show_label=True, # step=1,
elem_id="ppp_variable_seed", # show_label=True,
) min_width=100,
return [force_equal_seeds, unlink_seed, seed, variable_seed] 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",
)
gr.HTML("<br>")
with gr.Row(equal_height=True):
combinatorial = gr.Checkbox(
label="Combinatorial mode",
info="Generate all prompt combinations and cycle through them to fill the batch.",
value=PromptPostProcessor.DEFAULT_DO_COMBINATORIAL,
elem_id="ppp_combinatorial",
)
combinatorial_shuffle = gr.Checkbox(
label="Shuffle combinations",
info="Shuffle the combinatorial results.",
value=PromptPostProcessor.DEFAULT_COMBINATORIAL_SHUFFLE,
elem_id="ppp_combinatorial_shuffle",
)
combinatorial_limit = gr.Number(
label="Combinations limit (0 = no limit)",
value=PromptPostProcessor.DEFAULT_COMBINATORIAL_LIMIT,
precision=0,
min_width=120,
elem_id="ppp_combinatorial_limit",
)
return [
force_equal_seeds,
unlink_seed,
seed,
incremental_seed,
combinatorial,
combinatorial_shuffle,
combinatorial_limit,
]
def process( def process(
self, p: StableDiffusionProcessing, input_force_equal_seeds, input_unlink_seed, input_seed, input_variable_seed self,
p: StableDiffusionProcessing,
input_force_equal_seeds,
input_unlink_seed,
input_seed,
input_incremental_seed,
input_combinatorial,
input_combinatorial_shuffle,
input_combinatorial_limit,
): # pylint: disable=arguments-differ ): # 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.
input_combinatorial (bool): Flag indicating whether to use combinatorial mode.
input_combinatorial_shuffle (bool): Flag indicating whether to shuffle the combinatorial results.
input_combinatorial_limit (int): Maximum number of combinations (0 = no limit).
Returns: Returns:
None None
""" """
t1 = time.time()
if getattr(opts, "prompt_attention", "") == "Compel parser":
self.ppp_logger.warning("Compel parser is not supported!")
is_i2i = getattr(p, "init_images", [None])[0] is not None
self.ppp_debug_level = DEBUG_LEVEL(getattr(opts, "ppp_gen_debug_level", DEBUG_LEVEL.none.value))
do_i2i = getattr(opts, "ppp_gen_doi2i", False)
if is_i2i and not do_i2i:
if self.ppp_debug_level != DEBUG_LEVEL.none:
self.ppp_logger.info("Not processing the prompt for i2i")
return
if self.ppp_debug_level != DEBUG_LEVEL.none:
self.ppp_logger.info(f"Post-processing prompts ({'i2i' if is_i2i else 't2i'})")
app = ( app = (
"forge" SUPPORTED_APPS.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 (
SUPPORTED_APPS.reforge
if hasattr(p.sd_model, "forge_objects")
else (
SUPPORTED_APPS.sdnext
if hasattr(p.sd_model, "is_sdxl") and not hasattr(p.sd_model, "is_ssd")
else SUPPORTED_APPS.a1111
)
)
)
num_seeds = len(getattr(p, "all_seeds", []))
options = PPPStateOptions(
debug_level=DEBUG_LEVEL(getattr(opts, "ppp_gen_debug_level", PromptPostProcessor.DEFAULT_DEBUG_LEVEL)),
on_warning=ONWARNING_CHOICES(getattr(opts, "ppp_gen_onwarning", PromptPostProcessor.DEFAULT_ON_WARNING)),
strict_operators=getattr(opts, "ppp_gen_strict_operators", PromptPostProcessor.DEFAULT_STRICT_OPERATORS),
process_wildcards=getattr(opts, "ppp_wil_processwildcards", PromptPostProcessor.DEFAULT_PROCESS_WILDCARDS),
if_wildcards=IFWILDCARDS_CHOICES(
getattr(opts, "ppp_wil_ifwildcards", PromptPostProcessor.DEFAULT_IF_WILDCARDS)
),
choice_separator=getattr(opts, "ppp_wil_choice_separator", PromptPostProcessor.DEFAULT_CHOICE_SEPARATOR),
keep_choices_order=getattr(
opts, "ppp_wil_keep_choices_order", PromptPostProcessor.DEFAULT_KEEP_CHOICES_ORDER
),
stn_separator=getattr(opts, "ppp_stn_separator", PromptPostProcessor.DEFAULT_STN_SEPARATOR),
stn_ignore_repeats=getattr(opts, "ppp_stn_ignorerepeats", PromptPostProcessor.DEFAULT_STN_IGNORE_REPEATS),
cup_do_cleanup=True,
cup_cleanup_variables=True,
cup_extra_spaces=getattr(opts, "ppp_cup_extraspaces", PromptPostProcessor.DEFAULT_CUP_EXTRA_SPACES),
cup_empty_constructs=getattr(
opts, "ppp_cup_emptyconstructs", PromptPostProcessor.DEFAULT_CUP_EMPTY_CONSTRUCTS
),
cup_extra_separators=getattr(
opts, "ppp_cup_extraseparators", PromptPostProcessor.DEFAULT_CUP_EXTRA_SEPARATORS
),
cup_extra_separators2=getattr(
opts, "ppp_cup_extraseparators2", PromptPostProcessor.DEFAULT_CUP_EXTRA_SEPARATORS2
),
cup_extra_separators_include_eol=getattr(
opts,
"ppp_cup_extraseparators_include_eol",
PromptPostProcessor.DEFAULT_CUP_EXTRA_SEPARATORS_INCLUDE_EOL,
),
cup_breaks=getattr(opts, "ppp_cup_breaks", PromptPostProcessor.DEFAULT_CUP_BREAKS),
cup_breaks_eol=getattr(opts, "ppp_cup_breaks_eol", PromptPostProcessor.DEFAULT_CUP_BREAKS_EOL),
cup_ands=getattr(opts, "ppp_cup_ands", PromptPostProcessor.DEFAULT_CUP_ANDS),
cup_ands_eol=getattr(opts, "ppp_cup_ands_eol", PromptPostProcessor.DEFAULT_CUP_ANDS_EOL),
cup_extranetwork_tags=getattr(
opts, "ppp_cup_extranetworktags", PromptPostProcessor.DEFAULT_CUP_EXTRANETWORK_TAGS
),
cup_merge_attention=getattr(
opts, "ppp_cup_mergeattention", PromptPostProcessor.DEFAULT_CUP_MERGE_ATTENTION
),
cup_remove_extranetwork_tags=getattr(
opts, "ppp_rem_removeextranetworktags", PromptPostProcessor.DEFAULT_CUP_REMOVE_EXTRANETWORK_TAGS
),
do_combinatorial=input_combinatorial,
combinatorial_shuffle=input_combinatorial_shuffle,
combinatorial_limit=max(num_seeds, int(input_combinatorial_limit)) if input_combinatorial else 0,
)
if self.ppp_logger is None:
lf = PromptPostProcessorLogFactory()
self.ppp_logger = lf.log
self.ppp_debug_level = options.debug_level
self.lru_cache = PPPLRUCache(1000, logger=self.ppp_logger, debug_level=self.ppp_debug_level)
self.wildcards_obj = PPPWildcards(self.ppp_logger)
self.extranetwork_mappings_obj = PPPExtraNetworkMappings(self.ppp_logger)
log(
self.ppp_logger,
DEBUG_LEVEL.minimal,
logging.INFO,
f"{PromptPostProcessor.NAME} {PromptPostProcessor.VERSION} initialized, running on {SUPPORTED_APPS_NAMES[app]}",
)
t1 = time.monotonic_ns()
if getattr(opts, "prompt_attention", "") == "Compel parser":
log(self.ppp_logger, self.ppp_debug_level, logging.WARNING, "Compel parser is not supported!")
init_images = getattr(p, "init_images", [None]) or [None]
is_i2i = bool(init_images[0])
do_i2i = getattr(opts, "ppp_gen_doi2i", False)
add_prompts = getattr(opts, "ppp_gen_addpromptstometadata", True)
if is_i2i and not do_i2i:
log(self.ppp_logger, self.ppp_debug_level, logging.INFO, "Not processing the prompt for i2i")
return
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,
"PPP combinatorial": input_combinatorial,
}
)
log(
self.ppp_logger,
self.ppp_debug_level,
logging.INFO,
f"Post-processing prompts ({'i2i' if is_i2i else 't2i'})",
) )
env_info = { env_info = {
"app": app, "app": app.value,
"models_path": models_path, "models_path": models_path,
"model_filename": getattr(p.sd_model.sd_checkpoint_info, "filename", ""), "model_filename": getattr(p.sd_model.sd_checkpoint_info, "filename", ""),
"model_class": "", "model_class": p.sd_model.__class__.__name__,
"is_sd1": False, # Stable Diffusion 1 "property_base": p.sd_model,
"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
} }
if app == "sdnext": if app == SUPPORTED_APPS.forge:
# cannot differentiate SD1 and SD2, we set True to both
# LatentDiffusion is for the original backend, StableDiffusionPipeline is for the diffusers backend
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":
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_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")
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", "") wc_wildcards_folders = getattr(opts, "ppp_wil_wildcardsfolders", "")
if wc_wildcards_folders == "": if wc_wildcards_folders == "":
wc_wildcards_folders = os.getenv("WILDCARD_DIR", PPPWildcards.DEFAULT_WILDCARDS_FOLDER) wc_wildcards_folders = os.getenv("WILDCARD_DIR", PPPWildcards.DEFAULT_WILDCARDS_FOLDER)
@@ -199,57 +315,55 @@ class PromptPostProcessorA1111Script(scripts.Script):
for f in wc_wildcards_folders.split(",") for f in wc_wildcards_folders.split(",")
if f.strip() != "" if f.strip() != ""
] ]
options = { en_mappings_folders = getattr(opts, "ppp_en_mappingsfolders", "")
"debug_level": getattr(opts, "ppp_gen_debug_level", DEBUG_LEVEL.none.value), if en_mappings_folders == "":
"pony_substrings": getattr(opts, "ppp_gen_ponysubstrings", PromptPostProcessor.DEFAULT_PONY_SUBSTRINGS), en_mappings_folders = os.getenv(
"process_wildcards": getattr(opts, "ppp_wil_processwildcards", True), "EXTRANETWORKMAPPINGS_DIR",
"if_wildcards": getattr(opts, "ppp_wil_ifwildcards", PromptPostProcessor.IFWILDCARDS_CHOICES.ignore.value), PPPExtraNetworkMappings.DEFAULT_ENMAPPINGS_FOLDER,
"choice_separator": getattr(opts, "ppp_wil_choice_separator", PromptPostProcessor.DEFAULT_CHOICE_SEPARATOR), )
"keep_choices_order": getattr(opts, "ppp_wil_keep_choices_order", False), enmappings_folders = [
"stn_separator": getattr(opts, "ppp_stn_separator", PromptPostProcessor.DEFAULT_STN_SEPARATOR), (f if os.path.isabs(f) else os.path.abspath(os.path.join(models_path, f)))
"stn_ignore_repeats": getattr(opts, "ppp_stn_ignorerepeats", True), for f in en_mappings_folders.split(",")
"cleanup_extra_spaces": getattr(opts, "ppp_cup_extraspaces", True), if f.strip() != ""
"cleanup_empty_constructs": getattr(opts, "ppp_cup_emptyconstructs", True), ]
"cleanup_extra_separators": getattr(opts, "ppp_cup_extraseparators", True),
"cleanup_extra_separators2": getattr(opts, "ppp_cup_extraseparators2", True),
"cleanup_breaks": getattr(opts, "ppp_cup_breaks", True),
"cleanup_breaks_eol": getattr(opts, "ppp_cup_breaks_eol", False),
"cleanup_ands": getattr(opts, "ppp_cup_ands", True),
"cleanup_ands_eol": getattr(opts, "ppp_cup_ands_eol", False),
"cleanup_extranetwork_tags": getattr(opts, "ppp_cup_extranetworktags", False),
"cleanup_merge_attention": getattr(opts, "ppp_cup_mergeattention", True),
"remove_extranetwork_tags": getattr(opts, "ppp_rem_removeextranetworktags", False),
}
self.wildcards_obj.refresh_wildcards( self.wildcards_obj.refresh_wildcards(
self.ppp_debug_level, wildcards_folders if options["process_wildcards"] else None self.ppp_debug_level, wildcards_folders if options.process_wildcards else None
) )
self.extranetwork_mappings_obj.refresh_extranetwork_mappings(self.ppp_debug_level, enmappings_folders)
ppp = PromptPostProcessor( ppp = PromptPostProcessor(
self.ppp_logger, self.ppp_interrupt, env_info, options, self.grammar_content, self.wildcards_obj self.ppp_logger,
env_info,
options,
self.grammar_content,
self.ppp_interrupt,
self.wildcards_obj,
self.extranetwork_mappings_obj,
)
hash_fullenv = hash(
(ppp.envinfo_hash(), ppp.options_hash(), self.wildcards_obj, self.extranetwork_mappings_obj)
) )
prompts_list = []
if input_force_equal_seeds: if input_force_equal_seeds:
if self.ppp_debug_level != DEBUG_LEVEL.none: log(self.ppp_logger, self.ppp_debug_level, logging.INFO, "Forcing equal seeds")
self.ppp_logger.info("Forcing equal seeds") seeds: list[int] = getattr(p, "all_seeds", [])
seeds = getattr(p, "all_seeds", []) subseeds: list[int] = getattr(p, "all_subseeds", [])
subseeds = getattr(p, "all_subseeds", [])
p.all_seeds = [seeds[0] for _ in seeds] p.all_seeds = [seeds[0] for _ in seeds]
p.all_subseeds = [subseeds[0] for _ in subseeds] p.all_subseeds = [subseeds[0] for _ in subseeds]
calculated_seeds: list[int] = []
if input_unlink_seed: if input_unlink_seed:
if self.ppp_debug_level != DEBUG_LEVEL.none: log(self.ppp_logger, self.ppp_debug_level, logging.INFO, "Using unlinked seed")
self.ppp_logger.info("Using unlinked seed") if input_incremental_seed:
num_seeds = len(getattr(p, "all_seeds", [])) first_seed = np.random.randint(0, 2**32, dtype=np.int64) if input_seed == -1 else input_seed
if input_seed == -1: 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) 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: else:
calculated_seeds = [input_seed for _ in range(num_seeds)] calculated_seeds = [input_seed for _ in range(num_seeds)]
else: else:
seeds = getattr(p, "all_seeds", []) seeds: list[int] = getattr(p, "all_seeds", [])
subseeds = getattr(p, "all_subseeds", []) subseeds: list[int] = getattr(p, "all_subseeds", [])
subseed_strength = getattr(p, "subseed_strength", 0.0) subseed_strength: float = getattr(p, "subseed_strength", 0.0)
if subseed_strength > 0: if subseed_strength > 0:
calculated_seeds = [ calculated_seeds = [
int(subseed * subseed_strength + seed * (1 - subseed_strength)) int(subseed * subseed_strength + seed * (1 - subseed_strength))
@@ -261,54 +375,140 @@ class PromptPostProcessorA1111Script(scripts.Script):
else: else:
calculated_seeds = seeds calculated_seeds = seeds
# adds regular prompts # (prompt type, typeindex) -> (new positive prompt, new negative prompt)
rpr = getattr(p, "all_prompts", None) prompts_list: dict[tuple[str, int], tuple[str, str]] = {}
rnr = getattr(p, "all_negative_prompts", None) extra_params = {}
if rpr is not None and rnr is not None:
prompts_list += [
("regular", seed, prompt, negative_prompt)
for seed, prompt, negative_prompt in zip(calculated_seeds, rpr, rnr)
if (seed, prompt, negative_prompt) not in prompts_list
]
# make it compatible with A1111 hires fix
rph = getattr(p, "all_hr_prompts", None)
rnh = getattr(p, "all_hr_negative_prompts", None)
if rph is not None and rnh is not None:
prompts_list += [
("hiresfix", seed, prompt, negative_prompt)
for seed, prompt, negative_prompt in zip(calculated_seeds, rph, rnh)
if (seed, prompt, negative_prompt) not in prompts_list
]
# processes prompts # adds prompts
for i, (prompttype, seed, prompt, negative_prompt) in enumerate(prompts_list): regular_type = "regular"
if self.ppp_debug_level != DEBUG_LEVEL.none: rpr: list[str] = getattr(p, "all_prompts", None)
self.ppp_logger.info(f"processing prompts[{i+1}] ({prompttype})") rnr: list[str] = getattr(p, "all_negative_prompts", None)
if self.lru_cache.get((seed, hash(self.wildcards_obj), prompt, negative_prompt)) is None: regular_exists = rpr is not None and rnr is not None
posp, negp = ppp.process_prompt(prompt, negative_prompt, seed) hiresfix_type = "hiresfix"
self.lru_cache.put((seed, hash(self.wildcards_obj), prompt, negative_prompt), (posp, negp)) rph: list[str] = getattr(p, "all_hr_prompts", None)
# adds also the result so i2i doesn't process it unnecessarily rnh: list[str] = getattr(p, "all_hr_negative_prompts", None)
self.lru_cache.put((seed, hash(self.wildcards_obj), posp, negp), (posp, negp)) hiresfix_exists = rph is not None and rnh is not None
elif self.ppp_debug_level != DEBUG_LEVEL.none: for i in range(len(calculated_seeds)):
self.ppp_logger.info("result already in cache") if regular_exists:
prompts_list[(regular_type, i)] = None
if hiresfix_exists:
prompts_list[(hiresfix_type, i)] = None
ppp.process_prompts_group_start()
if input_combinatorial:
seed_for_comb = calculated_seeds[0] if calculated_seeds else 0
regular_copy = (rpr.copy() if rpr else None, rnr.copy() if rnr else None)
hiresfix_copy = (rph.copy() if rph else None, rnh.copy() if rnh else None)
regular_changes = False
hiresfix_changes = False
if regular_exists:
log(self.ppp_logger, self.ppp_debug_level, logging.INFO, "processing prompts combinatorially (regular)")
comb_results = ppp.process_prompt(rpr[0], rnr[0], seed_for_comb)
num_comb = len(comb_results)
for i in range(len(rpr)): # pylint: disable=consider-using-enumerate
posp, negp, _ = comb_results[i % num_comb]
prompts_list[(regular_type, i)] = (posp, negp)
extra_params["PPP combination"] = [str(1 + (i % num_comb)) for i in range(len(rpr))]
if hiresfix_exists:
hiresfix_equal = regular_exists and rph == rpr and rnh == rnr
if hiresfix_equal:
log(
self.ppp_logger,
self.ppp_debug_level,
logging.INFO,
"hiresfix prompts are the same as regular prompts, skipping combinatorial processing for hiresfix",
)
for i in range(len(rph)): # pylint: disable=consider-using-enumerate
prompts_list[(hiresfix_type, i)] = prompts_list.get((regular_type, i))
else:
log(
self.ppp_logger,
self.ppp_debug_level,
logging.INFO,
"processing prompts combinatorially (hiresfix)",
)
comb_results_hr = ppp.process_prompt(rph[0], rnh[0], seed_for_comb)
num_comb_hr = len(comb_results_hr)
for i in range(len(rph)): # pylint: disable=consider-using-enumerate
posp, negp, _ = comb_results_hr[i % num_comb_hr]
prompts_list[(hiresfix_type, i)] = (posp, negp)
extra_params["PPP HR combination"] = [str(1 + (i % num_comb_hr)) for i in range(len(rph))]
else:
# processes prompts
for prompttype, typeindex in prompts_list.keys():
log(
self.ppp_logger,
self.ppp_debug_level,
logging.INFO,
f"processing prompts ({prompttype}[{typeindex+1}])",
)
key = (
(hash_fullenv, calculated_seeds[typeindex], rpr[typeindex], rnr[typeindex])
if prompttype == regular_type
else (hash_fullenv, calculated_seeds[typeindex], rph[typeindex], rnh[typeindex])
)
cached = self.lru_cache.get(key)
if cached is None:
hsh, seed, prompt, negative_prompt = key
results = ppp.process_prompt(prompt, negative_prompt, seed)
posp, negp, _ = results[0]
cached = (posp, negp)
self.lru_cache.put(key, cached)
# adds also the result so i2i doesn't process it unnecessarily
self.lru_cache.put((hsh, seed, posp, negp), cached)
else:
log(self.ppp_logger, self.ppp_debug_level, logging.INFO, "result already in cache")
prompts_list[(prompttype, typeindex)] = cached
ppp.process_prompts_group_end()
# with open(os.path.join(os.path.dirname(os.path.realpath(__file__)), "..", "logs", f"last_prompts_{app.value}.txt"), "w", encoding="utf-8") as f:
# for (prompttype, typeindex), (posp, negp) in prompts_list.items():
# f.write(f"Key: {prompttype}[{typeindex}]\n")
# f.write(f"Seed: {calculated_seeds[typeindex]}\n")
# f.write(f"In Positive: {rpr[typeindex] if prompttype == regular_type else rph[typeindex]}\n")
# f.write(f"In Negative: {rnr[typeindex] if prompttype == regular_type else rnh[typeindex]}\n")
# f.write(f"Out Positive: {posp}\n")
# f.write(f"Out Negative: {negp}\n")
# f.write("\n")
# updates the prompts # updates the prompts
if rpr is not None and rnr is not None: regular_copy = (rpr.copy() if rpr else None, rnr.copy() if rnr else None)
for i, (seed, prompt, negative_prompt) in enumerate(zip(calculated_seeds, rpr, rnr)): hiresfix_copy = (rph.copy() if rph else None, rnh.copy() if rnh else None)
found = self.lru_cache.get((seed, hash(self.wildcards_obj), prompt, negative_prompt)) regular_changes = False
if found is not None: hiresfix_changes = False
rpr[i] = found[0] for (prompttype, typeindex), (posp, negp) in prompts_list.items():
rnr[i] = found[1] if prompttype == regular_type:
if rph is not None and rnh is not None: if rpr[typeindex].strip() != posp.strip() or rnr[typeindex].strip() != negp.strip():
for i, (seed, prompt, negative_prompt) in enumerate(zip(calculated_seeds, rph, rnh)): regular_changes = True
found = self.lru_cache.get((seed, hash(self.wildcards_obj), prompt, negative_prompt)) rpr[typeindex] = posp
if found is not None: rnr[typeindex] = negp
rph[i] = found[0] elif prompttype == hiresfix_type:
rnh[i] = found[1] if rph[typeindex].strip() != posp.strip() or rnh[typeindex].strip() != negp.strip():
hiresfix_changes = True
rph[typeindex] = posp
rnh[typeindex] = negp
t2 = time.time() # initialize extra generation parameters
if self.ppp_debug_level != DEBUG_LEVEL.none: if add_prompts:
self.ppp_logger.info(f"process time: {t2 - t1:.3f} seconds") if regular_changes:
extra_params["PPP original prompts"] = regular_copy[0]
extra_params["PPP original negative prompts"] = regular_copy[1]
if hiresfix_changes:
extra_params["PPP original HR prompts"] = hiresfix_copy[0]
extra_params["PPP original HR negative prompts"] = hiresfix_copy[1]
# 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.monotonic_ns()
log(
self.ppp_logger,
self.ppp_debug_level,
logging.INFO,
f"process time: {(t2 - t1) / 1_000_000_000:.3f} seconds",
)
def ppp_interrupt(self): def ppp_interrupt(self):
""" """
@@ -378,10 +578,25 @@ def on_ui_settings():
), ),
) )
shared.opts.add_option( shared.opts.add_option(
key="ppp_gen_ponysubstrings", key="ppp_gen_onwarning",
info=shared.OptionInfo( info=shared.OptionInfo(
PromptPostProcessor.DEFAULT_PONY_SUBSTRINGS, default=ONWARNING_CHOICES.warn.value,
label="Comma separated list of substrings to look for in the model full filename to flag it as Pony (case insensitive)", label="What to do on invalid content warnings",
component=gr.Radio,
component_args={
"choices": (
("Show warning in console", ONWARNING_CHOICES.warn.value),
("Stop the generation", ONWARNING_CHOICES.stop.value),
)
},
section=section,
),
)
shared.opts.add_option(
key="ppp_gen_strict_operators",
info=shared.OptionInfo(
default=PromptPostProcessor.DEFAULT_STRICT_OPERATORS,
label="Use strict operators",
section=section, section=section,
), ),
) )
@@ -394,6 +609,24 @@ 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,
),
)
shared.opts.add_option(
key="ppp_en_mappingsfolders",
info=shared.OptionInfo(
PPPExtraNetworkMappings.DEFAULT_ENMAPPINGS_FOLDER,
label="Extranetwork Mappings folders",
comment_after='<span class="info">(absolute or relative to the models folder)</span>',
section=section,
),
)
# wildcard settings # wildcard settings
shared.opts.add_option( shared.opts.add_option(
@@ -422,16 +655,16 @@ def on_ui_settings():
info=shared.OptionInfo( info=shared.OptionInfo(
default=import_old_settings( default=import_old_settings(
["ppp_gen_ifwildcards", "ppp_ifwildcards"], ["ppp_gen_ifwildcards", "ppp_ifwildcards"],
PromptPostProcessor.IFWILDCARDS_CHOICES.ignore.value, IFWILDCARDS_CHOICES.ignore.value,
), ),
label="What to do with remaining/invalid wildcards?", label="What to do with remaining/invalid wildcards?",
component=gr.Radio, component=gr.Radio,
component_args={ component_args={
"choices": ( "choices": (
("Ignore", PromptPostProcessor.IFWILDCARDS_CHOICES.ignore.value), ("Ignore", IFWILDCARDS_CHOICES.ignore.value),
("Remove", PromptPostProcessor.IFWILDCARDS_CHOICES.remove.value), ("Remove", IFWILDCARDS_CHOICES.remove.value),
("Add visible warning", PromptPostProcessor.IFWILDCARDS_CHOICES.warn.value), ("Add visible warning", IFWILDCARDS_CHOICES.warn.value),
("Stop the generation", PromptPostProcessor.IFWILDCARDS_CHOICES.stop.value), ("Stop the generation", IFWILDCARDS_CHOICES.stop.value),
) )
}, },
section=section, section=section,
@@ -518,6 +751,14 @@ def on_ui_settings():
section=section, section=section,
), ),
) )
shared.opts.add_option(
key="ppp_cup_extraseparators_include_eol",
info=shared.OptionInfo(
False,
label="The extra separators options also remove EOLs",
section=section,
),
)
shared.opts.add_option( shared.opts.add_option(
key="ppp_cup_breaks", key="ppp_cup_breaks",
info=shared.OptionInfo( info=shared.OptionInfo(
View File
+351
View File
@@ -0,0 +1,351 @@
from dataclasses import replace
import difflib
import os
import logging
from typing import Any, NamedTuple, Optional
import unittest
import datetime
from ppp_classes import IFWILDCARDS_CHOICES, ONWARNING_CHOICES, PPPStateOptions
from ppp_enmappings import PPPExtraNetworkMappings # type: ignore
from ppp_wildcards import PPPWildcards # type: ignore
from ppp import PromptPostProcessor # type: ignore
from ppp_logging import DEBUG_LEVEL, PromptPostProcessorLogFactory # type: ignore
class InputTuple(NamedTuple):
prompt: str = ""
negative_prompt: str = ""
class OutputTuple(NamedTuple):
prompt: str = ""
negative_prompt: str = ""
variables: dict[str, Any] = None
class TestPromptPostProcessorBase(unittest.TestCase):
"""
A test case class for testing the PromptPostProcessor class.
"""
def setUp(self, enable_file_logging=False):
"""
Set up the test case by initializing the necessary objects and configurations.
Args:
enable_file_logging (bool): Whether to enable logging to a file. Defaults to True.
"""
self.enable_file_logging = enable_file_logging
test_name = self.id().split(".")[-1] # Extract the test method name
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
if self.enable_file_logging:
log_filename = f"tests/logs/{test_name}_{timestamp}.log"
else:
log_filename = None # Disable file logging
self.lf = PromptPostProcessorLogFactory(log_filename)
self.ppp_logger = self.lf.log
self.ppp_logger.setLevel(logging.DEBUG)
self.grammar_content = None
self.defopts = PPPStateOptions(
debug_level=DEBUG_LEVEL.full,
on_warning=ONWARNING_CHOICES.stop,
strict_operators=True,
process_wildcards=True,
if_wildcards=IFWILDCARDS_CHOICES.stop,
choice_separator=", ",
keep_choices_order=False,
stn_separator=", ",
stn_ignore_repeats=True,
cup_do_cleanup=True,
cup_cleanup_variables=True,
cup_empty_constructs=True,
cup_extra_separators=True,
cup_extra_separators2=True,
cup_extra_separators_include_eol=False,
cup_extra_spaces=True,
cup_breaks=True,
cup_breaks_eol=False,
cup_ands=True,
cup_ands_eol=False,
cup_extranetwork_tags=True,
cup_merge_attention=True,
cup_remove_extranetwork_tags=False,
do_combinatorial=False,
combinatorial_limit=0,
combinatorial_shuffle=False,
)
self.def_env_info = {
"app": "tests",
"ppp_config": None,
"model_class": "SDXL",
"property_base": {"is_sdxl": True},
"models_path": "./webui/models",
"model_filename": "./webui/models/Stable-diffusion/testmodel.safetensors",
}
self.interrupted = False
self.wildcards_obj = PPPWildcards(self.lf.log)
self.extranetwork_maps_obj = PPPExtraNetworkMappings(self.lf.log)
self.wildcards_obj.refresh_wildcards(
DEBUG_LEVEL.full,
[
os.path.abspath(os.path.join(os.path.dirname(__file__), "wildcards")),
os.path.abspath(os.path.join(os.path.dirname(__file__), "wildcards2")),
],
"""
yaml_input:
wildcardI:
- choice1
- choice2
- choice3
""",
)
self.extranetwork_maps_obj.refresh_extranetwork_mappings(
DEBUG_LEVEL.full,
[os.path.abspath(os.path.join(os.path.dirname(__file__), "enmappings"))],
"""
""",
)
grammar_filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), "../grammar.lark")
with open(grammar_filename, "r", encoding="utf-8") as file:
self.grammar_content = file.read()
def interrupt(self):
self.interrupted = True
def init_ppp(
self,
ppp: Optional[str | PromptPostProcessor] = None,
combinatorial: bool = False,
combinatorial_limit: int = 0,
) -> PromptPostProcessor:
if isinstance(ppp, str):
if ppp == "nocup":
the_obj = PromptPostProcessor(
self.ppp_logger,
self.def_env_info,
replace(
self.defopts,
cup_do_cleanup=False,
cup_cleanup_variables=False,
cup_empty_constructs=False,
cup_extra_separators=False,
cup_extra_separators2=False,
cup_extra_separators_include_eol=False,
cup_extra_spaces=False,
cup_breaks=False,
cup_breaks_eol=False,
cup_ands=False,
cup_ands_eol=False,
cup_extranetwork_tags=False,
cup_merge_attention=False,
do_combinatorial=combinatorial,
combinatorial_limit=combinatorial_limit,
),
self.grammar_content,
self.interrupt,
self.wildcards_obj,
self.extranetwork_maps_obj,
)
elif ppp == "nostrict":
the_obj = PromptPostProcessor(
self.ppp_logger,
self.def_env_info,
replace(
self.defopts,
strict_operators=False,
do_combinatorial=combinatorial,
combinatorial_limit=combinatorial_limit,
),
self.grammar_content,
self.interrupt,
self.wildcards_obj,
self.extranetwork_maps_obj,
)
else:
the_obj = ppp
if not the_obj:
the_obj = PromptPostProcessor(
self.ppp_logger,
self.def_env_info,
replace(
self.defopts,
do_combinatorial=combinatorial,
combinatorial_limit=combinatorial_limit,
),
self.grammar_content,
self.interrupt,
self.wildcards_obj,
self.extranetwork_maps_obj,
)
return the_obj
def _comp_diff(self, result: str, expected: str) -> list[str]:
return list(
difflib.ndiff(
result.splitlines(True),
expected.splitlines(True),
linejunk=None,
charjunk=None,
)
)
def process(
self,
input_prompts: InputTuple,
expected_output: Optional[OutputTuple | list[OutputTuple]] = None,
seed: int = 1,
ppp: Optional[str | PromptPostProcessor] = None,
interrupted: bool = False,
combinatorial: bool = False,
combinatorial_limit: int = 0,
specific_wc_folders: Optional[list[str]] = None,
specific_em_folders: Optional[list[str]] = None,
):
"""
Process the prompt and compare the results with the expected prompts.
Args:
input_prompts (InputTuple): The input prompts.
expected_output (OutputTuple | list[OutputTuple], optional): The expected output. When a list is provided, the test will run once for each expected output, using the same input prompt, but seed will be incremented for each iteration.
seed (int, optional): The seed value. Defaults to 1.
ppp (Optional[str | PromptPostProcessor], optional): The PromptPostProcessor instance or type. Defaults to None.
interrupted (bool, optional): The interrupted flag. Defaults to False.
combinatorial (bool, optional): The combinatorial flag. Defaults to False.
combinatorial_limit (int, optional): The combinatorial limit. Defaults to 0.
specific_wc_folders (Optional[list[str]], optional): A list of specific wildcard folders to refresh. Defaults to None.
specific_em_folders (Optional[list[str]], optional): A list of specific extranetwork mapping folders to refresh. Defaults to None.
Returns:
None
"""
if specific_wc_folders is not None:
self.wildcards_obj.refresh_wildcards(
DEBUG_LEVEL.full,
specific_wc_folders,
)
if specific_em_folders is not None:
self.extranetwork_maps_obj.refresh_extranetwork_mappings(
DEBUG_LEVEL.full,
specific_em_folders,
)
the_obj: PromptPostProcessor = self.init_ppp(ppp, combinatorial, combinatorial_limit)
out = (
[OutputTuple("", "", None)]
if expected_output is None
else expected_output if isinstance(expected_output, list) else [expected_output]
)
if the_obj.state.options.do_combinatorial:
# combinatorial
errors = []
the_obj.process_prompts_group_start()
result = the_obj.process_prompt(
input_prompts.prompt,
input_prompts.negative_prompt,
seed,
)
the_obj.process_prompts_group_end()
self.assertTrue(
self.interrupted == interrupted,
f"Interrupted flag is incorrect: got {self.interrupted} but expected {interrupted}",
)
if not self.interrupted and expected_output is not None:
if len(result) != len(out):
errors.append(f"Incorrect number of combinations: got {len(result)} but expected {len(out)}")
for out_prompt, out_negative_prompt, out_variables in out:
found = None
for r_prompt, r_negative_prompt, r_variables in result:
if r_prompt == out_prompt and r_negative_prompt == out_negative_prompt:
found = OutputTuple(r_prompt, r_negative_prompt, r_variables)
break
if not found:
errors.extend(
[
"Combination not found in output",
"Prompt:",
out_prompt,
"Negative Prompt:",
out_negative_prompt,
]
)
elif out_variables:
missing_vars = {}
incorrect_vars = {}
expected_values = {}
sorted_var_keys = sorted(out_variables.keys())
for var_name in sorted_var_keys:
var_value = out_variables[var_name]
if var_name not in found.variables:
missing_vars[var_name] = var_value
elif found.variables[var_name] != var_value:
incorrect_vars[var_name] = found.variables[var_name]
expected_values[var_name] = var_value
if missing_vars or incorrect_vars:
errors.extend(
[
"Combination found, but variables do not match",
"Prompt:",
out_prompt,
"Negative Prompt:",
out_negative_prompt,
]
)
if missing_vars:
errors.append("Missing variables:")
errors.append(str(missing_vars))
if incorrect_vars:
errors.append("Incorrect variables:")
errors.extend(self._comp_diff(str(incorrect_vars), str(expected_values)))
if errors:
raise AssertionError("\n".join(errors))
return
# non-combinatorial
errors = []
the_obj.process_prompts_group_start()
for eo in out:
result = the_obj.process_prompt(
input_prompts.prompt,
input_prompts.negative_prompt,
seed,
)
self.assertTrue(
self.interrupted == interrupted,
f"Interrupted flag is incorrect: got {self.interrupted} but expected {interrupted}",
)
if not self.interrupted and expected_output is not None:
result_prompt, result_negative_prompt, output_variables = result[0] if result else (None, None, None)
if result_prompt != eo.prompt or result_negative_prompt != eo.negative_prompt:
errors.append("Incorrect result")
if result_prompt != eo.prompt:
errors.append("Prompt:")
errors.extend(self._comp_diff(result_prompt, eo.prompt))
if result_negative_prompt != eo.negative_prompt:
errors.append("Negative Prompt:")
errors.extend(self._comp_diff(result_negative_prompt, eo.negative_prompt))
if eo.variables:
missing_vars = {}
incorrect_vars = {}
expected_values = {}
sorted_var_keys = sorted(eo.variables.keys())
for var_name in sorted_var_keys:
var_value = eo.variables[var_name]
if var_name not in output_variables:
missing_vars[var_name] = var_value
elif output_variables[var_name] != var_value:
incorrect_vars[var_name] = output_variables[var_name]
expected_values[var_name] = var_value
if missing_vars or incorrect_vars:
errors.append("Result correct, but variables do not match")
if missing_vars:
errors.append("Missing variables:")
errors.append(str(missing_vars))
if incorrect_vars:
errors.append("Incorrect variables:")
errors.extend(self._comp_diff(str(incorrect_vars), str(expected_values)))
seed += 1
the_obj.process_prompts_group_end()
if errors:
raise AssertionError("\n".join(errors))
+20
View File
@@ -0,0 +1,20 @@
lora:
lora1:
- condition: _is_pony
name: lorapony
parameters: 0.8
triggers: ["triggerpony1", "triggerpony2"]
- condition: _is_illustrious
name: loraillustrious
parameters: "0.9:0.8"
triggers: ["triggerillustrious1", "triggerillustrious2"]
- triggers: ["triggergeneric1", "triggergeneric2", "{one|two}"]
loraany:
- condition: true
name: loraany1
parameters: 0.8
triggers: ["trigger1", "trigger2"]
- condition: true
name: loraany2
parameters: 1.0
triggers: ["trigger3", "trigger4"]
-865
View File
@@ -1,865 +0,0 @@
from collections import namedtuple
import logging
import unittest
import sys
import os
from ppp_wildcards import PPPWildcards
sys.path.append(os.path.join(sys.path[0], ".."))
from ppp import PromptPostProcessor
from ppp_logging import DEBUG_LEVEL, PromptPostProcessorLogFactory
PromptPair = namedtuple("PromptPair", ["prompt", "negative_prompt"], defaults=["", ""])
class TestPromptPostProcessor(unittest.TestCase):
"""
A test case class for testing the PromptPostProcessor class.
"""
def setUp(self):
"""
Set up the test case by initializing the necessary objects and configurations.
"""
lf = PromptPostProcessorLogFactory()
self.__ppp_logger = lf.log
self.__ppp_logger.setLevel(logging.DEBUG)
self.__defopts = {
"debug_level": DEBUG_LEVEL.full.value,
"pony_substrings": PromptPostProcessor.DEFAULT_PONY_SUBSTRINGS,
"process_wildcards": True,
"if_wildcards": PromptPostProcessor.IFWILDCARDS_CHOICES.ignore.value,
"choice_separator": ", ",
"keep_choices_order": False,
"stn_separator": ", ",
"stn_ignore_repeats": True,
"cleanup_empty_constructs": True,
"cleanup_extra_separators": True,
"cleanup_extra_separators2": True,
"cleanup_extra_spaces": True,
"cleanup_breaks": True,
"cleanup_breaks_eol": False,
"cleanup_ands": True,
"cleanup_ands_eol": False,
"cleanup_extranetwork_tags": True,
"cleanup_merge_attention": True,
"remove_extranetwork_tags": False,
}
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",
}
self.__interrupted = False
self.__wildcards_obj = PPPWildcards(lf.log)
self.__wildcards_obj.refresh_wildcards(
DEBUG_LEVEL.full,
[
os.path.abspath(os.path.join(os.path.dirname(__file__), "wildcards")),
os.path.abspath(os.path.join(os.path.dirname(__file__), "wildcards2")),
],
)
grammar_filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), "../grammar.lark")
with open(grammar_filename, "r", encoding="utf-8") as file:
self.__grammar_content = file.read()
self.__defppp = PromptPostProcessor(
self.__ppp_logger,
self.__interrupt,
self.__def_env_info,
self.__defopts,
self.__grammar_content,
self.__wildcards_obj,
)
self.__nocupppp = PromptPostProcessor(
self.__ppp_logger,
self.__interrupt,
self.__def_env_info,
{
**self.__defopts,
"cleanup_empty_constructs": False,
"cleanup_extra_separators": False,
"cleanup_extra_separators2": False,
"cleanup_extra_spaces": False,
"cleanup_breaks": False,
"cleanup_breaks_eol": False,
"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
def __process(
self,
input_prompts: PromptPair,
expected_output_prompts: PromptPair | list[PromptPair],
seed: int = 1,
ppp=None,
interrupted=False,
):
"""
Process the prompt and compare the results with the expected prompts.
Args:
input_prompts (PromptPair): The input prompts.
expected_output_prompts (PromptPair | list[PromptPair]): The expected prompts.
seed (int, optional): The seed value. Defaults to 1.
ppp (object, optional): The post-processor object. Defaults to None.
interrupted (bool, optional): The interrupted flag. Defaults to False.
Returns:
None
"""
the_obj = ppp or self.__defppp
out = expected_output_prompts if isinstance(expected_output_prompts, list) else [expected_output_prompts]
for eo in out:
result_prompt, result_negative_prompt = the_obj.process_prompt(
input_prompts.prompt,
input_prompts.negative_prompt,
seed,
)
self.assertEqual(self.__interrupted, interrupted, "Interrupted flag is incorrect")
if not self.__interrupted:
self.assertEqual(result_prompt, eo.prompt, "Incorrect prompt")
self.assertEqual(result_negative_prompt, eo.negative_prompt, "Incorrect negative prompt")
seed += 1
# Send To Negative tests
def test_stn_simple(self): # negtags with different parameters and separations
self.__process(
PromptPair(
"flowers<ppp:stn>red<ppp:/stn>, <ppp:stn s>green<ppp:/stn>, <ppp:stn e>blue<ppp:/stn><ppp:stn p0>yellow<ppp:/stn>, <ppp:stn p1>purple<ppp:/stn><ppp:stn p2>black<ppp:/stn>",
"<ppp:stn i0>normal quality<ppp:stn i1>, worse quality<ppp:stn i2>",
),
PromptPair("flowers", "red, green, yellow, normal quality, purple, worse quality, black, blue"),
)
def test_stn_complex(self): # complex negtags
self.__process(
PromptPair(
"<ppp:stn>red<ppp:/stn> ((<ppp:stn s>pink<ppp:/stn>)), flowers <ppp:stn e>purple<ppp:/stn>, <ppp:stn p0>mauve<ppp:/stn><ppp:stn e>blue<ppp:/stn>, <ppp:stn p0>yellow<ppp:/stn> <ppp:stn p1>green<ppp:/stn>",
"normal quality, <ppp:stn i0>, bad quality<ppp:stn i1>, worse quality",
),
PromptPair(
"flowers",
"red, (pink:1.21), normal quality, mauve, yellow, bad quality, green, worse quality, purple, blue",
),
)
def test_stn_complex_nocleanup(self): # complex negtags with no cleanup
self.__process(
PromptPair(
"<ppp:stn>red<ppp:/stn> ((<ppp:stn s>pink<ppp:/stn>)), flowers <ppp:stn e>purple<ppp:/stn>, <ppp:stn p0>mauve<ppp:/stn><ppp:stn e>blue<ppp:/stn>, <ppp:stn p0>yellow<ppp:/stn> <ppp:stn p1>green<ppp:/stn>",
"normal quality, <ppp:stn i0>, bad quality<ppp:stn i1>, worse quality",
),
PromptPair(
" (()), flowers , , ",
"red, ((pink)), normal quality, mauve, yellow, bad quality, green, worse quality, purple, blue",
),
ppp=self.__nocupppp,
)
def test_stn_inside_attention(self): # negtag inside attention
self.__process(
PromptPair(
"[<ppp:stn>neg1<ppp:/stn>] this is a ((test<ppp:stn e>neg2<ppp:/stn>) (test:2.0): 1.5 ) (red<ppp:stn>[square]<ppp:/stn>:1.5)",
"normal quality",
),
PromptPair(
"this is a ((test) (test:2):1.5) (red:1.5)", "[neg1], ([square]:1.5), normal quality, (neg2:1.65)"
),
)
def test_stn_inside_alternation(self): # negtag inside alternation
self.__process(
PromptPair(
"this is a (([complex<ppp:stn>neg1<ppp:/stn>|simple<ppp:stn>neg2<ppp:/stn>|regular<ppp:stn>neg3<ppp:/stn>] test)(test:2.0):1.5)",
"normal quality",
),
PromptPair(
"this is a (([complex|simple|regular] test)(test:2):1.5)",
"([neg1||]:1.65), ([|neg2|]:1.65), ([||neg3]:1.65), normal quality",
),
)
def test_stn_inside_alternation_recursive(self): # negtag inside alternation (recursive alternation)
self.__process(
PromptPair(
"this is a (([complex<ppp:stn>neg1<ppp:/stn>[one|two<ppp:stn>neg12<ppp:/stn>||three|four(<ppp:stn>neg14<ppp:/stn>)]|simple<ppp:stn>neg2<ppp:/stn>|regular<ppp:stn>neg3<ppp:/stn>] test)(test:2.0):1.5)",
"normal quality",
),
PromptPair(
"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",
),
)
def test_stn_inside_scheduling(self): # negtag inside scheduling
self.__process(
PromptPair("this is [abc<ppp:stn>neg1<ppp:/stn>:def<ppp:stn e>neg2<ppp:/stn>: 5 ]", "normal quality"),
[PromptPair("this is [abc:def:5]", "[neg1::5], normal quality, [neg2:5]")],
)
def test_stn_complex_features(self): # complex negtags with AND, BREAK and other features
self.__process(
PromptPair(
"[<ppp:stn>neg5<ppp:/stn>] this \\(is\\): a (([complex|simple<ppp:stn>neg6<ppp:/stn>|regular] test<ppp:stn>neg1<ppp:/stn>)(test:2.0):1.5) \nBREAK, BREAK with [abc<ppp:stn>neg4<ppp:/stn>:def<ppp:stn p0>neg2(neg3:1.6)<ppp:/stn>:5]:0.5 AND loratrigger <lora:xxx:1> AND AND hypernettrigger <hypernet:yyy>:0.3",
"normal quality, <ppp:stn i0>",
),
PromptPair(
"this \\(is\\): a (([complex|simple|regular] test)(test:2):1.5)\nBREAK with [abc:def:5]:0.5 AND loratrigger <lora:xxx:1> AND hypernettrigger <hypernet:yyy>:0.3",
"[neg5], ([|neg6|]:1.65), (neg1:1.65), [neg4::5], normal quality, [neg2(neg3:1.6):5]",
),
)
def test_stn_complex_features_newformat(self): # complex negtags with AND, BREAK and other features (new format)
self.__process(
PromptPair(
"[<ppp:stn>neg5<ppp:/stn>] this \\(is\\): a (([complex|simple<ppp:stn>neg6<ppp:/stn>|regular] test<ppp:stn>neg1<ppp:/stn>)(test:2.0):1.5) \nBREAK, BREAK with [abc<ppp:stn>neg4<ppp:/stn>:def<ppp:stn p0>neg2(neg3:1.6)<ppp:/stn>:5]:0.5 AND loratrigger <lora:xxx:1> AND AND hypernettrigger <hypernet:yyy>:0.3",
"normal quality, <ppp:stn i0>",
),
PromptPair(
"this \\(is\\): a (([complex|simple|regular] test)(test:2):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):1.5) (red:1.5)", "normal quality"),
)
def test_cl_complex(self): # complex cleanup
self.__process(
PromptPair(
" this is BREAKABLE a ((test)), ,AND AND(() [] <lora:test> ANDERSON (test:2.0):1.5) :o BREAK \n BREAK (red:1.5) ",
" [:hands, feet, :0.15]normal quality ",
),
PromptPair(
"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:1> test__yaml/wildcard7__", ""),
PromptPair("this is a test", ""),
ppp=PromptPostProcessor(
self.__ppp_logger,
self.__interrupt,
self.__def_env_info,
{**self.__defopts, "remove_extranetwork_tags": True},
self.__grammar_content,
self.__wildcards_obj,
),
)
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_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)",
"",
),
)
# Command tests
def test_cmd_stn_complex_features(self): # complex stn command with AND, BREAK and other features
self.__process(
PromptPair(
"[<ppp:stn>neg5<ppp:/stn>] this \\(is\\): a (([complex|simple<ppp:stn>neg6<ppp:/stn>|regular] test<ppp:stn>neg1<ppp:/stn>)(test:2.0):1.5) \nBREAK, BREAK with [abc<ppp:stn>neg4<ppp:/stn>:def<ppp:stn p0>neg2(neg3:1.6)<ppp:/stn>:5]:0.5 AND loratrigger <lora:xxx:1> AND AND hypernettrigger <hypernet:yyy>:0.3",
"normal quality, <ppp:stn i0>",
),
PromptPair(
"this \\(is\\): a (([complex|simple|regular] test)(test:2):1.5)\nBREAK with [abc:def:5]:0.5 AND loratrigger <lora:xxx:1> AND hypernettrigger <hypernet:yyy>:0.3",
"[neg5], ([|neg6|]:1.65), (neg1:1.65), [neg4::5], normal quality, [neg2(neg3:1.6):5]",
),
)
def test_cmd_if_complex_features(self): # complex if command
self.__process(
PromptPair(
"this \\(is\\): a (([complex|simple|regular] test)(test:2.0):1.5) \nBREAK, BREAK <ppp:if _is_sd1>with [abc<ppp:stn>neg4<ppp:/stn>:def:5]<ppp:/if>:0.5 AND <ppp:if _is_sd1>loratrigger <lora:xxx:1><ppp:elif _is_sdxl>hypernettrigger <hypernet:yyy><ppp:else>nothing<ppp:/if>:0.3",
"normal quality",
),
PromptPair(
"this \\(is\\): a (([complex|simple|regular] test)(test:2):1.5)\nBREAK :0.5 AND hypernettrigger <hypernet:yyy>:0.3",
"normal quality",
),
)
def test_cmd_if_nested(self): # nested if command
self.__process(
PromptPair(
"this is <ppp:if _sd eq 'sd1'>SD1<ppp:else><ppp:if _is_pony>PONY<ppp:else>SD2<ppp:/if><ppp:/if>", ""
),
PromptPair("this is PONY", ""),
ppp=PromptPostProcessor(
self.__ppp_logger,
self.__interrupt,
{
**self.__def_env_info,
"model_filename": "./webui/models/Stable-diffusion/ponymodel.safetensors",
},
self.__defopts,
self.__grammar_content,
self.__wildcards_obj,
),
)
def test_cmd_set_if(self): # set and if commands
self.__process(
PromptPair("<ppp:set v>value<ppp:/set>this test is <ppp:if v>OK<ppp:else>not OK<ppp:/if>", ""),
PromptPair("this test is OK", ""),
)
def test_cmd_set_eval_if(self): # set and if commands
self.__process(
PromptPair("<ppp:set v evaluate>value<ppp:/set>this test is <ppp:if v>OK<ppp:else>not OK<ppp:/if>", ""),
PromptPair("this test is OK", ""),
)
def test_cmd_set_if_echo_nested(self): # nested set, if and echo commands
self.__process(
PromptPair(
"<ppp:set v1>1<ppp:/set><ppp:if v1 gt 0><ppp:set v2>OK<ppp:/set><ppp:/if><ppp:if v2 eq 'OK'><ppp:echo v2><ppp:else>not OK<ppp:/if> <ppp:echo v2>NOK<ppp:/echo> <ppp:echo v3>OK<ppp:/echo>",
"",
),
PromptPair("OK OK OK", ""),
)
def test_cmd_set_if2(self): # set and more complex if commands
self.__process(
PromptPair(
"First: <ppp:set v>value1<ppp:/set>this test is <ppp:if v in ('value1','value2')>OK<ppp:else>not OK<ppp:/if>\nSecond: <ppp:set v2>value3<ppp:/set>this test is <ppp:if not v2 in ('value1','value2')>OK<ppp:else>not OK<ppp:/if>",
"",
),
PromptPair("First: this test is OK\nSecond: this test is OK", ""),
)
def test_cmd_set_add_if(self): # set, add and if commands
self.__process(
PromptPair(
"<ppp:set v>value<ppp:/set><ppp:set v add>2<ppp:/set>this test is <ppp:if v eq 'value2'>OK<ppp:else>not OK<ppp:/if>",
"",
),
PromptPair("this test is OK", ""),
)
def test_cmd_set_add_DP_if(self): # set, add (DP format) and if commands
self.__process(
PromptPair(
"${v=value}${v+=2}this test is <ppp:if v eq 'value2'>OK<ppp:else>not OK<ppp:/if>",
"",
),
PromptPair("this test is OK", ""),
)
def test_cmd_set_immediateeval(self): # set (DP format) with mixed evaluation
self.__process(
PromptPair(
"${var=!__yaml/wildcard1__}the choices are: ${var}, ${var}, ${var2:default}, ${var3=__yaml/wildcard1__}${var3}, ${var3}",
"",
),
PromptPair("the choices are: choice2, choice2, default, choice3, choice1", ""),
ppp=self.__nocupppp,
)
def test_cmd_set_mixeval(self): # set and add (DP format) with mixed evaluation
self.__process(
PromptPair(
"${var=__yaml/wildcard1__}the choices are: ${var}, ${var}, ${var+=, __yaml/wildcard2__}${var}, ${var}, ${var+=!, __yaml/wildcard3__}${var}, ${var}",
"",
),
PromptPair(
"the choices are: choice2, choice3, choice1, choice1- choice2 -choice3, choice2, choice2 -choice1-choice3, choice2, choice3-choice1- choice2 , choice1, choice2 , choice2, choice3-choice1- choice2 , choice1, choice2 ",
"",
),
ppp=self.__nocupppp,
)
# Choices tests
def test_ch_choices(self): # simple choices with weights
self.__process(
PromptPair("the choices are: {3::choice1|2::choice2|choice3}", ""),
PromptPair("the choices are: choice2", ""),
ppp=self.__nocupppp,
)
def test_ch_unsupportedsampler(self): # unsupported sampler
self.__process(
PromptPair("the choices are: {@choice1|choice2|choice3}", ""),
PromptPair("", ""),
ppp=self.__nocupppp,
interrupted=True,
)
def test_ch_choices_withcomments(self): # choices with comments and multiline
self.__process(
PromptPair(
"the choices are: {\n3::choice1 # this is option 1\n|2::choice2\n# this was option 2\n|choice3 # this is option 3\n}",
"",
),
PromptPair("the choices are: choice2", ""),
ppp=self.__nocupppp,
)
def test_ch_choices_multiple(self): # choices with multiple selection
self.__process(
PromptPair("the choices are: {~2$$, $$3::choice1|2:: choice2 |choice3}", ""),
PromptPair("the choices are: choice2 , choice3", ""),
ppp=self.__nocupppp,
)
def test_ch_choices_if_multiple(self): # choices with if and multiple selection
self.__process(
PromptPair("the choices are: {2$$, $$3::choice1|2 if _is_sd1::choice2|choice3}", ""),
PromptPair("the choices are: choice1, choice3", ""),
ppp=self.__nocupppp,
)
def test_ch_choices_set_if_multiple(self): # choices with if user variable and multiple selection
self.__process(
PromptPair("${var=test}the choices are: {2$$, $$3::choice1|2 if not var eq 'test'::choice2|choice3}", ""),
PromptPair("the choices are: choice1, choice3", ""),
ppp=self.__nocupppp,
)
def test_ch_choices_set_if_nested(self): # nested choices with if user variable and multiple selection
self.__process(
PromptPair(
"${var=test}the choices are: {2$$, $$3::choice1${var2=test2} {if var2 eq 'test2'::choice11|choice12}|2 if not var eq 'test'::choice2|choice3}",
"",
),
PromptPair("the choices are: choice1 choice11, choice3", ""),
ppp=self.__nocupppp,
)
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
self.__process(
PromptPair("__bad_wildcard__", "{option1|option2}"),
PromptPair("__bad_wildcard__", "{option1|option2}"),
ppp=PromptPostProcessor(
self.__ppp_logger,
self.__interrupt,
self.__def_env_info,
{
**self.__defopts,
"process_wildcards": False,
"if_wildcards": PromptPostProcessor.IFWILDCARDS_CHOICES.ignore.value,
},
self.__grammar_content,
self.__wildcards_obj,
),
)
def test_wc_remove(self): # wildcards with remove option
self.__process(
PromptPair(
"[<ppp:stn>neg5<ppp:/stn>] this is: __bad_wildcard__ a (([complex|simple<ppp:stn>neg6<ppp:/stn>|regular] test<ppp:stn>neg1<ppp:/stn>)(test:2.0):1.5) \nBREAK, BREAK with [abc<ppp:stn>neg4<ppp:/stn>:def<ppp:stn p0>neg2(neg3:1.6)<ppp:/stn>:5] <lora:xxx:1>",
"normal quality, <ppp:stn i0> {option1|option2}",
),
PromptPair(
"this is: a (([complex|simple|regular] test)(test:2):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_env_info,
{
**self.__defopts,
"process_wildcards": False,
"if_wildcards": PromptPostProcessor.IFWILDCARDS_CHOICES.remove.value,
},
self.__grammar_content,
self.__wildcards_obj,
),
)
def test_wc_warn(self): # wildcards with warn option
self.__process(
PromptPair("__bad_wildcard__", "{option1|option2}"),
PromptPair(PromptPostProcessor.WILDCARD_WARNING + "__bad_wildcard__", "{option1|option2}"),
ppp=PromptPostProcessor(
self.__ppp_logger,
self.__interrupt,
self.__def_env_info,
{
**self.__defopts,
"process_wildcards": False,
"if_wildcards": PromptPostProcessor.IFWILDCARDS_CHOICES.warn.value,
},
self.__grammar_content,
self.__wildcards_obj,
),
)
def test_wc_stop(self): # wildcards with stop option
self.__process(
PromptPair("__bad_wildcard__", "{option1|option2}"),
PromptPair(
PromptPostProcessor.WILDCARD_STOP.format("__bad_wildcard__") + "__bad_wildcard__",
"{option1|option2}",
),
ppp=PromptPostProcessor(
self.__ppp_logger,
self.__interrupt,
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__", ""),
PromptPair("the choices are: choice2", ""),
ppp=self.__nocupppp,
)
def test_wc_wildcard1a_json(self): # simple json wildcard
self.__process(
PromptPair("the choices are: __json/wildcard1__", ""),
PromptPair("the choices are: choice2", ""),
ppp=self.__nocupppp,
)
def test_wc_wildcard1a_yaml(self): # simple yaml wildcard
self.__process(
PromptPair("the choices are: __yaml/wildcard1__", ""),
PromptPair("the choices are: choice2", ""),
ppp=self.__nocupppp,
)
def test_wc_wildcard1b_text(self): # simple text wildcard with multiple choices
self.__process(
PromptPair("the choices are: __2-$$text/wildcard1__", ""),
PromptPair("the choices are: choice3, choice1", ""),
ppp=self.__nocupppp,
)
def test_wc_wildcard1b_json(self): # simple json wildcard with multiple choices
self.__process(
PromptPair("the choices are: __2-$$json/wildcard1__", ""),
PromptPair("the choices are: choice3, choice1", ""),
ppp=self.__nocupppp,
)
def test_wc_wildcard1b_yaml(self): # simple yaml wildcard with multiple choices
self.__process(
PromptPair("the choices are: __2-$$yaml/wildcard1__", ""),
PromptPair("the choices are: choice3, choice1", ""),
ppp=self.__nocupppp,
)
def test_wc_wildcard2_text(self): # simple text wildcard with default options
self.__process(
PromptPair("the choices are: __text/wildcard2__", ""),
PromptPair("the choices are: choice3-choice1", ""),
ppp=self.__nocupppp,
)
def test_wc_wildcard2_json(self): # simple json wildcard with default options
self.__process(
PromptPair("the choices are: __json/wildcard2__", ""),
PromptPair("the choices are: choice3-choice1", ""),
ppp=self.__nocupppp,
)
def test_wc_wildcard2_yaml(self): # simple yaml wildcard with default options
self.__process(
PromptPair("the choices are: __yaml/wildcard2__", ""),
PromptPair("the choices are: choice3-choice1", ""),
ppp=self.__nocupppp,
)
def test_wc_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__", ""),
PromptPair("the choices are: choice3,choice1- choice2 ,choice3", ""),
ppp=self.__nocupppp,
)
def test_wc_nested_wildcard_json(self): # nested json wildcard with repeating multiple choices
self.__process(
PromptPair("the choices are: __r3$$-$$json/wildcard3__", ""),
PromptPair("the choices are: choice3,choice1- choice2 ,choice3", ""),
ppp=self.__nocupppp,
)
def test_wc_nested_wildcard_yaml(self): # nested yaml wildcard with repeating multiple choices
self.__process(
PromptPair("the choices are: __r3$$-$$yaml/wildcard3__", ""),
PromptPair("the choices are: choice3,choice1- choice2 ,choice3", ""),
ppp=self.__nocupppp,
)
def test_wc_wildcard4_yaml(self): # simple yaml wildcard with one option
self.__process(
PromptPair("the choices are: __yaml/wildcard4__", ""),
PromptPair("the choices are: inline text", ""),
ppp=self.__nocupppp,
)
def test_wc_wildcard6_yaml(self): # simple yaml wildcard with object formatted choices
self.__process(
PromptPair("the choices are: __yaml/wildcard6__", ""),
PromptPair("the choices are: choice2", ""),
ppp=self.__nocupppp,
)
def test_wc_choice_wildcard_mix(self): # choices with wildcard mix
self.__process(
PromptPair("the choices are: {__~2$$yaml/wildcard2__|choice0}", ""),
[
PromptPair("the choices are: choice0", ""),
PromptPair("the choices are: choice1, choice3", ""),
PromptPair("the choices are: choice1, choice3", ""),
],
ppp=self.__nocupppp,
)
def test_wc_unsupportedsampler(self): # unsupported sampler
self.__process(
PromptPair("the choices are: __@yaml/wildcard2__", ""),
PromptPair("", ""),
ppp=self.__nocupppp,
interrupted=True,
)
def test_wc_wildcard_globbing(self): # wildcard with globbing
self.__process(
PromptPair("the choices are: __yaml/wildcard[12]__, __yaml/wildcard?__", ""),
PromptPair("the choices are: choice3-choice2, <lora:test2:1>- choice2 -choice3", ""),
ppp=self.__nocupppp,
)
def test_wc_wildcardwithvar(self): # wildcard with inline variable
self.__process(
PromptPair("the choices are: __yaml/wildcard5(var=test)__, __yaml/wildcard5__", ""),
PromptPair("the choices are: inline test, inline default", ""),
ppp=self.__nocupppp,
)
def test_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(
# "__text/wildcard1__ (__text/wildcard2__) (__text/wildcard3__:1.5) [__text/wildcard1__] [__text/wildcard2__:__text/wildcard3__:0.5] [__text/wildcard1__|__text/wildcard2__] # <lora:__text/wildcard3__:1> {opt1_1|opt1_2} ({opt2_1|opt2_2}) ({opt3_1|opt3_2}:1.5) [{opt4_1|opt4_2}] [{opt5_1|opt5_2}:{opt6_1|opt6_2}:0.5] [{opt7_1|opt7_2}|{opt8_1|opt8_2}] # <lora:{opt9_1|opt9_2}:1> {opt1_1|__text/wildcard1__} ({opt2_1|__text/wildcard2__}) ({opt3_1|__text/wildcard3__}:1.5) [{opt4_1|__text/wildcard1__}] [{opt5_1|__text/wildcard2__}# :{opt6_1|__text/wildcard3__}:0.5] [{opt7_1|__text/wildcard1__}|{opt8_1|__text/wildcard2__}] {<lora:opt9_1:1>|<lora:__text/wildcard3__:1>}",
# "",
# ),
# PromptPair(
# "choice2 ( choice2 -choice1) (choice1, choice2 :1.5) [choice1] [choice1-choice1:choice3, choice2 :0.5] [choice2| choice2 - choice2 ] <lora: choice2 ,choice1:1> opt1_1 # (opt2_2) (opt3_1:1.5) [opt4_2] [opt5_1:opt6_2:0.5] [opt7_1|opt8_1] <lora:opt9_2:1> choice3 (opt2_1) (choice1,choice3:1.5) [choice1] [choice3-choice3:opt6_1:0.5] [opt7_1|# opt8_1] <lora:choice1, choice2 :1>",
# "",
# ),
# ppp=self.__nocupppp,
# )
# def test_real(self):
# self.__wildcards_obj.refresh_wildcards(
# DEBUG_LEVEL.full,
# ["D:\\AI\\SD\\_configuraciones\\acb-wildcards\\wildcards"],
# )
# self.__process(
# PromptPair(
# "${separator=()}, __quality/high__ __misc/sep__, photograph of a __character__",
# "__negatives/ng_generic__",
# ),
# PromptPair("", ""),
# )
if __name__ == "__main__":
unittest.main()
+169
View File
@@ -0,0 +1,169 @@
from dataclasses import replace
from ppp import PromptPostProcessor # type: ignore
from .base_tests import OutputTuple, InputTuple, TestPromptPostProcessorBase
if __name__ == "__main__":
raise SystemExit("This script must not be run directly")
class TestChoices(TestPromptPostProcessorBase):
def setUp(self): # pylint: disable=arguments-differ
super().setUp(enable_file_logging=False)
# Choices tests
def test_ch_choices(self): # simple choices with weights
self.process(
InputTuple("the choices are: {3::choice1|2::choice2|choice3}", ""),
OutputTuple("the choices are: choice2", ""),
ppp="nocup",
)
def test_ch_cyclical(self): # cyclical sampler cycles through all choices
ppp_instance = self.init_ppp("nocup")
self.process(
InputTuple("the choices are: {@choice1|choice2|choice3}", ""),
[
OutputTuple("the choices are: choice1", ""),
OutputTuple("the choices are: choice2", ""),
OutputTuple("the choices are: choice3", ""),
OutputTuple("the choices are: choice1", ""), # cycles back
],
ppp=ppp_instance,
)
def test_ch_cyclical_multiple_constructs(self): # two independent @ constructs cycle together
ppp_instance = self.init_ppp("nocup")
self.process(
InputTuple("{@a|b} {@c|d}", ""),
[
OutputTuple("a c", ""),
OutputTuple("a d", ""),
OutputTuple("b c", ""),
OutputTuple("b d", ""),
OutputTuple("a c", ""), # cycles back
],
ppp=ppp_instance,
)
def test_ch_cyclical_resets_on_prompt_change(self): # state resets when the prompt pair changes
ppp_instance = self.init_ppp("nocup")
# Advance the cycle to position 1 (choice2).
self.process(
InputTuple("the choices are: {@choice1|choice2|choice3}", ""),
[
OutputTuple("the choices are: choice1", ""),
OutputTuple("the choices are: choice2", ""),
],
ppp=ppp_instance,
)
# A different prompt must restart from position 0 (choice1).
self.process(
InputTuple("the choices are: {@choice1|choice2|choice3} different", ""),
OutputTuple("the choices are: choice1 different", ""),
ppp=ppp_instance,
)
def test_ch_cyclical_mixed_samplers(self): # @ construct cycles while a ~ construct alongside is unaffected
ppp_instance = self.init_ppp("nocup")
self.process(
InputTuple("{@a|b|c} {x|y}", ""),
[
OutputTuple("a y", ""),
OutputTuple("b x", ""),
OutputTuple("c x", ""),
OutputTuple("a y", ""), # @ cycles back
],
ppp=ppp_instance,
)
def test_ch_choices_withcomments(self): # choices with comments and multiline
self.process(
InputTuple(
"the choices are: {\n3::choice1 # this is option 1\n|2::choice2\n# this was option 2\n|choice3 # this is option 3\n}",
"",
),
OutputTuple("the choices are: choice2", ""),
ppp="nocup",
)
def test_ch_choices_multiple(self): # choices with multiple selection
self.process(
InputTuple("the choices are: {~2$$, $$3::choice1|2:: choice2 |choice3}", ""),
OutputTuple("the choices are: choice2 , choice3", ""),
ppp="nocup",
)
def test_ch_choices_if_multiple(self): # choices with if and multiple selection
self.process(
InputTuple("the choices are: {2$$, $$3::choice1|2 if _is_sd1::choice2|choice3}", ""),
OutputTuple("the choices are: choice1, choice3", ""),
ppp="nocup",
)
def test_ch_choices_set_if_multiple(self): # choices with if user variable and multiple selection
self.process(
InputTuple("${var=test}the choices are: {2$$, $$3::choice1|2 if not var eq 'test'::choice2|choice3}", ""),
OutputTuple("the choices are: choice1, choice3", ""),
ppp="nocup",
)
def test_ch_choices_set_if_nested(self): # nested choices with if user variable and multiple selection
self.process(
InputTuple(
"${var=test}the choices are: {2$$, $$3::choice1${var2=test2} {if var2 eq 'test2'::choice11|choice12}|2 if not var eq 'test'::choice2|choice3}",
"",
),
OutputTuple("the choices are: choice1 choice11, choice3", ""),
ppp="nocup",
)
def test_ch_choicesinsidelora(self): # simple choices inside a lora
self.process(
InputTuple("<lora:test1:1><lora:test__other__name:1><lora:test2:{0.2|0.5|0.7|1}>", ""),
OutputTuple("<lora:test1:1><lora:test__other__name:1><lora:test2:0.7>", ""),
ppp="nocup",
)
def test_ch_removelorawithchoices(self):
self.process(
InputTuple("<lora:test1:1><lora:test2:{0.2|0.5|0.7|1}>", ""),
OutputTuple("", ""),
ppp=PromptPostProcessor(
self.ppp_logger,
self.def_env_info,
replace(
self.defopts,
cup_remove_extranetwork_tags=True,
),
self.grammar_content,
self.interrupt,
self.wildcards_obj,
self.extranetwork_maps_obj,
),
)
def test_ch_cmd_includewildcard(self):
self.process(
InputTuple("{ch_one|ch_two|%0.5::include yaml/wildcard1}", ""),
OutputTuple("ch_two", ""),
ppp="nocup",
)
# Combinatorial
def test_ch_combinatorial(self):
self.process(
InputTuple("{choice1|choice2|choice3}, ${v:{option1|option2}}", ""),
[
OutputTuple("choice1, option1", ""),
OutputTuple("choice1, option2", ""),
OutputTuple("choice2, option1", ""),
OutputTuple("choice2, option2", ""),
OutputTuple("choice3, option1", ""),
OutputTuple("choice3, option2", "", {"v": "option2"}),
],
combinatorial=True,
)
+199
View File
@@ -0,0 +1,199 @@
import logging
from dataclasses import replace
from ppp import PromptPostProcessor # type: ignore
from .base_tests import OutputTuple, InputTuple, TestPromptPostProcessorBase
if __name__ == "__main__":
raise SystemExit("This script must not be run directly")
class TestCleanup(TestPromptPostProcessorBase):
def setUp(self): # pylint: disable=arguments-differ
super().setUp(enable_file_logging=False)
# Cleanup tests
def test_cl_simple(self): # simple cleanup
self.process(
InputTuple(" this is a ((test ), , , (), , [] ( , test ,:2.0):1.5), (red:1.5) ", " normal quality "),
OutputTuple("this is a ((test), (test,:2):1.5), (red:1.5)", "normal quality"),
)
def test_cl_complex(self): # complex cleanup
self.process(
InputTuple(
" this is BREAKABLE a ((test)), ,AND AND(() [] <lora:test> ANDERSON (test:2.0):1.5) :o BREAK \n BREAK (red:1.5) ",
" [:hands, feet, :0.15]normal quality ",
),
OutputTuple(
"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(
InputTuple("this is a <lora:test:1> test__yaml/wildcard7__", ""),
OutputTuple("this is a test", ""),
ppp=PromptPostProcessor(
self.ppp_logger,
self.def_env_info,
replace(
self.defopts,
cup_remove_extranetwork_tags=True,
),
self.grammar_content,
self.interrupt,
self.wildcards_obj,
self.extranetwork_maps_obj,
),
)
def test_cl_dontremoveseparatorsoneol(self): # don't remove separators on eol
self.process(
InputTuple("this is a test,\nsecond line", ""),
OutputTuple("this is a test,\nsecond line", ""),
ppp=PromptPostProcessor(
self.ppp_logger,
self.def_env_info,
replace(
self.defopts,
cup_extra_separators2=False,
cup_extra_separators_include_eol=False,
),
self.grammar_content,
self.interrupt,
self.wildcards_obj,
self.extranetwork_maps_obj,
),
)
def test_cl_separatorswitheol(self): # don't remove eols with the separators
self.process(
InputTuple(
"""{ (d:0.9) ,, (l:1.1) | (l:1.1) (d:0.9),,, }
(l:1.1)
(d:0.9)""",
"",
),
OutputTuple(
""" (l:1.1) (d:0.9),
(l:1.1)
(d:0.9)""",
"",
),
ppp=PromptPostProcessor(
self.ppp_logger,
self.def_env_info,
replace(
self.defopts,
cup_empty_constructs=False,
cup_extra_separators=True,
cup_extra_separators2=False,
cup_extra_separators_include_eol=False,
cup_extra_spaces=False,
cup_breaks=False,
cup_breaks_eol=False,
cup_ands=False,
cup_ands_eol=False,
cup_extranetwork_tags=False,
cup_merge_attention=False,
),
self.grammar_content,
self.interrupt,
self.wildcards_obj,
self.extranetwork_maps_obj,
),
)
def test_cl_mergeattention(self): # merge attention
self.process(
InputTuple(
"this is (a test:0.9) of (attention (merging:1.2)) where ((this)) ((is joined:1.2)) and ([this too]:1.3)",
"",
),
OutputTuple(
"this is [a test] of (attention (merging:1.2)) where (this:1.21) (is joined:1.32) and (this too:1.17)",
"",
),
)
def test_cl_not_mergeattention(self): # not merge attention
self.process(
InputTuple(
"this is (a test:0.9) of not (attention (merging:1.2)) where ((this)) ((is not joined:1.2)) and neither is ([this]:1.3)",
"",
),
OutputTuple(
"this is (a test:0.9) of not (attention (merging:1.2)) where ((this)) ((is not joined:1.2)) and neither is ([this]:1.3)",
"",
),
ppp="nocup",
)
# Result warnings tests
def test_cl_warn_unmatched_open_paren(self): # unmatched open parenthesis triggers warning
with self.assertLogs("PromptPostProcessor", level=logging.WARNING) as cm:
self.process(
InputTuple("(unclosed paren", ""),
OutputTuple("(unclosed paren", ""),
)
self.assertTrue(
any("Unmatched" in msg for msg in cm.output),
"Expected an 'Unmatched' warning for open parenthesis",
)
def test_cl_warn_unmatched_close_paren(self): # unmatched close parenthesis triggers warning
with self.assertLogs("PromptPostProcessor", level=logging.WARNING) as cm:
self.process(
InputTuple("extra close paren)", ""),
OutputTuple("extra close paren)", ""),
)
self.assertTrue(
any("Unmatched" in msg for msg in cm.output),
"Expected an 'Unmatched' warning for close parenthesis",
)
def test_cl_warn_mismatched_brackets(self): # mismatched bracket types trigger warning
with self.assertLogs("PromptPostProcessor", level=logging.WARNING) as cm:
self.process(
InputTuple("(mismatched]", ""),
OutputTuple("(mismatched]", ""),
)
self.assertTrue(
any("Mismatched" in msg or "Unmatched" in msg for msg in cm.output),
"Expected a 'Mismatched' or 'Unmatched' warning for bracket mismatch",
)
def test_cl_warn_unmatched_open_bracket(self): # unmatched open bracket triggers warning
with self.assertLogs("PromptPostProcessor", level=logging.WARNING) as cm:
self.process(
InputTuple("unclosed [bracket", ""),
OutputTuple("unclosed [bracket", ""),
)
self.assertTrue(
any("Unmatched" in msg for msg in cm.output),
"Expected an 'Unmatched' warning for open bracket",
)
def test_cl_warn_unmatched_complex(self): # unmatched complex case triggers warning
with self.assertLogs("PromptPostProcessor", level=logging.WARNING) as cm:
self.process(
InputTuple("[(unmatched [bracket))", ""),
OutputTuple("[(unmatched [bracket))", ""),
)
self.assertTrue(
any("Unmatched" in msg for msg in cm.output),
"Expected an 'Unmatched' warning",
)
def test_cl_warn_escaped_unmatched_no_false_warning(self): # escaped unmatched paren/bracket does not trigger warning
with self.assertNoLogs("PromptPostProcessor", level=logging.WARNING):
self.process(
InputTuple(r"text with \(escaped unmatched\]", ""),
OutputTuple(r"text with \(escaped unmatched\]", ""),
)
+438
View File
@@ -0,0 +1,438 @@
from ppp import PromptPostProcessor # type: ignore
from .base_tests import OutputTuple, InputTuple, TestPromptPostProcessorBase
if __name__ == "__main__":
raise SystemExit("This script must not be run directly")
class TestHosts(TestPromptPostProcessorBase):
def setUp(self): # pylint: disable=arguments-differ
super().setUp(enable_file_logging=False)
# Hosts tests
def test_host_attention_parentheses(self):
self.process(
InputTuple(
"[test1] (test2) (test3:1.5) [(test4)]",
"",
),
OutputTuple("(test1:0.9) (test2) (test3:1.5) (test4:0.99)", ""),
ppp=PromptPostProcessor(
self.ppp_logger,
{
**self.def_env_info,
"ppp_config": {"hosts": {"tests": {"attention": "parentheses"}}},
},
self.defopts,
self.grammar_content,
self.interrupt,
self.wildcards_obj,
self.extranetwork_maps_obj,
),
)
def test_host_attention_disable(self):
self.process(
InputTuple(
"[test1] (test2) (test3:1.5)",
"",
),
OutputTuple("test1 test2 test3", ""),
ppp=PromptPostProcessor(
self.ppp_logger,
{
**self.def_env_info,
"ppp_config": {"hosts": {"tests": {"attention": "disable"}}},
},
self.defopts,
self.grammar_content,
self.interrupt,
self.wildcards_obj,
self.extranetwork_maps_obj,
),
)
def test_host_attention_remove(self):
self.process(
InputTuple(
"[test1] (test2) (test3:1.5)",
"",
),
OutputTuple("", ""),
ppp=PromptPostProcessor(
self.ppp_logger,
{
**self.def_env_info,
"ppp_config": {"hosts": {"tests": {"attention": "remove"}}},
},
self.defopts,
self.grammar_content,
self.interrupt,
self.wildcards_obj,
self.extranetwork_maps_obj,
),
)
def test_host_attention_error(self):
self.process(
InputTuple(
"[test1] (test2) (test3:1.5)",
"",
),
OutputTuple("", ""),
ppp=PromptPostProcessor(
self.ppp_logger,
{
**self.def_env_info,
"ppp_config": {"hosts": {"tests": {"attention": "error"}}},
},
self.defopts,
self.grammar_content,
self.interrupt,
self.wildcards_obj,
self.extranetwork_maps_obj,
),
interrupted=True,
)
def test_host_scheduling_before(self):
self.process(
InputTuple(
"[test1:test2:0.5]",
"",
),
OutputTuple("test1", ""),
ppp=PromptPostProcessor(
self.ppp_logger,
{
**self.def_env_info,
"ppp_config": {"hosts": {"tests": {"scheduling": "before"}}},
},
self.defopts,
self.grammar_content,
self.interrupt,
self.wildcards_obj,
self.extranetwork_maps_obj,
),
)
def test_host_scheduling_after(self):
self.process(
InputTuple(
"[test1:test2:0.5]",
"",
),
OutputTuple("test2", ""),
ppp=PromptPostProcessor(
self.ppp_logger,
{
**self.def_env_info,
"ppp_config": {"hosts": {"tests": {"scheduling": "after"}}},
},
self.defopts,
self.grammar_content,
self.interrupt,
self.wildcards_obj,
self.extranetwork_maps_obj,
),
)
def test_host_scheduling_first(self):
self.process(
InputTuple(
"[test1::0.5] [:test2:0.5] [test3:test4:0.5]",
"",
),
OutputTuple("test1 test3", ""),
ppp=PromptPostProcessor(
self.ppp_logger,
{
**self.def_env_info,
"ppp_config": {"hosts": {"tests": {"scheduling": "first"}}},
},
self.defopts,
self.grammar_content,
self.interrupt,
self.wildcards_obj,
self.extranetwork_maps_obj,
),
)
def test_host_scheduling_remove(self):
self.process(
InputTuple(
"[test1:test2:0.5]",
"",
),
OutputTuple("", ""),
ppp=PromptPostProcessor(
self.ppp_logger,
{
**self.def_env_info,
"ppp_config": {"hosts": {"tests": {"scheduling": "remove"}}},
},
self.defopts,
self.grammar_content,
self.interrupt,
self.wildcards_obj,
self.extranetwork_maps_obj,
),
)
def test_host_scheduling_error(self):
self.process(
InputTuple(
"[test1:test2:0.5]",
"",
),
OutputTuple("", ""),
ppp=PromptPostProcessor(
self.ppp_logger,
{
**self.def_env_info,
"ppp_config": {"hosts": {"tests": {"scheduling": "error"}}},
},
self.defopts,
self.grammar_content,
self.interrupt,
self.wildcards_obj,
self.extranetwork_maps_obj,
),
interrupted=True,
)
def test_host_alternation_first(self):
self.process(
InputTuple(
"[test1|test2|test3]",
"",
),
OutputTuple("test1", ""),
ppp=PromptPostProcessor(
self.ppp_logger,
{
**self.def_env_info,
"ppp_config": {"hosts": {"tests": {"alternation": "first"}}},
},
self.defopts,
self.grammar_content,
self.interrupt,
self.wildcards_obj,
self.extranetwork_maps_obj,
),
)
def test_host_alternation_remove(self):
self.process(
InputTuple(
"[test1|test2|test3]",
"",
),
OutputTuple("", ""),
ppp=PromptPostProcessor(
self.ppp_logger,
{
**self.def_env_info,
"ppp_config": {"hosts": {"tests": {"alternation": "remove"}}},
},
self.defopts,
self.grammar_content,
self.interrupt,
self.wildcards_obj,
self.extranetwork_maps_obj,
),
)
def test_host_alternation_error(self):
self.process(
InputTuple(
"[test1|test2|test3]",
"",
),
OutputTuple("", ""),
ppp=PromptPostProcessor(
self.ppp_logger,
{
**self.def_env_info,
"ppp_config": {"hosts": {"tests": {"alternation": "error"}}},
},
self.defopts,
self.grammar_content,
self.interrupt,
self.wildcards_obj,
self.extranetwork_maps_obj,
),
interrupted=True,
)
def test_host_and_eol(self):
self.process(
InputTuple(
"test1 AND test2:2",
"",
),
OutputTuple("test1\ntest2", ""),
ppp=PromptPostProcessor(
self.ppp_logger,
{
**self.def_env_info,
"ppp_config": {"hosts": {"tests": {"and": "eol"}}},
},
self.defopts,
self.grammar_content,
self.interrupt,
self.wildcards_obj,
self.extranetwork_maps_obj,
),
)
def test_host_and_comma(self):
self.process(
InputTuple(
"test1 AND test2:2",
"",
),
OutputTuple("test1, test2", ""),
ppp=PromptPostProcessor(
self.ppp_logger,
{
**self.def_env_info,
"ppp_config": {"hosts": {"tests": {"and": "comma"}}},
},
self.defopts,
self.grammar_content,
self.interrupt,
self.wildcards_obj,
self.extranetwork_maps_obj,
),
)
def test_host_and_remove(self):
self.process(
InputTuple(
"test1 AND test2:2",
"",
),
OutputTuple("test1 test2", ""),
ppp=PromptPostProcessor(
self.ppp_logger,
{
**self.def_env_info,
"ppp_config": {"hosts": {"tests": {"and": "remove"}}},
},
self.defopts,
self.grammar_content,
self.interrupt,
self.wildcards_obj,
self.extranetwork_maps_obj,
),
)
def test_host_and_error(self):
self.process(
InputTuple(
"test1 AND test2:2",
"",
),
OutputTuple("", ""),
ppp=PromptPostProcessor(
self.ppp_logger,
{
**self.def_env_info,
"ppp_config": {"hosts": {"tests": {"and": "error"}}},
},
self.defopts,
self.grammar_content,
self.interrupt,
self.wildcards_obj,
self.extranetwork_maps_obj,
),
interrupted=True,
)
def test_host_break_eol(self):
self.process(
InputTuple(
"test1 BREAK test2",
"",
),
OutputTuple("test1\ntest2", ""),
ppp=PromptPostProcessor(
self.ppp_logger,
{
**self.def_env_info,
"ppp_config": {"hosts": {"tests": {"break": "eol"}}},
},
self.defopts,
self.grammar_content,
self.interrupt,
self.wildcards_obj,
self.extranetwork_maps_obj,
),
)
def test_host_break_comma(self):
self.process(
InputTuple(
"test1 BREAK test2",
"",
),
OutputTuple("test1, test2", ""),
ppp=PromptPostProcessor(
self.ppp_logger,
{
**self.def_env_info,
"ppp_config": {"hosts": {"tests": {"break": "comma"}}},
},
self.defopts,
self.grammar_content,
self.interrupt,
self.wildcards_obj,
self.extranetwork_maps_obj,
),
)
def test_host_break_remove(self):
self.process(
InputTuple(
"test1 BREAK test2",
"",
),
OutputTuple("test1 test2", ""),
ppp=PromptPostProcessor(
self.ppp_logger,
{
**self.def_env_info,
"ppp_config": {"hosts": {"tests": {"break": "remove"}}},
},
self.defopts,
self.grammar_content,
self.interrupt,
self.wildcards_obj,
self.extranetwork_maps_obj,
),
)
def test_host_break_error(self):
self.process(
InputTuple(
"test1 BREAK test2",
"",
),
OutputTuple("", ""),
ppp=PromptPostProcessor(
self.ppp_logger,
{
**self.def_env_info,
"ppp_config": {"hosts": {"tests": {"break": "error"}}},
},
self.defopts,
self.grammar_content,
self.interrupt,
self.wildcards_obj,
self.extranetwork_maps_obj,
),
interrupted=True,
)
+75
View File
@@ -0,0 +1,75 @@
from .base_tests import InputTuple, TestPromptPostProcessorBase
if __name__ == "__main__":
raise SystemExit("This script must not be run directly")
class TestPerformance(TestPromptPostProcessorBase):
def setUp(self): # pylint: disable=arguments-differ
super().setUp(enable_file_logging=False)
# Performance tests
def test_parser_performance_simple_simpleparser(
self,
): # performance test with a large prompt without new constructs
large_prompt = ", ".join(
["(this:1.2) is a [test] using a [simple|low complexity] prompt with <lora:test:1>"] * 15
)
self.process(
InputTuple(large_prompt, ""),
ppp="nocup",
)
def test_parser_performance_simple_fullparser(
self,
): # performance test with a large prompt without new constructs but using full parser
# we trick it to use the full parser by including some characters
large_prompt = "{__${x:}}" + ", ".join(
["(this:1.2) is a [test] using a [simple|low complexity] prompt with <lora:test:1>"] * 15
)
self.process(
InputTuple(large_prompt, ""),
ppp="nocup",
)
def test_parser_performance_complex_fullparser(
self,
): # performance test with a large prompt with new constructs (full parser)
large_prompt = ", ".join(["__yaml/wildcard1__, (__yaml/wildcard2__), __yaml/wildcard3__, {one|two|three}"] * 15)
self.process(
InputTuple(large_prompt, ""),
ppp="nocup",
)
# the following tests are performance tests with only one kind of the old constructs
# same number of constructs and approximately the same full length
def test_parser_performance_simple_attention(self): # performance test with only attention
large_prompt = ", ".join(["(one:1.2) two (three) four [five] six"] * 20)
self.process(
InputTuple(large_prompt, ""),
ppp="nocup",
)
def test_parser_performance_simple_schedules(self): # performance test with only schedules
large_prompt = ", ".join(["[one:1:0.5] two [three:0.8] four [five:5:0.2] six"] * 20)
self.process(
InputTuple(large_prompt, ""),
ppp="nocup",
)
def test_parser_performance_simple_alternation(self): # performance test with only alternation
large_prompt = ", ".join(["[one|1] two [three|3] four [five|5] six"] * 20)
self.process(
InputTuple(large_prompt, ""),
ppp="nocup",
)
def test_parser_performance_simple_extranetwork(self): # performance test with only extra networks
large_prompt = ", ".join(["<lora:one:1> two <lora:three:1> four <lora:five:1> six"] * 20)
self.process(
InputTuple(large_prompt, ""),
ppp="nocup",
)
+124
View File
@@ -0,0 +1,124 @@
from .base_tests import OutputTuple, InputTuple, TestPromptPostProcessorBase
if __name__ == "__main__":
raise SystemExit("This script must not be run directly")
class TestSendToNegative(TestPromptPostProcessorBase):
def setUp(self): # pylint: disable=arguments-differ
super().setUp(enable_file_logging=False)
# Send To Negative tests
def test_stn_simple(self): # negtags with different parameters and separations
self.process(
InputTuple(
"flowers<ppp:stn>red<ppp:/stn>, <ppp:stn s>green<ppp:/stn>, <ppp:stn e>blue<ppp:/stn><ppp:stn p0>yellow<ppp:/stn>, <ppp:stn p1>purple<ppp:/stn><ppp:stn p2>black<ppp:/stn>",
"<ppp:stn i0/>normal quality<ppp:stn i1>, worse quality<ppp:stn i2/>",
),
OutputTuple("flowers", "red, green, yellow, normal quality, purple, worse quality, black, blue"),
)
def test_stn_complex(self): # complex negtags
self.process(
InputTuple(
"<ppp:stn>red<ppp:/stn> ((<ppp:stn s>pink<ppp:/stn>)), flowers <ppp:stn e>purple<ppp:/stn>, <ppp:stn p0>mauve<ppp:/stn><ppp:stn e>blue<ppp:/stn>, <ppp:stn p0>yellow<ppp:/stn> <ppp:stn p1>green<ppp:/stn>",
"normal quality, <ppp:stn i0/>, bad quality<ppp:stn i1/>, worse quality",
),
OutputTuple(
"flowers",
"red, (pink:1.21), normal quality, mauve, yellow, bad quality, green, worse quality, purple, blue",
),
)
def test_stn_complex_nocleanup(self): # complex negtags with no cleanup
self.process(
InputTuple(
"<ppp:stn>red<ppp:/stn> ((<ppp:stn s>pink<ppp:/stn>)), flowers <ppp:stn e>purple<ppp:/stn>, <ppp:stn p0>mauve<ppp:/stn><ppp:stn e>blue<ppp:/stn>, <ppp:stn p0>yellow<ppp:/stn> <ppp:stn p1>green<ppp:/stn>",
"normal quality, <ppp:stn i0/>, bad quality<ppp:stn i1/>, worse quality",
),
OutputTuple(
" (()), flowers , , ",
"red, ((pink)), normal quality, mauve, yellow, bad quality, green, worse quality, purple, blue",
),
ppp="nocup",
)
def test_stn_inside_attention(self): # negtag inside attention
self.process(
InputTuple(
"[<ppp:stn>neg1<ppp:/stn>] this is a ((test<ppp:stn e>neg2<ppp:/stn>) (test:2.0): 1.5 ) (red<ppp:stn>[square]<ppp:/stn>:1.5)",
"normal quality",
),
OutputTuple(
"this is a ((test) (test:2):1.5) (red:1.5)", "[neg1], (square:1.35), normal quality, (neg2:1.65)"
),
)
def test_stn_inside_alternation(self): # negtag inside alternation
self.process(
InputTuple(
"this is a (([complex<ppp:stn>neg1<ppp:/stn>|simple<ppp:stn>neg2<ppp:/stn>|regular<ppp:stn>neg3<ppp:/stn>] test)(test:2.0):1.5)",
"normal quality",
),
OutputTuple(
"this is a (([complex|simple|regular] test)(test:2):1.5)",
"([neg1||]:1.65), ([|neg2|]:1.65), ([||neg3]:1.65), normal quality",
),
)
def test_stn_inside_alternation_recursive(self): # negtag inside alternation (recursive alternation)
self.process(
InputTuple(
"this is a (([complex<ppp:stn>neg1<ppp:/stn>[one|two<ppp:stn>neg12<ppp:/stn>||three|four(<ppp:stn>neg14<ppp:/stn>)]|simple<ppp:stn>neg2<ppp:/stn>|regular<ppp:stn>neg3<ppp:/stn>] test)(test:2.0):1.5)",
"normal quality",
),
OutputTuple(
"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",
),
)
def test_stn_inside_scheduling(self): # negtag inside scheduling
self.process(
InputTuple("this is [abc<ppp:stn>neg1<ppp:/stn>:def<ppp:stn e>neg2<ppp:/stn>: 5 ]", "normal quality"),
OutputTuple("this is [abc:def:5]", "[neg1::5], normal quality, [neg2:5]"),
)
def test_stn_complex_features(self): # complex negtags with AND, BREAK and other features
self.process(
InputTuple(
"[<ppp:stn>neg5<ppp:/stn>] this \\(is\\): a (([complex|simple<ppp:stn>neg6<ppp:/stn>|regular] test<ppp:stn>neg1<ppp:/stn>)(test:2.0):1.5) \nBREAK, BREAK with [abc<ppp:stn>neg4<ppp:/stn>:def<ppp:stn p0>neg2(neg3:1.6)<ppp:/stn>:5]:0.5 AND loratrigger <lora:xxx:1> AND AND hypernettrigger <hypernet:yyy>:0.3",
"normal quality, <ppp:stn i0/>",
),
OutputTuple(
"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_complex_features_newformat(self): # complex negtags with AND, BREAK and other features (new format)
self.process(
InputTuple(
"[<ppp:stn>neg5<ppp:/stn>] this \\(is\\): a (([complex|simple<ppp:stn>neg6<ppp:/stn>|regular] test<ppp:stn>neg1<ppp:/stn>)(test:2.0):1.5) \nBREAK, BREAK with [abc<ppp:stn>neg4<ppp:/stn>:def<ppp:stn p0>neg2(neg3:1.6)<ppp:/stn>:5]:0.5 AND loratrigger <lora:xxx:1> AND AND hypernettrigger <hypernet:yyy>:0.3",
"normal quality, <ppp:stn i0/>",
),
OutputTuple(
"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(
InputTuple(
"[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>]",
"",
),
OutputTuple(
"[pos1[pos11|pos12||pos14|pos15]|pos2|pos3]",
"[neg1||], [[|neg12|||]||], [[||||neg15]||], [|neg2|], [||neg3]",
# "[neg1[|neg12|||neg15]|neg2|neg3]", # expected output if the constructs were unified
),
)
File diff suppressed because it is too large Load Diff
+75
View File
@@ -0,0 +1,75 @@
from dataclasses import replace
from ppp import PromptPostProcessor
from ppp_classes import ONWARNING_CHOICES # type: ignore
from .base_tests import OutputTuple, InputTuple, TestPromptPostProcessorBase
if __name__ == "__main__":
raise SystemExit("This script must not be run directly")
class TestModelVariants(TestPromptPostProcessorBase):
def setUp(self): # pylint: disable=arguments-differ
super().setUp(enable_file_logging=False)
# Model variants tests
def test_variants(self):
self.process(
InputTuple(
"<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>",
"",
),
OutputTuple("test1test2", ""),
ppp=PromptPostProcessor(
self.ppp_logger,
{
**self.def_env_info,
"model_filename": "./webui/models/Stable-diffusion/testmodel.safetensors",
"ppp_config": {
"models": {
"sd1": {
"detect": {"tests": {"class": ["SD15", "SD15_instructpix2pix"]}},
"variants": {
"test3": {"find_in_filename": "testmodel"},
"sdxl": {"find_in_filename": "testmodel"},
},
},
"sdxl": {
"detect": {
"tests": {
"class": [
"SDXL",
"SDXLRefiner",
"SDXL_instructpix2pix",
"Segmind_Vega",
"KOALA_700M",
"KOALA_1B",
]
}
},
"variants": {
"test1": {"find_in_filename": "testmodel"},
"test2": {"find_in_filename": "testmodel"},
},
},
"something": {
"detect": {"tests": {"class": ["something"]}},
"variants": {
"test4": {"find_in_filename": "testmodel"},
},
},
}
},
},
replace(
self.defopts,
on_warning=ONWARNING_CHOICES.warn,
),
self.grammar_content,
self.interrupt,
self.wildcards_obj,
self.extranetwork_maps_obj,
),
)
+624
View File
@@ -0,0 +1,624 @@
from dataclasses import replace
from ppp import PromptPostProcessor
from ppp_classes import IFWILDCARDS_CHOICES
from .base_tests import OutputTuple, InputTuple, TestPromptPostProcessorBase
if __name__ == "__main__":
raise SystemExit("This script must not be run directly")
class TestWildcards(TestPromptPostProcessorBase):
def setUp(self): # pylint: disable=arguments-differ
super().setUp(enable_file_logging=False)
# Wildcards tests
def test_wc_ignore(self): # wildcards with ignore option
self.process(
InputTuple("__bad_wildcard__", "{option1|option2}"),
OutputTuple("__bad_wildcard__", "{option1|option2}"),
ppp=PromptPostProcessor(
self.ppp_logger,
self.def_env_info,
replace(
self.defopts,
process_wildcards=False,
if_wildcards=IFWILDCARDS_CHOICES.ignore,
),
self.grammar_content,
self.interrupt,
self.wildcards_obj,
self.extranetwork_maps_obj,
),
)
def test_wc_remove(self): # wildcards with remove option
self.process(
InputTuple(
"[<ppp:stn>neg5<ppp:/stn>] this is: __bad_wildcard__ a (([complex|simple<ppp:stn>neg6<ppp:/stn>|regular] test<ppp:stn>neg1<ppp:/stn>)(test:2.0):1.5) \nBREAK, BREAK with [abc<ppp:stn>neg4<ppp:/stn>:def<ppp:stn p0>neg2(neg3:1.6)<ppp:/stn>:5] <lora:xxx:1>",
"normal quality, <ppp:stn i0/> {option1|option2}",
),
OutputTuple(
"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.def_env_info,
replace(
self.defopts,
process_wildcards=False,
if_wildcards=IFWILDCARDS_CHOICES.remove,
),
self.grammar_content,
self.interrupt,
self.wildcards_obj,
self.extranetwork_maps_obj,
),
)
def test_wc_warn(self): # wildcards with warn option
self.process(
InputTuple("__bad_wildcard__", "{option1|option2}"),
OutputTuple(PromptPostProcessor.WILDCARD_WARNING + "__bad_wildcard__", "{option1|option2}"),
ppp=PromptPostProcessor(
self.ppp_logger,
self.def_env_info,
replace(
self.defopts,
process_wildcards=False,
if_wildcards=IFWILDCARDS_CHOICES.warn,
),
self.grammar_content,
self.interrupt,
self.wildcards_obj,
self.extranetwork_maps_obj,
),
)
def test_wc_stop(self): # wildcards with stop option
self.process(
InputTuple("__bad_wildcard__", "{option1|option2}"),
OutputTuple(
PromptPostProcessor.WILDCARD_STOP.format("__bad_wildcard__") + "__bad_wildcard__",
"{option1|option2}",
),
ppp=PromptPostProcessor(
self.ppp_logger,
self.def_env_info,
replace(
self.defopts,
process_wildcards=False,
if_wildcards=IFWILDCARDS_CHOICES.stop,
),
self.grammar_content,
self.interrupt,
self.wildcards_obj,
self.extranetwork_maps_obj,
),
interrupted=True,
)
def test_wcinvar_warn(self): # wildcards in var with warn option
self.process(
InputTuple("${v=__bad_wildcard__}${v}", ""),
OutputTuple(PromptPostProcessor.WILDCARD_WARNING + "__bad_wildcard__", ""),
ppp=PromptPostProcessor(
self.ppp_logger,
self.def_env_info,
replace(
self.defopts,
process_wildcards=False,
if_wildcards=IFWILDCARDS_CHOICES.warn,
),
self.grammar_content,
self.interrupt,
self.wildcards_obj,
self.extranetwork_maps_obj,
),
)
def test_wc_invalid_name(self):
self.process(
InputTuple("the choices are: ___invalid__", ""),
OutputTuple("the choices are: ___invalid__", ""),
ppp="nocup",
interrupted=True,
)
def test_wc_wildcard1a_text(self): # simple text wildcard
self.process(
InputTuple("the choices are: __text/wildcard1__", ""),
OutputTuple("the choices are: choice2", ""),
ppp="nocup",
)
def test_wc_wildcard1a_json(self): # simple json wildcard
self.process(
InputTuple("the choices are: __json/wildcard1__", ""),
OutputTuple("the choices are: choice2", ""),
ppp="nocup",
)
def test_wc_wildcard1a_yaml(self): # simple yaml wildcard
self.process(
InputTuple("the choices are: __yaml/wildcard1__", ""),
OutputTuple("the choices are: choice2", ""),
ppp="nocup",
)
def test_wc_wildcard1b_text(self): # simple text wildcard with multiple choices
self.process(
InputTuple("the choices are: __2-$$text/wildcard1__", ""),
OutputTuple("the choices are: choice3, choice1", ""),
ppp="nocup",
)
def test_wc_wildcard1b_json(self): # simple json wildcard with multiple choices
self.process(
InputTuple("the choices are: __2-$$json/wildcard1__", ""),
OutputTuple("the choices are: choice3, choice1", ""),
ppp="nocup",
)
def test_wc_wildcard1b_yaml(self): # simple yaml wildcard with multiple choices
self.process(
InputTuple("the choices are: __2-$$yaml/wildcard1__", ""),
OutputTuple("the choices are: choice3, choice1", ""),
ppp="nocup",
)
def test_wc_wildcard2_text(self): # simple text wildcard with default options
self.process(
InputTuple("the choices are: __text/wildcard2__", ""),
OutputTuple("the choices are: choice3-choice1", ""),
ppp="nocup",
)
def test_wc_wildcard2_json(self): # simple json wildcard with default options
self.process(
InputTuple("the choices are: __json/wildcard2__", ""),
OutputTuple("the choices are: choice3-choice1", ""),
ppp="nocup",
)
def test_wc_wildcard2_yaml(self): # simple yaml wildcard with default options
self.process(
InputTuple("the choices are: __yaml/wildcard2__", ""),
OutputTuple("the choices are: choice3-choice1", ""),
ppp="nocup",
)
def test_wc_test2_yaml(self): # simple yaml wildcard
self.process(
InputTuple("the choice is: __testwc/test2__", ""),
OutputTuple("the choice is: 2", ""),
ppp="nocup",
)
def test_wc_test3_yaml(self): # simple yaml wildcard
self.process(
InputTuple("the choice is: __testwc/test3__", ""),
OutputTuple("the choice is: one choice", ""),
ppp="nocup",
)
def test_wc_test3_yaml_2(self): # simple yaml wildcard build from variable
self.process(
InputTuple("${v=3}the choice is: __testwc/test${v}__", ""),
OutputTuple("the choice is: one choice", ""),
ppp="nocup",
)
def test_wc_wildcard_filter_index(self): # wildcard with positional index filter
self.process(
InputTuple("the choice is: __yaml/wildcard2'2'__", ""),
OutputTuple("the choice is: choice3-choice3", ""),
ppp="nocup",
)
def test_wc_wildcard_filter_index_range(self): # wildcard with positional index range filter
self.process(
InputTuple("the choice is: __yaml/wildcard2'2-3'__", ""),
OutputTuple("the choice is: choice3-choice3", ""),
ppp="nocup",
)
def test_wc_wildcard_filter_label(self): # wildcard with label filter
self.process(
InputTuple("the choice is: __yaml/wildcard2'label1'__", ""),
OutputTuple("the choice is: choice3-choice1", ""),
ppp="nocup",
)
def test_wc_wildcard_filter_label2(self): # wildcard with label filter in multiple choices
self.process(
InputTuple("the choice is: __yaml/wildcard2'label2'__", ""),
OutputTuple("the choice is: choice1-choice1", ""),
ppp="nocup",
)
def test_wc_wildcard_filter_label3(self): # wildcard with multiple label filter
self.process(
InputTuple("the choice is: __4$$-$$yaml/wildcard2'label1,label2'__", ""),
OutputTuple("the choice is: choice1-choice3", ""),
ppp="nocup",
)
def test_wc_wildcard_filter_indexlabel(self): # wildcard with mixed index and label filter
self.process(
InputTuple("the choice is: __yaml/wildcard2'2,label2'__", ""),
OutputTuple("the choice is: choice3-choice1", ""),
ppp="nocup",
)
def test_wc_wildcard_filter_compound(self): # wildcard with compound filter
self.process(
InputTuple("the choice is: __4$$-$$yaml/wildcard2'label1+label3'__", ""),
OutputTuple("the choice is: choice3", ""),
ppp="nocup",
)
def test_wc_wildcard_filter_compound_var(self): # wildcard with compound filter in a variable
self.process(
InputTuple("${v[]=*('label1','label3')}the choice is: __4$$-$$yaml/wildcard2'${v[&'+']}'__", ""),
OutputTuple("the choice is: choice3", ""),
ppp="nocup",
)
def test_wc_wildcard_filter_compound2(self): # wildcard with inherited compound filter
self.process(
InputTuple("the choice is: __yaml/wildcard2bis'#label1+label3'__", ""),
OutputTuple("the choice is: choice3bis", ""),
ppp="nocup",
)
def test_wc_wildcard_filter_compound3(self): # wildcard with doubly inherited compound filter
self.process(
InputTuple("the choice is: __yaml/wildcard2bisbis'#label1+label3'__", ""),
OutputTuple("the choice is: choice3bisbis", ""),
ppp="nocup",
)
def test_wc_wildcard_filter_compound4(self): # wildcard with doubly inherited compound filter with variable
self.process(
InputTuple("${v=label1}the choice is: __yaml/wildcard2bisbis'#${v}+label3'__", ""),
OutputTuple("the choice is: choice3bisbis", ""),
ppp="nocup",
)
def test_wc_wildcard_default_filter(self): # wildcard with default filter
self.process(
InputTuple(
"<ppp:setwcdeffilter 'yaml/wildcard2' 'label1+label3' />the choice is: __yaml/wildcard2__, <ppp:setwcdeffilter 'yaml/wildcard2' />__yaml/wildcard2__",
"",
),
OutputTuple("the choice is: choice3-choice3, choice3-choice1- choice2 ", ""),
ppp="nocup",
)
def test_wc_wildcard_default_filter2(self): # wildcard with default filter with variable
self.process(
InputTuple(
"${v=label1}<ppp:setwcdeffilter 'yaml/wildcard2' '${v}+label3' />the choice is: __yaml/wildcard2__, <ppp:setwcdeffilter 'yaml/wildcard2' />__yaml/wildcard2__",
"",
),
OutputTuple("the choice is: choice3-choice3, choice3-choice1- choice2 ", ""),
ppp="nocup",
)
def test_wc_nested_wildcard_text(self): # nested text wildcard with repeating multiple choices
self.process(
InputTuple("the choices are: __r3$$-$$text/wildcard3__", ""),
OutputTuple("the choices are: choice3,choice1- choice2 ,choice3", ""),
ppp="nocup",
)
def test_wc_nested_wildcard_json(self): # nested json wildcard with repeating multiple choices
self.process(
InputTuple("the choices are: __r3$$-$$json/wildcard3__", ""),
OutputTuple("the choices are: choice3,choice1- choice2 ,choice3", ""),
ppp="nocup",
)
def test_wc_nested_wildcard_yaml(self): # nested yaml wildcard with repeating multiple choices
self.process(
InputTuple("the choices are: __r3$$-$$yaml/wildcard3__", ""),
OutputTuple("the choices are: choice3,choice1- choice2 ,choice3", ""),
ppp="nocup",
)
def test_wc_wildcard_optional(self): # empty wildcard with no error
self.process(
InputTuple("the choices are: __yaml/empty_wildcard__", ""),
OutputTuple("the choices are: ", ""),
ppp="nocup",
)
def test_wc_wildcard4_yaml(self): # simple yaml wildcard with one option
self.process(
InputTuple("the choices are: __yaml/wildcard4__", ""),
OutputTuple("the choices are: inline text", ""),
ppp="nocup",
)
def test_wc_wildcard6_yaml(self): # simple yaml wildcard with object formatted choices
self.process(
InputTuple("the choices are: __yaml/wildcard6__", ""),
OutputTuple("the choices are: choice2", ""),
ppp="nocup",
)
def test_wc_choice_wildcard_mix(self): # choices with wildcard mix
self.process(
InputTuple("the choices are: {__~2$$yaml/wildcard2__|choice0}", ""),
[
OutputTuple("the choices are: choice0", ""),
OutputTuple("the choices are: choice1, choice3", ""),
OutputTuple("the choices are: choice1, choice3", ""),
],
ppp="nocup",
)
def test_wc_wildcard_globbing(self): # wildcard with globbing
self.process(
InputTuple("the choices are: __yaml/wildcard[12]__, __yaml/wildcard?__", ""),
OutputTuple("the choices are: choice3-choice2, <lora:test2:1>- choice2 -choice3", ""),
ppp="nocup",
)
def test_wc_wildcardwithvar(self): # wildcard with inline variable
self.process(
InputTuple("the choices are: __yaml/wildcard5(var=test)__, __yaml/wildcard5__", ""),
OutputTuple("the choices are: inline test, inline default", ""),
ppp="nocup",
)
def test_wc_wildcardPS_yaml(self): # yaml wildcard with object formatted choices and options and prefix and suffix
self.process(
InputTuple("the choices are: __yaml/wildcardPS__", ""),
OutputTuple("the choices are: prefix1-choice2/choice3-suffix", ""),
ppp="nocup",
)
def test_wc_wildcardPS2_yaml(self): # yaml wildcard with object formatted choices and options and prefix and suffix
self.process(
InputTuple("the choices are: [__yaml/wildcardPS2__]", ""),
OutputTuple("the choices are: (prefix2-choice2-suffix:1.35)", ""),
)
def test_wc_wildcardContainer_yaml(self): # yaml wildcard with object formatted choices and options and container
self.process(
InputTuple("the choices are: [__yaml/wildcardContainer__]", ""),
OutputTuple("the choices are: (prefix1-choice2/choice3-suffix:1.35)", ""),
)
def test_wc_wildcardAt_yaml(self): # yaml wildcard with attention in choices
self.process(
InputTuple("the choices are: [__yaml/wildcardAt__]", ""),
OutputTuple("the choices are: (choice2:1.35)", ""),
)
def test_wc_merge_attention_bracket(self): # bracket attention from wildcard merges with outer attention
self.process(
InputTuple("(__yaml/wildcardAtBracket__:1.5)", ""),
OutputTuple("(the content:1.35)", ""),
)
def test_wc_no_merge_attention_alternation(self): # alternation from wildcard is not merged as attention
self.process(
InputTuple("(__yaml/wildcardAlt__:1.5)", ""),
OutputTuple("([cat|dog]:1.5)", ""),
)
def test_wc_no_merge_attention_scheduling(self): # scheduling from wildcard is not merged as attention
self.process(
InputTuple("(__yaml/wildcardSched__:1.5)", ""),
OutputTuple("([cat:dog:0.5]:1.5)", ""),
)
def test_wc_anonymouswildcard_yaml(self): # yaml anonymous wildcard
self.process(
InputTuple("the choices are: __yaml/anonwildcards__", ""),
OutputTuple("the choices are: six", ""),
ppp="nocup",
)
def test_wc_wildcard_input(self): # simple yaml wildcard input
self.process(
InputTuple("the choices are: __yaml_input/wildcardI__", ""),
OutputTuple("the choices are: choice2", ""),
ppp="nocup",
)
def test_wc_circular(self): # wildcard circular reference
self.process(
InputTuple("the choices are: __yaml/circular1__", ""),
OutputTuple("", ""),
ppp="nocup",
interrupted=True,
)
def test_wc_including(self): # wildcard including another wildcard
self.process(
InputTuple("the choices are: __yaml/including__", ""),
OutputTuple("the choices are: choice4", ""),
ppp="nocup",
)
def test_wc_circular_including(self): # wildcard including another wildcard in a circular reference
self.process(
InputTuple("the choices are: __yaml/including1__", ""),
OutputTuple("", ""),
ppp="nocup",
interrupted=True,
)
def test_wc_dynamicwildcard(self): # wildcard built from variables
self.process(
InputTuple(
"the choices are: ${x={1|2|3}}${w=yaml/wildcard${x}}__yaml/wildcard${x}__ __${w}__ __<ppp:echo w/>__",
"",
),
OutputTuple("the choices are: choice1-choice3-choice1 choice3- choice2 - choice2 choice3", ""),
ppp="nocup",
)
# Combinatorial
def test_wc_combinatorial_1(self): # combinatorial wildcard with variable
self.process(
InputTuple("the choices are: __2$$yaml/wildcard2__, ${v:{option1|option2}}", ""),
[ # 12 combinations
OutputTuple("the choices are: choice1, choice2, option1", "", {"v": "option1"}),
OutputTuple("the choices are: choice1, choice2, option2", "", {"v": "option2"}),
OutputTuple("the choices are: choice1, choice3, option1", "", {"v": "option1"}),
OutputTuple("the choices are: choice1, choice3, option2", "", {"v": "option2"}),
OutputTuple("the choices are: choice2, choice1, option1", "", {"v": "option1"}),
OutputTuple("the choices are: choice2, choice1, option2", "", {"v": "option2"}),
OutputTuple("the choices are: choice2, choice3, option1", "", {"v": "option1"}),
OutputTuple("the choices are: choice2, choice3, option2", "", {"v": "option2"}),
OutputTuple("the choices are: choice3, choice1, option1", "", {"v": "option1"}),
OutputTuple("the choices are: choice3, choice1, option2", "", {"v": "option2"}),
OutputTuple("the choices are: choice3, choice2, option1", "", {"v": "option1"}),
OutputTuple("the choices are: choice3, choice2, option2", "", {"v": "option2"}),
],
combinatorial=True,
)
def test_wc_combinatorial_2(self): # combinatorial wildcard
self.process(
InputTuple("__yaml/wildcard2__", ""),
[ # 36 combinations
# groups of 3
## same choice repeated 3 times
OutputTuple("choice1-choice1-choice1", ""),
OutputTuple(" choice2 - choice2 - choice2 ", ""),
OutputTuple("choice3-choice3-choice3", ""),
## one choice repeated 2 times in all positions
OutputTuple("choice1-choice1- choice2 ", ""),
OutputTuple("choice1-choice1-choice3", ""),
OutputTuple(" choice2 - choice2 -choice1", ""),
OutputTuple(" choice2 - choice2 -choice3", ""),
OutputTuple("choice3-choice3-choice1", ""),
OutputTuple("choice3-choice3- choice2 ", ""),
OutputTuple(" choice2 -choice1-choice1", ""),
OutputTuple("choice3-choice1-choice1", ""),
OutputTuple("choice1- choice2 - choice2 ", ""),
OutputTuple("choice3- choice2 - choice2 ", ""),
OutputTuple("choice1-choice3-choice3", ""),
OutputTuple(" choice2 -choice3-choice3", ""),
OutputTuple("choice1- choice2 -choice1", ""),
OutputTuple("choice1-choice3-choice1", ""),
OutputTuple(" choice2 -choice1- choice2 ", ""),
OutputTuple(" choice2 -choice3- choice2 ", ""),
OutputTuple("choice3-choice1-choice3", ""),
OutputTuple("choice3- choice2 -choice3", ""),
## choices 1, 2, 3 in all positions
OutputTuple("choice1- choice2 -choice3", ""),
OutputTuple("choice1-choice3- choice2 ", ""),
OutputTuple(" choice2 -choice1-choice3", ""),
OutputTuple(" choice2 -choice3-choice1", ""),
OutputTuple("choice3-choice1- choice2 ", ""),
OutputTuple("choice3- choice2 -choice1", ""),
# groups of 2
## same choice repeated 2 times
OutputTuple("choice1-choice1", ""),
OutputTuple(" choice2 - choice2 ", ""),
OutputTuple("choice3-choice3", ""),
## choices 1 and 2 in all positions
OutputTuple("choice1- choice2 ", ""),
OutputTuple(" choice2 -choice1", ""),
## choices 2 and 3 in all positions
OutputTuple(" choice2 -choice3", ""),
OutputTuple("choice3- choice2 ", ""),
## choices 1 and 3 in all positions
OutputTuple("choice1-choice3", ""),
OutputTuple("choice3-choice1", ""),
],
ppp="nocup",
combinatorial=True,
)
def test_wc_combinatorial_3(self): # combinatorial wildcard (keep choice order)
self.process(
InputTuple("__2-3$$-$$yaml/wildcard2__", ""),
[ # 4 combinations
# groups of 3
## choices 1, 2, 3
OutputTuple("choice1- choice2 -choice3", ""),
# groups of 2
## choices 1 and 2
OutputTuple("choice1- choice2 ", ""),
## choices 2 and 3
OutputTuple(" choice2 -choice3", ""),
## choices 1 and 3
OutputTuple("choice1-choice3", ""),
],
ppp=PromptPostProcessor(
self.ppp_logger,
self.def_env_info,
replace(
self.defopts,
keep_choices_order=True,
cup_do_cleanup=False,
do_combinatorial=True,
),
self.grammar_content,
self.interrupt,
self.wildcards_obj,
self.extranetwork_maps_obj,
),
)
def test_wc_combinatorial_4(self): # combinatorial wildcard (don't keep choice order)
self.process(
InputTuple("__2-3$$-$$yaml/wildcard2__", ""),
[ # 12 combinations
# groups of 3
## choices 1, 2, 3 in all positions
OutputTuple("choice1- choice2 -choice3", ""),
OutputTuple("choice1-choice3- choice2 ", ""),
OutputTuple(" choice2 -choice1-choice3", ""),
OutputTuple(" choice2 -choice3-choice1", ""),
OutputTuple("choice3-choice1- choice2 ", ""),
OutputTuple("choice3- choice2 -choice1", ""),
# groups of 2
## choices 1 and 2 in all positions
OutputTuple("choice1- choice2 ", ""),
OutputTuple(" choice2 -choice1", ""),
## choices 2 and 3 in all positions
OutputTuple(" choice2 -choice3", ""),
OutputTuple("choice3- choice2 ", ""),
## choices 1 and 3 in all positions
OutputTuple("choice1-choice3", ""),
OutputTuple("choice3-choice1", ""),
],
ppp="nocup",
combinatorial=True,
)
def test_wc_combinatorial_5(self): # combinatorial nested wildcards and multiselection enmappings
self.process(
InputTuple("{__yaml/wildcard1__|__yaml/wildcard3__|<ppp:ext $lora loraany/>}", ""),
[ # 11 combinations
# first wildcard
OutputTuple("choice1", ""),
OutputTuple("choice2", ""),
OutputTuple("choice3", ""),
# second wildcard (nested)
OutputTuple("choice1, choice2 ", ""),
OutputTuple(" choice2 ,choice1", ""),
OutputTuple("choice1,choice3", ""),
OutputTuple("choice3,choice1", ""),
OutputTuple(" choice2 ,choice3", ""),
OutputTuple("choice3, choice2 ", ""),
# ppp:ext
OutputTuple("<lora:loraany1:0.8> trigger1, trigger2, ", ""),
OutputTuple("<lora:loraany2:1> trigger3, trigger4, ", ""),
],
ppp="nocup",
combinatorial=True,
)
+3
View File
@@ -0,0 +1,3 @@
# invalid wildcard name
choice1
choice2
+1 -1
View File
@@ -6,7 +6,7 @@
"choice3" "choice3"
], ],
"wildcard2": [ "wildcard2": [
"r2-3$$-", "r2-3$$-$$",
"4::choice1", "4::choice1",
"3:: choice2 ", "3:: choice2 ",
"2::choice3", "2::choice3",
+71 -2
View File
@@ -1,11 +1,12 @@
yaml: yaml:
wildcard1: wildcard1:
- "'First wildcard'$$"
- choice1 - choice1
- choice2 - choice2
- choice3 - choice3
wildcard2: wildcard2:
- ~r2-3$$- - ~r2-3'Wildcard description'$$-$$
- "'label1,label2'4::choice1" - "'label1,label2'4::choice1"
- "3:: choice2 " - "3:: choice2 "
- { labels: ["label1", "label3"], weight: 2, content: choice3 } - { labels: ["label1", "label3"], weight: 2, content: choice3 }
@@ -31,16 +32,32 @@ yaml:
- { weight: 4, if: _is_ssd, text: choice4 } - { weight: 4, if: _is_ssd, text: choice4 }
wildcard7: wildcard7:
- { description: "Lora wildcard" }
- <lora:test1:1> - <lora:test1:1>
- <lora:test2:1> - <lora:test2:1>
- <lora:test3:1> - <lora:test3:1>
wildcardAt:
- (choice1:0.5)
- (choice2:1.5)
- (choice3:1)
wildcardAtBracket:
- "[the content]"
wildcardAlt:
- "[cat|dog]"
wildcardSched:
- "[cat:dog:0.5]"
wildcardPS: wildcardPS:
- { - {
sampler: "~", sampler: "~",
repeating: false, repeating: false,
optional: false,
count: 2, count: 2,
prefix: "prefix-", prefix: "prefix{1|2}-",
suffix: "-suffix", suffix: "-suffix",
separator: "/", separator: "/",
} }
@@ -48,6 +65,24 @@ yaml:
- { weight: 2, text: choice2 } - { weight: 2, text: choice2 }
- { weight: 1, text: choice3 } - { weight: 1, text: choice3 }
wildcardPS2:
- {
prefix: "(prefix{1|2}-",
suffix: "-suffix:1.5)",
}
- { text: choice1 }
- { text: choice2 }
- { text: choice3 }
wildcardContainer:
- {
count: 2,
container: "(prefix{1|2}-${_choices[&'/']}-suffix:1.5)",
}
- { weight: 3, text: choice1 }
- { weight: 2, text: choice2 }
- { weight: 1, text: choice3 }
more_nested: more_nested:
even_more_nested: # this would be __yaml/more_nested/even_more_nested__ even_more_nested: # this would be __yaml/more_nested/even_more_nested__
- one - one
@@ -67,3 +102,37 @@ yaml:
- { count: 2, prefix: "#" } - { count: 2, prefix: "#" }
- nine - nine
- ten - ten
empty_wildcard:
- o$$ # parameters: optional
- if false::1
- if false::2
- if false::3
- if _sd in ("test1", "test2")::4
- if (false or false)::5
circular1:
- 5::__yaml/circular2__
- choice1
- choice2
circular2:
- choice3
- choice4
- 5::__yaml/circular1__
including:
- "%0.5::include yaml/wildcard1"
- choice4
- choice5
- "%0.2::include yaml/wildcard7"
including1:
- choice1
- choice2
- "%5::include yaml/including2"
including2:
- choice3
- choice4
- "%5::include yaml/including1"
+1 -1
View File
@@ -1,5 +1,5 @@
# wildcard2 # wildcard2
r2-3$$- r2-3$$-$$
4::choice1 4::choice1
3:: choice2 3:: choice2
2::choice3 2::choice3
+3 -2
View File
@@ -1,2 +1,3 @@
# wildcard3 # wildcard3 (with windows-1252 encoding)
__2$$,$$text/wildcard2__ __2$$,$$text/wildcard2__
0::áüç
+16
View File
@@ -0,0 +1,16 @@
# ACB PPP Cleanup Options node
* **extra_spaces**: Removes other unnecessary spaces.
* **empty_constructs**: Removes attention/scheduling/alternation constructs when they are invalid.
* **extra_separators**: Removes unnecessary separators. This applies to the configured separator and regular commas.
* **extra_separators_additional**: Removes unnecessary separators at start or end of lines. This applies to the configured separator and regular commas.
* **extra_separators_include_eol**: In the previous two options it also removes EOLs attached to the separators.
* **around_breaks**: Removes consecutive BREAKs and unnecessary commas and space around them.
* **breaks_with_eol**: Add a newline before BREAKs.
* **around_ands**: Removes consecutive ANDs and unnecessary commas and space around them.
* **ands_with_eol**: Add a newline before ANDs.
* **around_extranetwork_tags**: Removes spaces around extra network tags.
* **merge_attention**: It merges attention modifiers when possible (merges into one, multiplying their values). Only merges individually nested modifiers.
* **remove_extranetwork_tags**: Removes all extra network tags.
Please note that *ComfyUI* does not natively support the `BREAK` and `AND` constructs, but the related settings are kept in that UI in case you use a node that supports them and the extension is configured to allow them (see the configuration file below).
+4
View File
@@ -0,0 +1,4 @@
# ACB PPP ExtraNetwork Mapping Options node
* **folders**: You can enter multiple folders separated by commas. You can leave it empty (the default) and add a `ppp_extranetworkmappings` entry in the **extra_model_paths.yaml** file (recommended).
* **definitions**: Extranetwork Mappings definitions (in yaml format). Direct input added to the ones found in the extranetwork mappings folders. Allows the mappings to be included in the workflow.
+4
View File
@@ -0,0 +1,4 @@
# ACB PPP Wildcard Options node
* **variables**: Connect to the variables output from the main node.
* **name**: Name of the variable to get, or empty to get all of them.
+4
View File
@@ -0,0 +1,4 @@
# ACB PPP Send-To-Negative Options node
* **separator**: You can specify the separator used when adding to the negative prompt (by default it's ", ").
* **ignore_repeats**: It ignores repeated content to avoid repetitions in the negative prompt.
+11
View File
@@ -0,0 +1,11 @@
# ACB PPP Wildcard Options node
* **folders**: You can enter multiple folders separated by commas. You can leave it empty (the default) and add a `ppp_wildcards` or `wildcards` entry in the **extra_model_paths.yaml** file (recommended).
* **definitions**: 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.
* **if_wildcards**: Select what do you want to do with any found wildcards/choices (when process wildcards is off or after the processing).
* **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.
* **choice_separator**: What do you want to use by default to separate multiple choices when the options allow it (by default it's ', ").
* **keep_choices_order**: If checked, a multiple choice construct will return them in the order they are in the construct.
+31
View File
@@ -0,0 +1,31 @@
# 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 detection of model variants.
* **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.
* **debug_level**: What to write to the console.
* **on_warnings**: Warn on the console or stop the generation.
* **process_wildcards**: Activates the wildcard processing.
* **do_cleanup**: Activates the cleanup processing.
* **cleanup_variables**: Do a cleanup of the output variables (depends on do_cleanup).
* **do_combinatorial**: Activates combinatorial mode, where the output are all the combinations of choices/wildcards of the prompt.
* **combinatorial_shuffle**: It shuffles the combinatorial results.
* **combinatorial_limit**: Limit for the number of generated combinations.
* **wc_options**: Connection to a Wildcards options node.
* **stn_options**: Connection to a Send-To-Negative options node.
* **cup_options**: Connection to a Cleanup options node.
* **en_options**: Connection to a ExtraNetworkMapping options node.
The options nodes are optional. If you don't need to change any of the default values then you don't need to use them.
## 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 use `${head}` as prompt for the head/face detailer.
+59
View File
@@ -0,0 +1,59 @@
import { app } from "../../scripts/app.js";
import { api } from "../../scripts/api.js";
app.registerExtension({
name: "ACB.PPP.WildcardConcat",
async beforeRegisterNodeDef(nodeType, nodeData) {
if (nodeData.name !== "ACBPPPWildcardConcat") return;
const onNodeCreated = nodeType.prototype.onNodeCreated;
nodeType.prototype.onNodeCreated = function () {
const result = onNodeCreated?.apply(this, arguments);
const self = this;
const refreshWildcards = async () => {
const filterWidget = self.widgets?.find((w) => w.name === "filter");
if (!filterWidget) return;
try {
const resp = await api.fetchApi(
`/acb_ppp/wildcards?filter=${encodeURIComponent(filterWidget.value ?? "")}`
);
const data = await resp.json();
const wildcards = data.wildcards ?? [];
for (let i = 1; i <= 10; i++) {
const widget = self.widgets?.find((w) => w.name === `wildcard_${i}`);
if (widget) {
const current = widget.value;
widget.options.values = wildcards;
widget.value = wildcards.includes(current)
? current
: wildcards[0] ?? "(none)";
}
}
app.graph.setDirtyCanvas(true, false);
} catch (err) {
console.error("[ACB PPP] Failed to refresh wildcard list:", err);
}
};
// Hook the filter widget so that committing a new value (Enter / blur)
// immediately refreshes all 10 wildcard dropdowns without running the workflow.
const filterWidget = this.widgets?.find((w) => w.name === "filter");
if (filterWidget) {
const origCallback = filterWidget.callback;
filterWidget.callback = async function (...args) {
if (origCallback) origCallback.apply(this, args);
await refreshWildcards();
};
}
return result;
};
},
});
Binary file not shown.

After

Width:  |  Height:  |  Size: 169 KiB

+988
View File
@@ -0,0 +1,988 @@
{
"id": "0f75c56e-adb4-41f6-b957-81b6772cb8b5",
"revision": 0,
"last_node_id": 44,
"last_link_id": 116,
"nodes": [
{
"id": 9,
"type": "SaveImage",
"pos": [
1880,
-820
],
"size": [
210,
270
],
"flags": {},
"order": 16,
"mode": 0,
"inputs": [
{
"name": "images",
"type": "IMAGE",
"link": 9
}
],
"outputs": [],
"properties": {
"cnr_id": "comfy-core",
"ver": "0.3.27"
},
"widgets_values": [
"ComfyUI"
]
},
{
"id": 7,
"type": "CLIPTextEncode",
"pos": [
940,
-460
],
"size": [
220,
88
],
"flags": {
"collapsed": true
},
"order": 10,
"mode": 0,
"inputs": [
{
"name": "clip",
"type": "CLIP",
"link": 45
},
{
"name": "text",
"type": "STRING",
"widget": {
"name": "text"
},
"link": 114
}
],
"outputs": [
{
"name": "CONDITIONING",
"type": "CONDITIONING",
"slot_index": 0,
"links": [
6
]
}
],
"properties": {
"cnr_id": "comfy-core",
"ver": "0.3.27",
"Node name for S&R": "CLIPTextEncode"
},
"widgets_values": [
""
]
},
{
"id": 8,
"type": "VAEDecode",
"pos": [
1700,
-800
],
"size": [
210,
46
],
"flags": {
"collapsed": true
},
"order": 15,
"mode": 0,
"inputs": [
{
"name": "samples",
"type": "LATENT",
"link": 7
},
{
"name": "vae",
"type": "VAE",
"link": 20
}
],
"outputs": [
{
"name": "IMAGE",
"type": "IMAGE",
"slot_index": 0,
"links": [
9
]
}
],
"properties": {
"cnr_id": "comfy-core",
"ver": "0.3.27",
"Node name for S&R": "VAEDecode"
},
"widgets_values": []
},
{
"id": 3,
"type": "KSampler",
"pos": [
1320,
-740
],
"size": [
315,
571
],
"flags": {},
"order": 13,
"mode": 0,
"inputs": [
{
"name": "model",
"type": "MODEL",
"link": 16
},
{
"name": "positive",
"type": "CONDITIONING",
"link": 4
},
{
"name": "negative",
"type": "CONDITIONING",
"link": 6
},
{
"name": "latent_image",
"type": "LATENT",
"link": 2
},
{
"name": "seed",
"type": "INT",
"widget": {
"name": "seed"
},
"link": 24
}
],
"outputs": [
{
"name": "LATENT",
"type": "LATENT",
"slot_index": 0,
"links": [
7
]
}
],
"properties": {
"cnr_id": "comfy-core",
"ver": "0.3.27",
"Node name for S&R": "KSampler"
},
"widgets_values": [
607033707847523,
"randomize",
20,
8,
"euler",
"normal",
1
]
},
{
"id": 35,
"type": "PreviewAny",
"pos": [
940,
-400
],
"size": [
320,
120
],
"flags": {},
"order": 9,
"mode": 0,
"inputs": [
{
"name": "source",
"type": "*",
"link": 113
}
],
"outputs": [],
"title": "positive result",
"properties": {
"cnr_id": "comfy-core",
"ver": "0.3.34",
"Node name for S&R": "PreviewAny"
},
"widgets_values": [
null,
null,
null
]
},
{
"id": 36,
"type": "PreviewAny",
"pos": [
940,
-240
],
"size": [
320,
120
],
"flags": {},
"order": 11,
"mode": 0,
"inputs": [
{
"name": "source",
"type": "*",
"link": 115
}
],
"outputs": [],
"title": "negative result",
"properties": {
"cnr_id": "comfy-core",
"ver": "0.3.34",
"Node name for S&R": "PreviewAny"
},
"widgets_values": [
null,
null,
null
]
},
{
"id": 37,
"type": "PreviewAny",
"pos": [
1180,
-60
],
"size": [
440,
520
],
"flags": {},
"order": 14,
"mode": 0,
"inputs": [
{
"name": "source",
"type": "*",
"link": 87
}
],
"outputs": [],
"title": "variables result",
"properties": {
"cnr_id": "comfy-core",
"ver": "0.3.34",
"Node name for S&R": "PreviewAny"
},
"widgets_values": [
null,
null,
null
]
},
{
"id": 40,
"type": "ACBPPPWildcardOptions",
"pos": [
0,
-380
],
"size": [
340,
240
],
"flags": {},
"order": 0,
"mode": 0,
"inputs": [],
"outputs": [
{
"name": "options",
"type": "PPP_OPTIONS_WC",
"links": [
106
]
}
],
"properties": {
"cnr_id": "sd-webui-prompt-postprocessor",
"ver": "84d31ef07286983a329de9d31c106efc836205ff",
"Node name for S&R": "ACBPPPWildcardOptions"
},
"widgets_values": [
"",
"color:\n - red\n - blonde\n - brunette\naction:\n - running\n - sleeping",
"stop",
", ",
true
]
},
{
"id": 42,
"type": "ACBPPPSendToNegativeOptions",
"pos": [
0,
300
],
"size": [
340,
82
],
"flags": {},
"order": 1,
"mode": 0,
"inputs": [],
"outputs": [
{
"name": "options",
"type": "PPP_OPTIONS_STN",
"links": [
107
]
}
],
"properties": {
"cnr_id": "sd-webui-prompt-postprocessor",
"ver": "84d31ef07286983a329de9d31c106efc836205ff",
"Node name for S&R": "ACBPPPSendToNegativeOptions"
},
"widgets_values": [
", ",
true
]
},
{
"id": 41,
"type": "ACBPPPCleanupOptions",
"pos": [
0,
-80
],
"size": [
340,
322
],
"flags": {},
"order": 2,
"mode": 0,
"inputs": [],
"outputs": [
{
"name": "options",
"type": "PPP_OPTIONS_CUP",
"links": [
108
]
}
],
"properties": {
"cnr_id": "sd-webui-prompt-postprocessor",
"ver": "84d31ef07286983a329de9d31c106efc836205ff",
"Node name for S&R": "ACBPPPCleanupOptions"
},
"widgets_values": [
true,
true,
true,
true,
false,
false,
false,
false,
false,
false,
true,
false
]
},
{
"id": 20,
"type": "ACBPPPSelectVariable",
"pos": [
860,
-20
],
"size": [
220,
60
],
"flags": {},
"order": 12,
"mode": 0,
"inputs": [
{
"name": "variables",
"type": "PPP_DICT",
"link": 116
}
],
"outputs": [
{
"name": "value",
"type": "STRING",
"links": [
87
]
}
],
"properties": {
"cnr_id": "sd-webui-prompt-postprocessor",
"ver": "5e3c8cbc44bacd3b4da2ef33588f152a571775dd",
"Node name for S&R": "ACBPPPSelectVariable"
},
"widgets_values": [
""
]
},
{
"id": 6,
"type": "CLIPTextEncode",
"pos": [
940,
-520
],
"size": [
240,
88
],
"flags": {
"collapsed": true
},
"order": 8,
"mode": 0,
"inputs": [
{
"name": "clip",
"type": "CLIP",
"link": 19
},
{
"name": "text",
"type": "STRING",
"widget": {
"name": "text"
},
"link": 112
}
],
"outputs": [
{
"name": "CONDITIONING",
"type": "CONDITIONING",
"slot_index": 0,
"links": [
4
]
}
],
"properties": {
"cnr_id": "comfy-core",
"ver": "0.3.27",
"Node name for S&R": "CLIPTextEncode"
},
"widgets_values": [
""
]
},
{
"id": 17,
"type": "PrimitiveInt",
"pos": [
20,
-560
],
"size": [
315,
82
],
"flags": {},
"order": 3,
"mode": 0,
"inputs": [],
"outputs": [
{
"name": "INT",
"type": "INT",
"links": [
24,
111
]
}
],
"title": "Seed",
"properties": {
"cnr_id": "comfy-core",
"ver": "0.3.27",
"Node name for S&R": "PrimitiveInt"
},
"widgets_values": [
-323628412266755,
"randomize"
]
},
{
"id": 43,
"type": "ACBPPPENMappingOptions",
"pos": [
0,
440
],
"size": [
340,
112
],
"flags": {},
"order": 4,
"mode": 0,
"inputs": [],
"outputs": [
{
"name": "options",
"type": "PPP_OPTIONS_EN",
"links": [
109
]
}
],
"properties": {
"cnr_id": "sd-webui-prompt-postprocessor",
"ver": "84d31ef07286983a329de9d31c106efc836205ff",
"Node name for S&R": "ACBPPPENMappingOptions"
},
"widgets_values": [
"",
""
]
},
{
"id": 5,
"type": "EmptyLatentImage",
"pos": [
20,
-900
],
"size": [
315,
106
],
"flags": {},
"order": 5,
"mode": 0,
"inputs": [],
"outputs": [
{
"name": "LATENT",
"type": "LATENT",
"slot_index": 0,
"links": [
2
]
}
],
"properties": {
"cnr_id": "comfy-core",
"ver": "0.3.27",
"Node name for S&R": "EmptyLatentImage"
},
"widgets_values": [
1024,
1024,
1
]
},
{
"id": 16,
"type": "Checkpoint Loader with Name (Image Saver)",
"pos": [
-220,
-740
],
"size": [
560,
118
],
"flags": {},
"order": 6,
"mode": 0,
"inputs": [],
"outputs": [
{
"name": "MODEL",
"type": "MODEL",
"links": [
16,
105
]
},
{
"name": "CLIP",
"type": "CLIP",
"links": [
19,
45
]
},
{
"name": "VAE",
"type": "VAE",
"links": [
20
]
},
{
"name": "model_name",
"type": "STRING",
"links": [
110
]
}
],
"properties": {
"cnr_id": "comfyui-image-saver",
"ver": "f8520bcfe5339ba7b4bb64ba96f6ed03da87fb89",
"Node name for S&R": "Checkpoint Loader with Name (Image Saver)"
},
"widgets_values": [
"zz_Archived\\PDXL\\ponyDiffusionV6XL_v6StartWithThisOne.safetensors"
]
},
{
"id": 44,
"type": "ACBPromptPostProcessor",
"pos": [
500,
-380
],
"size": [
400,
510
],
"flags": {},
"order": 7,
"mode": 0,
"inputs": [
{
"name": "model",
"shape": 7,
"type": "MODEL,STRING",
"link": 105
},
{
"name": "wc_options",
"shape": 7,
"type": "PPP_OPTIONS_WC",
"link": 106
},
{
"name": "stn_options",
"shape": 7,
"type": "PPP_OPTIONS_STN",
"link": 107
},
{
"name": "cup_options",
"shape": 7,
"type": "PPP_OPTIONS_CUP",
"link": 108
},
{
"name": "en_options",
"shape": 7,
"type": "PPP_OPTIONS_EN",
"link": 109
},
{
"name": "modelname",
"shape": 7,
"type": "STRING",
"widget": {
"name": "modelname"
},
"link": 110
},
{
"name": "seed",
"shape": 7,
"type": "INT",
"widget": {
"name": "seed"
},
"link": 111
}
],
"outputs": [
{
"name": "pos_prompt",
"shape": 6,
"type": "STRING",
"links": [
112,
113
]
},
{
"name": "neg_prompt",
"shape": 6,
"type": "STRING",
"links": [
114,
115
]
},
{
"name": "variables",
"shape": 6,
"type": "PPP_DICT",
"links": [
116
]
}
],
"properties": {
"cnr_id": "sd-webui-prompt-postprocessor",
"ver": "2c34ec9cdf22365f623953bf764bcedaf506a3f9",
"Node name for S&R": "ACBPromptPostProcessor"
},
"widgets_values": [
"score_9, score_8_up, score_7_up, score_6_up, woman with __color__ hair, __action__",
"score_4, score_5",
"",
-1,
"randomize",
"minimal",
"stop",
true,
true,
true,
true,
false,
false,
100
]
}
],
"links": [
[
2,
5,
0,
3,
3,
"LATENT"
],
[
4,
6,
0,
3,
1,
"CONDITIONING"
],
[
6,
7,
0,
3,
2,
"CONDITIONING"
],
[
7,
3,
0,
8,
0,
"LATENT"
],
[
9,
8,
0,
9,
0,
"IMAGE"
],
[
16,
16,
0,
3,
0,
"MODEL"
],
[
19,
16,
1,
6,
0,
"CLIP"
],
[
20,
16,
2,
8,
1,
"VAE"
],
[
24,
17,
0,
3,
4,
"INT"
],
[
45,
16,
1,
7,
0,
"CLIP"
],
[
87,
20,
0,
37,
0,
"*"
],
[
105,
16,
0,
44,
0,
"MODEL"
],
[
106,
40,
0,
44,
1,
"PPP_OPTIONS_WC"
],
[
107,
42,
0,
44,
2,
"PPP_OPTIONS_STN"
],
[
108,
41,
0,
44,
3,
"PPP_OPTIONS_CUP"
],
[
109,
43,
0,
44,
4,
"PPP_OPTIONS_EN"
],
[
110,
16,
3,
44,
5,
"STRING"
],
[
111,
17,
0,
44,
6,
"INT"
],
[
112,
44,
0,
6,
1,
"STRING"
],
[
113,
44,
0,
35,
0,
"STRING"
],
[
114,
44,
1,
7,
1,
"STRING"
],
[
115,
44,
1,
36,
0,
"STRING"
],
[
116,
44,
2,
20,
0,
"PPP_DICT"
]
],
"groups": [],
"config": {},
"extra": {
"ds": {
"scale": 0.8580000000000002,
"offset": [
503.3154432844885,
1065.5795532391492
]
},
"frontendVersion": "1.39.19",
"VHS_latentpreview": true,
"VHS_latentpreviewrate": 0,
"VHS_MetadataImage": true,
"VHS_KeepIntermediate": true,
"workflowRendererVersion": "LG"
},
"version": 0.4
}