refactoring

better readme
This commit is contained in:
Antonio Cordero Balcazar
2023-05-13 20:53:00 +02:00
parent ce64906850
commit f23ad8fbd1
3 changed files with 72 additions and 28 deletions
+23 -2
View File
@@ -2,9 +2,15 @@
## Purpose
This extension allows the marking of parts of the prompt and moves them to the negative prompt. This allows useful tricks when using a wildcard extension since you can add negative parts from choices made in the positive prompt.
This extension allows the tagging of parts of the prompt and moves them to the
negative prompt. This allows useful tricks when using a wildcard extension
since you can add negative content from choices made in the positive prompt.
The extension must be loaded after the wildcard extension. With the "Dynamic Prompts" extension this happens by default due to default folder names for both extensions.
Note: The extension must be loaded after the wildcard extension.
With the "Dynamic Prompts" extension this happens by default due to default
folder names for both extensions. But if this is not the case, you can just
rename the extension folder so the ordering works out.
## Format
@@ -27,3 +33,18 @@ Where position can be:
* pN: at the position of the insertion point "<!!iN!!>" with N being 0-9
The default position is the start, and also if the insertion point is not found.
## 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:
```text
blonde
strawberry blonde <!strawberry!>
brunette
```
Then, if that option is chosen this extension will process it later and move
that part to the negative prompt.
+13 -15
View File
@@ -1,28 +1,26 @@
if __name__ == "__main__":
raise SystemExit("This script must be run from a Stable Diffusion WebUI")
import logging
from modules import scripts, shared, script_callbacks
from modules.processing import StableDiffusionProcessing
from modules.shared import opts
from sendtonegative import SendToNegative
import sys
import os
sys.path.insert(1, os.path.join(sys.path[0], ".."))
__all__ = ["SendToNegativeScript"]
from sendtonegative import SendToNegative
import logging
from modules import scripts, shared, script_callbacks
from modules.processing import StableDiffusionProcessing
from modules.shared import opts
class SendToNegativeScript(scripts.Script):
def __init__(self):
self.logger = logging.getLogger(__name__)
self.logger.setLevel(logging.INFO)
if getattr(opts, "is_debug", False):
self.logger.setLevel(logging.DEBUG)
if not hasattr(self, "callbacks_added"):
script_callbacks.on_ui_settings(on_ui_settings)
script_callbacks.on_ui_settings(_on_ui_settings)
self.callbacks_added = True
def title(self):
@@ -39,12 +37,12 @@ class SendToNegativeScript(scripts.Script):
)
def on_ui_settings():
def _on_ui_settings():
section = ("send-to-negative", SendToNegative.NAME)
shared.opts.add_option(
key="stn_tagstart",
info=shared.OptionInfo(
"<!",
SendToNegative.DEFAULT_tagStart,
label="Tag start",
section=section,
),
@@ -52,7 +50,7 @@ def on_ui_settings():
shared.opts.add_option(
key="stn_tagend",
info=shared.OptionInfo(
"!>",
SendToNegative.DEFAULT_tagEnd,
label="Tag end",
section=section,
),
@@ -60,7 +58,7 @@ def on_ui_settings():
shared.opts.add_option(
key="stn_tagparamstart",
info=shared.OptionInfo(
"!",
SendToNegative.DEFAULT_tagParamStart,
label="Tag parameter start",
section=section,
),
@@ -68,7 +66,7 @@ def on_ui_settings():
shared.opts.add_option(
key="stn_tagparamend",
info=shared.OptionInfo(
"!",
SendToNegative.DEFAULT_tagParamEnd,
label="Tag parameter end",
section=section,
),
@@ -76,7 +74,7 @@ def on_ui_settings():
shared.opts.add_option(
key="stn_separator",
info=shared.OptionInfo(
", ",
SendToNegative.DEFAULT_separator,
label="Separator used when adding to the negative prompt",
section=section,
),
@@ -93,7 +91,7 @@ def on_ui_settings():
key="stn_cleanup",
info=shared.OptionInfo(
True,
label="Try to clean-up the prompt after processing",
label="Try to clean-up the prompt after processing. Removes extra spaces or separators (the configured separator).",
section=section,
),
)
+36 -11
View File
@@ -3,10 +3,15 @@ import re
class SendToNegative:
NAME = "Send to Negative"
VERSION = "0.3"
DEFAULT_tagStart = "<!"
DEFAULT_tagEnd = "!>"
DEFAULT_tagParamStart = "!"
DEFAULT_tagParamEnd = "!"
DEFAULT_separator = ", "
def __init__(
self,
tagStart=None,
@@ -32,9 +37,7 @@ class SendToNegative:
pN - content is added where the insertion point N is in the negative prompt or at the start if it does not exist. N can be 0 to 9.
iN - marks the position of insertion point N. Used only in the negative prompt and does not accept content. N can be 0 to 9.
The tags will be removed from the prompt or negative prompt without considering neighboring whitespace or separators.
iN - tags the position of insertion point N. Used only in the negative prompt and does not accept content. N can be 0 to 9.
"""
if logger is None:
self.logger = logging.getLogger(__name__)
@@ -43,18 +46,32 @@ class SendToNegative:
self.logger = logger
strStart = (
tagStart if tagStart is not None else getattr(opts, "stn_tagstart", "<!")
tagStart
if tagStart is not None
else getattr(opts, "stn_tagstart", self.DEFAULT_tagStart)
if opts is not None
else self.DEFAULT_tagStart
)
strEnd = (
tagEnd
if tagEnd is not None
else getattr(opts, "stn_tagend", self.DEFAULT_tagEnd)
if opts is not None
else self.DEFAULT_tagEnd
)
strEnd = tagEnd if tagEnd is not None else getattr(opts, "stn_tagend", "!>")
strParamStart = (
tagParamStart
if tagParamStart is not None
else getattr(opts, "stn_tagparamstart", "!")
else getattr(opts, "stn_tagparamstart", self.DEFAULT_tagParamStart)
if opts is not None
else self.DEFAULT_tagParamStart
)
strParamEnd = (
tagParamEnd
if tagParamEnd is not None
else getattr(opts, "stn_tagparamend", "!")
else getattr(opts, "stn_tagparamend", self.DEFAULT_tagParamEnd)
if opts is not None
else self.DEFAULT_tagParamEnd
)
escapeSequence = r"(?<!\\)"
self.ignoreRepeats = (
@@ -63,10 +80,18 @@ class SendToNegative:
else getattr(opts, "stn_ignorerepeats", True)
)
self.cleanup = (
cleanup if cleanup is not None else getattr(opts, "stn_cleanup", True)
cleanup
if cleanup is not None
else getattr(opts, "stn_cleanup", True)
if opts is not None
else True
)
self.separator = (
separator if separator is not None else getattr(opts, "stn_separator", ", ")
separator
if separator is not None
else getattr(opts, "stn_separator", self.DEFAULT_separator)
if opts is not None
else self.DEFAULT_separator
)
self.insertionPointTags = [
(strStart + strParamStart + "i" + str(x) + strParamEnd + strEnd)
@@ -89,7 +114,7 @@ class SendToNegative:
def processPrompt(self, original_prompt, original_negative_prompt):
"""
Extract from the prompt the marked parts and add them to the negative prompt
Extract from the prompt the tagged parts and add them to the negative prompt
"""
try:
prompt = original_prompt