nai-cli 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- nai_cli/__init__.py +3 -0
- nai_cli/__main__.py +5 -0
- nai_cli/api.py +529 -0
- nai_cli/auth.py +240 -0
- nai_cli/batch.py +126 -0
- nai_cli/caches.py +56 -0
- nai_cli/charge.py +231 -0
- nai_cli/cli.py +110 -0
- nai_cli/comfyui.py +194 -0
- nai_cli/commands/__init__.py +61 -0
- nai_cli/commands/account.py +79 -0
- nai_cli/commands/batch.py +142 -0
- nai_cli/commands/config.py +57 -0
- nai_cli/commands/context.py +201 -0
- nai_cli/commands/copies.py +138 -0
- nai_cli/commands/cost.py +70 -0
- nai_cli/commands/generate.py +257 -0
- nai_cli/commands/generation.py +550 -0
- nai_cli/commands/grid.py +99 -0
- nai_cli/commands/image_tools.py +248 -0
- nai_cli/commands/interrogate.py +77 -0
- nai_cli/commands/lint.py +24 -0
- nai_cli/commands/local.py +62 -0
- nai_cli/commands/meta.py +102 -0
- nai_cli/commands/models.py +62 -0
- nai_cli/commands/output.py +165 -0
- nai_cli/commands/redact.py +48 -0
- nai_cli/commands/references.py +87 -0
- nai_cli/commands/spec.py +245 -0
- nai_cli/commands/stats.py +54 -0
- nai_cli/commands/strip.py +15 -0
- nai_cli/commands/tags.py +32 -0
- nai_cli/commands/texts.py +71 -0
- nai_cli/commands/vibe.py +151 -0
- nai_cli/commands/watermark.py +70 -0
- nai_cli/config.py +369 -0
- nai_cli/cost.py +114 -0
- nai_cli/data/random_prompt.json +2536 -0
- nai_cli/decode.py +87 -0
- nai_cli/enhance.py +137 -0
- nai_cli/errors.py +40 -0
- nai_cli/fs.py +209 -0
- nai_cli/grid.py +244 -0
- nai_cli/images.py +250 -0
- nai_cli/import_image.py +235 -0
- nai_cli/inpaint.py +476 -0
- nai_cli/ledger.py +146 -0
- nai_cli/lint.py +302 -0
- nai_cli/local_models.py +135 -0
- nai_cli/meta.py +606 -0
- nai_cli/models.py +308 -0
- nai_cli/naming.py +426 -0
- nai_cli/parser.py +611 -0
- nai_cli/pixel_snap.py +525 -0
- nai_cli/presets.py +276 -0
- nai_cli/prompt.py +457 -0
- nai_cli/random_prompt.py +285 -0
- nai_cli/request.py +621 -0
- nai_cli/rerun.py +177 -0
- nai_cli/resample.py +133 -0
- nai_cli/sam.py +63 -0
- nai_cli/share.py +392 -0
- nai_cli/sheet.py +86 -0
- nai_cli/tagger.py +464 -0
- nai_cli/tags.py +44 -0
- nai_cli/tidy.py +176 -0
- nai_cli/tokenizer.py +313 -0
- nai_cli/values.py +114 -0
- nai_cli/vibe.py +464 -0
- nai_cli-0.1.0.dist-info/METADATA +105 -0
- nai_cli-0.1.0.dist-info/RECORD +75 -0
- nai_cli-0.1.0.dist-info/WHEEL +4 -0
- nai_cli-0.1.0.dist-info/entry_points.txt +2 -0
- nai_cli-0.1.0.dist-info/licenses/LICENSE +21 -0
- nai_cli-0.1.0.dist-info/licenses/THIRD_PARTY_NOTICES.md +56 -0
nai_cli/prompt.py
ADDED
|
@@ -0,0 +1,457 @@
|
|
|
1
|
+
"""Prompt text rules: quality suffix, V5 automatic text block, UC presets and nsfw."""
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
import unicodedata
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
|
|
7
|
+
MIX_SEPARATOR = "|"
|
|
8
|
+
MIX_ESCAPE = "||"
|
|
9
|
+
MAX_MIX_CHUNKS = 6
|
|
10
|
+
|
|
11
|
+
# JavaScript's \s, which the official rules use: unlike Python's it excludes \x1c-\x1f and \x85 and includes \ufeff
|
|
12
|
+
JS_SPACE = "\t\n\v\f\r \u00a0\u1680" + "".join(map(chr, range(0x2000, 0x200B))) + "\u2028\u2029\u202f\u205f\u3000\ufeff"
|
|
13
|
+
_S = "[" + re.escape(JS_SPACE) + "]"
|
|
14
|
+
TEXT_MARKER = re.compile(r"(?:^|" + _S + r"|[,.:\[\]{}、。])text:(?!:)", re.IGNORECASE)
|
|
15
|
+
ENHANCE_HINT = ", -2::upscaled, blurry::,"
|
|
16
|
+
_ENHANCE_HINT_PRESENT = "upscaled, blurry"
|
|
17
|
+
AUTO_TEXT_MARKER = "teXt:"
|
|
18
|
+
_GENERATED_MARKER = re.compile(r"(?:^|" + _S + r"|[,.:\[\]{}、。])teXt:(?!:)")
|
|
19
|
+
CJK = re.compile(r"[\u3000-\u303f\u3040-\u309f\u30a0-\u30ff\uff00-\uff9f\u4e00-\u9faf\u3400-\u4dbf]")
|
|
20
|
+
_QUOTE_PAIRS = {'"': '"', "“": "”", "「": "」", "'": "'", "‘": "’"}
|
|
21
|
+
_SINGLE_QUOTE_BOUNDARY = set(JS_SPACE + ",.")
|
|
22
|
+
NSFW = "nsfw"
|
|
23
|
+
FURRY_PREFIX = "fur dataset, "
|
|
24
|
+
_DATASET_PREFIXES = ("fur dataset", "background dataset")
|
|
25
|
+
_NSFW = re.compile(r"[\{\[]*nsfw[\}\]]*", re.IGNORECASE)
|
|
26
|
+
_NSFW_WITH_SEP = re.compile(r"[\{\[]*nsfw[\}\]]*" + _S + "*,?" + _S + "*", re.IGNORECASE)
|
|
27
|
+
_TRAILING_SEPARATORS = re.compile("[" + re.escape(JS_SPACE) + ",]+$")
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def js_trim(text: str) -> str:
|
|
31
|
+
return text.strip(JS_SPACE)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def split_mix_chunks(prompt: str) -> list[str]:
|
|
35
|
+
chunks: list[str] = []
|
|
36
|
+
buf: list[str] = []
|
|
37
|
+
escaped = False
|
|
38
|
+
i = 0
|
|
39
|
+
while i < len(prompt):
|
|
40
|
+
ch = prompt[i]
|
|
41
|
+
if ch == MIX_SEPARATOR and i + 1 < len(prompt) and prompt[i + 1] == MIX_SEPARATOR:
|
|
42
|
+
escaped = not escaped
|
|
43
|
+
buf.append(MIX_ESCAPE)
|
|
44
|
+
i += 2
|
|
45
|
+
continue
|
|
46
|
+
if ch == MIX_SEPARATOR and not escaped:
|
|
47
|
+
chunks.append("".join(buf))
|
|
48
|
+
buf = []
|
|
49
|
+
i += 1
|
|
50
|
+
continue
|
|
51
|
+
buf.append(ch)
|
|
52
|
+
i += 1
|
|
53
|
+
chunks.append("".join(buf))
|
|
54
|
+
if len(chunks) <= MAX_MIX_CHUNKS:
|
|
55
|
+
return chunks
|
|
56
|
+
return [*chunks[: MAX_MIX_CHUNKS - 1], MIX_SEPARATOR.join(chunks[MAX_MIX_CHUNKS - 1 :])]
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def js_length(text: str) -> int:
|
|
60
|
+
# JavaScript string length counts UTF-16 code units
|
|
61
|
+
return len(text.encode("utf-16-le", "surrogatepass")) // 2
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def longest_alternatives(prompt: str) -> str:
|
|
65
|
+
"""Each ||a|b|| group becomes its longest option, ties to the later one, as the official token counter does."""
|
|
66
|
+
parts = prompt.split(MIX_ESCAPE)
|
|
67
|
+
for i in range(1, len(parts), 2):
|
|
68
|
+
best, *rest = parts[i].split(MIX_SEPARATOR)
|
|
69
|
+
for option in rest:
|
|
70
|
+
if js_length(best) <= js_length(option):
|
|
71
|
+
best = option
|
|
72
|
+
parts[i] = best
|
|
73
|
+
return "".join(parts)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def apply_furry(prompt: str) -> str:
|
|
77
|
+
"""Furry mode starts the prompt with fur dataset unless it already names a dataset."""
|
|
78
|
+
return prompt if prompt.startswith(_DATASET_PREFIXES) else FURRY_PREFIX + prompt
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def append_suffix(prompt: str, suffix: str | None) -> str:
|
|
82
|
+
"""Appends ", suffix" without trimming, so "wind," becomes "wind,, suffix" as in the official client."""
|
|
83
|
+
if not suffix:
|
|
84
|
+
return prompt
|
|
85
|
+
return f"{prompt}, {suffix}" if prompt else suffix
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def apply_suffix(prompt: str, suffix: str | None) -> str:
|
|
89
|
+
"""The suffix goes into the first mix segment before the first text:, and every text: marker takes the first one's spelling."""
|
|
90
|
+
if not suffix:
|
|
91
|
+
return prompt
|
|
92
|
+
chunks = split_mix_chunks(prompt)
|
|
93
|
+
match = TEXT_MARKER.search(chunks[0])
|
|
94
|
+
if match is None:
|
|
95
|
+
chunks[0] = append_suffix(chunks[0], suffix)
|
|
96
|
+
else:
|
|
97
|
+
pieces = TEXT_MARKER.split(chunks[0])
|
|
98
|
+
pieces[0] = append_suffix(pieces[0], suffix)
|
|
99
|
+
chunks[0] = match.group(0).join(pieces)
|
|
100
|
+
return MIX_SEPARATOR.join(chunks)
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def compose_suffix(quality: str | None, transparent: bool) -> str | None:
|
|
104
|
+
if not transparent:
|
|
105
|
+
return quality or None
|
|
106
|
+
return f"transparent background, {quality}" if quality else "transparent background"
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
@dataclass
|
|
110
|
+
class AutoTextCharacter:
|
|
111
|
+
prompt: str
|
|
112
|
+
x: float = 0.5
|
|
113
|
+
y: float = 0.5
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def quoted_ranges(prompt: str) -> list[tuple[int, int]]:
|
|
117
|
+
ranges: list[tuple[int, int]] = []
|
|
118
|
+
cursor = 0
|
|
119
|
+
while cursor < len(prompt):
|
|
120
|
+
opening = prompt[cursor]
|
|
121
|
+
closing = _QUOTE_PAIRS.get(opening)
|
|
122
|
+
accepts = closing is not None and (opening != "'" or _is_single_quote_boundary(prompt, cursor - 1))
|
|
123
|
+
if not accepts:
|
|
124
|
+
cursor += 1
|
|
125
|
+
continue
|
|
126
|
+
apostrophe_style = closing in ("'", "’")
|
|
127
|
+
end = cursor + 1
|
|
128
|
+
while end < len(prompt) and (
|
|
129
|
+
prompt[end] != closing or (apostrophe_style and _is_letter_or_number(prompt, end + 1))
|
|
130
|
+
):
|
|
131
|
+
end += 1
|
|
132
|
+
if end >= len(prompt):
|
|
133
|
+
cursor += 1
|
|
134
|
+
continue
|
|
135
|
+
ranges.append((cursor, end + 1))
|
|
136
|
+
cursor = end + 1
|
|
137
|
+
return ranges
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def _is_letter_or_number(text: str, index: int) -> bool:
|
|
141
|
+
# JavaScript sees half of a surrogate pair there, which is neither
|
|
142
|
+
if not 0 <= index < len(text) or ord(text[index]) > 0xFFFF:
|
|
143
|
+
return False
|
|
144
|
+
return unicodedata.category(text[index])[0] in "LN"
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def _is_single_quote_boundary(text: str, index: int) -> bool:
|
|
148
|
+
if index < 0 or index >= len(text):
|
|
149
|
+
return True
|
|
150
|
+
return text[index] in _SINGLE_QUOTE_BOUNDARY
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def _extract_quoted(prompt: str) -> list[str]:
|
|
154
|
+
out = []
|
|
155
|
+
for start, end in quoted_ranges(prompt):
|
|
156
|
+
value = js_trim(prompt[start + 1 : end - 1])
|
|
157
|
+
if value:
|
|
158
|
+
out.append(value)
|
|
159
|
+
return out
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def _split_rows(chars: list[AutoTextCharacter]) -> list[list[AutoTextCharacter]]:
|
|
163
|
+
if len(chars) <= 1:
|
|
164
|
+
return [chars]
|
|
165
|
+
total_span = chars[-1].y - chars[0].y
|
|
166
|
+
split_index, largest_gap = 1, -1.0
|
|
167
|
+
for i in range(1, len(chars)):
|
|
168
|
+
gap = chars[i].y - chars[i - 1].y
|
|
169
|
+
if gap > largest_gap:
|
|
170
|
+
largest_gap, split_index = gap, i
|
|
171
|
+
if total_span <= 0.15 and largest_gap <= 0.1:
|
|
172
|
+
return [chars]
|
|
173
|
+
return [*_split_rows(chars[:split_index]), *_split_rows(chars[split_index:])]
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def _reading_order(chars: list[AutoTextCharacter]) -> list[AutoTextCharacter]:
|
|
177
|
+
by_y = sorted(chars, key=lambda c: c.y)
|
|
178
|
+
return [c for row in _split_rows(by_y) for c in sorted(row, key=lambda c: c.x)]
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
def auto_text_block(prompt: str, chars: list[AutoTextCharacter], use_coords: bool) -> str | None:
|
|
182
|
+
enabled = [c for c in chars if c.prompt]
|
|
183
|
+
if TEXT_MARKER.search(prompt) or any(TEXT_MARKER.search(c.prompt) for c in enabled):
|
|
184
|
+
return None
|
|
185
|
+
texts = _collect_quoted(split_mix_chunks(prompt)[0], enabled, use_coords)
|
|
186
|
+
if not texts:
|
|
187
|
+
return None
|
|
188
|
+
return f"{AUTO_TEXT_MARKER} " + "\n\n".join(texts)
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def apply_auto_text(prompt: str, chars: list[AutoTextCharacter], use_coords: bool) -> str:
|
|
192
|
+
"""V5 copies quoted text into a trailing teXt: block so the model renders it as text."""
|
|
193
|
+
block = auto_text_block(prompt, chars, use_coords)
|
|
194
|
+
if block is None:
|
|
195
|
+
return prompt
|
|
196
|
+
chunks = split_mix_chunks(prompt)
|
|
197
|
+
base = _TRAILING_SEPARATORS.sub("", chunks[0])
|
|
198
|
+
chunks[0] = block if not base else f"{base}, {block}"
|
|
199
|
+
return MIX_SEPARATOR.join(chunks)
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def strip_auto_text(prompt: str, chars: list[AutoTextCharacter], use_coords: bool) -> str:
|
|
203
|
+
out = []
|
|
204
|
+
for chunk in split_mix_chunks(prompt):
|
|
205
|
+
match = _GENERATED_MARKER.search(chunk)
|
|
206
|
+
if match is None:
|
|
207
|
+
out.append(chunk)
|
|
208
|
+
continue
|
|
209
|
+
prefix = chunk[: match.start()]
|
|
210
|
+
expected = "\n\n".join(_collect_quoted(prefix, chars, use_coords))
|
|
211
|
+
if js_trim(chunk[match.end() :]) != expected:
|
|
212
|
+
out.append(chunk)
|
|
213
|
+
continue
|
|
214
|
+
out.append(_TRAILING_SEPARATORS.sub("", prefix))
|
|
215
|
+
return MIX_SEPARATOR.join(out)
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
def _collect_quoted(base: str, chars: list[AutoTextCharacter], use_coords: bool) -> list[str]:
|
|
219
|
+
ordered = _reading_order(chars) if use_coords else chars
|
|
220
|
+
groups = [_extract_quoted(base), *(_extract_quoted(c.prompt) for c in ordered)]
|
|
221
|
+
combined = "".join(t for g in groups for t in g)
|
|
222
|
+
cjk = len(CJK.findall(combined))
|
|
223
|
+
# Quote order is reversed within each group when the text is mostly CJK
|
|
224
|
+
if cjk and cjk / js_length(combined) > 0.3:
|
|
225
|
+
groups = [list(reversed(g)) for g in groups]
|
|
226
|
+
return [t for g in groups for t in g]
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
def add_enhance_hint(text: str) -> str:
|
|
230
|
+
"""Add the Enhance down-weighting before the first text: segment or at the end, unless already present."""
|
|
231
|
+
if _ENHANCE_HINT_PRESENT in text:
|
|
232
|
+
return text
|
|
233
|
+
match = TEXT_MARKER.search(text)
|
|
234
|
+
if match is None:
|
|
235
|
+
return text + ENHANCE_HINT
|
|
236
|
+
return text[: match.start()] + ENHANCE_HINT + text[match.start() :]
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
def compose_negative(negative: str, preset_text: str, nsfw: bool) -> str:
|
|
240
|
+
"""Prefix the first mix segment with the UC preset and then nsfw; the user's text is untouched."""
|
|
241
|
+
chunks = negative.split(MIX_SEPARATOR)
|
|
242
|
+
if preset_text:
|
|
243
|
+
chunks[0] = f"{preset_text}, {chunks[0]}" if chunks[0] else preset_text
|
|
244
|
+
text = MIX_SEPARATOR.join(chunks)
|
|
245
|
+
return f"{NSFW}, {text}" if nsfw else text
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
def contains_nsfw(prompt: str) -> bool:
|
|
249
|
+
return bool(_NSFW.search(prompt))
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
def official_character_prompt(text: str) -> str:
|
|
253
|
+
"""The official client sends the first 1girl and 1boy of a character prompt as girl and boy."""
|
|
254
|
+
return text.replace("1girl", "girl", 1).replace("1boy", "boy", 1)
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
def remove_nsfw(negative: str) -> str:
|
|
258
|
+
"""Remove nsfw, {nsfw} and [nsfw] with their trailing comma; text without nsfw is untouched."""
|
|
259
|
+
if not negative or not _NSFW.search(negative):
|
|
260
|
+
return negative
|
|
261
|
+
result = _NSFW_WITH_SEP.sub("", negative)
|
|
262
|
+
result = re.sub("," + _S + "*,", ",", result)
|
|
263
|
+
result = re.sub("^" + _S + "*," + _S + "*", "", result)
|
|
264
|
+
result = re.sub(_S + "*," + _S + "*$", "", result)
|
|
265
|
+
return js_trim(result)
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
def nsfw_removal_damage(text: str) -> list[str]:
|
|
269
|
+
"""Tags containing more than a bare nsfw, which removal would break, such as 2::nsfw:: becoming 2::::."""
|
|
270
|
+
return [t.strip() for t in text.split(",") if _NSFW.search(t) and not _NSFW.fullmatch(t.strip())]
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
def strip_nsfw_prefix(negative: str) -> str:
|
|
274
|
+
"""Strip a leading nsfw and its ", ", the only form recognized when reading images."""
|
|
275
|
+
if not negative.startswith(NSFW):
|
|
276
|
+
return negative
|
|
277
|
+
return negative[len(NSFW) :].removeprefix(", ")
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
def strip_uc_preset(negative: str, preset_text: str) -> tuple[bool, str]:
|
|
281
|
+
"""Inverse of compose_negative after nsfw removal: drop the preset and its ", " from the first mix segment."""
|
|
282
|
+
chunks = negative.split(MIX_SEPARATOR)
|
|
283
|
+
if not preset_text or not chunks[0].startswith(preset_text):
|
|
284
|
+
return False, negative
|
|
285
|
+
chunks[0] = chunks[0][len(preset_text) :].removeprefix(", ")
|
|
286
|
+
return True, MIX_SEPARATOR.join(chunks)
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
def strip_suffix(prompt: str, suffix: str) -> tuple[bool, str]:
|
|
290
|
+
"""Inverse of apply_suffix, recovering the prompt as written."""
|
|
291
|
+
chunks = split_mix_chunks(prompt)
|
|
292
|
+
match = TEXT_MARKER.search(chunks[0])
|
|
293
|
+
pieces = [chunks[0]] if match is None else TEXT_MARKER.split(chunks[0])
|
|
294
|
+
head = pieces[0]
|
|
295
|
+
if head == suffix:
|
|
296
|
+
pieces[0] = ""
|
|
297
|
+
elif head.endswith(", " + suffix):
|
|
298
|
+
pieces[0] = head[: -len(", " + suffix)]
|
|
299
|
+
else:
|
|
300
|
+
return False, prompt
|
|
301
|
+
chunks[0] = pieces[0] if match is None else match.group(0).join(pieces)
|
|
302
|
+
return True, MIX_SEPARATOR.join(chunks)
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
_WEIGHT_NUMBER = re.compile(r"(?<![\w.])(-?[0-9]*\.?[0-9]+)$")
|
|
306
|
+
# A number right before :: starts a new weight, so one before a closing :: weights every following tag
|
|
307
|
+
NUMBER_BEFORE_CLOSE = re.compile(r"-?\d*\.?\d*$")
|
|
308
|
+
_SPACED_NUMBER = re.compile(r"(?:^|" + _S + r"|,)(-?\d*\.?\d* )$")
|
|
309
|
+
_NUMBER_AFTER = re.compile(r"^ ?-?\d*\.?\d*(?![\d.-]*::)")
|
|
310
|
+
# Only numbers up to 20 are flagged as misplaced weights
|
|
311
|
+
_PLAUSIBLE_WEIGHT = 21
|
|
312
|
+
|
|
313
|
+
|
|
314
|
+
@dataclass
|
|
315
|
+
class WeightMark:
|
|
316
|
+
kind: str
|
|
317
|
+
start: int
|
|
318
|
+
end: int
|
|
319
|
+
number: str = ""
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
def weight_marks(text: str) -> list[WeightMark]:
|
|
323
|
+
"""Classify each :: of n::...:: pairs: open, close, numeric_close, stray, spaced, number_after or unclosed."""
|
|
324
|
+
marks: list[WeightMark] = []
|
|
325
|
+
open_at: WeightMark | None = None
|
|
326
|
+
for m in re.finditer("::", text):
|
|
327
|
+
before = text[: m.start()]
|
|
328
|
+
opening = _WEIGHT_NUMBER.search(before)
|
|
329
|
+
if open_at is not None:
|
|
330
|
+
content = text[open_at.end : m.start()]
|
|
331
|
+
if opening and content.strip() == opening.group(1):
|
|
332
|
+
open_at = WeightMark("open", opening.start(), m.end(), opening.group(1))
|
|
333
|
+
marks.append(open_at)
|
|
334
|
+
continue
|
|
335
|
+
if opening and text[open_at.end : opening.start()].rstrip(" \t").endswith((",", MIX_SEPARATOR, "\n")):
|
|
336
|
+
# A number as its own tag switches weights without closing
|
|
337
|
+
open_at = WeightMark("switch", opening.start(), m.end(), opening.group(1))
|
|
338
|
+
marks.append(open_at)
|
|
339
|
+
continue
|
|
340
|
+
number = NUMBER_BEFORE_CLOSE.search(before).group(0)
|
|
341
|
+
marks.append(WeightMark("numeric_close" if number else "close", m.start(), m.end(), number))
|
|
342
|
+
open_at = None
|
|
343
|
+
continue
|
|
344
|
+
if opening:
|
|
345
|
+
open_at = WeightMark("open", opening.start(), m.end(), opening.group(1))
|
|
346
|
+
marks.append(open_at)
|
|
347
|
+
continue
|
|
348
|
+
if before[-4:].lower() == "text":
|
|
349
|
+
continue
|
|
350
|
+
spaced = _SPACED_NUMBER.search(before)
|
|
351
|
+
spaced_weight = _as_weight(spaced.group(1)) if spaced else None
|
|
352
|
+
after = _NUMBER_AFTER.search(text[m.end() :])
|
|
353
|
+
after_weight = _as_weight(after.group(0)) if after and after.group(0).strip() else None
|
|
354
|
+
if spaced_weight is not None and abs(spaced_weight) < _PLAUSIBLE_WEIGHT:
|
|
355
|
+
# Meant to open a weight, so the next :: closes it instead of being reported as stray
|
|
356
|
+
open_at = WeightMark("spaced", spaced.start(1), m.end(), spaced.group(1).strip())
|
|
357
|
+
marks.append(open_at)
|
|
358
|
+
elif after_weight is not None and abs(after_weight) < _PLAUSIBLE_WEIGHT:
|
|
359
|
+
marks.append(WeightMark("number_after", m.start(), m.end() + len(after.group(0)), after.group(0).strip()))
|
|
360
|
+
else:
|
|
361
|
+
marks.append(WeightMark("stray", m.start(), m.end(), NUMBER_BEFORE_CLOSE.search(before).group(0)))
|
|
362
|
+
if open_at is not None:
|
|
363
|
+
marks.append(WeightMark("unclosed", open_at.start, open_at.end, open_at.number))
|
|
364
|
+
return marks
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
def _as_weight(number: str) -> float | None:
|
|
368
|
+
try:
|
|
369
|
+
return float(number)
|
|
370
|
+
except ValueError:
|
|
371
|
+
return None
|
|
372
|
+
|
|
373
|
+
|
|
374
|
+
# Underscores in Danbooru kaomoji are part of the face and must survive underscore replacement
|
|
375
|
+
_KAOMOJI = [
|
|
376
|
+
"(o)_(o)",
|
|
377
|
+
"<o>_<o>",
|
|
378
|
+
"<|>_<|>",
|
|
379
|
+
"||_||",
|
|
380
|
+
"0_0",
|
|
381
|
+
"+_+",
|
|
382
|
+
"+_-",
|
|
383
|
+
"._.",
|
|
384
|
+
"=_=",
|
|
385
|
+
">_<",
|
|
386
|
+
"3_3",
|
|
387
|
+
"6_9",
|
|
388
|
+
">_o",
|
|
389
|
+
"@_@",
|
|
390
|
+
"^_^",
|
|
391
|
+
"o_o",
|
|
392
|
+
"u_u",
|
|
393
|
+
"x_x",
|
|
394
|
+
"|_|",
|
|
395
|
+
]
|
|
396
|
+
_KAOMOJI_PATTERN = re.compile(
|
|
397
|
+
r"(?<![^\W_])(?:" + "|".join(re.escape(k) for k in sorted(_KAOMOJI, key=len, reverse=True)) + r")(?![^\W_])"
|
|
398
|
+
)
|
|
399
|
+
_ANGLE = re.compile(r"<[^<>\n]*>")
|
|
400
|
+
|
|
401
|
+
|
|
402
|
+
def literal_ranges(text: str) -> list[tuple[int, int]]:
|
|
403
|
+
"""Spans that tidying and lint leave alone: quoted text copied into teXt:, text: segments up to the end
|
|
404
|
+
of their mix segment, <markers> and kaomoji."""
|
|
405
|
+
ranges = list(quoted_ranges(text))
|
|
406
|
+
for m in TEXT_MARKER.finditer(text):
|
|
407
|
+
end = m.end()
|
|
408
|
+
while end < len(text):
|
|
409
|
+
if text[end] == MIX_SEPARATOR:
|
|
410
|
+
if end + 1 < len(text) and text[end + 1] == MIX_SEPARATOR:
|
|
411
|
+
end += 2
|
|
412
|
+
continue
|
|
413
|
+
break
|
|
414
|
+
end += 1
|
|
415
|
+
ranges.append((m.end() - len("text:"), end))
|
|
416
|
+
ranges += [(m.start(), m.end()) for m in _ANGLE.finditer(text)]
|
|
417
|
+
ranges += [(m.start(), m.end()) for m in _KAOMOJI_PATTERN.finditer(text)]
|
|
418
|
+
merged: list[tuple[int, int]] = []
|
|
419
|
+
for start, end in sorted(ranges):
|
|
420
|
+
if merged and start < merged[-1][1]:
|
|
421
|
+
merged[-1] = (merged[-1][0], max(merged[-1][1], end))
|
|
422
|
+
else:
|
|
423
|
+
merged.append((start, end))
|
|
424
|
+
return merged
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+
def with_fixed(text: str, prefix: str, suffix: str) -> str:
|
|
428
|
+
"""Fixed tags around the prompt, joined with commas; empty parts drop out."""
|
|
429
|
+
return ", ".join(part for part in (prefix, text, suffix) if js_trim(part))
|
|
430
|
+
|
|
431
|
+
|
|
432
|
+
def fill_drawn(text: str, stored: str, drawn: str) -> str:
|
|
433
|
+
"""Each ||a|b|| group of text replaced by the option an image drew for the same group, found by matching
|
|
434
|
+
the image's stored prompt against the prompt it actually sent; text is kept when they do not match."""
|
|
435
|
+
groups, stored_parts = text.split(MIX_ESCAPE), stored.split(MIX_ESCAPE)
|
|
436
|
+
if len(groups) < 3 or len(groups) % 2 == 0 or len(stored_parts) % 2 == 0:
|
|
437
|
+
return text
|
|
438
|
+
pattern = "".join(
|
|
439
|
+
re.escape(part) if i % 2 == 0 else "(" + "|".join(map(re.escape, part.split(MIX_SEPARATOR))) + ")"
|
|
440
|
+
for i, part in enumerate(stored_parts)
|
|
441
|
+
)
|
|
442
|
+
match = re.fullmatch(pattern, drawn)
|
|
443
|
+
if match is None:
|
|
444
|
+
return text
|
|
445
|
+
out, searched_from = [], 0
|
|
446
|
+
for i, part in enumerate(groups):
|
|
447
|
+
if i % 2 == 0:
|
|
448
|
+
out.append(part)
|
|
449
|
+
continue
|
|
450
|
+
# Groups pair up in order, so a repeated group takes the next draw of that group
|
|
451
|
+
found = next((j for j in range(searched_from, len(stored_parts) // 2) if stored_parts[2 * j + 1] == part), None)
|
|
452
|
+
if found is None:
|
|
453
|
+
out.append(f"{MIX_ESCAPE}{part}{MIX_ESCAPE}")
|
|
454
|
+
else:
|
|
455
|
+
out.append(match.group(found + 1))
|
|
456
|
+
searched_from = found + 1
|
|
457
|
+
return "".join(out)
|