Skip to content

Commit

Permalink
* New cleanup options: Remove additional extra separators, Use EOL in…
Browse files Browse the repository at this point in the history
…stead of Space before BREAKs, Use EOL instead of Space before ANDs

* New format for commands
* New commands: "set", "echo" and "if/elif/else"
* Option to Remove extra network tags.
  • Loading branch information
acorderob committed Apr 1, 2024
1 parent e864368 commit bad0d24
Show file tree
Hide file tree
Showing 5 changed files with 889 additions and 220 deletions.
5 changes: 5 additions & 0 deletions .markdownlint.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"default": true,
"MD013": false,
"MD024": false
}
181 changes: 138 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ The Prompt Postprocessor for Stable Diffusion WebUI, formerly known as "sd-webui

Currently this extension has these functions:

* Allows the tagging of parts of the prompt and moves them to the negative prompt. This allows for useful tricks when using a wildcard extension since you can add negative content from choices made in the positive prompt.
* Allows marking parts of the prompt and moves them to the negative prompt. This allows for useful tricks when using a wildcard extension since you can add negative content from choices made in the positive prompt.
* Set values to local variables.
* Filter content based on the loaded SD model version or a set variable.
* Detect invalid wildcards and act on them.
* Clean up the prompt and negative prompt.

Expand All @@ -20,12 +22,12 @@ Notes:

1. It only recognizes regular A1111 prompt formats. So:

* **Attention**: \[prompt\] (prompt) (prompt:weight)
* **Alternation**: \[prompt1|prompt2|...\]
* **Scheduling**: \[prompt1:prompt2:step\]
* **Extra networks**: \<kind:model...\>
* **BREAK**: prompt1 BREAK prompt2
* **Composable Diffusion**: prompt1 AND prompt2
* **Attention**: `\[prompt\] (prompt) (prompt:weight)`
* **Alternation**: `\[prompt1|prompt2|...\]`
* **Scheduling**: `\[prompt1:prompt2:step\]`
* **Extra networks**: `\<kind:model...\>`
* **BREAK**: `prompt1 BREAK prompt2`
* **Composable Diffusion**: `prompt1 AND prompt2`

In SD.Next that means only the *A1111* or *Full* parsers. It will warn you if you use the *Compel* parser.
2. It only recognizes wildcards in the *\_\_wildcard\_\_* and *{choice|choice}* formats.
Expand All @@ -47,40 +49,151 @@ This extension should run after any wildcard extensions, so any remaining wildca

If you choose to not ignore wildcards, the extension will look for any *\_\_wildcard\_\_* or *{choice|choice}* constructs and act as configured.

### Sending content to the negative prompt
### Commands

The format of the tags is like this:
The extension uses now a new format for its commands. The format is similar to an extranetwork, but it has a "ppp:" prefix followed by the command, and then a space and any parameters (if any).

```text
<!content!>
<ppp:command parameters>
```

And an optional position in the negative prompt can be specified like this:
When a command is associated with any content, it will be between an opening and a closing command:

```text
<!!position!content!>
<ppp:command parameters>content<ppp:/command>
```

The `set` and `if` commands are the first to be processed.

### 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>
```

### Echo command

This command prints the value of a variable.

The format is:

```text
<ppp:echo varname>
```

### If command

This command allows you to filter content based on conditions.

The 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. The operation can be `eq`, `ne`, `gt`, `lt`, `ge`, `le` and the value can be a quoted string or an integer.

The variable can be one set with the `set` command or special variables like:

* `_sd` : the loaded model version (`"sd1"`, `"sd2"`, `"sdxl"`)

Any `elif`s (there can be multiple) and the `else` are optional.

#### Example

(multiline to be easier to read)

```text
<ppp:if _sd eq "sd1"><lora:test_sd1> test sd1
<ppp:elif _sd eq "sd2"><lora:test_sd2> test sd2
<ppp:elif _sd eq "sdxl"><lora:test_sdxl> test sdxl
<ppp:else>unknown model
<ppp:/if>
```

Where position can be:
Only one of the options will end up in the prompt, depending on the loaded model.

* **s**: at the start (the default)
* **e**: at the end
* **pN**: at the position of the insertion point "**<!!iN!!>**" with N being 0-9
### 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>
```

The insertion point of course must be in the negative prompt. If the insertion point is not found it inserts at the start.
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 tag to add to the negative prompt, like so:
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 <!strawberry!>
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 still supported (for now) and is like this:

```text
<!content!>
```

And an optional position can be specified like this:

```text
<!!position!content!>
```

With the insertion point like this:

```text
<!!iN!!>
```

### Notes on negative commands

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)
* However `(red<ppp:stn>[square]<ppp:/stn>:1.5)` will end up as `([square]:1.5)` in the negative prompt. The content of the negative tag is copied as is, and not joined with the surrounding modifier.

#### 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

### General settings
Expand All @@ -92,46 +205,28 @@ Then, if that option is chosen this extension will process it later and move tha
* **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.

### Content removal settings

* **Remove extra network tags**: removes all extra network tags.

### Send to negative prompt settings

* **Apply in img2img**: check if you want to do this processing in img2img processes.
* **Separator used when adding to the negative prompt**: you can specify the separator used when adding to the negative prompt (by default it's ", ").
* **Ignore tags with repeated content**: it ignores repeated content to avoid repetitions in the negative prompt.
* **Ignore repeated content**: it ignores repeated content to avoid repetitions in the negative prompt.
* **Join attention modifiers (weights) when possible**: it joins attention modifiers when possible (joins into one, multipliying their values).

### Clean up settings

* **Apply in img2img**: check if you want to do this processing in img2img processes.
* **Remove empty constructs**: removes attention/scheduling/alternation constructs when they are invalid.
* **Remove 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.
* **Clean up around ANDs**: removes consecutive ANDs and unnecessary commas and space around them.
* **Clean up around extra network tags**: removes spaces around them.
* **Remove extra spaces**: removes other unnecessary spaces.

## Notes on negative tags

Positional insertion tags have less priority that start/end tags, 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) tags.

The content of the negative tags is not processed and is copied as-is to the negative prompt. Other modifiers around the tags are processed in the following way.

### Attention modifiers (weights)

They will be translated to the negative prompt. For example:

* `(red<!square!>:1.5)` will end up as `(square:1.5)` in the negative prompt
* `(red[<!square!>]:1.5)` will end up as `(square:1.35)` in the negative prompt (weight=1.5*0.9)
* However `(red<![square]!>:1.5)` will end up as `([square]:1.5)` in the negative prompt. The content of the negative tag is copied as is, and not joined with the surrounding modifier.

### Prompt editing constructs (alternation and scheduling)

Negative tags inside such constructs will copy the construct to the negative prompt, but separating its elements. For example:

* **Alternation**: `[red<!square!>|blue<!circle!>]` will end up as `[square|], [|circle]` in the negative prompt, instead of `[square|circle]`
* **Scheduling**: `[red<!square!>:blue<!circle!>: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.

## License

MIT
Expand Down
Loading

0 comments on commit bad0d24

Please sign in to comment.