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,165 @@
1
+ """Where a command saves images and what it names them; checked before anything is paid for."""
2
+
3
+ import uuid
4
+ from collections.abc import Callable, Iterator
5
+ from contextlib import contextmanager
6
+ from dataclasses import dataclass, replace
7
+ from datetime import datetime
8
+ from pathlib import Path
9
+ from typing import Any
10
+
11
+ from nai_cli import config, decode, errors, fs, naming, values
12
+
13
+ IMAGE_SUFFIXES = (".jpg", ".jpeg", ".webp", ".gif", ".bmp", ".tif", ".tiff", ".avif")
14
+
15
+
16
+ @dataclass(frozen=True)
17
+ class Output:
18
+ destination: naming.Destination
19
+ # The config's file name template; a file name given with -o replaces it
20
+ template: naming.Template | None
21
+
22
+
23
+ def resolve(target: str | None, cfg: config.Config) -> Output:
24
+ """-o for this run: a .png name, or a folder without the config's folder rule."""
25
+ template = _template(cfg, "filename", naming.parse)
26
+ if not target:
27
+ return Output(filed(cfg), template)
28
+ path = values.path(target)
29
+ suffix = path.suffix.lower()
30
+ if suffix == ".png":
31
+ return Output(naming.Destination(path.parent, exact=path), None)
32
+ if suffix in IMAGE_SUFFIXES and not fs.is_folder(path):
33
+ raise errors.NaiError(errors.BAD_ARGS, f"images are saved as PNG; give -o a .png name or a folder: {path}")
34
+ return Output(naming.Destination(path), template)
35
+
36
+
37
+ def filed(cfg: config.Config) -> naming.Destination:
38
+ """The output folder with the config's folder rule."""
39
+ return naming.Destination(cfg.output_dir, _template(cfg, "folder", naming.parse_folders))
40
+
41
+
42
+ def recorded(record: dict[str, Any], kind: str) -> naming.Meta:
43
+ """What a template names an image by, from the settings read back from it."""
44
+ seed, steps, scale = record.get("seed"), record.get("steps"), record.get("scale")
45
+ return naming.Meta(
46
+ prompt=str(record.get("user_prompt", record.get("prompt")) or ""),
47
+ seed=seed if values.is_whole(seed) else None,
48
+ model=record.get("model"),
49
+ steps=steps if values.is_whole(steps) else None,
50
+ cfg=scale if values.is_number(scale) else None,
51
+ sampler=str(record.get("sampler") or ""),
52
+ type=kind,
53
+ characters=tuple(naming.Character(str(c.get("prompt") or "")) for c in record.get("characters") or []),
54
+ )
55
+
56
+
57
+ def _template(cfg: config.Config, key: str, parse: Callable[[str], naming.Template]) -> naming.Template | None:
58
+ text = getattr(cfg, key)
59
+ if not text:
60
+ return None
61
+ # The official client falls back to its default name instead; refusing here comes before any spend
62
+ try:
63
+ return parse(text)
64
+ except ValueError as e:
65
+ raise errors.NaiError(errors.CONFIG_MISSING, f"{cfg.path}: [output] {key}: {e}") from None
66
+
67
+
68
+ def ready(output: Output) -> None:
69
+ """Fail on an unusable output folder before anything is charged; the folders under it are made on saving. A
70
+ name the system refuses falls back to the default name, so -o needs no trying here."""
71
+ ready_folder(output.destination.checked_folder)
72
+
73
+
74
+ def ready_folder(folder: Path, name: Path | None = None) -> None:
75
+ """name is a file given with -o, tried as well, as a file other than an image has no fallback name."""
76
+ # A file is written and removed, since Windows takes NUL and names ending in a dot for folders that exist
77
+ with _refused(folder, name):
78
+ fs.make_folder(folder)
79
+ fs.try_names(folder, name)
80
+
81
+
82
+ def check(output: Output) -> None:
83
+ check_folder(output.destination.checked_folder)
84
+
85
+
86
+ def check_folder(folder: Path, name: Path | None = None) -> None:
87
+ """ready_folder for a dry run, which leaves nothing behind."""
88
+ with _refused(folder, name):
89
+ fs.try_names(folder, name)
90
+
91
+
92
+ @contextmanager
93
+ def _refused(folder: Path, name: Path | None) -> Iterator[None]:
94
+ try:
95
+ yield
96
+ except (OSError, ValueError) as e:
97
+ where = f"as {name}" if name else f"in the output folder {folder}"
98
+ raise errors.NaiError(errors.BAD_ARGS, f"cannot save {where}: {e}") from e
99
+
100
+
101
+ def save_unpaid(data: bytes | str, path: Path) -> str:
102
+ """Write a result nothing was paid for, under path or the next free number."""
103
+ try:
104
+ return str(fs.create_numbered(path, data))
105
+ except (OSError, ValueError) as e:
106
+ raise errors.NaiError(errors.BAD_ARGS, f"cannot write {path}: {e}") from e
107
+
108
+
109
+ @dataclass(frozen=True)
110
+ class Named:
111
+ """One image's values for the folder rule and the template, and the name the template gives, if any."""
112
+
113
+ meta: naming.Meta
114
+ name: str | None
115
+
116
+
117
+ def named(
118
+ output: Output, base: naming.Meta | None, data: bytes, when: datetime, templated: bool = True, **fields: Any
119
+ ) -> Named:
120
+ """base is None when the source of a tool's result has no settings: the default name, in the folder of
121
+ the time. templated is False where the template does not name such results."""
122
+ try:
123
+ width, height = decode.size_of_bytes(data)
124
+ except decode.BAD_IMAGE:
125
+ width = height = None
126
+ meta = replace(base or naming.Meta(), id=str(uuid.uuid4()), date=when, width=width, height=height, **fields)
127
+ if output.template is None or base is None or not templated:
128
+ return Named(meta, None)
129
+ return Named(meta, naming.render(output.template, meta) or naming.fallback(meta))
130
+
131
+
132
+ # Saves bytes under a path or the next free number there, returning where
133
+ Keep = Callable[[bytes, Path], str]
134
+
135
+
136
+ def keep_free(data: bytes, path: Path) -> str:
137
+ """Keep for results nothing was paid for."""
138
+ try:
139
+ return str(fs.create_numbered(path, data))
140
+ except (OSError, ValueError) as e:
141
+ raise errors.NaiError(errors.SAVE_FAILED, f"cannot save the result: {e}") from e
142
+
143
+
144
+ def save_image(
145
+ keep: Keep,
146
+ data: bytes,
147
+ output: Output,
148
+ image: Named,
149
+ warnings: list[str],
150
+ seed: int = 0,
151
+ index: int = 0,
152
+ suffix: str = "",
153
+ ) -> str:
154
+ destination = output.destination
155
+ chosen = destination.image_path(image.meta, seed, index, suffix, image.name)
156
+ try:
157
+ return keep(data, chosen)
158
+ except errors.NaiError as e:
159
+ fallback = destination.fallback_path(image.meta, seed, index, suffix)
160
+ if e.code != errors.SAVE_FAILED or fallback == chosen:
161
+ raise
162
+ # A name or folder the file system refuses must not lose an image already made
163
+ reason = e.__cause__ or e.message
164
+ warnings.append(f"cannot save as {chosen.name}: {reason}; the default name in {fallback.parent} is used")
165
+ return keep(data, fallback)
@@ -0,0 +1,48 @@
1
+ """redact: copies of images with regions pixelated, blurred or filled, for sharing."""
2
+
3
+ from dataclasses import dataclass
4
+ from pathlib import Path
5
+ from typing import Any
6
+
7
+ from nai_cli import errors, images, inpaint, share
8
+ from nai_cli.commands import copies, spec
9
+ from nai_cli.commands.context import Context
10
+
11
+
12
+ @dataclass(frozen=True)
13
+ class RedactSettings:
14
+ targets: copies.Targets
15
+ redaction: share.Redaction
16
+ keep_metadata: bool
17
+ segment: inpaint.Segment
18
+
19
+
20
+ def resolve(opts: dict[str, Any], ctx: Context) -> RedactSettings:
21
+ amount = opts.get("amount")
22
+ if amount is not None and not 0 < amount <= 1:
23
+ raise errors.NaiError(errors.BAD_ARGS, "--amount is a fraction of the short edge, above 0 and at most 1")
24
+ opacity = opts.get("opacity")
25
+ if opacity is not None and not 0 < opacity <= 1:
26
+ raise errors.NaiError(errors.BAD_ARGS, "--opacity is above 0 and at most 1")
27
+ redaction = share.Redaction(
28
+ regions=tuple(inpaint.parse_region(r) for r in opts["region"]),
29
+ effect=opts.get("effect") or "pixelate",
30
+ amount=amount,
31
+ color=copies.color(opts.get("color"), (0, 0, 0)),
32
+ opacity=1.0 if opacity is None else opacity,
33
+ invert=bool(opts.get("invert")),
34
+ )
35
+ return RedactSettings(
36
+ copies.resolve(opts, ctx), redaction, bool(opts.get("keep_metadata")), spec.segmenter(ctx.config.local.sam_dir)
37
+ )
38
+
39
+
40
+ def run(s: RedactSettings, ctx: Context) -> dict[str, Any]:
41
+ warnings: list[str] = []
42
+
43
+ def make(source: Path) -> bytes:
44
+ return copies.finish(
45
+ source, share.redact(images.open_still(source), s.redaction, s.segment), s.keep_metadata, warnings
46
+ )
47
+
48
+ return copies.write(s.targets, "redacted", make, warnings)
@@ -0,0 +1,87 @@
1
+ """FILE[:A[:B]] values of --vibe, --ref and vibe sources, and the files they name."""
2
+
3
+ import math
4
+ from dataclasses import dataclass
5
+ from pathlib import Path
6
+
7
+ from nai_cli import errors, images, values, vibe
8
+ from nai_cli import request as R
9
+
10
+ PRECISE_KINDS = {
11
+ "character": "character",
12
+ "style": "style",
13
+ "both": "character&style",
14
+ "character&style": "character&style",
15
+ }
16
+
17
+
18
+ @dataclass(frozen=True)
19
+ class VibeRef:
20
+ path: Path
21
+ strength: float | None
22
+ information_extracted: float | None
23
+
24
+
25
+ @dataclass(frozen=True)
26
+ class PreciseRef:
27
+ path: Path
28
+ kind: str
29
+ strength: float
30
+ fidelity: float
31
+
32
+
33
+ def vibe_ref(value: str, strength: float | None = None, info: float | None = None) -> VibeRef:
34
+ """Fields left empty take strength and info, then what the file stores."""
35
+ path, (s, i) = _fields(value, ["STRENGTH", "INFO"])
36
+ return VibeRef(path, _number(s, strength, value), _number(i, info, value))
37
+
38
+
39
+ def precise_ref(value: str) -> PreciseRef:
40
+ path, (kind_name, strength, fidelity) = _fields(value, ["KIND", "STRENGTH", "FIDELITY"])
41
+ # Character and style by default, as the official client adds a reference
42
+ kind = PRECISE_KINDS.get(kind_name or "both")
43
+ if kind is None:
44
+ raise errors.NaiError(errors.BAD_ARGS, f"precise reference kind must be both, character or style: {value}")
45
+ return PreciseRef(path, kind, _number(strength, 1.0, value), _number(fidelity, 1.0, value))
46
+
47
+
48
+ def load_vibes(refs: tuple[VibeRef, ...], model: str) -> list[vibe.VibeSource]:
49
+ return [source for ref in refs for source in vibe.load(ref.path, ref.strength, ref.information_extracted, model)]
50
+
51
+
52
+ def load_precise(refs: tuple[PreciseRef, ...], transparent: bool) -> list[R.PreciseReference]:
53
+ return [
54
+ R.PreciseReference(images.precise_reference_image(ref.path, transparent), ref.kind, ref.strength, ref.fidelity)
55
+ for ref in refs
56
+ ]
57
+
58
+
59
+ def _split(value: str) -> tuple[Path, list[str]]:
60
+ # A drive letter's colon is not a separator
61
+ head = ""
62
+ if len(value) > 2 and value[1] == ":" and value[2] in "\\/":
63
+ head, value = value[:2], value[2:]
64
+ parts = value.split(":")
65
+ return values.path(head + parts[0]), parts[1:]
66
+
67
+
68
+ def _fields(value: str, names: list[str]) -> tuple[Path, list[str]]:
69
+ """FILE:A:B with at most len(names) fields after the file; missing fields are empty."""
70
+ path, rest = _split(value)
71
+ if len(rest) > len(names):
72
+ usage = "".join(f"[:{name}" for name in names) + "]" * len(names)
73
+ raise errors.NaiError(errors.BAD_ARGS, f"expected FILE{usage}: {value}")
74
+ return path, rest + [""] * (len(names) - len(rest))
75
+
76
+
77
+ def _number(field: str, default: float | None, value: str) -> float | None:
78
+ """An empty field takes the default."""
79
+ if not field:
80
+ return default
81
+ try:
82
+ number = float(field)
83
+ except ValueError:
84
+ raise errors.NaiError(errors.BAD_ARGS, f"not a number: {field!r}: {value}") from None
85
+ if not math.isfinite(number):
86
+ raise errors.NaiError(errors.BAD_ARGS, f"not a finite number: {field!r}: {value}")
87
+ return number
@@ -0,0 +1,245 @@
1
+ """Generation settings turned into the request: texts as sent, source images, Vibes, references and lint."""
2
+
3
+ from dataclasses import dataclass, replace
4
+ from pathlib import Path
5
+
6
+ from PIL import Image
7
+
8
+ from nai_cli import caches, enhance, images, inpaint, lint, local_models, models, sam, vibe
9
+ from nai_cli import prompt as P
10
+ from nai_cli import request as R
11
+ from nai_cli.commands.generation import (
12
+ FROM_IMAGE,
13
+ Blank,
14
+ CharacterText,
15
+ Enhanced,
16
+ FromImage,
17
+ GenSettings,
18
+ Origin,
19
+ Repaint,
20
+ Text,
21
+ )
22
+ from nai_cli.commands.references import load_precise, load_vibes
23
+
24
+
25
+ @dataclass
26
+ class Prepared:
27
+ spec: R.GenSpec
28
+ built: R.Built
29
+ job: inpaint.Job | None
30
+ vibe_sources: list[vibe.VibeSource]
31
+ pending_encodes: list[vibe.Encode]
32
+ warnings: list[str]
33
+ issues: list[lint.Issue]
34
+ tokens: lint.TokenCount
35
+
36
+
37
+ def prepare(s: GenSettings) -> Prepared:
38
+ """Everything up to pricing, shared by the generation commands and lint."""
39
+ spec, warnings, job = _spec(s)
40
+ sources = [*s.stored_vibes, *load_vibes(s.vibes, spec.model.id)]
41
+ # Checked before anything is encoded for a model that would refuse it
42
+ R.check_references(spec.model, len(sources), len(spec.references))
43
+ pending = attach_vibes(spec, sources)
44
+ # A damaged image fails here, before any other Vibe is paid for
45
+ for encode in pending:
46
+ vibe.check_image(encode.source)
47
+ built = R.build(spec)
48
+ warnings += built.warnings
49
+ issues, tokens = _lint(spec, built)
50
+ return Prepared(spec, built, job, sources, pending, warnings, issues, tokens)
51
+
52
+
53
+ def attach_vibes(
54
+ spec: R.GenSpec, sources: list[vibe.VibeSource], fresh: dict[int, str] | None = None
55
+ ) -> list[vibe.Encode]:
56
+ """Set the Vibes from encodings just made, stored in their files or cached; returns the encodings still to
57
+ pay for when they are sent."""
58
+ sent = R.vibes_sent(spec.action, len(spec.references))
59
+ fresh = fresh or {}
60
+ spec.vibes, pending = [], []
61
+ for i, source in enumerate(sources):
62
+ # Vibes that are not sent need no encoding, not even a stored one
63
+ encoding = fresh.get(i) or (caches.vibe_encoding(source, spec.model.id) if sent else None)
64
+ if encoding is None and sent:
65
+ pending.append((i, source))
66
+ spec.vibes.append(R.Vibe(encoding or "", source.strength))
67
+ return vibe.encodes(pending, spec.model.id)
68
+
69
+
70
+ def _spec(s: GenSettings) -> tuple[R.GenSpec, list[str], inpaint.Job | None]:
71
+ warnings = list(s.warnings)
72
+ canvas = s.canvas
73
+ (width, height), image_bytes, mask_bytes, job = _canvas_input(s, warnings)
74
+ plan = canvas.plan if isinstance(canvas, Enhanced) else None
75
+ chars = _characters(s, job, warnings)
76
+ spec = R.GenSpec(
77
+ model=s.model,
78
+ prompt=_sent(s, s.prompt, s.drawn.prompt if s.drawn else None, s.fixed.prompt if s.fixed else None),
79
+ negative=_sent(s, s.negative, s.drawn.negative if s.drawn else None, s.fixed.negative if s.fixed else None),
80
+ characters=chars,
81
+ action=s.action,
82
+ width=width,
83
+ height=height,
84
+ steps=s.steps,
85
+ scale=s.cfg,
86
+ sampler=s.sampler,
87
+ noise_schedule=s.noise_schedule,
88
+ cfg_rescale=s.cfg_rescale,
89
+ seed=s.seed,
90
+ # Variations count follows the size requested; a source above the pixel limit would otherwise allow none
91
+ n_samples=s.n if s.n is not None else enhance.variation_samples(width, height),
92
+ quality=s.quality,
93
+ uc_preset=s.uc_preset,
94
+ negative_verbatim=s.negative_verbatim,
95
+ official_nsfw=s.official_nsfw,
96
+ use_coords=s.use_coords,
97
+ auto_text=s.auto_text,
98
+ normalize_vibes=s.normalize_vibes,
99
+ transparent=s.transparent,
100
+ straight_alpha=not s.premultiplied_alpha,
101
+ furry=s.furry,
102
+ variety=s.variety,
103
+ image=image_bytes,
104
+ mask=mask_bytes,
105
+ strength=s.strength,
106
+ noise=s.noise,
107
+ inpaint_strength=canvas.area.strength if isinstance(canvas, Repaint) else 1.0,
108
+ enhance_hint=plan is not None and not plan.upscaled,
109
+ upscaled_enhance=plan is not None and plan.upscaled,
110
+ legacy_uc=s.legacy_uc,
111
+ references=load_precise(s.references, s.model.transparent),
112
+ )
113
+ return spec, warnings, job
114
+
115
+
116
+ def _canvas_input(
117
+ s: GenSettings, warnings: list[str]
118
+ ) -> tuple[tuple[int, int], bytes | None, bytes | None, inpaint.Job | None]:
119
+ """The request size, the image and mask to send, and the inpainting job when there is one."""
120
+ keep_alpha = models.for_action(s.model, s.action).transparent
121
+ match s.canvas:
122
+ case Repaint() as canvas:
123
+ job = _inpaint_job(s, canvas, keep_alpha)
124
+ warnings += job.warnings
125
+ return (job.width, job.height), job.image, job.mask, job
126
+ case Enhanced() as canvas:
127
+ image = images.source_image(canvas.image, *canvas.plan.image_size, keep_alpha=keep_alpha)
128
+ return canvas.plan.request_size, image, None, None
129
+ case FromImage() as canvas:
130
+ size = _import_size(canvas, s.current_size, warnings)
131
+ return size, images.source_image(canvas.image, *size, keep_alpha=keep_alpha), None, None
132
+ case Blank() as canvas:
133
+ return canvas.size, None, None, None
134
+
135
+
136
+ def _import_size(canvas: FromImage, current: tuple[int, int], warnings: list[str]) -> tuple[int, int]:
137
+ """The size given, or else the size the official client gives an imported image."""
138
+ if canvas.size is not None:
139
+ return canvas.size
140
+ source_w, source_h = images.size_of(canvas.image)
141
+ width, height = images.import_size(source_w, source_h, current)
142
+ if width * height < source_w * source_h:
143
+ warnings.append(
144
+ f"source {source_w}x{source_h} is sent at {width}x{height}, as the official client imports it; "
145
+ "use --size for a larger request (above 1 MP costs Anlas)"
146
+ )
147
+ return width, height
148
+
149
+
150
+ def _inpaint_job(s: GenSettings, canvas: Repaint, keep_alpha: bool) -> inpaint.Job:
151
+ area = canvas.area
152
+ return inpaint.prepare(
153
+ images.open_image(canvas.image),
154
+ mask=images.open_image(area.mask) if area.mask else None,
155
+ regions=[inpaint.parse_region(r) for r in area.regions],
156
+ grow_pixels=area.grow,
157
+ expand=inpaint.parse_expand(area.expand) if area.expand else None,
158
+ size=canvas.size,
159
+ current=s.current_size,
160
+ focus=area.focus,
161
+ padding=area.focus_padding,
162
+ keep_alpha=keep_alpha,
163
+ segment=segmenter(area.sam_dir),
164
+ )
165
+
166
+
167
+ def segmenter(folder: Path) -> inpaint.Segment:
168
+ """Object regions segmented with the SAM model in folder, loaded only when one asks for it."""
169
+
170
+ def segment(image: Image.Image, points: list[tuple[float, float]]) -> Image.Image:
171
+ return sam.segment(image, points, *local_models.sam(folder))
172
+
173
+ return segment
174
+
175
+
176
+ def _sent(s: GenSettings, text: Text, drawn: tuple[str, str] | None, fixed: tuple[str, str] | None) -> str:
177
+ """The text as sent: the source image's draws, then tidying, then the fixed tags for texts typed."""
178
+ value = P.fill_drawn(text.value, *drawn) if drawn else text.value
179
+ value = s.tidy[text.origin].apply(value)
180
+ if fixed is None or text.origin is not Origin.TYPED:
181
+ return value
182
+ return P.with_fixed(value, *fixed)
183
+
184
+
185
+ def _characters(s: GenSettings, job: inpaint.Job | None, warnings: list[str]) -> list[R.Character]:
186
+ drawn = s.drawn
187
+ # The official client sends a character's draws again only while the image's characters are all there
188
+ paired = drawn is not None and len(drawn.characters) == len(s.characters)
189
+ negatives_paired = drawn is not None and len(drawn.character_negatives) == len(s.characters)
190
+ chars = [
191
+ _character(
192
+ s,
193
+ c,
194
+ drawn.characters[i] if paired and drawn else None,
195
+ drawn.character_negatives[i] if negatives_paired and drawn else None,
196
+ job,
197
+ )
198
+ for i, c in enumerate(s.characters)
199
+ ]
200
+ # Enhance and variations send the image's characters as stored; only the editor rewrites them
201
+ if s.command in FROM_IMAGE:
202
+ return chars
203
+ sent = [replace(c, prompt=P.official_character_prompt(c.prompt)) for c in chars]
204
+ if sent != chars:
205
+ warnings.append("1girl and 1boy in character prompts are sent as girl and boy, as the official client does")
206
+ return sent
207
+
208
+
209
+ def _character(
210
+ s: GenSettings,
211
+ c: CharacterText,
212
+ drawn: tuple[str, str] | None,
213
+ drawn_negative: tuple[str, str] | None,
214
+ job: inpaint.Job | None,
215
+ ) -> R.Character:
216
+ x, y = c.position if c.position is not None else (None, None)
217
+ if job is not None and x is not None and y is not None:
218
+ x, y = job.remap(x, y)
219
+ # A model that sends no custom negatives drops one filled in for the user without a warning about it
220
+ negative = "" if s.model.fixed and c.negative.origin is Origin.MODEL else _sent(s, c.negative, drawn_negative, None)
221
+ return R.Character(_sent(s, c.prompt, drawn, None), negative, x, y)
222
+
223
+
224
+ def _lint(spec: R.GenSpec, built: R.Built) -> tuple[list[lint.Issue], lint.TokenCount]:
225
+ # Counted on the texts as sent, the way the official prompt boxes count them
226
+ tokens = lint.count_tokens(
227
+ built.model,
228
+ caches.load_tokenizer(built.model.tokenizer),
229
+ built.prompt,
230
+ built.negative,
231
+ [(c.prompt, c.negative) for c in built.characters],
232
+ )
233
+ issues = lint.run(
234
+ built.model,
235
+ spec.prompt,
236
+ spec.negative,
237
+ [(c.prompt, c.negative) for c in spec.characters],
238
+ spec.quality,
239
+ built.prompt,
240
+ spec.uc_preset,
241
+ spec.transparent,
242
+ spec.negative_verbatim,
243
+ tokens,
244
+ )
245
+ return issues, tokens
@@ -0,0 +1,54 @@
1
+ """stats: the ledger's requests, images and spend over a range of days."""
2
+
3
+ from dataclasses import dataclass
4
+ from datetime import date, timedelta
5
+ from typing import Any
6
+
7
+ from nai_cli import errors, ledger
8
+ from nai_cli.commands.context import Context
9
+
10
+ DEFAULT_DAYS = 30
11
+
12
+
13
+ @dataclass(frozen=True)
14
+ class StatsSettings:
15
+ since: date
16
+ until: date
17
+ by: str
18
+
19
+
20
+ def resolve(opts: dict[str, Any], ctx: Context) -> StatsSettings:
21
+ if opts.get("days") is not None and opts.get("since"):
22
+ raise errors.NaiError(errors.BAD_ARGS, "give --days or --since, not both")
23
+ until = _day(opts.get("until"), "--until") or ctx.now().date()
24
+ days = DEFAULT_DAYS if opts.get("days") is None else opts["days"]
25
+ if days < 1:
26
+ raise errors.NaiError(errors.BAD_ARGS, f"--days needs at least 1: {days}")
27
+ since = _day(opts.get("since"), "--since") or _days_before(until, days - 1)
28
+ if since > until:
29
+ raise errors.NaiError(errors.BAD_ARGS, f"--since {since} comes after --until {until}")
30
+ return StatsSettings(since, until, opts.get("by") or "day")
31
+
32
+
33
+ def _days_before(day: date, days: int) -> date:
34
+ # A window reaching before year 1 starts there
35
+ try:
36
+ return day - timedelta(days=days)
37
+ except OverflowError:
38
+ return date.min
39
+
40
+
41
+ def _day(text: str | None, option: str) -> date | None:
42
+ if not text:
43
+ return None
44
+ try:
45
+ return date.fromisoformat(text)
46
+ except ValueError:
47
+ raise errors.NaiError(errors.BAD_ARGS, f"{option} takes YYYY-MM-DD: {text!r}") from None
48
+
49
+
50
+ def run(s: StatsSettings, ctx: Context) -> dict[str, Any]:
51
+ try:
52
+ return {"ok": True, **ledger.summary(s.since, s.until, s.by)}
53
+ except OSError as e:
54
+ raise errors.NaiError(errors.LEDGER, f"cannot read the ledger: {e}") from e
@@ -0,0 +1,15 @@
1
+ """strip: copies of images without generation settings, EXIF or stealth data, for sharing."""
2
+
3
+ from typing import Any
4
+
5
+ from nai_cli import images
6
+ from nai_cli.commands import copies
7
+ from nai_cli.commands.context import Context
8
+
9
+
10
+ def resolve(opts: dict[str, Any], ctx: Context) -> copies.Targets:
11
+ return copies.resolve(opts, ctx)
12
+
13
+
14
+ def run(s: copies.Targets, ctx: Context) -> dict[str, Any]:
15
+ return copies.write(s, "clean", images.strip_file)
@@ -0,0 +1,32 @@
1
+ """tags: tag suggestions the way the official prompt box makes them."""
2
+
3
+ import time
4
+ from dataclasses import dataclass
5
+ from typing import Any
6
+
7
+ from nai_cli import models, tags
8
+ from nai_cli.commands.context import Context
9
+
10
+
11
+ @dataclass(frozen=True)
12
+ class TagsSettings:
13
+ model: models.Model
14
+ furry: bool
15
+ queries: tuple[str, ...]
16
+
17
+
18
+ def resolve(opts: dict[str, Any], ctx: Context) -> TagsSettings:
19
+ model = models.resolve(opts.get("model") or ctx.config.defaults.get("model"))
20
+ return TagsSettings(model, bool(opts.get("furry")), tuple(opts["query"]))
21
+
22
+
23
+ def run(s: TagsSettings, ctx: Context) -> dict[str, Any]:
24
+ lookups = [tags.lookup(q, s.model, s.furry) for q in s.queries]
25
+ client = ctx.client()
26
+ results = []
27
+ for i, item in enumerate(lookups):
28
+ if i:
29
+ time.sleep(tags.PAUSE)
30
+ found = client.suggest_tags(item.text, item.model, item.lexicon, item.lang)
31
+ results.append({"query": item.query, "lang": item.lang or "en", "tags": found})
32
+ return {"ok": True, "model": s.model.id, "furry": s.furry, "results": results}