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
@@ -0,0 +1,248 @@
1
+ """upscale and the Director tools: priced by the source's size, paid under the lock, saved like generations."""
2
+
3
+ from collections.abc import Callable
4
+ from dataclasses import dataclass, replace
5
+ from pathlib import Path
6
+ from typing import Any
7
+
8
+ import numpy as np
9
+ from PIL import Image
10
+
11
+ from nai_cli import api, cost, decode, errors, images, meta, naming, pixel_snap, presets, values
12
+ from nai_cli.charge import authorize, keep_usable, price_json
13
+ from nai_cli.commands import output, texts
14
+ from nai_cli.commands.context import Context
15
+
16
+
17
+ @dataclass(frozen=True)
18
+ class SnapOptions:
19
+ # "auto", 0 for none, or a palette size
20
+ colors: int | str
21
+ max_detail: float | None
22
+ # Scale the cells back up to about the source's size
23
+ upscale: bool
24
+
25
+
26
+ @dataclass(frozen=True)
27
+ class ImageJob:
28
+ command: str
29
+ image: Path
30
+ output: output.Output
31
+ dry_run: bool
32
+ yes: bool
33
+ # The {type} a file name template gives the results
34
+ kind: str
35
+ # Names of the images of the reply, for the default file names
36
+ outputs: tuple[str, ...] = ()
37
+ tool: str | None = None
38
+ prompt: str = ""
39
+ defry: int = 0
40
+ snap: SnapOptions | None = None
41
+
42
+
43
+ def resolve_upscale(opts: dict[str, Any], ctx: Context) -> ImageJob:
44
+ return ImageJob(
45
+ "upscale",
46
+ Path(opts["image"]),
47
+ output.resolve(opts.get("output"), ctx.config),
48
+ bool(opts.get("dry_run")) or ctx.dry_run,
49
+ bool(opts.get("yes")),
50
+ "upscale",
51
+ )
52
+
53
+
54
+ def resolve_tool(opts: dict[str, Any], ctx: Context) -> ImageJob:
55
+ tool = opts["tool"]
56
+ emotion = opts.get("emotion") or presets.EMOTIONS[0]
57
+ prompt = texts.read_typed(opts, ("prompt",), ctx)["prompt"] or ""
58
+ return ImageJob(
59
+ f"tool-{tool}",
60
+ Path(opts["image"]),
61
+ output.resolve(opts.get("output"), ctx.config),
62
+ bool(opts.get("dry_run")) or ctx.dry_run,
63
+ bool(opts.get("yes")),
64
+ # The official client names emotion results by the emotion too
65
+ f"emotion-{emotion}" if tool == "emotion" else tool,
66
+ presets.TOOL_OUTPUTS.get(tool, ()),
67
+ tool,
68
+ presets.emotion_prompt(emotion, prompt) if tool == "emotion" else prompt,
69
+ opts.get("defry") or 0,
70
+ _snap_options(opts) if tool == presets.PIXEL_SNAP else None,
71
+ )
72
+
73
+
74
+ def _snap_options(opts: dict[str, Any]) -> SnapOptions:
75
+ text = str(opts.get("colors") or "auto").strip().lower()
76
+ if text in ("auto", "off"):
77
+ colors: int | str = 0 if text == "off" else "auto"
78
+ elif values.is_digits(text) and presets.SNAP_COLORS[0] <= int(text) <= presets.SNAP_COLORS[1]:
79
+ colors = int(text)
80
+ else:
81
+ low, high = presets.SNAP_COLORS
82
+ raise errors.NaiError(errors.BAD_ARGS, f"--colors is auto, off or {low} to {high}: {text}")
83
+ max_detail = None if opts.get("avoid_over_refining") else pixel_snap.MAX_DETAIL
84
+ return SnapOptions(colors, max_detail, bool(opts.get("upscale")))
85
+
86
+
87
+ def run_upscale(s: ImageJob, ctx: Context) -> dict[str, Any]:
88
+ # Upscale never resizes the source; larger images are refused
89
+ result, out = _run(s, ctx, lambda w, h, opus: cost.upscale(w, h), _as_is, lambda c, data, w, h: c.upscale(data))
90
+ if out:
91
+ ow, oh = decode.size_of_bytes(out[0])
92
+ result["size"] = f"{ow}x{oh}"
93
+ return result
94
+
95
+
96
+ def run_tool(s: ImageJob, ctx: Context) -> dict[str, Any]:
97
+ if s.snap is not None:
98
+ return _run_snap(s, s.snap, ctx)
99
+ tool = s.tool or ""
100
+ result, _ = _run(
101
+ s,
102
+ ctx,
103
+ lambda w, h, opus: cost.augment(w, h, bg_removal=tool == "bg-removal", opus=opus),
104
+ images.tool_image,
105
+ lambda c, data, w, h: c.augment(data, tool, w, h, s.prompt, s.defry, expected=max(len(s.outputs), 1)),
106
+ )
107
+ return result
108
+
109
+
110
+ def _run_snap(s: ImageJob, o: SnapOptions, ctx: Context) -> dict[str, Any]:
111
+ """Runs here, as the official client runs it in the browser, so it sends nothing and costs nothing."""
112
+ # One picture is snapped; an animation would lose its other frames
113
+ img = images.open_still(s.image)
114
+ size = {"size": f"{img.width}x{img.height}"}
115
+ # Snapped in a dry run too, since an image it cannot snap fails there
116
+ snapped = pixel_snap.snap(np.asarray(img.convert("RGBA")), o.colors, o.max_detail)
117
+ if s.dry_run:
118
+ output.check(s.output)
119
+ return {"ok": True, "dry_run": True, **size, "cost": {"estimate": 0}, "cells": _cells(snapped)}
120
+ rows, cols = snapped.rgb.shape[:2]
121
+ alpha = snapped.alpha if snapped.alpha is not None else np.full((rows, cols), 255, np.uint8)
122
+ result = Image.fromarray(np.dstack([snapped.rgb, alpha]), "RGBA")
123
+ if o.upscale:
124
+ # Half up, as the browser rounds the factor
125
+ factor = max(1, int((img.width / cols + img.height / rows) / 2 + 0.5))
126
+ result = result.resize((cols * factor, rows * factor), Image.Resampling.NEAREST)
127
+ output.ready(s.output)
128
+ warnings = [f"the image was {snapped.note}"] if snapped.note else []
129
+ paths = _save(s, ctx, output.keep_free, [images.to_png(result)], _source_naming(s), warnings)
130
+ out = {
131
+ "ok": True,
132
+ "images": paths,
133
+ **size,
134
+ "cost": {"estimate": 0, "spent": 0},
135
+ "cells": _cells(snapped),
136
+ "pitch": [round(p, 3) for p in snapped.pitch],
137
+ "warnings": warnings,
138
+ }
139
+ if snapped.palette_size is not None:
140
+ out["colors"] = snapped.palette_size
141
+ return out
142
+
143
+
144
+ def _cells(snapped: pixel_snap.Snapped) -> str:
145
+ rows, cols = snapped.rgb.shape[:2]
146
+ return f"{cols}x{rows}"
147
+
148
+
149
+ def _as_is(img: Image.Image) -> tuple[bytes, int, int]:
150
+ return images.to_png(img), img.width, img.height
151
+
152
+
153
+ def _run(
154
+ s: ImageJob,
155
+ ctx: Context,
156
+ price_of: Callable[[int, int, bool], int],
157
+ prepare: Callable[[Image.Image], tuple[bytes, int, int]],
158
+ send: Callable[[api.Client, bytes, int, int], list[bytes]],
159
+ ) -> tuple[dict[str, Any], list[bytes]]:
160
+ """Price by source size, confirm under the lock, save, then measure the spend. prepare turns the source into
161
+ the image and size to send."""
162
+ img = images.open_image(s.image)
163
+ w, h = img.size
164
+ if price_of(w, h, True) < 0:
165
+ raise errors.NaiError(errors.BAD_ARGS, f"{w}x{h} exceeds the maximum area of this endpoint")
166
+ data, send_w, send_h = prepare(img)
167
+ if s.dry_run:
168
+ output.check(s.output)
169
+ warnings: list[str] = []
170
+ account = ctx.dry_account(warnings)
171
+ price = price_of(w, h, account.opus if account else True)
172
+ dry = {"ok": True, "dry_run": True, "size": f"{w}x{h}", "sent_size": f"{send_w}x{send_h}"}
173
+ if warnings:
174
+ dry["warnings"] = warnings
175
+ dry["cost"] = price_json(price, account)
176
+ refused = ctx.refusal(price, account, s.yes, dry["cost"])
177
+ return ({**dry, "refused": refused} if refused else dry), []
178
+ # Fails without a token before any folder is made
179
+ ctx.client()
180
+ output.ready(s.output)
181
+ warnings: list[str] = []
182
+ named = _source_naming(s)
183
+ with ctx.charged(warnings, command=s.command, source=str(s.image)) as charge:
184
+ price = price_of(w, h, charge.before.opus)
185
+ authorize(price, max(len(s.outputs), 1), charge, ctx.config, s.yes, price_json(price, charge.before))
186
+
187
+ def save(pictures: list[bytes | None]) -> list[str]:
188
+ return _save(s, ctx, charge.keep, pictures, named, warnings)
189
+
190
+ try:
191
+ out = charge.pay(send, charge.client, data, send_w, send_h)
192
+ except api.DamagedImages as e:
193
+ keep_usable(e, save, warnings)
194
+ paths = save(list(out))
195
+ result: dict[str, Any] = {
196
+ "ok": True,
197
+ "images": paths,
198
+ "cost": price_json(price, None, charge),
199
+ "warnings": warnings,
200
+ }
201
+ if charge.client.retries:
202
+ result["retries"] = charge.client.retries
203
+ return result, out
204
+
205
+
206
+ def _source_naming(s: ImageJob) -> naming.Meta | None:
207
+ """A tool's result is named and filed by the settings of the image it worked on, as the official client
208
+ names it."""
209
+ try:
210
+ record = meta.parse(s.image)
211
+ except errors.NaiError:
212
+ # Without generation settings the default name is kept, as the official client keeps the source's name
213
+ return None
214
+ return output.recorded(record, s.kind)
215
+
216
+
217
+ def _save(
218
+ s: ImageJob,
219
+ ctx: Context,
220
+ keep: output.Keep,
221
+ pictures: list[bytes | None],
222
+ named: naming.Meta | None,
223
+ warnings: list[str],
224
+ ) -> list[str]:
225
+ """Named by the template when it has {type}, as the official client names these results, else the source's
226
+ name and the tool's; numbered when taken. Either way a result of several keeps its label, as the official
227
+ client adds it."""
228
+ paths = []
229
+ seed = named.seed if named is not None and named.seed is not None else 0
230
+ template = s.output.template
231
+ templated = template is not None and naming.names_type(template)
232
+ for i, data in enumerate(pictures):
233
+ if data is None:
234
+ continue
235
+ image = output.named(s.output, named, data, ctx.now(), templated=templated, index=i)
236
+ label = s.outputs[i] if i < len(s.outputs) else (str(i) if len(pictures) > 1 else "")
237
+ tail = f"_{label}" if label else ""
238
+ if image.name is None and s.output.destination.exact is None:
239
+ # A long source name is cut before the tool's name, which tells the results apart
240
+ image = replace(image, name=s.image.stem)
241
+ tail = f"_{_named_by(s)}{tail}"
242
+ paths.append(output.save_image(keep, data, s.output, image, warnings, seed=seed, suffix=tail))
243
+ return paths
244
+
245
+
246
+ def _named_by(s: ImageJob) -> str:
247
+ # The official client names an emotion result by the tool, then the emotion
248
+ return f"emotion_{s.kind.removeprefix('emotion-')}" if s.tool == "emotion" else s.kind
@@ -0,0 +1,77 @@
1
+ """interrogate: tags read from an image by an ONNX tagger on this computer, joined into a prompt."""
2
+
3
+ from dataclasses import dataclass
4
+ from pathlib import Path
5
+ from typing import Any
6
+
7
+ from nai_cli import errors, fs, images, local_models, tagger
8
+ from nai_cli.commands.context import Context
9
+
10
+
11
+ @dataclass(frozen=True)
12
+ class InterrogateSettings:
13
+ image: Path
14
+ tagger: tagger.Settings
15
+
16
+
17
+ def resolve(opts: dict[str, Any], ctx: Context) -> InterrogateSettings:
18
+ local = ctx.config.local
19
+ model = opts["model"] if opts.get("model") else local.tagger
20
+ if model is None:
21
+ raise errors.NaiError(
22
+ errors.CONFIG_MISSING, "no tagger model: give --model or set tagger under [local] in the config"
23
+ )
24
+ given = opts.get("labels") or (local.tagger_labels if not opts.get("model") else None)
25
+ labels = given if given else _labels_beside(model)
26
+ kind_text = opts.get("kind") or (local.tagger_kind if not opts.get("model") else "")
27
+ try:
28
+ kind = tagger.Kind(kind_text) if kind_text else tagger.guess_kind(model, labels)
29
+ except ValueError:
30
+ choices = ", ".join(k.value for k in tagger.Kind)
31
+ raise errors.NaiError(errors.BAD_ARGS, f"the tagger kind is one of {choices}: {kind_text}") from None
32
+ thresholds = {}
33
+ for name in ("threshold", "character_threshold"):
34
+ value = opts.get(name)
35
+ if value is not None and not 0 <= value <= 1:
36
+ raise errors.NaiError(errors.BAD_ARGS, f"--{name.replace('_', '-')} is within 0-1: {value}")
37
+ thresholds[name] = tagger.DEFAULT_THRESHOLD if value is None else value
38
+ return InterrogateSettings(
39
+ Path(opts["image"]),
40
+ tagger.Settings(model, labels, kind, categories=_categories(opts.get("categories")), **thresholds),
41
+ )
42
+
43
+
44
+ def _labels_beside(model: Path) -> Path:
45
+ """The first of the tag list's usual places that holds a file, else a tagger's config.json."""
46
+ found = next((p for p in tagger.label_candidates(model) if fs.is_file(p)), None)
47
+ config_file = model.parent / "config.json"
48
+ if found is None and tagger.is_split_config(config_file):
49
+ found = config_file
50
+ if found is None:
51
+ raise errors.NaiError(errors.CONFIG_MISSING, f"no tag list found beside {model}; give it with --labels")
52
+ return found
53
+
54
+
55
+ def _categories(text: str | None) -> tuple[tagger.Category, ...]:
56
+ if not text:
57
+ return tagger.DEFAULT_CATEGORIES
58
+ names = {c.value: c for c in tagger.CHOOSABLE}
59
+ chosen = [part.strip().lower() for part in text.split(",") if part.strip()]
60
+ unknown = [part for part in chosen if part not in names]
61
+ if unknown or not chosen:
62
+ raise errors.NaiError(errors.BAD_ARGS, f"--categories takes a comma list of {', '.join(names)}: {text}")
63
+ return tuple(dict.fromkeys(names[part] for part in chosen))
64
+
65
+
66
+ def run(s: InterrogateSettings, ctx: Context) -> dict[str, Any]:
67
+ image = images.open_still(s.image)
68
+ labels = tagger.load_labels(s.tagger.labels)
69
+ tags = tagger.interrogate(image, s.tagger, labels, local_models.load(s.tagger.model))
70
+ return {
71
+ "ok": True,
72
+ "image": str(s.image),
73
+ "prompt": ", ".join(t.text for t in tags),
74
+ "tags": [{"tag": t.text, "score": round(t.score, 4), "category": t.category.value} for t in tags],
75
+ "model": str(s.tagger.model),
76
+ "kind": s.tagger.kind.value,
77
+ }
@@ -0,0 +1,24 @@
1
+ """lint: check prompts as gen would send them, without sending anything."""
2
+
3
+ from typing import Any
4
+
5
+ from nai_cli.commands import generation
6
+ from nai_cli.commands.context import Context
7
+ from nai_cli.commands.generation import GenSettings
8
+ from nai_cli.commands.spec import prepare
9
+
10
+
11
+ def resolve(opts: dict[str, Any], ctx: Context) -> GenSettings:
12
+ return generation.resolve(opts, ctx, "lint")
13
+
14
+
15
+ def run(s: GenSettings, ctx: Context) -> dict[str, Any]:
16
+ prep = prepare(s)
17
+ return {
18
+ "ok": True,
19
+ "issues": [i.to_json() for i in prep.issues],
20
+ "tokens": prep.tokens.to_json(),
21
+ "prompt": prep.built.prompt,
22
+ "negative": prep.built.negative,
23
+ "warnings": prep.warnings,
24
+ }
@@ -0,0 +1,62 @@
1
+ """local: what the optional models run on this computer need, and downloading the SAM model when asked."""
2
+
3
+ import hashlib
4
+ from dataclasses import dataclass
5
+ from typing import Any
6
+
7
+ from nai_cli import api, errors, fs, local_models
8
+ from nai_cli.commands.context import Context
9
+
10
+
11
+ @dataclass(frozen=True)
12
+ class LocalSettings:
13
+ action: str
14
+ dry_run: bool
15
+
16
+
17
+ def resolve(opts: dict[str, Any], ctx: Context) -> LocalSettings:
18
+ action = opts.get("local_command") or "status"
19
+ if action == "download" and opts.get("model") != "sam":
20
+ raise errors.NaiError(errors.BAD_ARGS, "download takes the model to fetch: nai local download sam")
21
+ if action == "status" and opts.get("model"):
22
+ raise errors.NaiError(errors.BAD_ARGS, f"status takes no model: {opts['model']}")
23
+ return LocalSettings(action, bool(opts.get("dry_run")) or ctx.dry_run)
24
+
25
+
26
+ def run(s: LocalSettings, ctx: Context) -> dict[str, Any]:
27
+ return _download_sam(s, ctx) if s.action == "download" else _status(ctx)
28
+
29
+
30
+ def _status(ctx: Context) -> dict[str, Any]:
31
+ local = ctx.config.local
32
+ out: dict[str, Any] = {
33
+ "ok": True,
34
+ "onnxruntime": local_models.available(),
35
+ "tagger": str(local.tagger) if local.tagger else None,
36
+ "sam": {
37
+ "dir": str(local.sam_dir),
38
+ "missing": [a.name for a in local_models.missing(local.sam_dir, local_models.SAM)],
39
+ },
40
+ }
41
+ if not local_models.available():
42
+ out["install"] = local_models.INSTALL_HINT
43
+ return out
44
+
45
+
46
+ def _download_sam(s: LocalSettings, ctx: Context) -> dict[str, Any]:
47
+ """Files already there are kept only when their hash matches; each download is checked before it replaces
48
+ anything."""
49
+ folder = ctx.config.local.sam_dir
50
+ wanted = local_models.unverified(folder, local_models.SAM)
51
+ plan = {"dir": str(folder), "files": [a.name for a in wanted], "bytes": sum(a.size for a in wanted)}
52
+ if s.dry_run or not wanted:
53
+ return {"ok": True, "dry_run": s.dry_run, **plan}
54
+ for asset in wanted:
55
+ data = api.fetch_file(asset.url, asset.size)
56
+ try:
57
+ if hashlib.sha256(data).hexdigest() != asset.sha256:
58
+ raise errors.NaiError(errors.NETWORK, f"{asset.name} does not match its published hash")
59
+ fs.replace(folder / asset.name, data)
60
+ except OSError as e:
61
+ raise errors.NaiError(errors.SAVE_FAILED, f"cannot save {asset.name}: {e}") from e
62
+ return {"ok": True, **plan}
@@ -0,0 +1,102 @@
1
+ """meta: the generation settings an image keeps, with the nai gen arguments that rerun it."""
2
+
3
+ from dataclasses import dataclass
4
+ from pathlib import Path
5
+ from typing import Any
6
+
7
+ from nai_cli import errors, fs, ledger, meta, rerun
8
+ from nai_cli.commands.context import LINES_IN_JOB, Context
9
+
10
+ # What a folder is searched for: the formats the official client writes settings into
11
+ IMAGE_SUFFIXES = (".png", ".webp")
12
+
13
+
14
+ @dataclass(frozen=True)
15
+ class MetaSettings:
16
+ images: tuple[Path, ...]
17
+ raw: bool
18
+ # One JSON line per image and a summary, even for a single image
19
+ lines: bool
20
+ hidden_options: bool
21
+
22
+
23
+ def resolve(opts: dict[str, Any], ctx: Context) -> MetaSettings:
24
+ images: list[Path] = []
25
+ folders = False
26
+ for given in map(Path, opts["image"]):
27
+ if fs.is_folder(given):
28
+ folders = True
29
+ pattern = "**/*" if opts.get("recursive") else "*"
30
+ images.extend(
31
+ sorted(p for p in given.glob(pattern) if p.suffix.lower() in IMAGE_SUFFIXES and fs.is_file(p))
32
+ )
33
+ else:
34
+ images.append(given)
35
+ lines = bool(opts.get("jsonl")) or folders or len(images) > 1
36
+ return MetaSettings(tuple(images), bool(opts.get("raw")), lines, bool(ctx.config.defaults.get("v5_hidden_options")))
37
+
38
+
39
+ def run(s: MetaSettings, ctx: Context) -> dict[str, Any]:
40
+ if s.lines and ctx.in_job:
41
+ raise errors.NaiError(errors.BAD_ARGS, LINES_IN_JOB)
42
+ warnings = []
43
+ try:
44
+ files = ledger.by_file()
45
+ except OSError as e:
46
+ files = {}
47
+ warnings.append(f"cannot read the ledger: {e}")
48
+ if not s.lines:
49
+ (image,) = s.images
50
+ return {"ok": True, **_intact(_read(image, s, files, warnings))}
51
+ counts = {"images": len(s.images), "without_settings": 0, "damaged": 0, "failed": 0}
52
+ for image in s.images:
53
+ try:
54
+ line = {"ok": True, "type": "image", **_intact(_read(image, s, files, warnings))}
55
+ except errors.NaiError as e:
56
+ # An image without settings is an answer about it, not a failure to read it
57
+ if e.code == errors.NO_METADATA:
58
+ counts["without_settings"] += 1
59
+ else:
60
+ counts["damaged" if e.detail.get("image_ok") is False else "failed"] += 1
61
+ line = {**e.to_json(), "type": "image", "image": str(image)}
62
+ ctx.emit(line)
63
+ if counts["failed"] or counts["damaged"]:
64
+ unread = [f"{counts['failed']} could not be read"] if counts["failed"] else []
65
+ damaged = [f"{counts['damaged']} are damaged"] if counts["damaged"] else []
66
+ raise errors.NaiError(
67
+ errors.BAD_ARGS,
68
+ f"of {len(s.images)} images, " + " and ".join(unread + damaged),
69
+ type="summary",
70
+ meta=counts,
71
+ )
72
+ return {"ok": True, "type": "summary", "meta": counts}
73
+
74
+
75
+ def _intact(out: dict[str, Any]) -> dict[str, Any]:
76
+ """The settings of a damaged image are still given, but the image does not pass as a good one."""
77
+ if out["image_ok"] is not False:
78
+ return out
79
+ raise errors.NaiError(
80
+ errors.BAD_ARGS,
81
+ f"{Path(out['image']).name} is damaged; its settings were read from the text kept before the damage",
82
+ **out,
83
+ )
84
+
85
+
86
+ def _damage(intact: bool | None) -> list[str]:
87
+ if intact is None:
88
+ return ["the image is past the pixel limit, too large to check that every pixel decodes"]
89
+ return [] if intact else ["the image is damaged: not every pixel decodes"]
90
+
91
+
92
+ def _read(image: Path, s: MetaSettings, files: dict[Path, dict[str, Any]], warnings: list[str]) -> dict[str, Any]:
93
+ record, intact = meta.parse_checked(image)
94
+ if not s.raw:
95
+ record.pop("raw", None)
96
+ out = {"image": str(image), "image_ok": intact, **record}
97
+ out["gen_args"] = rerun.to_gen_args(record, image, hidden_options=s.hidden_options)
98
+ out["warnings"] = [*warnings, *_damage(intact), *rerun.rerun_gaps(record)]
99
+ entry = files.get(image.resolve())
100
+ if entry:
101
+ out["ledger"] = entry
102
+ return out
@@ -0,0 +1,62 @@
1
+ """models: the models and what each supports."""
2
+
3
+ from dataclasses import dataclass
4
+ from typing import Any
5
+
6
+ from nai_cli import models, presets
7
+ from nai_cli.commands.context import Context
8
+
9
+
10
+ @dataclass(frozen=True)
11
+ class ModelsSettings:
12
+ pass
13
+
14
+
15
+ def resolve(opts: dict[str, Any], ctx: Context) -> ModelsSettings:
16
+ return ModelsSettings()
17
+
18
+
19
+ def run(s: ModelsSettings, ctx: Context) -> dict[str, Any]:
20
+ aliases: dict[str, list[str]] = {}
21
+ for alias, target in models.ALIASES.items():
22
+ aliases.setdefault(target, []).append(alias)
23
+ d = ctx.config.defaults
24
+ default = models.with_effort(models.resolve(d.get("model")), None, d.get("effort"))
25
+ offered = (models.with_hidden_options(m, bool(d.get("v5_hidden_options"))) for m in models.MODELS.values())
26
+ return {
27
+ "ok": True,
28
+ "default": default.id,
29
+ "models": [_model_json(m, aliases.get(m.id, [])) for m in offered],
30
+ }
31
+
32
+
33
+ def _model_json(model: models.Model, aliases: list[str]) -> dict[str, Any]:
34
+ fixed = model.fixed
35
+ return {
36
+ "id": model.id,
37
+ "name": model.name,
38
+ "aliases": aliases,
39
+ "family": model.family,
40
+ "inpainting_model": model.inpainting_id,
41
+ "token_limit": model.token_limit,
42
+ "tokenizer": model.tokenizer,
43
+ "text_limit": model.text_limit,
44
+ "max_characters": model.max_characters,
45
+ "vibe": model.vibe,
46
+ "precise_reference": model.precise_reference,
47
+ "transparent": model.transparent,
48
+ "furry": model.furry_mode,
49
+ "auto_text": model.auto_text,
50
+ "variety": model.variety_plus,
51
+ "noise_schedule": model.noise_schedule,
52
+ "cfg_rescale": model.cfg_rescale,
53
+ "legacy_uc": model.legacy_uc,
54
+ "effort": models.effort_of(model.id),
55
+ "quality": presets.quality_tiers(model),
56
+ "default_quality": model.default_quality,
57
+ "uc_presets": presets.uc_presets(model),
58
+ "default_uc_preset": model.default_uc_preset,
59
+ "default_steps": model.default_steps,
60
+ "default_cfg": model.default_scale,
61
+ "fixed": {"steps": fixed.steps, "sampler": fixed.sampler, "uc_preset": fixed.uc_preset} if fixed else None,
62
+ }