feat: convert redundant checkboxes to radio buttons

spaces to underscore, underscore to spaces, or ignore
main v0.5.2
uwidev 2024-09-30 11:08:54 -07:00
parent 4716fe1149
commit b843d73bd1
4 changed files with 68 additions and 46 deletions

View File

@ -1,17 +1,16 @@
"""Enter format prompt.""" """Enter format prompt."""
import gradio as gr import gradio as gr
from modules import script_callbacks, scripts, shared from modules import script_callbacks, scripts, shared
# from prompt_formatting_pipeline import pipeline
from scripts import prompt_formatting_pipeline as pipeline from scripts import prompt_formatting_pipeline as pipeline
from scripts.prompt_formatting_definitions import UnderSpaceEnum
"""
Formatting settings
"""
SPACE_COMMAS = True SPACE_COMMAS = True
BRACKET2WEIGHT = True BRACKET2WEIGHT = True
SPACE2UNDERSCORE = False # SPACE2UNDERSCORE = False
IGNOREUNDERSCORES = True # IGNOREUNDERSCORES = True
PREFER_SPACING = UnderSpaceEnum.IGNORE
ui_prompts = set() ui_prompts = set()
@ -34,11 +33,9 @@ def format_prompt(*prompts: tuple[dict]):
prompt = pipeline.remove_whitespace_excessive(prompt) prompt = pipeline.remove_whitespace_excessive(prompt)
# Replace Spaces and/or underscores, unless disabled # Replace Spaces and/or underscores, unless disabled
prompt = pipeline.space_to_underscore(prompt, opposite=IGNOREUNDERSCORES) prompt = pipeline.space_to_underscore(prompt, mode=PREFER_SPACING)
prompt = pipeline.align_brackets(prompt) prompt = pipeline.align_brackets(prompt)
prompt = pipeline.space_and( prompt = pipeline.space_and(prompt) # for proper compositing alignment on colon
prompt
) # for proper compositing alignment on colons
prompt = pipeline.space_bracekts(prompt) prompt = pipeline.space_bracekts(prompt)
prompt = pipeline.align_colons(prompt) prompt = pipeline.align_colons(prompt)
prompt = pipeline.align_commas(prompt, do_it=SPACE_COMMAS) prompt = pipeline.align_commas(prompt, do_it=SPACE_COMMAS)
@ -76,6 +73,7 @@ def on_before_component(component: gr.component, **kwargs: dict):
def on_ui_settings(): def on_ui_settings():
section = ("pformat", "Prompt Formatter") section = ("pformat", "Prompt Formatter")
shared.opts.add_option( shared.opts.add_option(
"pformat_space_commas", "pformat_space_commas",
shared.OptionInfo( shared.OptionInfo(
@ -96,36 +94,48 @@ def on_ui_settings():
section=section, section=section,
), ),
) )
# shared.opts.add_option(
# "pfromat_space2underscore",
# shared.OptionInfo(
# False,
# "Convert spaces to underscores (default: underscore to spaces)",
# gr.Checkbox,
# {"interactive": True},
# section=section,
# ),
# )
# shared.opts.add_option(
# "pfromat_ignoreunderscores",
# shared.OptionInfo(
# True,
# "Do not convert either spaces or underscores (preserves DanBooru tag formatting)",
# gr.Checkbox,
# {"interactive": True},
# section=section,
# ),
shared.opts.add_option( shared.opts.add_option(
"pfromat_space2underscore", 'pformat_preferspacing',
shared.OptionInfo( shared.OptionInfo(
False, 'Space',
"Convert spaces to underscores (default: underscore to spaces)", "Preference in formatter to use spaces or underscore or ignore.",
gr.Checkbox, gr.Radio,
{"interactive": True}, {"choices": ["Space", "Underscore", "Ignore"]},
section=section, section=section
), )
)
shared.opts.add_option(
"pfromat_ignoreunderscores",
shared.OptionInfo(
True,
"Do not convert either spaces or underscores (preserves DanBooru tag formatting)",
gr.Checkbox,
{"interactive": True},
section=section,
),
) )
sync_settings() sync_settings()
def sync_settings(): def sync_settings():
global SPACE_COMMAS, BRACKET2WEIGHT, SPACE2UNDERSCORE, IGNOREUNDERSCORES global SPACE_COMMAS, BRACKET2WEIGHT, SPACE2UNDERSCORE, IGNOREUNDERSCORES, PREFER_SPACING
SPACE_COMMAS = shared.opts.pformat_space_commas SPACE_COMMAS = shared.opts.pformat_space_commas
BRACKET2WEIGHT = shared.opts.pfromat_bracket2weight BRACKET2WEIGHT = shared.opts.pfromat_bracket2weight
SPACE2UNDERSCORE = shared.opts.pfromat_space2underscore # SPACE2UNDERSCORE = shared.opts.pfromat_space2underscore
IGNOREUNDERSCORES = shared.opts.pfromat_ignoreunderscores # IGNOREUNDERSCORES = shared.opts.pfromat_ignoreunderscores
PREFER_SPACING = UnderSpaceEnum(shared.opts.pformat_preferspacing)
script_callbacks.on_before_component(on_before_component) script_callbacks.on_before_component(on_before_component)

View File

@ -0,0 +1,9 @@
"""Definitions to be pulled by other scripts."""
from enum import Enum
class UnderSpaceEnum(Enum):
SPACE = 'Space'
UNDERSCORE = 'Underscore'
IGNORE = 'Ignore'

View File

@ -4,6 +4,8 @@ import unicodedata
import regex as re import regex as re
from scripts.prompt_formatting_definitions import UnderSpaceEnum
brackets_opening = "([{<" brackets_opening = "([{<"
brackets_closing = ")]}>" brackets_closing = ")]}>"
@ -463,9 +465,7 @@ def calculate_weight(d: str, *, is_square_brackets: bool):
return 1 / 1.1 ** int(d) if is_square_brackets else 1 * 1.1 ** int(d) return 1 / 1.1 ** int(d) if is_square_brackets else 1 * 1.1 ** int(d)
def space_to_underscore(prompt: str, mode: UnderSpaceEnum = UnderSpaceEnum.SPACE):
def space_to_underscore(prompt: str, *, opposite = True):
"""Replace space with underscore or vice versa. """Replace space with underscore or vice versa.
It's a but funky right now because it uses the tokenizer to chunk for sub. It's a but funky right now because it uses the tokenizer to chunk for sub.
@ -475,15 +475,18 @@ def space_to_underscore(prompt: str, *, opposite = True):
This has been patched by requiring the match to be surrounded with a This has been patched by requiring the match to be surrounded with a
character, but I'm sure there's better solutions. It will work for now. character, but I'm sure there's better solutions. It will work for now.
""" """
match = ( if mode == UnderSpaceEnum.IGNORE:
r"(?<!BREAK)(?<=\w) +(?=\w)(?!BREAK)(?![^<]*>)" return prompt
if opposite
else r"(?<!BREAK)(?<=\w)_+(?=\w)(?!BREAK)(?![^<]*>)" if mode == UnderSpaceEnum.SPACE:
) match = r"(?<!BREAK)(?<=\w)_+(?=\w)(?!BREAK)(?![^<]*>)"
replace = "_" if opposite else " " replace = " "
elif mode == UnderSpaceEnum.UNDERSCORE:
match = r"(?<!BREAK)(?<=\w) +(?=\w)(?!BREAK)(?![^<]*>)"
replace = "_"
tokens: str = tokenize(prompt) tokens: str = tokenize(prompt)
print(tokens)
return ",".join(map(lambda t: re.sub(match, replace, t), tokens)) return ",".join(map(lambda t: re.sub(match, replace, t), tokens))

View File

@ -7,6 +7,7 @@ from typing import TYPE_CHECKING
import pytest import pytest
from scripts import prompt_formatting_pipeline as pipeline from scripts import prompt_formatting_pipeline as pipeline
from scripts.prompt_formatting_definitions import UnderSpaceEnum
def test_get_bracket_closing(): def test_get_bracket_closing():
@ -120,11 +121,10 @@ def test_bracket_to_weights():
assert pipeline.bracket_to_weights('((a), ((b)))') == '((a:1.10), (b:1.21):1.10)' assert pipeline.bracket_to_weights('((a), ((b)))') == '((a:1.10), (b:1.21):1.10)'
def test_space_to_underscore(): def test_space_to_underscore():
assert pipeline.space_to_underscore('<lora:chicken butt>, multiple subjects') == '<lora:chicken butt>, multiple_subjects' assert pipeline.space_to_underscore('<lora:chicken butt>, multiple subjects') == '<lora:chicken butt>, multiple subjects'
assert pipeline.space_to_underscore('one two three') == 'one_two_three' assert pipeline.space_to_underscore('one_two_three') == 'one two three'
assert pipeline.space_to_underscore('this is a test') == 'this_is_a_test' assert pipeline.space_to_underscore('this is_a test') == 'this is a test'
assert pipeline.space_to_underscore('<embed:foo bar>, baz') == '<embed:foo bar>, baz' assert pipeline.space_to_underscore('<embed:foo bar>, baz', UnderSpaceEnum.IGNORE) == '<embed:foo bar>, baz'
pipeline.BRACKET2WEIGHT = False assert pipeline.space_to_underscore('one two three', UnderSpaceEnum.UNDERSCORE) == 'one_two_three'
assert pipeline.space_to_underscore('some_var_name', opposite=pipeline.BRACKET2WEIGHT) == 'some var name'