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/cli.py ADDED
@@ -0,0 +1,110 @@
1
+ """The command line: argv into settings, the command run, its result printed as JSON with an exit code."""
2
+
3
+ import sys
4
+ import traceback
5
+ from collections.abc import Callable
6
+ from contextlib import redirect_stdout
7
+ from pathlib import Path
8
+ from typing import Any
9
+
10
+ from nai_cli import config, errors, parser, values
11
+ from nai_cli.commands import COMMANDS
12
+ from nai_cli.commands.context import Context, Result
13
+
14
+
15
+ def main(argv: list[str] | None = None) -> int:
16
+ if hasattr(sys.stdout, "reconfigure"):
17
+ sys.stdout.reconfigure(encoding="utf-8")
18
+ result, code = run(sys.argv[1:] if argv is None else list(argv))
19
+ # Commands that stream lines, batch, grid and nai meta of several images, end with a summary line
20
+ text, code = _dumps(result, code, None if result.get("type") == "summary" else 1)
21
+ print(text)
22
+ return code
23
+
24
+
25
+ def run(argv: list[str]) -> Result:
26
+ """One command, as its result and exit code; nothing is printed except the lines a batch streams."""
27
+ return _guarded(lambda: _run(argv, None))
28
+
29
+
30
+ def _run(argv: list[str], parent: Context | None) -> dict[str, Any]:
31
+ opts = vars(parser.build_parser().parse_args(argv))
32
+ config_path: Path | None = opts.pop("config")
33
+ account: str | None = opts.pop("account")
34
+ if parent is None:
35
+ path = config_path or config.default_path()
36
+ ctx = Context.create(path, _stdin_reader(), _emit, _run_job, _parse_job, tuple(argv), account)
37
+ else:
38
+ # A job keeps the batch's config, and so its limits; batch refuses --config in a job
39
+ ctx = parent if account is None else parent.with_account(account)
40
+ try:
41
+ command = COMMANDS[opts.pop("cmd")]
42
+ return command.run(command.resolve(opts, ctx), ctx)
43
+ finally:
44
+ ctx.close()
45
+
46
+
47
+ def _run_job(argv: list[str], ctx: Context) -> Result:
48
+ try:
49
+ # Only the job's line belongs on standard output
50
+ with redirect_stdout(sys.stderr):
51
+ return _guarded(lambda: _run(argv, ctx))
52
+ except SystemExit:
53
+ # --help and --version print their text and exit
54
+ return {"ok": False, "code": errors.BAD_ARGS, "message": "--help and --version cannot run in a batch"}, 1
55
+
56
+
57
+ def _parse_job(argv: list[str]) -> dict[str, Any]:
58
+ try:
59
+ with redirect_stdout(sys.stderr):
60
+ return vars(parser.build_parser().parse_args(argv))
61
+ except SystemExit:
62
+ raise errors.NaiError(errors.BAD_ARGS, "--help and --version cannot run in a grid") from None
63
+
64
+
65
+ def _guarded(command: Callable[[], dict[str, Any]]) -> Result:
66
+ try:
67
+ return command(), 0
68
+ except errors.NaiError as e:
69
+ # Interrupted or failed after paying: reported with the spend, otherwise as below
70
+ if e.code == errors.INTERNAL and e.__cause__ is not None:
71
+ traceback.print_exception(e.__cause__)
72
+ return e.to_json(), 130 if e.code == errors.INTERRUPTED else 1
73
+ except KeyboardInterrupt:
74
+ return {"ok": False, "code": errors.INTERRUPTED, "message": "interrupted"}, 130
75
+ except Exception as e:
76
+ traceback.print_exc()
77
+ return {"ok": False, "code": errors.INTERNAL, "message": f"{type(e).__name__}: {e}"}, 1
78
+
79
+
80
+ def _stdin_reader() -> Callable[[], str]:
81
+ taken = False
82
+
83
+ def read() -> str:
84
+ nonlocal taken
85
+ if taken:
86
+ raise errors.NaiError(errors.BAD_ARGS, "standard input is already in use, so @- cannot read it")
87
+ taken = True
88
+ try:
89
+ raw = sys.stdin.buffer.read() if hasattr(sys.stdin, "buffer") else sys.stdin.read().encode("utf-8")
90
+ return raw.decode("utf-8-sig")
91
+ except (OSError, UnicodeDecodeError) as e:
92
+ raise errors.NaiError(errors.BAD_ARGS, f"cannot read standard input: {e}") from e
93
+
94
+ return read
95
+
96
+
97
+ def _emit(line: dict[str, Any]) -> None:
98
+ text, code = _dumps(line, 0, None)
99
+ print(text, flush=True)
100
+ if code:
101
+ # The summary after it must not report a line that was lost
102
+ raise errors.NaiError(errors.INTERNAL, "a line of output is not valid JSON")
103
+
104
+
105
+ def _dumps(result: dict[str, Any], code: int, indent: int | None) -> tuple[str, int]:
106
+ try:
107
+ return values.to_json(result, indent), code
108
+ except ValueError as e:
109
+ result = {"ok": False, "code": errors.INTERNAL, "message": f"output is not valid JSON: {e}"}
110
+ return values.to_json(result, indent), 1
nai_cli/comfyui.py ADDED
@@ -0,0 +1,194 @@
1
+ """ComfyUI multi-character prompts, split into a base prompt and characters at their regions' centres."""
2
+
3
+ import math
4
+ import re
5
+ from dataclasses import dataclass
6
+
7
+ from nai_cli import prompt as P
8
+
9
+ # A comma ends the tag before COUPLE as a space does
10
+ _COUPLE = re.compile(r"(?:^|[\s,]+)COUPLE\b")
11
+ _COUPLE_REGION = re.compile(r"^\s*\(([^)]+)\)")
12
+ _AND = re.compile(r"\s+AND\s+")
13
+ _REGION_CALL = re.compile(r"\b(?:MASK|AREA)\s*\(")
14
+ _MASK = re.compile(r"\bMASK\s*\(([^)]+)\)")
15
+ _AREA = re.compile(r"\bAREA\s*\(([^)]+)\)")
16
+ _IMASK = re.compile(r"\bIMASK\s*\([^)]*\)")
17
+ _FILL = re.compile(r"\bFILL\s*\(\s*\)")
18
+ _TRAILING_WEIGHT = re.compile(r"\s*:\s*[\d.]+\s*$")
19
+ _LINE_START = re.compile(r"(?:^|[\r\n])[ \t]*$")
20
+ _SPACES = " \t\r\n"
21
+ # Stands in for quoted text; no syntax matches it
22
+ _BLANK = "\0"
23
+
24
+
25
+ @dataclass(frozen=True)
26
+ class Character:
27
+ prompt: str
28
+ # The centre of the region it was given, within 0-1
29
+ center: tuple[float, float] | None
30
+
31
+
32
+ @dataclass(frozen=True)
33
+ class Split:
34
+ base: str
35
+ characters: tuple[Character, ...]
36
+
37
+
38
+ @dataclass(frozen=True)
39
+ class _Text:
40
+ """A prompt beside a copy of it with quoted text blanked, so syntax is found only outside quotes and cut from
41
+ both at the same places."""
42
+
43
+ text: str
44
+ bare: str
45
+
46
+ @classmethod
47
+ def of(cls, text: str) -> "_Text":
48
+ bare = list(text)
49
+ for start, end in P.quoted_ranges(text):
50
+ # Single quotes are apostrophes as often, as in '90s or girls', so a pair spanning tags is not quoted
51
+ if text[end - 1] in "'\u2019" and any(c in ",\r\n" for c in text[start:end]):
52
+ continue
53
+ bare[start:end] = _BLANK * (end - start)
54
+ return cls(text, "".join(bare))
55
+
56
+ def __getitem__(self, at: slice) -> "_Text":
57
+ return _Text(self.text[at], self.bare[at])
58
+
59
+ def __add__(self, other: "_Text") -> "_Text":
60
+ return _Text(self.text + other.text, self.bare + other.bare)
61
+
62
+ def cut(self, pattern: re.Pattern[str]) -> list["_Text"]:
63
+ parts, start = [], 0
64
+ for match in pattern.finditer(self.bare):
65
+ parts.append(self[start : match.start()])
66
+ start = match.end()
67
+ return [*parts, self[start:]]
68
+
69
+ def without(self, pattern: re.Pattern[str]) -> "_Text":
70
+ parts = self.cut(pattern)
71
+ return sum(parts[1:], parts[0])
72
+
73
+ def strip(self) -> "_Text":
74
+ return self[len(self.bare) - len(self.bare.lstrip()) : len(self.bare.rstrip())]
75
+
76
+ def take(self, pattern: re.Pattern[str]) -> tuple["_Text", tuple[float, float] | None]:
77
+ """Without the first call, and the centre of the region it gave."""
78
+ match = pattern.search(self.bare)
79
+ if match is None:
80
+ return self, None
81
+ return self[: match.start()] + self[match.end() :], _center(self.text[match.start(1) : match.end(1)])
82
+
83
+
84
+ def split(text: str) -> Split | None:
85
+ """COUPLE, AND with MASK or AREA, or a base prompt with a character after each | line; None for other
86
+ prompts. How many characters a model takes is checked with the request."""
87
+ typed = _Text.of(text)
88
+ if _COUPLE.search(typed.bare):
89
+ found = _couple(typed)
90
+ elif _AND.search(typed.bare) and _REGION_CALL.search(typed.bare):
91
+ found = _and_mask(typed)
92
+ else:
93
+ found = _pipes(typed)
94
+ return found if found is not None and found.characters else None
95
+
96
+
97
+ def _couple(typed: _Text) -> Split:
98
+ base, *parts = typed.cut(_COUPLE)
99
+ characters = []
100
+ for part in parts:
101
+ rest = part.strip()
102
+ region = _COUPLE_REGION.match(rest.bare)
103
+ # A bracket that gives no region is a prompt of its own, such as (1girl:1.2)
104
+ center = _center(rest.text[region.start(1) : region.end(1)]) if region else None
105
+ if region and center is not None:
106
+ rest = rest[region.end() :]
107
+ rest, masked = rest.take(_MASK)
108
+ characters.append((rest, center or masked))
109
+ return Split(_tidy(base.without(_FILL).text), _characters(characters))
110
+
111
+
112
+ def _and_mask(typed: _Text) -> Split:
113
+ characters = []
114
+ for part in typed.cut(_AND):
115
+ rest = part.strip().without(_TRAILING_WEIGHT)
116
+ rest, masked = rest.take(_MASK)
117
+ rest, area = rest.take(_AREA)
118
+ characters.append((rest, masked or area))
119
+ # Every part is a character; there is no shared prompt
120
+ return Split("", _characters(characters))
121
+
122
+
123
+ def _characters(parts: list[tuple[_Text, tuple[float, float] | None]]) -> tuple[Character, ...]:
124
+ out = []
125
+ for text, center in parts:
126
+ prompt = _tidy(text.without(_IMASK).text)
127
+ if prompt:
128
+ out.append(Character(prompt, center))
129
+ return tuple(out)
130
+
131
+
132
+ def _center(params: str) -> tuple[float, float] | None:
133
+ """(x1 x2) or (x1 x2, y1 y2, weight...); y spans the whole height when left out."""
134
+ ranges = [p.split() for p in params.split(",")]
135
+ try:
136
+ x1, x2 = (float(v) for v in ranges[0][:2])
137
+ y1, y2 = (float(v) for v in ranges[1][:2]) if len(ranges) > 1 and len(ranges[1]) >= 2 else (0.0, 1.0)
138
+ except ValueError:
139
+ return None
140
+ # nan and inf parse as floats but place nothing
141
+ if not all(math.isfinite(v) for v in (x1, x2, y1, y2)):
142
+ return None
143
+ return _clamp((x1 + x2) / 2), _clamp((y1 + y2) / 2)
144
+
145
+
146
+ def _clamp(value: float) -> float:
147
+ return min(1.0, max(0.0, value))
148
+
149
+
150
+ def _tidy(text: str) -> str:
151
+ text = re.sub(r",\s*,", ",", text)
152
+ text = re.sub(r"^\s*,\s*|\s*,\s*$", "", text)
153
+ return re.sub(r"\s+", " ", text).strip()
154
+
155
+
156
+ def _pipes(typed: _Text) -> Split | None:
157
+ """A | at the start of a line, or between spaces, outside brackets, quotes and ||a|b|| groups."""
158
+ text, bare = typed.text, typed.bare
159
+ parts = []
160
+ start = 0
161
+ depth = {"{": 0, "[": 0, "(": 0}
162
+ closing = {"}": "{", "]": "[", ")": "("}
163
+ escaped = grouped = False
164
+ i = 0
165
+ while i < len(bare):
166
+ ch = bare[i]
167
+ if escaped:
168
+ escaped = False
169
+ elif ch == "\\":
170
+ escaped = True
171
+ elif ch in depth:
172
+ depth[ch] += 1
173
+ elif ch in closing:
174
+ depth[closing[ch]] = max(0, depth[closing[ch]] - 1)
175
+ elif ch == "|" and bare[i + 1 : i + 2] == "|":
176
+ grouped = not grouped
177
+ i += 1
178
+ elif ch == "|" and not grouped and not any(depth.values()) and _divides(text, i):
179
+ parts.append(text[start:i].strip())
180
+ start = i + 1
181
+ i += 1
182
+ parts.append(text[start:].strip())
183
+ # A | on the first line leaves the base prompt empty
184
+ base, *characters = parts
185
+ characters = [p for p in characters if p]
186
+ if not characters:
187
+ return None
188
+ return Split(base, tuple(Character(p, None) for p in characters))
189
+
190
+
191
+ def _divides(text: str, at: int) -> bool:
192
+ if _LINE_START.search(text, 0, at):
193
+ return True
194
+ return 0 < at < len(text) - 1 and text[at - 1] in _SPACES and text[at + 1] in _SPACES
@@ -0,0 +1,61 @@
1
+ """Every command as a pair: resolve options into settings, then run the settings."""
2
+
3
+ from collections.abc import Callable
4
+ from dataclasses import dataclass
5
+ from functools import partial
6
+ from typing import Any
7
+
8
+ from nai_cli.commands import (
9
+ account,
10
+ batch,
11
+ config,
12
+ cost,
13
+ generate,
14
+ generation,
15
+ grid,
16
+ image_tools,
17
+ interrogate,
18
+ lint,
19
+ local,
20
+ meta,
21
+ models,
22
+ redact,
23
+ stats,
24
+ strip,
25
+ tags,
26
+ vibe,
27
+ watermark,
28
+ )
29
+ from nai_cli.commands.context import Context
30
+
31
+
32
+ @dataclass(frozen=True)
33
+ class Command:
34
+ resolve: Callable[[dict[str, Any], Context], Any]
35
+ run: Callable[[Any, Context], dict[str, Any]]
36
+
37
+
38
+ COMMANDS = {
39
+ **{
40
+ name: Command(partial(generation.resolve, command=name), generate.run)
41
+ for name in ("gen", "i2i", "inpaint", "enhance", "variations")
42
+ },
43
+ "upscale": Command(image_tools.resolve_upscale, image_tools.run_upscale),
44
+ "tool": Command(image_tools.resolve_tool, image_tools.run_tool),
45
+ "vibe": Command(vibe.resolve, vibe.run),
46
+ "strip": Command(strip.resolve, strip.run),
47
+ "redact": Command(redact.resolve, redact.run),
48
+ "watermark": Command(watermark.resolve, watermark.run),
49
+ "batch": Command(batch.resolve, batch.run),
50
+ "grid": Command(grid.resolve, grid.run),
51
+ "meta": Command(meta.resolve, meta.run),
52
+ "lint": Command(lint.resolve, lint.run),
53
+ "tags": Command(tags.resolve, tags.run),
54
+ "models": Command(models.resolve, models.run),
55
+ "cost": Command(cost.resolve, cost.run),
56
+ "account": Command(account.resolve, account.run),
57
+ "stats": Command(stats.resolve, stats.run),
58
+ "interrogate": Command(interrogate.resolve, interrogate.run),
59
+ "local": Command(local.resolve, local.run),
60
+ "config": Command(config.resolve, config.run),
61
+ }
@@ -0,0 +1,79 @@
1
+ """account: the balance and this month's spend, the accounts there are, and email sign-in."""
2
+
3
+ import calendar
4
+ from dataclasses import dataclass
5
+ from datetime import date
6
+ from typing import Any
7
+
8
+ from nai_cli import auth, errors, ledger
9
+ from nai_cli.commands.context import Context
10
+
11
+
12
+ @dataclass(frozen=True)
13
+ class AccountSettings:
14
+ action: str
15
+ name: str | None
16
+ email: str | None
17
+ month: date
18
+ # Asked by a dry run of several jobs: checked, but the store is left as it was
19
+ dry_run: bool
20
+
21
+
22
+ def resolve(opts: dict[str, Any], ctx: Context) -> AccountSettings:
23
+ action, name, email = opts["action"], opts.get("name"), opts.get("email")
24
+ if action in ("use", "login", "logout") and not name:
25
+ raise errors.NaiError(errors.BAD_ARGS, f"account {action} needs the account's name")
26
+ if action in ("show", "list") and name:
27
+ raise errors.NaiError(errors.BAD_ARGS, f"account {action} takes no name; use --account NAME")
28
+ if (action == "login") != bool(email):
29
+ raise errors.NaiError(errors.BAD_ARGS, "--email goes with account login, which needs it")
30
+ if opts.get("month") and action != "show":
31
+ raise errors.NaiError(errors.BAD_ARGS, "--month only applies to account show")
32
+ if action == "login" and ctx.dry_run:
33
+ raise errors.NaiError(errors.BAD_ARGS, "account login signs in for real, so a dry run cannot include it")
34
+ return AccountSettings(action, name, email, _month(opts.get("month"), ctx), ctx.dry_run)
35
+
36
+
37
+ def _month(text: str | None, ctx: Context) -> date:
38
+ if not text:
39
+ return ctx.now().date().replace(day=1)
40
+ try:
41
+ return date.fromisoformat(f"{text}-01")
42
+ except ValueError:
43
+ raise errors.NaiError(errors.BAD_ARGS, f"--month takes YYYY-MM: {text!r}") from None
44
+
45
+
46
+ def run(s: AccountSettings, ctx: Context) -> dict[str, Any]:
47
+ match s.action:
48
+ case "list":
49
+ return {"ok": True, "accounts": auth.listing(ctx.config)}
50
+ case "use":
51
+ auth.use(s.name or "", ctx.config, dry_run=s.dry_run)
52
+ return _changed(s)
53
+ case "login":
54
+ # The password comes from standard input only, so it stays out of the shell history and the config
55
+ password = ctx.read_stdin().rstrip("\r\n")
56
+ if not password:
57
+ raise errors.NaiError(errors.BAD_ARGS, "pipe the password to standard input")
58
+ return {"ok": True, **auth.login(s.name or "", s.email or "", password, ctx.config, ctx.now())}
59
+ case "logout":
60
+ if not auth.logout(s.name or "", dry_run=s.dry_run):
61
+ raise errors.NaiError(errors.BAD_ARGS, f"{s.name} is not an email login")
62
+ return _changed(s)
63
+ account = ctx.balance()
64
+ until = s.month.replace(day=calendar.monthrange(s.month.year, s.month.month)[1])
65
+ try:
66
+ month = ledger.summary(s.month, until, "command")
67
+ except OSError as e:
68
+ raise errors.NaiError(errors.LEDGER, f"cannot read the ledger: {e}") from e
69
+ return {
70
+ "ok": True,
71
+ "account": ctx.account_name,
72
+ **account.to_json(),
73
+ # The ledger is shared by every account on this computer, so the month is not this account's alone
74
+ "month": {"month": s.month.isoformat()[:7], "scope": "all_accounts", **month},
75
+ }
76
+
77
+
78
+ def _changed(s: AccountSettings) -> dict[str, Any]:
79
+ return {"ok": True, "account": s.name, "dry_run": True} if s.dry_run else {"ok": True, "account": s.name}
@@ -0,0 +1,142 @@
1
+ """batch: run the jobs of a JSONL file in process, one result line each as soon as it ends."""
2
+
3
+ import random
4
+ import re
5
+ from dataclasses import dataclass, field
6
+ from typing import Any
7
+
8
+ from nai_cli import config, errors, values
9
+ from nai_cli.batch import Job, Totals, read_jobs, succeeded
10
+ from nai_cli.commands.context import Context
11
+
12
+ _NUMBER = r"((?:\d+\.?\d*|\.\d+)(?:[eE][-+]?\d+)?)"
13
+ _INTERVAL = re.compile(rf"{_NUMBER}(?:\s*-\s*{_NUMBER})?")
14
+
15
+
16
+ @dataclass(frozen=True)
17
+ class RunOptions:
18
+ stop_on_error: bool
19
+ dry_run: bool
20
+ # Seconds from the last request sent to the first of the next job, drawn between the two for each job
21
+ interval: tuple[float, float]
22
+
23
+
24
+ @dataclass(frozen=True)
25
+ class BatchSettings:
26
+ jobs: tuple[Job, ...]
27
+ # Jobs a previous run finished, skipped this time
28
+ done: set[str]
29
+ options: RunOptions
30
+
31
+
32
+ def resolve(opts: dict[str, Any], ctx: Context) -> BatchSettings:
33
+ try:
34
+ source = opts["jobs"]
35
+ jobs = read_jobs(ctx.read_stdin() if source == "-" else _read(source, "jobs"), source)
36
+ done = succeeded(_read(opts["skip_done"], "results")) if opts.get("skip_done") else set()
37
+ options = run_options(opts, ctx)
38
+ except errors.NaiError as e:
39
+ raise as_summary(e) from None
40
+ return BatchSettings(tuple(jobs), done, options)
41
+
42
+
43
+ def run_options(opts: dict[str, Any], ctx: Context) -> RunOptions:
44
+ return RunOptions(
45
+ bool(opts.get("stop_on_error")),
46
+ bool(opts.get("dry_run")),
47
+ _interval(opts.get("interval"), ctx),
48
+ )
49
+
50
+
51
+ def as_summary(e: errors.NaiError) -> errors.NaiError:
52
+ """An error before any job ran, still printed as one summary line, as a run of jobs always prints."""
53
+ e.detail.setdefault("type", "summary")
54
+ e.detail.setdefault("batch", None)
55
+ return e
56
+
57
+
58
+ def _interval(text: str | None, ctx: Context) -> tuple[float, float]:
59
+ if text is None:
60
+ return ctx.config.interval
61
+ # The - of 1e-2 is part of a number, not a range
62
+ found = _INTERVAL.fullmatch(text.strip())
63
+ try:
64
+ if found is None:
65
+ raise ValueError(text)
66
+ low, high = found.group(1), found.group(2) or found.group(1)
67
+ return config.interval([float(low), float(high)])
68
+ except ValueError as e:
69
+ raise errors.NaiError(
70
+ errors.BAD_ARGS, f"--interval takes seconds or MIN-MAX, up to {config.MAX_INTERVAL}: {text!r}"
71
+ ) from e
72
+
73
+
74
+ def _read(path: str, what: str) -> str:
75
+ # Decoded without newline translation, so only line feeds end a line, as on standard input
76
+ try:
77
+ return values.path(path).read_bytes().decode("utf-8-sig")
78
+ except (OSError, UnicodeDecodeError) as e:
79
+ raise errors.NaiError(errors.BAD_ARGS, f"cannot read the {what} in {path}: {e}") from e
80
+
81
+
82
+ def run(s: BatchSettings, ctx: Context) -> dict[str, Any]:
83
+ return summary(run_jobs(s.jobs, s.done, s.options, ctx), s.options.dry_run)
84
+
85
+
86
+ @dataclass
87
+ class Ran:
88
+ totals: Totals
89
+ # The result of each job that ran, by id
90
+ results: dict[str, dict[str, Any]] = field(default_factory=dict)
91
+
92
+
93
+ def run_jobs(jobs: tuple[Job, ...], done: set[str], o: RunOptions, ctx: Context) -> Ran:
94
+ """Each job runs as its own command under the same config, or as its own dry run. Its line is emitted as soon as
95
+ it ends, so a run cut short still leaves the results of the jobs that ran."""
96
+ ran = Ran(Totals(len(jobs)))
97
+ try:
98
+ for job in jobs:
99
+ if job.id in done:
100
+ ran.totals.skipped += 1
101
+ skipped = {"type": "job", "job": job.id, "line": job.line, "skipped": "succeeded"}
102
+ ctx.emit({**skipped, "dry_run": True} if o.dry_run else skipped)
103
+ continue
104
+ # Spent before the job's first request, so a job that sends nothing does not wait
105
+ ctx.traffic.wait = 0.0 if o.dry_run else random.uniform(*o.interval)
106
+ result, code = ctx.run_job(job.argv, ctx.job(job.argv, o.dry_run))
107
+ ctx.emit(_job_line(job, code, result, o.dry_run))
108
+ ran.results[job.id] = result
109
+ ran.totals.add(result, code)
110
+ if ctx.shared.interrupted:
111
+ ran.totals.code = 130
112
+ if ran.totals.code == 130 or (code and o.stop_on_error):
113
+ break
114
+ except KeyboardInterrupt:
115
+ ran.totals.code = 130
116
+ return ran
117
+
118
+
119
+ def _job_line(job: Job, code: int, result: dict[str, Any], dry_run: bool) -> dict[str, Any]:
120
+ """The batch's own keys over the job's result, so a result that has a type or a line of its own cannot pass
121
+ for another line; in a dry run every line says so, whether or not its command has a dry run."""
122
+ line: dict[str, Any] = {"type": "job", "job": job.id, "line": job.line, "exit": code}
123
+ line.update((key, value) for key, value in result.items() if key not in line)
124
+ if dry_run:
125
+ line["dry_run"] = True
126
+ return line
127
+
128
+
129
+ def summary(ran: Ran, dry_run: bool, **extra: Any) -> dict[str, Any]:
130
+ totals = ran.totals
131
+ batch = totals.to_json(dry_run)
132
+ if totals.code == 130:
133
+ raise errors.NaiError(errors.INTERRUPTED, "interrupted", type="summary", batch=batch, **extra)
134
+ if totals.code:
135
+ raise errors.NaiError(
136
+ errors.BATCH_FAILED,
137
+ f"{totals.failed} of {totals.jobs} jobs failed",
138
+ type="summary",
139
+ batch=batch,
140
+ **extra,
141
+ )
142
+ return {"type": "summary", "ok": True, "batch": batch, **extra}
@@ -0,0 +1,57 @@
1
+ """config: show the settings in use, or write the config template."""
2
+
3
+ from dataclasses import dataclass
4
+ from pathlib import Path
5
+ from typing import Any
6
+
7
+ from nai_cli import auth, config, errors, fs
8
+ from nai_cli.commands.context import Context
9
+
10
+
11
+ @dataclass(frozen=True)
12
+ class ConfigSettings:
13
+ path: Path
14
+ init: bool
15
+ force: bool
16
+ dry_run: bool
17
+
18
+
19
+ def resolve(opts: dict[str, Any], ctx: Context) -> ConfigSettings:
20
+ if opts.get("force") and opts["action"] != "init":
21
+ raise errors.NaiError(errors.BAD_ARGS, "--force only applies to config init")
22
+ return ConfigSettings(ctx.shared.config_path, opts["action"] == "init", bool(opts.get("force")), ctx.dry_run)
23
+
24
+
25
+ def run(s: ConfigSettings, ctx: Context) -> dict[str, Any]:
26
+ if s.init:
27
+ if s.dry_run:
28
+ return {"ok": True, "dry_run": True, "path": str(s.path), "exists": fs.exists(s.path)}
29
+ try:
30
+ written = config.init(s.path, s.force)
31
+ except OSError as e:
32
+ raise errors.NaiError(errors.BAD_ARGS, f"cannot write {s.path}: {e}") from e
33
+ ctx.forget_config()
34
+ return {"ok": True, "path": str(s.path), "created": written}
35
+ cfg = ctx.config
36
+ return {
37
+ "ok": True,
38
+ "path": str(cfg.path),
39
+ "exists": fs.exists(cfg.path),
40
+ "novelai_token": bool(cfg.novelai_token),
41
+ "token_source": cfg.token_source,
42
+ "output_dir": str(cfg.output_dir),
43
+ "filename": cfg.filename,
44
+ "folder": cfg.folder,
45
+ "max_per_request": cfg.max_per_request,
46
+ "batch_interval": list(cfg.interval),
47
+ "defaults": cfg.defaults,
48
+ "account": ctx.account_name,
49
+ "accounts": auth.listing(cfg),
50
+ "local": {
51
+ "tagger": str(cfg.local.tagger) if cfg.local.tagger else None,
52
+ "tagger_labels": str(cfg.local.tagger_labels) if cfg.local.tagger_labels else None,
53
+ "tagger_kind": cfg.local.tagger_kind or None,
54
+ "sam_dir": str(cfg.local.sam_dir),
55
+ },
56
+ "data_dir": str(config.data_dir()),
57
+ }