-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
232 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from __future__ import annotations | ||
|
||
import dataclasses | ||
import logging | ||
import re | ||
|
||
from dynamicprompts.commands import Command | ||
from dynamicprompts.enums import SamplingMethod | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
WRAP_MARKER_CHARACTERS = { | ||
"\u1801", # Mongolian ellipsis | ||
"\u2026", # Horizontal ellipsis | ||
"\u22EE", # Vertical ellipsis | ||
"\u22EF", # Midline horizontal ellipsis | ||
"\u22F0", # Up right diagonal ellipsis | ||
"\u22F1", # Down right diagonal ellipsis | ||
"\uFE19", # Presentation form for vertical horizontal ellipsis | ||
} | ||
|
||
WRAP_MARKER_RE = re.compile( | ||
f"[{''.join(WRAP_MARKER_CHARACTERS)}]+" # One or more wrap marker characters | ||
"|" | ||
r"\.{3,}", # ASCII ellipsis of 3 or more dots | ||
) | ||
|
||
|
||
def split_wrapper_string(s: str) -> tuple[str, str]: | ||
""" | ||
Split a string into a prefix and suffix at the first wrap marker. | ||
""" | ||
match = WRAP_MARKER_RE.search(s) | ||
if match is None: | ||
log.warning("Found no wrap marker in string %r", s) | ||
return s, "" | ||
else: | ||
return s[: match.start()], s[match.end() :] | ||
|
||
|
||
@dataclasses.dataclass(frozen=True) | ||
class WrapCommand(Command): | ||
wrapper: Command | ||
inner: Command | ||
sampling_method: SamplingMethod | None = None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Art Deco, ..., sleek, geometric forms, art deco style | ||
Pop Art, ....., vivid colors, flat color, 2D, strong lines, Pop Art |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import pytest | ||
from dynamicprompts.enums import SamplingMethod | ||
from dynamicprompts.parser.parse import parse | ||
from dynamicprompts.sampling_context import SamplingContext | ||
from dynamicprompts.wildcards import WildcardManager | ||
|
||
from tests.utils import sample_n | ||
|
||
|
||
# Methods currently supported by wrap command | ||
@pytest.fixture( | ||
params=[ | ||
SamplingMethod.COMBINATORIAL, | ||
SamplingMethod.RANDOM, | ||
], | ||
) | ||
def scon(request, wildcard_manager: WildcardManager) -> SamplingContext: | ||
return SamplingContext( | ||
default_sampling_method=request.param, | ||
wildcard_manager=wildcard_manager, | ||
) | ||
|
||
|
||
def test_wrap_with_wildcard(scon: SamplingContext): | ||
cmd = parse("%{__wrappers__$${fox|cow}}") | ||
assert sample_n(cmd, scon, n=4) == { | ||
"Art Deco, cow, sleek, geometric forms, art deco style", | ||
"Art Deco, fox, sleek, geometric forms, art deco style", | ||
"Pop Art, cow, vivid colors, flat color, 2D, strong lines, Pop Art", | ||
"Pop Art, fox, vivid colors, flat color, 2D, strong lines, Pop Art", | ||
} | ||
|
||
|
||
@pytest.mark.parametrize("placeholder", ["…", "᠁", ".........", "..."]) | ||
def test_wrap_with_literal(scon: SamplingContext, placeholder: str): | ||
cmd = parse("%{happy ... on a meadow$${fox|cow}}".replace("...", placeholder)) | ||
assert sample_n(cmd, scon, n=2) == { | ||
"happy fox on a meadow", | ||
"happy cow on a meadow", | ||
} | ||
|
||
|
||
def test_bad_wrap_is_prefix(scon: SamplingContext): | ||
cmd = parse("%{happy $${fox|cow}}") | ||
assert sample_n(cmd, scon, n=2) == { | ||
"happy fox", | ||
"happy cow", | ||
} | ||
|
||
|
||
def test_wrap_suffix(scon: SamplingContext): | ||
cmd = parse("%{... in jail$${fox|cow}}") | ||
assert sample_n(cmd, scon, n=2) == { | ||
"fox in jail", | ||
"cow in jail", | ||
} | ||
|
||
|
||
def test_wrap_with_variant(scon): | ||
cmd = parse("%{ {cool|hot} ...$${fox|cow}}") | ||
assert sample_n(cmd, scon, n=4) == { | ||
"cool fox", | ||
"cool cow", | ||
"hot fox", | ||
"hot cow", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.