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.
Files changed (75) hide show
  1. nai_cli/__init__.py +3 -0
  2. nai_cli/__main__.py +5 -0
  3. nai_cli/api.py +529 -0
  4. nai_cli/auth.py +240 -0
  5. nai_cli/batch.py +126 -0
  6. nai_cli/caches.py +56 -0
  7. nai_cli/charge.py +231 -0
  8. nai_cli/cli.py +110 -0
  9. nai_cli/comfyui.py +194 -0
  10. nai_cli/commands/__init__.py +61 -0
  11. nai_cli/commands/account.py +79 -0
  12. nai_cli/commands/batch.py +142 -0
  13. nai_cli/commands/config.py +57 -0
  14. nai_cli/commands/context.py +201 -0
  15. nai_cli/commands/copies.py +138 -0
  16. nai_cli/commands/cost.py +70 -0
  17. nai_cli/commands/generate.py +257 -0
  18. nai_cli/commands/generation.py +550 -0
  19. nai_cli/commands/grid.py +99 -0
  20. nai_cli/commands/image_tools.py +248 -0
  21. nai_cli/commands/interrogate.py +77 -0
  22. nai_cli/commands/lint.py +24 -0
  23. nai_cli/commands/local.py +62 -0
  24. nai_cli/commands/meta.py +102 -0
  25. nai_cli/commands/models.py +62 -0
  26. nai_cli/commands/output.py +165 -0
  27. nai_cli/commands/redact.py +48 -0
  28. nai_cli/commands/references.py +87 -0
  29. nai_cli/commands/spec.py +245 -0
  30. nai_cli/commands/stats.py +54 -0
  31. nai_cli/commands/strip.py +15 -0
  32. nai_cli/commands/tags.py +32 -0
  33. nai_cli/commands/texts.py +71 -0
  34. nai_cli/commands/vibe.py +151 -0
  35. nai_cli/commands/watermark.py +70 -0
  36. nai_cli/config.py +369 -0
  37. nai_cli/cost.py +114 -0
  38. nai_cli/data/random_prompt.json +2536 -0
  39. nai_cli/decode.py +87 -0
  40. nai_cli/enhance.py +137 -0
  41. nai_cli/errors.py +40 -0
  42. nai_cli/fs.py +209 -0
  43. nai_cli/grid.py +244 -0
  44. nai_cli/images.py +250 -0
  45. nai_cli/import_image.py +235 -0
  46. nai_cli/inpaint.py +476 -0
  47. nai_cli/ledger.py +146 -0
  48. nai_cli/lint.py +302 -0
  49. nai_cli/local_models.py +135 -0
  50. nai_cli/meta.py +606 -0
  51. nai_cli/models.py +308 -0
  52. nai_cli/naming.py +426 -0
  53. nai_cli/parser.py +611 -0
  54. nai_cli/pixel_snap.py +525 -0
  55. nai_cli/presets.py +276 -0
  56. nai_cli/prompt.py +457 -0
  57. nai_cli/random_prompt.py +285 -0
  58. nai_cli/request.py +621 -0
  59. nai_cli/rerun.py +177 -0
  60. nai_cli/resample.py +133 -0
  61. nai_cli/sam.py +63 -0
  62. nai_cli/share.py +392 -0
  63. nai_cli/sheet.py +86 -0
  64. nai_cli/tagger.py +464 -0
  65. nai_cli/tags.py +44 -0
  66. nai_cli/tidy.py +176 -0
  67. nai_cli/tokenizer.py +313 -0
  68. nai_cli/values.py +114 -0
  69. nai_cli/vibe.py +464 -0
  70. nai_cli-0.1.0.dist-info/METADATA +105 -0
  71. nai_cli-0.1.0.dist-info/RECORD +75 -0
  72. nai_cli-0.1.0.dist-info/WHEEL +4 -0
  73. nai_cli-0.1.0.dist-info/entry_points.txt +2 -0
  74. nai_cli-0.1.0.dist-info/licenses/LICENSE +21 -0
  75. nai_cli-0.1.0.dist-info/licenses/THIRD_PARTY_NOTICES.md +56 -0
nai_cli/meta.py ADDED
@@ -0,0 +1,606 @@
1
+ """Generation settings read from NovelAI images: PNG text, EXIF UserComment or stealth data in the alpha."""
2
+
3
+ import io
4
+ import json
5
+ import math
6
+ import struct
7
+ import zlib
8
+ from collections.abc import Callable
9
+ from dataclasses import dataclass, replace
10
+ from pathlib import Path
11
+ from typing import Any, TypeVar
12
+
13
+ from PIL import Image
14
+
15
+ from nai_cli import decode, errors, presets, values
16
+ from nai_cli import prompt as P
17
+ from nai_cli import request as R
18
+ from nai_cli.models import CFG_RESCALE_RANGE, MAX_SEED, MAX_STEPS, MODELS, Model, resolve
19
+
20
+ _STEALTH_MAGIC = {b"stealth_pnginfo": False, decode.STEALTH_COMPRESSED: True}
21
+ _EXIF_IFD = 0x8769
22
+ T = TypeVar("T")
23
+ _USER_COMMENT = 0x9286
24
+
25
+ _QUALITY_FROM_HINT = {0: "none", 1: "standard", 3: "light"}
26
+ _UC_FROM_HINT = {2: "heavy", 3: "light", 4: "human", 5: "furry", 0: "none"}
27
+ _UC_FROM_ID = {v: k for k, v in presets.UC_PRESET_ID.items()}
28
+ # Models are identified by the hash in Source; unlisted V5, V4.5 and V4 hashes are the Curated model of each
29
+ _V5_SOURCES = {
30
+ "NovelAI Diffusion V5 657484A5": "nai-diffusion-5-full",
31
+ "NovelAI Diffusion V5 0ADF9AB7": "nai-diffusion-5-full",
32
+ "NovelAI Diffusion V5 DB276663": "nai-diffusion-5-curated",
33
+ "NovelAI Diffusion V5 93F4BD30": "nai-diffusion-5-full-medium",
34
+ "NovelAI Diffusion V5 70AB5786": "nai-diffusion-5-full-medium",
35
+ }
36
+ _V4_SOURCES = {
37
+ "DiffusionModelMetaName.NAIv4next 4BDE2A90": "nai-diffusion-4-5-full",
38
+ "NovelAI Diffusion V4.5 4BDE2A90": "nai-diffusion-4-5-full",
39
+ "NovelAI Diffusion V4.5 1229B44F": "nai-diffusion-4-5-full",
40
+ "NovelAI Diffusion V4.5 B9F340FD": "nai-diffusion-4-5-full",
41
+ "NovelAI Diffusion V4.5 F3D95188": "nai-diffusion-4-5-full",
42
+ "DiffusionModelMetaName.NAIv4next C02D4F98": "nai-diffusion-4-5-curated",
43
+ "NovelAI Diffusion V4.5 C02D4F98": "nai-diffusion-4-5-curated",
44
+ "NovelAI Diffusion V4.5 5AB81C7C": "nai-diffusion-4-5-curated",
45
+ "NovelAI Diffusion V4.5 B5A2A797": "nai-diffusion-4-5-curated",
46
+ "NovelAI Diffusion V4 5AB81C7C": "nai-diffusion-4-5-curated",
47
+ "NovelAI Diffusion V4 B5A2A797": "nai-diffusion-4-5-curated",
48
+ "NovelAI Diffusion V4 37442FCA": "nai-diffusion-4-full",
49
+ "NovelAI Diffusion V4 4F49EC75": "nai-diffusion-4-full",
50
+ "NovelAI Diffusion V4 CA4B7203": "nai-diffusion-4-full",
51
+ "NovelAI Diffusion V4 79F47848": "nai-diffusion-4-full",
52
+ "NovelAI Diffusion V4 F6302A9D": "nai-diffusion-4-full",
53
+ "NovelAI Diffusion V4 7ABFFA2A": "nai-diffusion-4-curated-preview",
54
+ "NovelAI Diffusion V4 C1CCBA86": "nai-diffusion-4-curated-preview",
55
+ "NovelAI Diffusion V4 770A9E12": "nai-diffusion-4-curated-preview",
56
+ }
57
+
58
+
59
+ def png_text_entries(data: bytes) -> list[tuple[str, str]]:
60
+ """Every tEXt, iTXt and zTXt chunk as (keyword, text), in file order."""
61
+ out: list[tuple[str, str]] = []
62
+ for kind, body, _, _ in decode.png_chunks(data):
63
+ try:
64
+ if kind == b"tEXt":
65
+ key, value = body.split(b"\0", 1)
66
+ out.append((key.decode("latin1"), value.decode("utf-8", "replace")))
67
+ elif kind == b"iTXt":
68
+ key, rest = body.split(b"\0", 1)
69
+ compressed = rest[0]
70
+ rest = rest[2:]
71
+ _, rest = rest.split(b"\0", 1)
72
+ _, text = rest.split(b"\0", 1)
73
+ text = decode.inflate(text) if compressed else text
74
+ out.append((key.decode("latin1"), text.decode("utf-8", "replace")))
75
+ elif kind == b"zTXt":
76
+ key, rest = body.split(b"\0", 1)
77
+ out.append((key.decode("latin1"), decode.inflate(rest[1:]).decode("utf-8", "replace")))
78
+ except (ValueError, IndexError):
79
+ continue
80
+ return out
81
+
82
+
83
+ def png_text_chunks(data: bytes) -> dict[str, str]:
84
+ """By keyword; a keyword repeated keeps its last text."""
85
+ return dict(png_text_entries(data))
86
+
87
+
88
+ def stealth_text(img: Image.Image) -> dict[str, str]:
89
+ """Read alpha LSBs column-major: magic, 32-bit length in bits, payload."""
90
+ if "A" not in img.getbands() or img.width * img.height > decode.STEALTH_MAX_PIXELS:
91
+ return {}
92
+ alpha = img.getchannel("A")
93
+ width, height = alpha.size
94
+ pixels = alpha.load()
95
+
96
+ def bits():
97
+ for x in range(width):
98
+ for y in range(height):
99
+ yield pixels[x, y] & 1
100
+
101
+ stream = bits()
102
+
103
+ def read_bytes(n: int) -> bytes | None:
104
+ out = bytearray()
105
+ for _ in range(n):
106
+ byte = 0
107
+ for _ in range(8):
108
+ bit = next(stream, None)
109
+ if bit is None:
110
+ return None
111
+ byte = (byte << 1) | bit
112
+ out.append(byte)
113
+ return bytes(out)
114
+
115
+ magic = read_bytes(len(b"stealth_pnginfo"))
116
+ if magic not in _STEALTH_MAGIC:
117
+ return {}
118
+ length_bytes = read_bytes(4)
119
+ if length_bytes is None:
120
+ return {}
121
+ (bit_length,) = struct.unpack(">I", length_bytes)
122
+ if bit_length <= 0 or bit_length > width * height - (len(magic) + 4) * 8:
123
+ return {}
124
+ payload = read_bytes(-(-bit_length // 8))
125
+ if payload is None:
126
+ return {}
127
+ try:
128
+ text = decode.inflate(payload, decode.GZIP) if _STEALTH_MAGIC[magic] else payload
129
+ parsed = values.parse_json(text.decode("utf-8"))
130
+ except (OSError, ValueError, EOFError, zlib.error):
131
+ return {}
132
+ if isinstance(parsed, dict):
133
+ return {k: v if isinstance(v, str) else json.dumps(v, ensure_ascii=False) for k, v in parsed.items()}
134
+ return {}
135
+
136
+
137
+ def exif_text(img: Image.Image) -> dict[str, str]:
138
+ """Settings in the EXIF UserComment, where NovelAI's WebP images carry them, decoded as the official
139
+ importer does: an 8-byte charset header, UTF-16 for UNICODE, and trailing NULs dropped."""
140
+ try:
141
+ raw = img.getexif().get_ifd(_EXIF_IFD).get(_USER_COMMENT)
142
+ except (OSError, ValueError, SyntaxError, struct.error, KeyError, TypeError):
143
+ return {}
144
+ if not isinstance(raw, bytes) or len(raw) < 8:
145
+ return {}
146
+ head, body = raw[:8], raw[8:]
147
+ if head == b"UNICODE\0":
148
+ # The official decoder follows a byte order mark and otherwise reads big-endian
149
+ text = body.decode("utf-16" if body[:2] in (b"\xff\xfe", b"\xfe\xff") else "utf-16-be", "replace")
150
+ else:
151
+ text = (body if head in (b"ASCII\0\0\0", b"\0" * 8) else raw).decode("utf-8", "replace")
152
+ try:
153
+ parsed = values.parse_json(text.rstrip("\0"))
154
+ except ValueError:
155
+ return {}
156
+ if not isinstance(parsed, dict):
157
+ return {}
158
+ return {
159
+ k: v.replace("\0", "") if isinstance(v, str) else json.dumps(v, ensure_ascii=False) for k, v in parsed.items()
160
+ }
161
+
162
+
163
+ def read_raw(path: str | Path) -> dict[str, str]:
164
+ path = Path(path)
165
+ return raw_of(_bytes(path), path.name)
166
+
167
+
168
+ def raw_of(data: bytes, name: str) -> dict[str, str]:
169
+ """The settings an image's bytes keep, from PNG text, EXIF or the alpha channel; bytes that are no image are
170
+ refused, not taken for an image without settings."""
171
+ chunks = png_text_chunks(data)
172
+ if "Comment" in chunks:
173
+ return chunks
174
+ try:
175
+ with Image.open(io.BytesIO(data)) as img:
176
+ found = exif_text(img)
177
+ if "Comment" not in found:
178
+ img.load()
179
+ found = stealth_text(img)
180
+ except decode.BAD_IMAGE as e:
181
+ raise errors.NaiError(errors.BAD_ARGS, f"cannot read image {name}: {e}") from e
182
+ if "Comment" in found:
183
+ return found
184
+ raise errors.NaiError(errors.NO_METADATA, f"{name} has no NovelAI generation settings")
185
+
186
+
187
+ def _stored_vibes(encodings: Any) -> int:
188
+ if not isinstance(encodings, list):
189
+ return 0
190
+ return sum(1 for e in encodings if isinstance(e, str) and e)
191
+
192
+
193
+ def _normalized_vibes(strengths: Any) -> bool | None:
194
+ """Guessed as the official import does: several strengths summing to 1 were normalized, above 1 they were
195
+ not; otherwise unknown."""
196
+ if not isinstance(strengths, list) or len(strengths) < 2:
197
+ return None
198
+ try:
199
+ total = sum(abs(float(s)) for s in strengths)
200
+ except (TypeError, ValueError):
201
+ return None
202
+ if 0.99 <= total <= 1.01:
203
+ return True
204
+ return False if total > 1 else None
205
+
206
+
207
+ def model_from_source(source: str | None) -> str | None:
208
+ """Model id supported by nai-cli, or None for V3 and earlier."""
209
+ if not source:
210
+ return None
211
+ if "NovelAI Diffusion V5" in source:
212
+ return _V5_SOURCES.get(source, "nai-diffusion-5-curated")
213
+ if "NovelAI Diffusion V4" in source or "DiffusionModelMetaName.NAIv4next" in source:
214
+ curated = "nai-diffusion-4-5-curated" if "V4.5" in source else "nai-diffusion-4-curated-preview"
215
+ return _V4_SOURCES.get(source, curated)
216
+ return None
217
+
218
+
219
+ # The key V5 had while in testing, which the test site ran with the Curated weights
220
+ _STAGING_MODELS = {"custom": "nai-diffusion-5-curated"}
221
+
222
+
223
+ def model_of(cm: dict[str, Any], source: str | None) -> str | None:
224
+ """The model named in the settings, else the one the Source line points to."""
225
+ named = _STAGING_MODELS.get(str(cm.get("model_name") or ""), str(cm.get("model_name") or ""))
226
+ try:
227
+ return resolve(named).id if named else model_from_source(source)
228
+ except errors.NaiError:
229
+ return model_from_source(source)
230
+
231
+
232
+ def parse(path: str | Path) -> dict[str, Any]:
233
+ return parse_raw(read_raw(path))
234
+
235
+
236
+ def parse_checked(path: str | Path) -> tuple[dict[str, Any], bool | None]:
237
+ """The settings, and whether every pixel decodes, None for an image too large to decode in full; settings kept
238
+ before a file was cut off are still read."""
239
+ path = Path(path)
240
+ data = _bytes(path)
241
+ record = parse_raw(raw_of(data, path.name))
242
+ try:
243
+ decode.verify(data)
244
+ except Image.DecompressionBombError:
245
+ return record, None
246
+ except decode.BAD_IMAGE:
247
+ return record, False
248
+ return record, True
249
+
250
+
251
+ def _bytes(path: Path) -> bytes:
252
+ try:
253
+ return path.read_bytes()
254
+ except OSError as e:
255
+ raise errors.NaiError(errors.BAD_ARGS, f"cannot read {path}: {e}") from e
256
+
257
+
258
+ @dataclass(frozen=True)
259
+ class Drawn:
260
+ """An image's prompts as stored, with ||a|b|| groups, and as actually sent once each group was drawn."""
261
+
262
+ prompt: tuple[str, str]
263
+ negative: tuple[str, str]
264
+ characters: tuple[tuple[str, str], ...]
265
+ character_negatives: tuple[tuple[str, str], ...]
266
+
267
+
268
+ def drawn(raw: dict[str, str]) -> Drawn | None:
269
+ """None when the image keeps no actual_prompts, which only random prompts leave."""
270
+ return _readable(lambda: _drawn(_comment(raw)))
271
+
272
+
273
+ def _drawn(cm: dict[str, Any]) -> Drawn | None:
274
+ actual = cm.get("actual_prompts")
275
+ if not isinstance(actual, dict):
276
+ return None
277
+ sent, sent_neg = actual.get("prompt") or {}, actual.get("negative_prompt") or {}
278
+ stored = (cm.get("v4_prompt") or {}).get("caption") or {}
279
+ stored_neg = (cm.get("v4_negative_prompt") or {}).get("caption") or {}
280
+
281
+ def pairs(stored_caption: dict, sent_caption: dict) -> tuple[tuple[str, str], ...]:
282
+ before = [_text(c.get("char_caption", "")) for c in stored_caption.get("char_captions") or []]
283
+ after = [_text(c.get("char_caption", "")) for c in sent_caption.get("char_captions") or []]
284
+ return tuple(zip(before, after, strict=False)) if len(before) == len(after) else ()
285
+
286
+ return Drawn(
287
+ (_text(cm.get("prompt", "")), _text(sent.get("base_caption", ""))),
288
+ (_text(cm.get("uc", "")), _text(sent_neg.get("base_caption", ""))),
289
+ pairs(stored, sent),
290
+ pairs(stored_neg, sent_neg),
291
+ )
292
+
293
+
294
+ def with_drawn_prompts(raw: dict[str, str]) -> dict[str, str]:
295
+ """The settings as if the prompts actually sent had been typed, as the import dialog's Actual Prompt reads
296
+ them: both captions replaced whole. The switches beside them are settings, not prompts, and stay."""
297
+ return _readable(lambda: _with_drawn_prompts(raw, _comment(raw)))
298
+
299
+
300
+ def _with_drawn_prompts(raw: dict[str, str], cm: dict[str, Any]) -> dict[str, str]:
301
+ actual = cm.get("actual_prompts")
302
+ if not isinstance(actual, dict):
303
+ return raw
304
+ switches = {k: v for k, v in (cm.get("v4_negative_prompt") or {}).items() if k != "caption"}
305
+ use_coords = (cm.get("v4_prompt") or {}).get("use_coords", False)
306
+ cm["v4_prompt"] = {"caption": dict(actual.get("prompt") or {}), "use_coords": use_coords}
307
+ cm["v4_negative_prompt"] = {"caption": dict(actual.get("negative_prompt") or {}), **switches}
308
+ return {**raw, "Comment": json.dumps(cm)}
309
+
310
+
311
+ def parse_raw(raw: dict[str, str]) -> dict[str, Any]:
312
+ """raw holds the key-value pairs from text chunks or stealth data and must contain Comment; settings of a
313
+ shape nai-cli cannot read are no_metadata."""
314
+ return _readable(lambda: _record(raw, _comment(raw)))
315
+
316
+
317
+ def _comment(raw: dict[str, str]) -> dict[str, Any]:
318
+ try:
319
+ cm = values.parse_json(raw["Comment"])
320
+ except ValueError as e:
321
+ raise errors.NaiError(errors.NO_METADATA, f"Comment is not JSON: {e}") from e
322
+ if not isinstance(cm, dict):
323
+ raise errors.NaiError(errors.NO_METADATA, "Comment is not a JSON object")
324
+ return cm
325
+
326
+
327
+ def _readable(read: Callable[[], T]) -> T:
328
+ try:
329
+ return read()
330
+ except (AttributeError, TypeError, KeyError, IndexError, ValueError) as e:
331
+ # Other tools write Comment too, with any shape inside
332
+ raise errors.NaiError(errors.NO_METADATA, f"the settings cannot be read: {type(e).__name__}: {e}") from e
333
+
334
+
335
+ def _text(value: Any) -> str:
336
+ if not isinstance(value, str):
337
+ raise TypeError(f"a prompt is {type(value).__name__}, not text")
338
+ return value
339
+
340
+
341
+ def _whole(cm: dict[str, Any], key: str) -> int | None:
342
+ value = cm.get(key)
343
+ if value is None or (values.is_whole(value) and values.is_number(value)):
344
+ return value
345
+ raise TypeError(f"{key} is {value!r}, not a whole number")
346
+
347
+
348
+ def _string(cm: dict[str, Any], key: str) -> str | None:
349
+ value = cm.get(key)
350
+ if value is None or isinstance(value, str):
351
+ return value
352
+ raise TypeError(f"{key} is {value!r}, not text")
353
+
354
+
355
+ def _premultiplied(straight_alpha: Any) -> bool | None:
356
+ if straight_alpha is None or isinstance(straight_alpha, bool):
357
+ return None if straight_alpha is None else not straight_alpha
358
+ raise TypeError(f"straight_alpha is {straight_alpha!r}, not true or false")
359
+
360
+
361
+ def _number(value: Any, name: str) -> float | None:
362
+ # true counts as 1 in Python, and would come back as --cfg=True
363
+ if value is None or values.is_number(value):
364
+ return value
365
+ raise TypeError(f"{name} is {value!r}, not a number")
366
+
367
+
368
+ def _record(raw: dict[str, str], cm: dict[str, Any]) -> dict[str, Any]:
369
+ v4p = (cm.get("v4_prompt") or {}).get("caption") or {}
370
+ v4n = (cm.get("v4_negative_prompt") or {}).get("caption") or {}
371
+ use_coords = bool((cm.get("v4_prompt") or {}).get("use_coords", False))
372
+ source = raw.get("Source")
373
+ model_id = model_of(cm, source)
374
+ record: dict[str, Any] = {
375
+ "source": source,
376
+ "software": raw.get("Software"),
377
+ "model": model_id,
378
+ "prompt": _text(v4p.get("base_caption", cm.get("prompt", ""))),
379
+ "negative": _text(v4n.get("base_caption", cm.get("uc", ""))),
380
+ "characters": _characters_of(v4p, v4n),
381
+ "seed": _whole(cm, "seed"),
382
+ "width": _whole(cm, "width"),
383
+ "height": _whole(cm, "height"),
384
+ "steps": _whole(cm, "steps"),
385
+ "scale": _number(cm.get("scale"), "scale"),
386
+ "cfg_rescale": _number(cm.get("cfg_rescale"), "cfg_rescale"),
387
+ "sampler": _string(cm, "sampler"),
388
+ "noise_schedule": _string(cm, "noise_schedule"),
389
+ "use_coords": use_coords,
390
+ "strength": _number(cm.get("strength"), "strength"),
391
+ "noise": _number(cm.get("noise"), "noise"),
392
+ "request_type": _string(cm, "request_type"),
393
+ "vibes": len(cm.get("reference_strength_multiple") or []),
394
+ # Encodings the official import restores; gen_args reads them back with --vibes-from
395
+ "stored_vibes": _stored_vibes(cm.get("reference_image_multiple")),
396
+ "normalize_vibe_strength": _normalized_vibes(cm.get("reference_strength_multiple")),
397
+ "precise_references": len(cm.get("director_reference_strengths") or []),
398
+ "transparent": bool(cm.get("tag_hint_transparent_background")),
399
+ "premultiplied_alpha": _premultiplied(cm.get("straight_alpha")),
400
+ # Variety+ is on whenever a sigma is sent; its value follows from the size
401
+ "variety": cm.get("skip_cfg_above_sigma") is not None,
402
+ "legacy_uc": bool((cm.get("v4_negative_prompt") or {}).get("legacy_uc") or cm.get("legacy_uc")),
403
+ # What a random prompt actually sent, which --actual-prompts imports
404
+ "actual_prompts": cm.get("actual_prompts") if isinstance(cm.get("actual_prompts"), dict) else None,
405
+ "raw": cm,
406
+ }
407
+ if model_id:
408
+ record.update(_recover_user_input(cm, record, MODELS[model_id]))
409
+ return record
410
+
411
+
412
+ def _characters_of(v4p: dict[str, Any], v4n: dict[str, Any]) -> list[dict[str, Any]]:
413
+ """Characters with the negative at the same position and their first centre."""
414
+ negatives = v4n.get("char_captions") or []
415
+ chars = []
416
+ for i, c in enumerate(v4p.get("char_captions") or []):
417
+ center = (c.get("centers") or [{}])[0]
418
+ chars.append(
419
+ {
420
+ "prompt": _text(c.get("char_caption", "")),
421
+ "negative": _text(negatives[i].get("char_caption", "")) if i < len(negatives) else "",
422
+ "x": _number(center.get("x"), "a character's x"),
423
+ "y": _number(center.get("y"), "a character's y"),
424
+ }
425
+ )
426
+ return chars
427
+
428
+
429
+ def _hint(cm: dict, key: str, table: dict[int, str]) -> str | None:
430
+ value = cm.get(key)
431
+ return table.get(value) if values.is_whole(value) else None
432
+
433
+
434
+ def _strip_negative(negative: str, model: Model, hinted: str | None, strip_nsfw: bool) -> tuple[str, str]:
435
+ """Strip a leading nsfw added by the official rule, then the hinted UC preset or the longest matching one.
436
+ Returns (preset, remaining negative prompt)."""
437
+ body = P.strip_nsfw_prefix(negative) if strip_nsfw else negative
438
+ if hinted == "none":
439
+ return "none", body
440
+ by_length = sorted(
441
+ (name for name in presets.uc_presets(model) if name != "none"),
442
+ key=lambda name: len(presets.uc_preset_text(model, name)),
443
+ reverse=True,
444
+ )
445
+ for name in ([hinted] if hinted in by_length else []) + by_length:
446
+ ok, rest = P.strip_uc_preset(body, presets.uc_preset_text(model, name))
447
+ if ok:
448
+ return name, rest
449
+ return "none", body
450
+
451
+
452
+ def _recover_user_input(cm: dict, record: dict, model: Model) -> dict[str, Any]:
453
+ """Remove the automatic teXt block, quality tags, UC preset and nsfw to recover rerunnable settings.
454
+ Anything that would not rebuild exactly is kept verbatim."""
455
+ quality, uc_hint = _hints(cm, model)
456
+ typed = _user_prompt(record, model, quality)
457
+ spec = R.GenSpec(
458
+ model=model,
459
+ prompt=typed.prompt,
460
+ characters=[R.Character(c["prompt"], c["negative"], c["x"], c["y"]) for c in record["characters"]],
461
+ quality=typed.quality,
462
+ use_coords=record["use_coords"],
463
+ auto_text=typed.auto_text,
464
+ transparent=typed.transparent,
465
+ furry=typed.furry,
466
+ )
467
+ uc, user_negative, official_nsfw, matched = _user_negative(record["negative"], spec, uc_hint)
468
+ out: dict[str, Any] = {
469
+ "user_prompt": typed.prompt,
470
+ "user_negative": user_negative,
471
+ "quality": typed.quality,
472
+ "uc_preset": uc,
473
+ "transparent": typed.transparent,
474
+ "auto_text": typed.auto_text,
475
+ "furry": typed.furry,
476
+ "official_nsfw": official_nsfw,
477
+ # Neither rule rebuilds it (usually edited by another client), so send the negative prompt verbatim
478
+ "negative_verbatim": not matched,
479
+ "quality_from": "tag_hint" if quality else "detected",
480
+ "uc_preset_from": "tag_hint" if uc_hint else "detected",
481
+ }
482
+ spec = replace(spec, negative=user_negative, uc_preset=uc, official_nsfw=official_nsfw)
483
+ try:
484
+ rebuilt = R.effective_prompt(spec, R.resolve_centers(spec))
485
+ except errors.NaiError:
486
+ rebuilt = None
487
+ prompt_text = record["prompt"]
488
+ if rebuilt is not None and rebuilt != prompt_text:
489
+ # Send verbatim when it would not rebuild; a trailing transparency tag is re-added, so drop it
490
+ trailing, raw_prompt = (
491
+ P.strip_suffix(prompt_text, P.compose_suffix(None, True)) if typed.transparent else (False, prompt_text)
492
+ )
493
+ out.update(user_prompt=raw_prompt, quality="none", transparent=trailing, auto_text=False, furry=False)
494
+ return out
495
+
496
+
497
+ def _hints(cm: dict, model: Model) -> tuple[str | None, str | None]:
498
+ """The quality tier and UC preset the image names, when the model has them."""
499
+ quality = _hint(cm, "tag_hint_qt", _QUALITY_FROM_HINT)
500
+ uc = _hint(cm, "tag_hint_uc_preset", _UC_FROM_HINT)
501
+ if uc is None and isinstance(cm.get("ucPresetId"), str):
502
+ uc = _UC_FROM_ID.get(cm["ucPresetId"])
503
+ return (
504
+ quality if quality in presets.quality_tiers(model) else None,
505
+ uc if uc in presets.uc_presets(model) else None,
506
+ )
507
+
508
+
509
+ @dataclass(frozen=True)
510
+ class _Typed:
511
+ prompt: str
512
+ quality: str
513
+ transparent: bool
514
+ auto_text: bool
515
+ furry: bool
516
+
517
+
518
+ def _user_prompt(record: dict, model: Model, quality: str | None) -> _Typed:
519
+ prompt = record["prompt"]
520
+ # Like the official import, a leading fur dataset means furry mode
521
+ furry = model.furry_mode and prompt.startswith(P.FURRY_PREFIX)
522
+ if furry:
523
+ prompt = prompt.removeprefix(P.FURRY_PREFIX)
524
+ auto_text = model.auto_text
525
+ if model.auto_text:
526
+ chars = [
527
+ P.AutoTextCharacter(
528
+ c["prompt"], c["x"] if c["x"] is not None else 0.5, c["y"] if c["y"] is not None else 0.5
529
+ )
530
+ for c in record["characters"]
531
+ ]
532
+ stripped = P.strip_auto_text(prompt, chars, record["use_coords"])
533
+ if stripped == prompt and P.auto_text_block(prompt, chars, record["use_coords"]) is not None:
534
+ # Quotes without a teXt block mean automatic text was off
535
+ auto_text = False
536
+ prompt = stripped
537
+ # Transparency counts only when its tag is actually removed, as in the official import
538
+ for tier, with_transparency in _quality_candidates(model, quality, record["transparent"] and model.transparent):
539
+ tags = presets.quality_tags(model, tier) if tier != "none" else None
540
+ ok, stripped = P.strip_suffix(prompt, P.compose_suffix(tags, with_transparency))
541
+ if ok:
542
+ return _Typed(stripped, tier, with_transparency, auto_text, furry)
543
+ return _Typed(prompt, "none", False, auto_text, furry)
544
+
545
+
546
+ def _user_negative(negative: str, spec: R.GenSpec, uc_hint: str | None) -> tuple[str, str, bool, bool]:
547
+ """UC preset, typed negative, nsfw rule, and whether they rebuild the stored text. Both nsfw rules are tried,
548
+ preferring a recognized UC preset, then the default rule."""
549
+ matches = []
550
+ for official in (False, True):
551
+ uc, body = _strip_negative(negative, spec.model, uc_hint, strip_nsfw=official)
552
+ if R.effective_negative(replace(spec, negative=body, uc_preset=uc, official_nsfw=official)) == negative:
553
+ matches.append((uc == "none", official, uc, body))
554
+ if not matches:
555
+ uc, body = _strip_negative(negative, spec.model, uc_hint, strip_nsfw=False)
556
+ return uc, body, False, False
557
+ _, official, uc, body = min(matches)
558
+ return uc, body, official, True
559
+
560
+
561
+ def _quality_candidates(model: Model, hinted: str | None, transparent: bool) -> list[tuple[str, bool]]:
562
+ """(tier, transparency) in the order the official import tries them: the hinted tier first, then
563
+ every tier with transparency, transparency alone and every tier without; a hint of none tries only
564
+ transparency alone."""
565
+ tiers = [t for t in presets.quality_tiers(model) if t != "none"]
566
+ every = ([(t, True) for t in [*tiers, "none"]] if transparent else []) + [(t, False) for t in tiers]
567
+ if hinted is None:
568
+ return every
569
+ if hinted == "none":
570
+ return [("none", True)] if transparent else []
571
+ return ([(hinted, True)] if transparent else []) + [(hinted, False), *every]
572
+
573
+
574
+ @dataclass(frozen=True)
575
+ class ImageNumber:
576
+ """A number of the image's settings and where it goes; the model feature it needs, if any."""
577
+
578
+ key: str
579
+ attr: str
580
+ low: float
581
+ high: float
582
+ whole: bool = False
583
+ feature: str | None = None
584
+ feature_name: str = ""
585
+
586
+ def read(self, record: dict[str, Any]) -> int | float | None:
587
+ value = record.get(self.key)
588
+ if not values.is_number(value) or not self.low <= value <= self.high:
589
+ return None
590
+ if self.whole:
591
+ return value if values.is_whole(value) else None
592
+ return float(value)
593
+
594
+
595
+ SEED = ImageNumber("seed", "seed", 0, MAX_SEED, whole=True)
596
+ NUMERIC_SETTINGS = (
597
+ ImageNumber("steps", "steps", 1, MAX_STEPS, whole=True),
598
+ ImageNumber("scale", "cfg", 0, math.inf),
599
+ ImageNumber(
600
+ "cfg_rescale",
601
+ "cfg_rescale",
602
+ *CFG_RESCALE_RANGE,
603
+ feature="cfg_rescale",
604
+ feature_name="prompt guidance rescale",
605
+ ),
606
+ )