nai-cli 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- nai_cli/__init__.py +3 -0
- nai_cli/__main__.py +5 -0
- nai_cli/api.py +529 -0
- nai_cli/auth.py +240 -0
- nai_cli/batch.py +126 -0
- nai_cli/caches.py +56 -0
- nai_cli/charge.py +231 -0
- nai_cli/cli.py +110 -0
- nai_cli/comfyui.py +194 -0
- nai_cli/commands/__init__.py +61 -0
- nai_cli/commands/account.py +79 -0
- nai_cli/commands/batch.py +142 -0
- nai_cli/commands/config.py +57 -0
- nai_cli/commands/context.py +201 -0
- nai_cli/commands/copies.py +138 -0
- nai_cli/commands/cost.py +70 -0
- nai_cli/commands/generate.py +257 -0
- nai_cli/commands/generation.py +550 -0
- nai_cli/commands/grid.py +99 -0
- nai_cli/commands/image_tools.py +248 -0
- nai_cli/commands/interrogate.py +77 -0
- nai_cli/commands/lint.py +24 -0
- nai_cli/commands/local.py +62 -0
- nai_cli/commands/meta.py +102 -0
- nai_cli/commands/models.py +62 -0
- nai_cli/commands/output.py +165 -0
- nai_cli/commands/redact.py +48 -0
- nai_cli/commands/references.py +87 -0
- nai_cli/commands/spec.py +245 -0
- nai_cli/commands/stats.py +54 -0
- nai_cli/commands/strip.py +15 -0
- nai_cli/commands/tags.py +32 -0
- nai_cli/commands/texts.py +71 -0
- nai_cli/commands/vibe.py +151 -0
- nai_cli/commands/watermark.py +70 -0
- nai_cli/config.py +369 -0
- nai_cli/cost.py +114 -0
- nai_cli/data/random_prompt.json +2536 -0
- nai_cli/decode.py +87 -0
- nai_cli/enhance.py +137 -0
- nai_cli/errors.py +40 -0
- nai_cli/fs.py +209 -0
- nai_cli/grid.py +244 -0
- nai_cli/images.py +250 -0
- nai_cli/import_image.py +235 -0
- nai_cli/inpaint.py +476 -0
- nai_cli/ledger.py +146 -0
- nai_cli/lint.py +302 -0
- nai_cli/local_models.py +135 -0
- nai_cli/meta.py +606 -0
- nai_cli/models.py +308 -0
- nai_cli/naming.py +426 -0
- nai_cli/parser.py +611 -0
- nai_cli/pixel_snap.py +525 -0
- nai_cli/presets.py +276 -0
- nai_cli/prompt.py +457 -0
- nai_cli/random_prompt.py +285 -0
- nai_cli/request.py +621 -0
- nai_cli/rerun.py +177 -0
- nai_cli/resample.py +133 -0
- nai_cli/sam.py +63 -0
- nai_cli/share.py +392 -0
- nai_cli/sheet.py +86 -0
- nai_cli/tagger.py +464 -0
- nai_cli/tags.py +44 -0
- nai_cli/tidy.py +176 -0
- nai_cli/tokenizer.py +313 -0
- nai_cli/values.py +114 -0
- nai_cli/vibe.py +464 -0
- nai_cli-0.1.0.dist-info/METADATA +105 -0
- nai_cli-0.1.0.dist-info/RECORD +75 -0
- nai_cli-0.1.0.dist-info/WHEEL +4 -0
- nai_cli-0.1.0.dist-info/entry_points.txt +2 -0
- nai_cli-0.1.0.dist-info/licenses/LICENSE +21 -0
- nai_cli-0.1.0.dist-info/licenses/THIRD_PARTY_NOTICES.md +56 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
"""What a command sees of the outside: config, account, client, clock, standard input and job runs."""
|
|
2
|
+
|
|
3
|
+
import time
|
|
4
|
+
from collections.abc import Callable
|
|
5
|
+
from contextlib import AbstractContextManager, suppress
|
|
6
|
+
from dataclasses import dataclass, field, replace
|
|
7
|
+
from datetime import datetime
|
|
8
|
+
from functools import cached_property
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
from typing import Any
|
|
11
|
+
|
|
12
|
+
from nai_cli import api, auth, charge, config, errors
|
|
13
|
+
|
|
14
|
+
Result = tuple[dict[str, Any], int]
|
|
15
|
+
# A balance as read, or the error of a read that failed
|
|
16
|
+
Balance = tuple[api.Account | None, errors.NaiError | None]
|
|
17
|
+
LINES_IN_JOB = "a job prints one result; give each image its own nai meta job"
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@dataclass
|
|
21
|
+
class _Shared:
|
|
22
|
+
"""What every job of a run shares: the config file is read once."""
|
|
23
|
+
|
|
24
|
+
config_path: Path
|
|
25
|
+
interrupted: bool = False
|
|
26
|
+
loaded: config.Config | None = None
|
|
27
|
+
# Read once per account for the dry runs of a run, as none of them spends
|
|
28
|
+
balances: dict[str, Balance] = field(default_factory=dict)
|
|
29
|
+
|
|
30
|
+
def interrupt(self) -> None:
|
|
31
|
+
self.interrupted = True
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
@dataclass
|
|
35
|
+
class Traffic:
|
|
36
|
+
"""Requests put on the wire by every client of a run, which a batch paces."""
|
|
37
|
+
|
|
38
|
+
# When the last request was sent, by the monotonic clock
|
|
39
|
+
last: float | None = None
|
|
40
|
+
# Seconds the next request waits after the last one; spent once, so a job's own requests do not wait
|
|
41
|
+
wait: float = 0.0
|
|
42
|
+
|
|
43
|
+
def before_send(self) -> None:
|
|
44
|
+
if self.last is not None and self.wait > 0:
|
|
45
|
+
time.sleep(max(0.0, self.wait - (time.monotonic() - self.last)))
|
|
46
|
+
self.wait = 0.0
|
|
47
|
+
self.last = time.monotonic()
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
@dataclass
|
|
51
|
+
class Context:
|
|
52
|
+
shared: _Shared
|
|
53
|
+
# From the front end: stdin read once, lines printed as they come, jobs run and parsed in process
|
|
54
|
+
read_stdin: Callable[[], str]
|
|
55
|
+
emit: Callable[[dict[str, Any]], None]
|
|
56
|
+
run_job: Callable[[list[str], "Context"], Result]
|
|
57
|
+
parse_job: Callable[[list[str]], dict[str, Any]]
|
|
58
|
+
now: Callable[[], datetime] = datetime.now
|
|
59
|
+
# As typed, for the ledger
|
|
60
|
+
argv: tuple[str, ...] = ()
|
|
61
|
+
# A job of a batch or grid dry run
|
|
62
|
+
dry_run: bool = False
|
|
63
|
+
# Run as a job of a batch or grid, which prints one line for it
|
|
64
|
+
in_job: bool = False
|
|
65
|
+
# --account; otherwise the one chosen with nai account use
|
|
66
|
+
account: str | None = None
|
|
67
|
+
traffic: Traffic = field(default_factory=Traffic)
|
|
68
|
+
_client: api.Client | None = field(default=None, init=False)
|
|
69
|
+
|
|
70
|
+
@classmethod
|
|
71
|
+
def create(
|
|
72
|
+
cls,
|
|
73
|
+
config_path: Path,
|
|
74
|
+
read_stdin: Callable[[], str],
|
|
75
|
+
emit: Callable[[dict[str, Any]], None],
|
|
76
|
+
run_job: Callable[[list[str], "Context"], Result],
|
|
77
|
+
parse_job: Callable[[list[str]], dict[str, Any]],
|
|
78
|
+
argv: tuple[str, ...],
|
|
79
|
+
account: str | None = None,
|
|
80
|
+
) -> "Context":
|
|
81
|
+
return cls(_Shared(config_path), read_stdin, emit, run_job, parse_job, argv=argv, account=account)
|
|
82
|
+
|
|
83
|
+
@property
|
|
84
|
+
def config(self) -> config.Config:
|
|
85
|
+
if self.shared.loaded is None:
|
|
86
|
+
self.shared.loaded = config.load(self.shared.config_path)
|
|
87
|
+
return self.shared.loaded
|
|
88
|
+
|
|
89
|
+
def forget_config(self) -> None:
|
|
90
|
+
"""After the config file is written, so later jobs of a batch read it again."""
|
|
91
|
+
self.shared.loaded = None
|
|
92
|
+
|
|
93
|
+
def refusal(
|
|
94
|
+
self, price: int, account: api.Account | None, yes: bool, cost_json: dict[str, Any]
|
|
95
|
+
) -> dict[str, Any] | None:
|
|
96
|
+
"""The refusal the real run would meet before sending."""
|
|
97
|
+
refused = charge.refusal(price, account, self.config, yes, cost_json)
|
|
98
|
+
if self.dry_run and refused:
|
|
99
|
+
raise errors.NaiError(refused["code"], refused["message"], dry_run=True, cost=cost_json)
|
|
100
|
+
return refused
|
|
101
|
+
|
|
102
|
+
@cached_property
|
|
103
|
+
def account_name(self) -> str:
|
|
104
|
+
"""--account, else the one chosen with nai account use."""
|
|
105
|
+
return self.account or auth.current()
|
|
106
|
+
|
|
107
|
+
def charged(self, warnings: list[str], **entry: Any) -> AbstractContextManager[charge.Charge]:
|
|
108
|
+
"""Paid requests under the lock, written to the ledger with the account and the command line."""
|
|
109
|
+
client = self.client()
|
|
110
|
+
entry = {**entry, "account": self.account_name, "argv": list(self.argv)}
|
|
111
|
+
return charge.charged(client, warnings, entry, self.now, self.shared.interrupt)
|
|
112
|
+
|
|
113
|
+
def client(self) -> api.Client:
|
|
114
|
+
"""Created on first use; a command without a token fails here, before anything is paid for."""
|
|
115
|
+
if self._client is None:
|
|
116
|
+
name = self._known_account()
|
|
117
|
+
self._client = self._new_client(name, renew=not self.dry_run)
|
|
118
|
+
return self._client
|
|
119
|
+
|
|
120
|
+
def balance(self) -> api.Account:
|
|
121
|
+
"""The account's balance; a dry run reads it without renewing a login, as it writes nothing."""
|
|
122
|
+
if not self.dry_run:
|
|
123
|
+
return self.client().account()
|
|
124
|
+
account, failed = self._balance(self._known_account())
|
|
125
|
+
if failed is not None:
|
|
126
|
+
raise failed
|
|
127
|
+
# No token: the client fails as the real run would
|
|
128
|
+
return account or self.client().account()
|
|
129
|
+
|
|
130
|
+
def dry_account(self, warnings: list[str]) -> api.Account | None:
|
|
131
|
+
"""For a command that sends nothing paid: the balance, read without renewing a login, or None to price as
|
|
132
|
+
Opus when there is no token or it cannot be read."""
|
|
133
|
+
# An account the real run would not find fails it before anything is priced
|
|
134
|
+
account, failed = self._balance(self._known_account())
|
|
135
|
+
if failed is not None:
|
|
136
|
+
warnings.append(f"the balance could not be read, so the price assumes Opus: {failed.message}")
|
|
137
|
+
return account
|
|
138
|
+
|
|
139
|
+
def _balance(self, name: str) -> Balance:
|
|
140
|
+
if not auth.has_token(self.config, name):
|
|
141
|
+
return None, None
|
|
142
|
+
if not self.dry_run:
|
|
143
|
+
return self._read_balance(name)
|
|
144
|
+
if name not in self.shared.balances:
|
|
145
|
+
self.shared.balances[name] = self._read_balance(name)
|
|
146
|
+
return self.shared.balances[name]
|
|
147
|
+
|
|
148
|
+
def _known_account(self) -> str:
|
|
149
|
+
name = self.account_name
|
|
150
|
+
if name not in auth.names(self.config):
|
|
151
|
+
raise errors.NaiError(errors.BAD_ARGS, f"no account named {name}", choices=auth.names(self.config))
|
|
152
|
+
return name
|
|
153
|
+
|
|
154
|
+
def _new_client(self, name: str, renew: bool) -> api.Client:
|
|
155
|
+
found = auth.credentials(self.config, name, self.now(), renew=renew)
|
|
156
|
+
return api.Client(found.token, found.endpoint, before_send=self.traffic.before_send)
|
|
157
|
+
|
|
158
|
+
def _read_balance(self, name: str) -> Balance:
|
|
159
|
+
try:
|
|
160
|
+
client = self._new_client(name, renew=False)
|
|
161
|
+
try:
|
|
162
|
+
return client.account(), None
|
|
163
|
+
finally:
|
|
164
|
+
with suppress(Exception):
|
|
165
|
+
client.close()
|
|
166
|
+
except errors.NaiError as e:
|
|
167
|
+
return None, e
|
|
168
|
+
|
|
169
|
+
def with_account(self, account: str) -> "Context":
|
|
170
|
+
"""A job that names its own account, with a client of its own."""
|
|
171
|
+
return replace(self, account=account)
|
|
172
|
+
|
|
173
|
+
def job(self, argv: list[str], dry_run: bool) -> "Context":
|
|
174
|
+
# A job's standard input and output lines belong to the batch
|
|
175
|
+
return replace(
|
|
176
|
+
self,
|
|
177
|
+
read_stdin=self._refuse_stdin,
|
|
178
|
+
emit=self._refuse_lines,
|
|
179
|
+
argv=tuple(argv),
|
|
180
|
+
dry_run=dry_run,
|
|
181
|
+
in_job=True,
|
|
182
|
+
)
|
|
183
|
+
|
|
184
|
+
def close(self) -> None:
|
|
185
|
+
if self._client is not None:
|
|
186
|
+
client, self._client = self._client, None
|
|
187
|
+
# Closing runs after the result is known, which a failure here must not replace
|
|
188
|
+
try:
|
|
189
|
+
client.close()
|
|
190
|
+
except KeyboardInterrupt:
|
|
191
|
+
self.shared.interrupt()
|
|
192
|
+
except Exception:
|
|
193
|
+
pass
|
|
194
|
+
|
|
195
|
+
@staticmethod
|
|
196
|
+
def _refuse_lines(line: dict[str, Any]) -> None:
|
|
197
|
+
raise errors.NaiError(errors.BAD_ARGS, LINES_IN_JOB)
|
|
198
|
+
|
|
199
|
+
@staticmethod
|
|
200
|
+
def _refuse_stdin() -> str:
|
|
201
|
+
raise errors.NaiError(errors.BAD_ARGS, "standard input is already in use, so @- cannot read it")
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
"""Sharing copies named after their source with a label, written all or none."""
|
|
2
|
+
|
|
3
|
+
from collections.abc import Callable
|
|
4
|
+
from dataclasses import dataclass, replace
|
|
5
|
+
from datetime import datetime
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
from PIL import Image
|
|
10
|
+
|
|
11
|
+
from nai_cli import decode, errors, fs, images, meta, naming, values
|
|
12
|
+
from nai_cli.commands import output
|
|
13
|
+
from nai_cli.commands.context import Context
|
|
14
|
+
from nai_cli.commands.output import IMAGE_SUFFIXES
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
@dataclass(frozen=True)
|
|
18
|
+
class Targets:
|
|
19
|
+
images: tuple[Path, ...]
|
|
20
|
+
# A .png name for one image; taken, it gets the next number, as every name does
|
|
21
|
+
exact: Path | None
|
|
22
|
+
folder: Path | None
|
|
23
|
+
# Without -o: the output folder and its folder rule, as generated images are filed
|
|
24
|
+
filed: naming.Destination
|
|
25
|
+
when: datetime
|
|
26
|
+
dry_run: bool
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def resolve(opts: dict[str, Any], ctx: Context) -> Targets:
|
|
30
|
+
sources = tuple(map(Path, opts["images"]))
|
|
31
|
+
given = values.path(opts["output"]) if opts.get("output") else None
|
|
32
|
+
exact = folder = None
|
|
33
|
+
if given is not None and given.suffix.lower() == ".png":
|
|
34
|
+
if len(sources) > 1:
|
|
35
|
+
raise errors.NaiError(errors.BAD_ARGS, "-o names one file; give a folder for several images")
|
|
36
|
+
exact = given
|
|
37
|
+
elif given is not None:
|
|
38
|
+
if given.suffix.lower() in IMAGE_SUFFIXES and not fs.is_folder(given):
|
|
39
|
+
raise errors.NaiError(errors.BAD_ARGS, f"copies are saved as PNG; give -o a .png name or a folder: {given}")
|
|
40
|
+
folder = given
|
|
41
|
+
return Targets(sources, exact, folder, output.filed(ctx.config), ctx.now(), ctx.dry_run)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def write(t: Targets, label: str, make: Callable[[Path], bytes], warnings: list[str] | None = None) -> dict[str, Any]:
|
|
45
|
+
"""NAME_label.png in the output folder, or NAME.png in the folder given; warnings are those make adds."""
|
|
46
|
+
# Every image is made before anything is written, so a bad one leaves no partial set
|
|
47
|
+
copies = [make(source) for source in t.images]
|
|
48
|
+
targets = [_target(t, source, label, data) for source, data in zip(t.images, copies, strict=True)]
|
|
49
|
+
noted = {"warnings": warnings} if warnings else {}
|
|
50
|
+
if t.dry_run:
|
|
51
|
+
for target in targets:
|
|
52
|
+
output.check_folder(target.parent, target if target == t.exact else None)
|
|
53
|
+
return {"ok": True, "dry_run": True, "files": _listing(t.images, _free_names(targets)), **noted}
|
|
54
|
+
written: list[Path] = []
|
|
55
|
+
try:
|
|
56
|
+
for target, data in zip(targets, copies, strict=True):
|
|
57
|
+
written.append(fs.create_numbered(target, data))
|
|
58
|
+
except BaseException as e:
|
|
59
|
+
for path in written:
|
|
60
|
+
fs.remove(path)
|
|
61
|
+
if isinstance(e, OSError | ValueError):
|
|
62
|
+
raise errors.NaiError(errors.BAD_ARGS, f"cannot write {target}: {e}") from e
|
|
63
|
+
raise
|
|
64
|
+
return {"ok": True, "files": _listing(t.images, written), **noted}
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def _free_names(targets: list[Path]) -> list[Path]:
|
|
68
|
+
"""The names the copies would be written under, each after those before it."""
|
|
69
|
+
taken: set[Path] = set()
|
|
70
|
+
out = []
|
|
71
|
+
for target in targets:
|
|
72
|
+
path = fs.free_name(target, taken)
|
|
73
|
+
taken.add(path)
|
|
74
|
+
out.append(path)
|
|
75
|
+
return out
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def _target(t: Targets, source: Path, label: str, data: bytes) -> Path:
|
|
79
|
+
if t.exact is not None:
|
|
80
|
+
return t.exact
|
|
81
|
+
if t.folder is not None:
|
|
82
|
+
return t.folder / f"{naming.fit(source.stem)}.png"
|
|
83
|
+
return t.filed.folder_for(_kept(data, t.when)) / f"{naming.fit(source.stem, '_' + label)}.png"
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def _kept(data: bytes, when: datetime) -> naming.Meta:
|
|
87
|
+
# Filed by the settings the copy keeps, so one without them is filed by date and size alone
|
|
88
|
+
width, height = decode.size_of_bytes(data)
|
|
89
|
+
try:
|
|
90
|
+
record = meta.parse_raw(meta.raw_of(data, "the copy"))
|
|
91
|
+
except errors.NaiError:
|
|
92
|
+
return naming.Meta(date=when, width=width, height=height)
|
|
93
|
+
return replace(output.recorded(record, ""), date=when, width=width, height=height)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def _listing(sources: tuple[Path, ...], files: list[Path]) -> list[dict[str, str]]:
|
|
97
|
+
return [{"source": str(a), "file": str(b)} for a, b in zip(sources, files, strict=True)]
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def color(text: str | None, default: tuple[int, int, int]) -> tuple[int, int, int]:
|
|
101
|
+
if text is None:
|
|
102
|
+
return default
|
|
103
|
+
value = text.strip().removeprefix("#")
|
|
104
|
+
if len(value) != 6 or any(c not in "0123456789abcdefABCDEF" for c in value):
|
|
105
|
+
raise errors.NaiError(errors.BAD_ARGS, f"a colour is #RRGGBB: {text}")
|
|
106
|
+
return int(value[0:2], 16), int(value[2:4], 16), int(value[4:6], 16)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def finish(source: Path, edited: Image.Image, keep_metadata: bool, warnings: list[str]) -> bytes:
|
|
110
|
+
"""The edited pixels as a bare PNG; with keep_metadata, the source's PNG text is carried over as it is. Settings
|
|
111
|
+
it keeps in EXIF or in the alpha channel, which the edit loses, are written as text in place of PNG text of the
|
|
112
|
+
same keywords, so the copy reads back as the source did."""
|
|
113
|
+
out = images.strip_metadata(edited)
|
|
114
|
+
if not keep_metadata:
|
|
115
|
+
return out
|
|
116
|
+
data = source.read_bytes()
|
|
117
|
+
try:
|
|
118
|
+
settings = meta.raw_of(data, source.name)
|
|
119
|
+
except errors.NaiError:
|
|
120
|
+
settings = {}
|
|
121
|
+
if not settings or "Comment" in meta.png_text_chunks(data):
|
|
122
|
+
# The settings are the PNG text itself, or there are none
|
|
123
|
+
return images.copy_text_chunks(data, out)
|
|
124
|
+
out = images.copy_text_chunks(data, out, leaving=set(settings))
|
|
125
|
+
return images.add_text(out, _writable(settings, source.name, warnings))
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def _writable(settings: dict[str, str], name: str, warnings: list[str]) -> dict[str, str]:
|
|
129
|
+
# EXIF and alpha-channel settings may hold any key, which PNG text cannot always carry
|
|
130
|
+
out = {}
|
|
131
|
+
for key, text in settings.items():
|
|
132
|
+
try:
|
|
133
|
+
decode.itxt(key, text)
|
|
134
|
+
except ValueError as e:
|
|
135
|
+
warnings.append(f"{name}: {e}, so the copy leaves it out")
|
|
136
|
+
continue
|
|
137
|
+
out[key] = text
|
|
138
|
+
return out
|
nai_cli/commands/cost.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"""cost: the Anlas a request would cost, priced against the account when a token is set."""
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
from typing import Any
|
|
5
|
+
|
|
6
|
+
from nai_cli import cost, errors, models, presets
|
|
7
|
+
from nai_cli import request as R
|
|
8
|
+
from nai_cli.charge import cost_json
|
|
9
|
+
from nai_cli.commands.context import Context
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@dataclass(frozen=True)
|
|
13
|
+
class CostSettings:
|
|
14
|
+
model: models.Model
|
|
15
|
+
action: str
|
|
16
|
+
size: tuple[int, int]
|
|
17
|
+
steps: int
|
|
18
|
+
n: int
|
|
19
|
+
strength: float
|
|
20
|
+
vibes: int
|
|
21
|
+
encode: int
|
|
22
|
+
refs: int
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def resolve(opts: dict[str, Any], ctx: Context) -> CostSettings:
|
|
26
|
+
d = ctx.config.defaults
|
|
27
|
+
action = "infill" if opts.get("inpaint") else "generate"
|
|
28
|
+
picked = models.resolve(opts.get("model") or d.get("model"))
|
|
29
|
+
chosen = models.with_effort(picked, opts.get("effort"), d.get("effort"))
|
|
30
|
+
refs, vibes, encode = opts.get("refs") or 0, opts.get("vibes") or 0, opts.get("encode") or 0
|
|
31
|
+
if vibes < 0 or refs < 0 or not 0 <= encode <= vibes:
|
|
32
|
+
raise errors.NaiError(
|
|
33
|
+
errors.BAD_ARGS, "--vibes and --refs cannot be negative, and --encode must be within 0 and --vibes"
|
|
34
|
+
)
|
|
35
|
+
R.check_references(chosen, vibes, refs)
|
|
36
|
+
model = models.for_action(chosen, action)
|
|
37
|
+
w, h = presets.resolve_size(opts.get("size") or d.get("size"))
|
|
38
|
+
n = opts["n"] if opts.get("n") is not None else d.get("n", 1)
|
|
39
|
+
limit = presets.max_samples(w, h)
|
|
40
|
+
if not 1 <= n <= limit:
|
|
41
|
+
raise errors.NaiError(errors.BAD_ARGS, f"{w}x{h} allows 1 to {limit} images per request")
|
|
42
|
+
steps = opts["steps"] if opts.get("steps") is not None else d.get("steps", model.default_steps)
|
|
43
|
+
R.check_range("steps", steps, (1, models.MAX_STEPS))
|
|
44
|
+
strength = opts["strength"] if opts.get("strength") is not None else 1.0
|
|
45
|
+
# img2img strength or inpaint strength, whichever is the wider
|
|
46
|
+
R.check_range("strength", strength, models.INPAINT_STRENGTH_RANGE)
|
|
47
|
+
if not R.vibes_sent(action, refs):
|
|
48
|
+
vibes = encode = 0
|
|
49
|
+
return CostSettings(model, action, (w, h), steps, n, strength, vibes, encode, refs)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def run(s: CostSettings, ctx: Context) -> dict[str, Any]:
|
|
53
|
+
warnings: list[str] = []
|
|
54
|
+
account = ctx.dry_account(warnings)
|
|
55
|
+
est = cost.generation(
|
|
56
|
+
s.model,
|
|
57
|
+
*s.size,
|
|
58
|
+
s.steps,
|
|
59
|
+
n_samples=s.n,
|
|
60
|
+
strength=s.strength,
|
|
61
|
+
opus=account.opus if account else True,
|
|
62
|
+
quota_exhausted=account.quota_negative if account else False,
|
|
63
|
+
precise_references=s.refs,
|
|
64
|
+
vibes=s.vibes,
|
|
65
|
+
vibes_to_encode=s.encode,
|
|
66
|
+
)
|
|
67
|
+
cost.require_valid(est)
|
|
68
|
+
request_model = s.model.inpainting_id if s.action == "infill" else s.model.id
|
|
69
|
+
out = {"ok": True, "model": request_model, "size": "{}x{}".format(*s.size), "cost": cost_json(est, account)}
|
|
70
|
+
return {**out, "warnings": warnings} if warnings else out
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
"""gen, i2i, inpaint, enhance and variations: price the request, pay under the lock, save the images."""
|
|
2
|
+
|
|
3
|
+
import base64
|
|
4
|
+
from dataclasses import replace
|
|
5
|
+
from datetime import datetime
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
from nai_cli import api, caches, cost, images, inpaint, naming, values, vibe
|
|
9
|
+
from nai_cli import request as R
|
|
10
|
+
from nai_cli.charge import Charge, authorize, cost_json, keep_usable
|
|
11
|
+
from nai_cli.commands import output
|
|
12
|
+
from nai_cli.commands.context import Context
|
|
13
|
+
from nai_cli.commands.generation import Enhanced, GenSettings, Repaint
|
|
14
|
+
from nai_cli.commands.spec import Prepared, attach_vibes, prepare
|
|
15
|
+
|
|
16
|
+
# The label the official client gives each kind of image, which {type} names; plain generation has none
|
|
17
|
+
_TYPE_LABELS = {"i2i": "image2image", "inpaint": "inpaint", "enhance": "enhanced", "variations": "variations"}
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def run(s: GenSettings, ctx: Context) -> dict[str, Any]:
|
|
21
|
+
prep = prepare(s)
|
|
22
|
+
built = prep.built
|
|
23
|
+
preview = _preview(s, prep)
|
|
24
|
+
# A run that sends nothing paid is priced here; a real one against the balance under the lock
|
|
25
|
+
dry_warnings: list[str] = []
|
|
26
|
+
account = ctx.dry_account(dry_warnings) if s.dry_run or preview else None
|
|
27
|
+
estimate = _price(s, prep, account, len(prep.pending_encodes))
|
|
28
|
+
result = _result(s, prep, estimate, account)
|
|
29
|
+
result["warnings"] += dry_warnings
|
|
30
|
+
# A preview is refused on the same grounds as the request it previews
|
|
31
|
+
cost.require_valid(estimate)
|
|
32
|
+
if preview is not None and not s.dry_run:
|
|
33
|
+
return _save_preview(s, ctx, preview, built, result)
|
|
34
|
+
if s.dry_run:
|
|
35
|
+
return _dry_run(s, ctx, prep, estimate, account, result)
|
|
36
|
+
# Fails without a token before any folder is made
|
|
37
|
+
ctx.client()
|
|
38
|
+
output.ready(s.output)
|
|
39
|
+
with ctx.charged(result["warnings"], command=s.action, model=built.body["model"], seed=built.seed) as charge:
|
|
40
|
+
estimate, built = _pay_vibes(s, ctx, prep, charge, result)
|
|
41
|
+
files, raw_files = _generate(s, ctx, prep, built, charge, result["warnings"])
|
|
42
|
+
result["images"] = files
|
|
43
|
+
if raw_files:
|
|
44
|
+
result["raw_images"] = raw_files
|
|
45
|
+
result["cost"] = cost_json(estimate, None, charge)
|
|
46
|
+
if charge.client.retries:
|
|
47
|
+
result["retries"] = charge.client.retries
|
|
48
|
+
return result
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def _dry_run(
|
|
52
|
+
s: GenSettings,
|
|
53
|
+
ctx: Context,
|
|
54
|
+
prep: Prepared,
|
|
55
|
+
estimate: cost.Estimate,
|
|
56
|
+
account: api.Account | None,
|
|
57
|
+
result: dict[str, Any],
|
|
58
|
+
) -> dict[str, Any]:
|
|
59
|
+
"""The request priced and checked as the real run checks it before sending, with the refusal it would meet."""
|
|
60
|
+
output.check(s.output)
|
|
61
|
+
result["dry_run"] = True
|
|
62
|
+
if _preview(s, prep) is not None:
|
|
63
|
+
# A preview sends nothing paid, so no limit refuses it and a batch adds nothing for it
|
|
64
|
+
result["preview"] = None
|
|
65
|
+
return result
|
|
66
|
+
refused = ctx.refusal(estimate.anlas, account, s.yes, result["cost"])
|
|
67
|
+
if refused:
|
|
68
|
+
result["refused"] = refused
|
|
69
|
+
return result
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def _preview(s: GenSettings, prep: Prepared) -> inpaint.Job | None:
|
|
73
|
+
"""The repaint job to show instead of sending, when --preview asks for it."""
|
|
74
|
+
return prep.job if isinstance(s.canvas, Repaint) and s.canvas.area.preview else None
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def _price(s: GenSettings, prep: Prepared, account: api.Account | None, to_encode: int) -> cost.Estimate:
|
|
78
|
+
built, spec = prep.built, prep.spec
|
|
79
|
+
sent = built.body["parameters"]
|
|
80
|
+
billed_width, billed_height = (
|
|
81
|
+
s.canvas.plan.billed_size if isinstance(s.canvas, Enhanced) else (sent["width"], sent["height"])
|
|
82
|
+
)
|
|
83
|
+
return cost.generation(
|
|
84
|
+
built.model,
|
|
85
|
+
billed_width,
|
|
86
|
+
billed_height,
|
|
87
|
+
sent["steps"],
|
|
88
|
+
n_samples=spec.n_samples,
|
|
89
|
+
strength=_cost_strength(spec),
|
|
90
|
+
opus=account.opus if account else True,
|
|
91
|
+
quota_exhausted=account.quota_negative if account else False,
|
|
92
|
+
precise_references=len(spec.references),
|
|
93
|
+
vibes=len(spec.vibes) if R.vibes_used(spec) else 0,
|
|
94
|
+
vibes_to_encode=to_encode,
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def _cost_strength(spec: R.GenSpec) -> float:
|
|
99
|
+
# img2img is discounted by strength and inpainting by inpaint strength
|
|
100
|
+
if spec.action == "img2img":
|
|
101
|
+
return spec.strength
|
|
102
|
+
if spec.action == "infill":
|
|
103
|
+
return spec.inpaint_strength
|
|
104
|
+
return 1.0
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def _result(s: GenSettings, prep: Prepared, estimate: cost.Estimate, account: api.Account | None) -> dict[str, Any]:
|
|
108
|
+
built, spec = prep.built, prep.spec
|
|
109
|
+
result: dict[str, Any] = {
|
|
110
|
+
"ok": True,
|
|
111
|
+
"model": built.body["model"],
|
|
112
|
+
"action": s.action,
|
|
113
|
+
"seed": built.seed,
|
|
114
|
+
"size": f"{spec.width}x{spec.height}",
|
|
115
|
+
"prompt": built.prompt,
|
|
116
|
+
"negative": built.negative,
|
|
117
|
+
"characters": [{"prompt": c.prompt, "negative": c.negative} for c in built.characters],
|
|
118
|
+
"cost": cost_json(estimate, account),
|
|
119
|
+
"lint": [i.to_json() for i in prep.issues],
|
|
120
|
+
"tokens": prep.tokens.to_json(),
|
|
121
|
+
"warnings": prep.warnings,
|
|
122
|
+
}
|
|
123
|
+
if prep.job is not None:
|
|
124
|
+
result["inpaint"] = prep.job.to_json()
|
|
125
|
+
if s.show_body or s.dry_run:
|
|
126
|
+
result["body"] = redact(built.body)
|
|
127
|
+
if isinstance(s.canvas, Enhanced):
|
|
128
|
+
result["enhance"] = s.canvas.plan.to_json()
|
|
129
|
+
return result
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def redact(body: dict[str, Any]) -> dict[str, Any]:
|
|
133
|
+
"""The body as sent, with images replaced by their length."""
|
|
134
|
+
body = values.as_js(body)
|
|
135
|
+
params = dict(body["parameters"])
|
|
136
|
+
for key in ("image", "mask"):
|
|
137
|
+
if key in params:
|
|
138
|
+
params[key] = f"<{len(params[key])} base64 characters>"
|
|
139
|
+
for key in ("reference_image_multiple", "director_reference_images"):
|
|
140
|
+
if key in params:
|
|
141
|
+
params[key] = [f"<{len(v)} base64 characters>" for v in params[key]]
|
|
142
|
+
return {**body, "parameters": params}
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def _save_preview(
|
|
146
|
+
s: GenSettings, ctx: Context, job: inpaint.Job, built: R.Built, result: dict[str, Any]
|
|
147
|
+
) -> dict[str, Any]:
|
|
148
|
+
when = ctx.now()
|
|
149
|
+
picture = images.to_png(job.preview())
|
|
150
|
+
image = output.named(s.output, naming_meta(s, built), picture, when, seed=built.seed, index=0)
|
|
151
|
+
result["preview"] = output.save_unpaid(
|
|
152
|
+
picture, s.output.destination.image_path(image.meta, built.seed, suffix="_preview", name=image.name)
|
|
153
|
+
)
|
|
154
|
+
return result
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def _pay_vibes(
|
|
158
|
+
s: GenSettings, ctx: Context, prep: Prepared, charge: Charge, result: dict[str, Any]
|
|
159
|
+
) -> tuple[cost.Estimate, R.Built]:
|
|
160
|
+
"""Price against the balance read under the lock, then encode the Vibes still missing."""
|
|
161
|
+
spec, built = prep.spec, prep.built
|
|
162
|
+
# Another process may have encoded the same Vibes while this one waited
|
|
163
|
+
pending = attach_vibes(spec, prep.vibe_sources)
|
|
164
|
+
estimate = _price(s, prep, charge.before, len(pending))
|
|
165
|
+
authorize(estimate.anlas, spec.n_samples, charge, ctx.config, s.yes, cost_json(estimate, charge.before))
|
|
166
|
+
fresh = {}
|
|
167
|
+
for encode in pending:
|
|
168
|
+
source = encode.source
|
|
169
|
+
encoding = charge.pay(
|
|
170
|
+
charge.client.encode_vibe,
|
|
171
|
+
base64.b64decode(source.image_b64 or ""),
|
|
172
|
+
spec.model.id,
|
|
173
|
+
source.information_extracted,
|
|
174
|
+
source.mask,
|
|
175
|
+
)
|
|
176
|
+
fresh.update(dict.fromkeys(encode.positions, encoding))
|
|
177
|
+
remember(source, spec.model.id, encoding, result["warnings"])
|
|
178
|
+
if prep.vibe_sources:
|
|
179
|
+
attach_vibes(spec, prep.vibe_sources, fresh)
|
|
180
|
+
built = R.build(replace(spec, seed=built.seed))
|
|
181
|
+
if "body" in result:
|
|
182
|
+
result["body"] = redact(built.body)
|
|
183
|
+
return estimate, built
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
def remember(source: vibe.VibeSource, model: str, encoding: str, warnings: list[str]) -> None:
|
|
187
|
+
# The encoding is used from memory, so a cache that cannot be written only means paying again next time
|
|
188
|
+
try:
|
|
189
|
+
caches.remember_vibe(source, model, encoding)
|
|
190
|
+
except OSError as e:
|
|
191
|
+
warnings.append(f"the Vibe encoding could not be cached, so encoding this image again costs Anlas: {e}")
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def _generate(
|
|
195
|
+
s: GenSettings,
|
|
196
|
+
ctx: Context,
|
|
197
|
+
prep: Prepared,
|
|
198
|
+
built: R.Built,
|
|
199
|
+
charge: Charge,
|
|
200
|
+
warnings: list[str],
|
|
201
|
+
) -> tuple[list[str], list[str]]:
|
|
202
|
+
def save(pictures: list[bytes | None]) -> tuple[list[str], list[str]]:
|
|
203
|
+
return _save_images(s, ctx, charge, pictures, built, prep.job, warnings)
|
|
204
|
+
|
|
205
|
+
try:
|
|
206
|
+
pictures = charge.pay(charge.client.generate, built.body)
|
|
207
|
+
except api.DamagedImages as e:
|
|
208
|
+
keep_usable(e, save, warnings)
|
|
209
|
+
return save(pictures)
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
def _save_images(
|
|
213
|
+
s: GenSettings,
|
|
214
|
+
ctx: Context,
|
|
215
|
+
charge: Charge,
|
|
216
|
+
pictures: list[bytes | None],
|
|
217
|
+
built: R.Built,
|
|
218
|
+
job: inpaint.Job | None,
|
|
219
|
+
warnings: list[str],
|
|
220
|
+
) -> tuple[list[str], list[str]]:
|
|
221
|
+
keep_raw = isinstance(s.canvas, Repaint) and s.canvas.area.keep_raw
|
|
222
|
+
named = naming_meta(s, built)
|
|
223
|
+
files, raw_files = [], []
|
|
224
|
+
for i, data in enumerate(pictures):
|
|
225
|
+
# Damaged in the reply; the others keep their own numbers
|
|
226
|
+
if data is None:
|
|
227
|
+
continue
|
|
228
|
+
result = data
|
|
229
|
+
if job is not None:
|
|
230
|
+
try:
|
|
231
|
+
result = job.paste(data)
|
|
232
|
+
except Exception as e:
|
|
233
|
+
# Anlas is already spent, so keep the server image even if compositing fails
|
|
234
|
+
warnings.append(
|
|
235
|
+
f"compositing failed, saved the image as returned by the server: {type(e).__name__}: {e}"
|
|
236
|
+
)
|
|
237
|
+
when: datetime = ctx.now()
|
|
238
|
+
# The server gives each image of a batch the next seed; sizes are the server's, as the official names use
|
|
239
|
+
image = output.named(s.output, named, data, when, seed=built.seed + i, index=i)
|
|
240
|
+
files.append(output.save_image(charge.keep, result, s.output, image, warnings, built.seed, i))
|
|
241
|
+
if job is not None and keep_raw:
|
|
242
|
+
raw_files.append(output.save_image(charge.keep, data, s.output, image, warnings, built.seed, i, "_raw"))
|
|
243
|
+
return files, raw_files
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
def naming_meta(s: GenSettings, built: R.Built) -> naming.Meta:
|
|
247
|
+
"""{prompt} is the prompt as written, without the fixed tags or tidying; the settings are those sent."""
|
|
248
|
+
params = built.body["parameters"]
|
|
249
|
+
return naming.Meta(
|
|
250
|
+
prompt=s.prompt.value,
|
|
251
|
+
model=s.model.id,
|
|
252
|
+
steps=params.get("steps"),
|
|
253
|
+
cfg=params.get("scale"),
|
|
254
|
+
sampler=params.get("sampler", ""),
|
|
255
|
+
type=_TYPE_LABELS.get(s.command, ""),
|
|
256
|
+
characters=tuple(naming.Character(c.prompt.value) for c in s.characters),
|
|
257
|
+
)
|