From b437fe40d7b2ab8049a563d12906755e9648fdaa Mon Sep 17 00:00:00 2001 From: Vladimir Mandic Date: Fri, 14 Feb 2025 15:14:38 -0500 Subject: [PATCH 1/2] prompt parsing with explicit masking Signed-off-by: Vladimir Mandic --- CHANGELOG.md | 5 +++++ modules/prompt_parser.py | 39 +++++++++++++++++++++++++++------------ wiki | 2 +- 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ed4b8ec0..fd311a4d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -77,6 +77,11 @@ We're back with another update with over 50 commits! - **Text encoder** add advanced per-model options for text encoder set in *settings -> text encoder -> Optional* + - **Subpath** + allow setting additional mount subpath over which server url will be accessible + set in *settings -> user interface* + - **Prompt parsing** + better handling of prompt parsing when using masking char `\` - **Fixes** - update torch nightly urls - docs/wiki always use relative links diff --git a/modules/prompt_parser.py b/modules/prompt_parser.py index 2a1dac2cc..d1a46f545 100644 --- a/modules/prompt_parser.py +++ b/modules/prompt_parser.py @@ -46,11 +46,19 @@ re_clean = re.compile(r"^\W+", re.S) re_whitespace = re.compile(r"\s+", re.S) re_break = re.compile(r"\s*\bBREAK\b|##\s*", re.S) re_attention_v2 = re.compile(r""" -\(|\[|\\\(|\\\[|\\|\\\\| -:([+-]?[.\d]+)| -\)|\]|\\\)|\\\]| -[^\(\)\[\]:]+| -: +\\\( | # Allow masked '\(' +\\\) | # Allow masked '\)' +\\\: | # Allow masked '\:' +\\\[ | # Allow masked '\[' +\\\] | # Allow masked '\]' +\\\\ | # Allow masked '\\' +\\ | # Removes '\' +\( | # Start '(' +\[ | # Start '[' +:([+-]?[.\d]+)\) | # Weight ':', followed by an optional sign and a number (integer or decimal), and then ')' +\) | # End ')' +\] | # End ']' +[^\\()\[\]:]+ | # Content matches any character except '\', '(', ')', '[', ']', ':' """, re.X) re_attention_v1 = re.compile(r""" \\\(| @@ -325,7 +333,7 @@ def parse_prompt_attention(text): re_attention = re_attention_v1 whitespace = '' else: - re_attention = re_attention_v1 + re_attention = re_attention_v2 if native and opts.sd_textencder_linebreak: text = text.replace('\n', ' BREAK ') else: @@ -335,7 +343,7 @@ def parse_prompt_attention(text): def multiply_range(start_position, multiplier): try: for p in range(start_position, len(res)): - res[p][1] *= multiplier + res[p][1] = round(res[p][1] * multiplier, 3) except Exception as e: log(f'Prompt parser: {e}') @@ -343,8 +351,14 @@ def parse_prompt_attention(text): try: section = m.group(0) weight = m.group(1) - if section.startswith('\\'): - res.append([section[1:], 1.0]) + # log.trace(f'Prompt: text="{text[m.start():m.end()]}" section="{section}" weight="{weight}"') + if len(section) == 0: + continue + elif section.startswith('\\'): + if len(res) > 0 and text[m.start()-1] != ' ': + res[-1][0] += section[1:] # append literal character to the last section + else: + res.append([section[1:], 1.0]) elif section == '(': round_brackets.append(len(res)) elif section == '[': @@ -377,9 +391,10 @@ def parse_prompt_attention(text): # merge runs of identical weights i = 0 while i + 1 < len(res): - if res[i][1] == res[i + 1][1]: - res[i][0] += whitespace + res[i + 1][0] - res.pop(i + 1) + if res[i][1] == res[i+1][1]: + sep = whitespace if res[i][0][-1].isalnum() else '' + res[i][0] += sep + res[i+1][0] + res.pop(i+1) else: i += 1 debug(f'Prompt: parser="{opts.prompt_attention}" {res}') diff --git a/wiki b/wiki index 068dbdabc..5c16be849 160000 --- a/wiki +++ b/wiki @@ -1 +1 @@ -Subproject commit 068dbdabc39f886a7b09c12a90e2363a1c6ced6a +Subproject commit 5c16be84937bdd4633537bdced89741f3425775d From 73b065215de53a77fb7961b7e6b5470fa74f1dc9 Mon Sep 17 00:00:00 2001 From: Vladimir Mandic Date: Fri, 14 Feb 2025 15:21:27 -0500 Subject: [PATCH 2/2] update changelog Signed-off-by: Vladimir Mandic --- CHANGELOG.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fd311a4d3..79ab30311 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,8 @@ # Change Log for SD.Next -## Update for 2025-02-13 +## Update for 2025-02-14 -### Highlight for 2025-02-13 +### Highlight for 2025-02-14 We're back with another update with over 50 commits! - Starting with massive UI update with full [localization](https://vladmandic.github.io/sdnext-docs/Locale/) for 8 languages @@ -11,10 +11,12 @@ We're back with another update with over 50 commits! - A lot of [outpainting](https://vladmandic.github.io/sdnext-docs/Outpaint/) goodies - Support for new models: [AlphaVLLM Lumina 2](https://github.com/Alpha-VLLM/Lumina-Image-2.0) and [Ostris Flex.1-Alpha](https://huggingface.co/ostris/Flex.1-alpha) - And new **Mixture-of-Diffusers** regional tiling pipeline +- Some changes to **prompt parsing** to allow more control as well as more flexibility when mouting SDNext server to custom URL +- Of course, cumulative fixes... *...and more* - see [changelog](https://github.com/vladmandic/sdnext/blob/dev/CHANGELOG.md) for full details! -### Details for 2025-02-13 +### Details for 2025-02-14 - **User Interface** - **Hints** @@ -79,7 +81,7 @@ We're back with another update with over 50 commits! set in *settings -> text encoder -> Optional* - **Subpath** allow setting additional mount subpath over which server url will be accessible - set in *settings -> user interface* + set in *settings -> user interface* - **Prompt parsing** better handling of prompt parsing when using masking char `\` - **Fixes**