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,71 @@
|
|
|
1
|
+
"""Text typed for an option: @FILE reads a file, @- standard input and @@ a literal @."""
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
from typing import Any
|
|
5
|
+
|
|
6
|
+
from nai_cli import errors, fs, values
|
|
7
|
+
from nai_cli.commands.context import Context
|
|
8
|
+
|
|
9
|
+
STDIN = "@-"
|
|
10
|
+
# A path separator or a file extension, and no comma as tag lists have
|
|
11
|
+
_LOOKS_LIKE_PATH = re.compile(r"^[^,]*(?:[\\/]|\.[A-Za-z]\w*$)[^,]*$")
|
|
12
|
+
# Starts the way only a path does, so a comma later on does not matter
|
|
13
|
+
_PATH_START = re.compile(r"^(?:[A-Za-z]:[\\/]|\.{1,2}[\\/]|~[\\/]|[\\/])")
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def read_typed(opts: dict[str, Any], names: tuple[str, ...], ctx: Context) -> dict[str, Any]:
|
|
17
|
+
"""The named options with their text as meant: a single value or a list for repeated options, None when not
|
|
18
|
+
given. Only one of them may read standard input."""
|
|
19
|
+
readers = [(name, i) for name in names for i, value in enumerate(_values(opts.get(name))) if value == STDIN]
|
|
20
|
+
if len(readers) > 1:
|
|
21
|
+
raise errors.NaiError(errors.BAD_ARGS, "only one option can read standard input with @-")
|
|
22
|
+
# Standard input is taken as it is, so a leading @ there reads no file
|
|
23
|
+
piped = ctx.read_stdin().strip() if readers else None
|
|
24
|
+
out: dict[str, Any] = {}
|
|
25
|
+
for name in names:
|
|
26
|
+
value = opts.get(name)
|
|
27
|
+
if isinstance(value, list):
|
|
28
|
+
out[name] = [_sendable(piped if item == STDIN else text_arg(item)) for item in value]
|
|
29
|
+
elif value is not None:
|
|
30
|
+
out[name] = _sendable(piped if value == STDIN else text_arg(value))
|
|
31
|
+
else:
|
|
32
|
+
out[name] = None
|
|
33
|
+
return out
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _sendable(text: str) -> str:
|
|
37
|
+
# Checked once read, since a file or standard input can hold what an argument cannot
|
|
38
|
+
reason = values.unsendable(text)
|
|
39
|
+
if reason is not None:
|
|
40
|
+
raise errors.NaiError(errors.BAD_ARGS, f"{reason} in {text!r}, which no request can carry")
|
|
41
|
+
return text
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def _values(value: Any) -> list[Any]:
|
|
45
|
+
return value if isinstance(value, list) else [value]
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def text_arg(value: str) -> str:
|
|
49
|
+
"""A value of @PATH is read from that file, and @@ stands for a literal @. Other text starting with @,
|
|
50
|
+
such as the tag @_@, is sent as typed unless it looks like a path."""
|
|
51
|
+
if not value.startswith("@"):
|
|
52
|
+
return value
|
|
53
|
+
if value.startswith("@@"):
|
|
54
|
+
return value[1:]
|
|
55
|
+
target = value[1:]
|
|
56
|
+
try:
|
|
57
|
+
path = values.path(target)
|
|
58
|
+
except (OSError, ValueError, RuntimeError):
|
|
59
|
+
path = None
|
|
60
|
+
is_file, exists = (fs.is_file(path), fs.exists(path)) if path is not None else (False, False)
|
|
61
|
+
if is_file:
|
|
62
|
+
try:
|
|
63
|
+
# Notepad saves UTF-8 with a byte order mark
|
|
64
|
+
return path.read_text(encoding="utf-8-sig").strip()
|
|
65
|
+
except (OSError, UnicodeDecodeError) as e:
|
|
66
|
+
raise errors.NaiError(errors.BAD_ARGS, f"cannot read {path}: {e}") from e
|
|
67
|
+
if target and (exists or _PATH_START.match(target) or _LOOKS_LIKE_PATH.match(target)):
|
|
68
|
+
raise errors.NaiError(
|
|
69
|
+
errors.BAD_ARGS, f"{value}: no such file to read the text from; write @@ to send text starting with @"
|
|
70
|
+
)
|
|
71
|
+
return value
|
nai_cli/commands/vibe.py
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
"""vibe: write Vibes to a .naiv4vibe or .naiv4vibebundle file, or into a PNG, encoding the images that need it."""
|
|
2
|
+
|
|
3
|
+
import base64
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
from nai_cli import caches, cost, decode, errors, fs, images, models, naming, values, vibe
|
|
9
|
+
from nai_cli.charge import authorize, price_json
|
|
10
|
+
from nai_cli.commands import output
|
|
11
|
+
from nai_cli.commands.context import Context
|
|
12
|
+
from nai_cli.commands.generate import remember
|
|
13
|
+
from nai_cli.commands.references import VibeRef, load_vibes, vibe_ref
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@dataclass(frozen=True)
|
|
17
|
+
class VibeSettings:
|
|
18
|
+
model: models.Model
|
|
19
|
+
sources: tuple[VibeRef, ...]
|
|
20
|
+
# The name given with -o, before fitting it to the file system
|
|
21
|
+
exact: Path | None
|
|
22
|
+
into: Path | None
|
|
23
|
+
keep_image: bool
|
|
24
|
+
dry_run: bool
|
|
25
|
+
yes: bool
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def resolve(opts: dict[str, Any], ctx: Context) -> VibeSettings:
|
|
29
|
+
model = models.resolve(opts.get("model") or "v4.5")
|
|
30
|
+
if not model.vibe:
|
|
31
|
+
raise errors.NaiError(errors.MODEL_UNSUPPORTED, f"{model.name} does not support Vibe", feature="vibe")
|
|
32
|
+
exact = values.path(opts["output"]) if opts.get("output") else None
|
|
33
|
+
return VibeSettings(
|
|
34
|
+
model=model,
|
|
35
|
+
sources=tuple(vibe_ref(v, opts.get("strength"), opts.get("info")) for v in opts["sources"]),
|
|
36
|
+
exact=exact,
|
|
37
|
+
into=Path(opts["into"]) if opts.get("into") else None,
|
|
38
|
+
keep_image=opts.get("keep_image") is not False,
|
|
39
|
+
dry_run=bool(opts.get("dry_run")) or ctx.dry_run,
|
|
40
|
+
yes=bool(opts.get("yes")),
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def run(s: VibeSettings, ctx: Context) -> dict[str, Any]:
|
|
45
|
+
sources = load_vibes(s.sources, s.model.id)
|
|
46
|
+
bundled = len(sources) > 1 or s.into is not None or str(s.exact or "").lower().endswith(".naiv4vibebundle")
|
|
47
|
+
target = _target(s, bundled, ctx)
|
|
48
|
+
# Made and tried before paying, as for images; a dry run only looks
|
|
49
|
+
(output.check_folder if s.dry_run else output.ready_folder)(target.parent, target if s.exact else None)
|
|
50
|
+
holder = _holding_png(s.into) if s.into else None
|
|
51
|
+
export = _Export(s.model.id, sources, [None] * len(sources), bundled, holder, not s.keep_image)
|
|
52
|
+
export.look_up()
|
|
53
|
+
# A damaged image fails here, before any other is paid for
|
|
54
|
+
for encode in export.encodes():
|
|
55
|
+
vibe.check_image(encode.source)
|
|
56
|
+
result: dict[str, Any] = {"ok": True, "file": str(target), "model": s.model.id, "vibes": len(sources)}
|
|
57
|
+
if s.dry_run:
|
|
58
|
+
warnings: list[str] = []
|
|
59
|
+
account = ctx.dry_account(warnings)
|
|
60
|
+
to_encode = len(export.encodes())
|
|
61
|
+
price = cost.VIBE_ENCODE_COST * to_encode
|
|
62
|
+
result.update(
|
|
63
|
+
file=str(fs.free_name(target)), cached=not to_encode, dry_run=True, cost=price_json(price, account)
|
|
64
|
+
)
|
|
65
|
+
if warnings:
|
|
66
|
+
result["warnings"] = warnings
|
|
67
|
+
refused = ctx.refusal(price, account, s.yes, result["cost"])
|
|
68
|
+
return {**result, "refused": refused} if refused else result
|
|
69
|
+
if not export.pending:
|
|
70
|
+
return _unpaid(result, export, target)
|
|
71
|
+
return _encode(s, ctx, export, target, result)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
@dataclass
|
|
75
|
+
class _Export:
|
|
76
|
+
"""The Vibes to write; encodings are filled in as they are found or paid for."""
|
|
77
|
+
|
|
78
|
+
model_id: str
|
|
79
|
+
sources: list[vibe.VibeSource]
|
|
80
|
+
found: list[str | None]
|
|
81
|
+
bundled: bool
|
|
82
|
+
holder: bytes | None
|
|
83
|
+
without_images: bool
|
|
84
|
+
|
|
85
|
+
@property
|
|
86
|
+
def pending(self) -> list[int]:
|
|
87
|
+
return [i for i, encoding in enumerate(self.found) if encoding is None]
|
|
88
|
+
|
|
89
|
+
def encodes(self) -> list[vibe.Encode]:
|
|
90
|
+
return vibe.encodes([(i, self.sources[i]) for i in self.pending], self.model_id)
|
|
91
|
+
|
|
92
|
+
def look_up(self) -> None:
|
|
93
|
+
# Encodings come from the files or the cache, so an image encoded before costs nothing again
|
|
94
|
+
for i in self.pending:
|
|
95
|
+
self.found[i] = caches.vibe_encoding(self.sources[i], self.model_id)
|
|
96
|
+
|
|
97
|
+
def content(self) -> str | bytes:
|
|
98
|
+
entries = [
|
|
99
|
+
vibe.export(source, self.model_id, self.found[i], self.without_images)
|
|
100
|
+
for i, source in enumerate(self.sources)
|
|
101
|
+
]
|
|
102
|
+
text = vibe.dumps(vibe.bundle(entries) if self.bundled else entries[0])
|
|
103
|
+
return vibe.embed(self.holder, text) if self.holder is not None else text
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def _unpaid(result: dict[str, Any], export: _Export, target: Path) -> dict[str, Any]:
|
|
107
|
+
file = output.save_unpaid(export.content(), target)
|
|
108
|
+
return {**result, "file": file, "cached": True, "cost": {"estimate": 0, "spent": 0}}
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def _encode(s: VibeSettings, ctx: Context, export: _Export, target: Path, result: dict[str, Any]) -> dict[str, Any]:
|
|
112
|
+
warnings: list[str] = []
|
|
113
|
+
with ctx.charged(warnings, command="vibe", output=str(target)) as charge:
|
|
114
|
+
client = charge.client
|
|
115
|
+
# Another process may have encoded the same images while this one waited
|
|
116
|
+
export.look_up()
|
|
117
|
+
if not export.pending:
|
|
118
|
+
return _unpaid(result, export, target)
|
|
119
|
+
encodes = export.encodes()
|
|
120
|
+
price = cost.VIBE_ENCODE_COST * len(encodes)
|
|
121
|
+
authorize(price, 0, charge, ctx.config, s.yes, price_json(price, charge.before))
|
|
122
|
+
for encode in encodes:
|
|
123
|
+
source = encode.source
|
|
124
|
+
image = base64.b64decode(source.image_b64 or "")
|
|
125
|
+
encoding = charge.pay(client.encode_vibe, image, s.model.id, source.information_extracted, source.mask)
|
|
126
|
+
for i in encode.positions:
|
|
127
|
+
export.found[i] = encoding
|
|
128
|
+
remember(source, s.model.id, encoding, warnings)
|
|
129
|
+
result["file"] = charge.keep(export.content(), target, image=False)
|
|
130
|
+
result.update(cached=False, cost=price_json(price, None, charge), warnings=warnings)
|
|
131
|
+
if client.retries:
|
|
132
|
+
result["retries"] = client.retries
|
|
133
|
+
return result
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def _target(s: VibeSettings, bundled: bool, ctx: Context) -> Path:
|
|
137
|
+
first = s.sources[0].path
|
|
138
|
+
# Cut to fit as an image's -o is, since a name refused on saving fails after paying
|
|
139
|
+
if s.exact is not None and s.exact.name in ("", ".."):
|
|
140
|
+
raise errors.NaiError(errors.BAD_ARGS, "-o needs a file name")
|
|
141
|
+
return naming.fitted(s.exact) if s.exact else naming.vibe_path(first, bundled, s.into, ctx.now())
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def _holding_png(path: Path) -> bytes:
|
|
145
|
+
"""Decoded in full before anything is paid for, so a damaged image fails here and not after encoding."""
|
|
146
|
+
img = images.open_image(path)
|
|
147
|
+
try:
|
|
148
|
+
data = path.read_bytes()
|
|
149
|
+
except OSError as e:
|
|
150
|
+
raise errors.NaiError(errors.BAD_ARGS, f"cannot read image {path}: {e}") from e
|
|
151
|
+
return data if data.startswith(decode.PNG_SIGNATURE) else images.to_png(img)
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"""watermark: copies of images with visible text or a logo, for sharing."""
|
|
2
|
+
|
|
3
|
+
import unicodedata
|
|
4
|
+
from collections.abc import Callable
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
from nai_cli import errors, images, share
|
|
10
|
+
from nai_cli.commands import copies
|
|
11
|
+
from nai_cli.commands.context import Context
|
|
12
|
+
|
|
13
|
+
# Drawn when neither --text nor --logo is given
|
|
14
|
+
DEFAULT_TEXT = "NovelAI"
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
@dataclass(frozen=True)
|
|
18
|
+
class WatermarkSettings:
|
|
19
|
+
targets: copies.Targets
|
|
20
|
+
mark: share.Watermark
|
|
21
|
+
keep_metadata: bool
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def resolve(opts: dict[str, Any], ctx: Context) -> WatermarkSettings:
|
|
25
|
+
logo = images.open_still(opts["logo"]) if opts.get("logo") else None
|
|
26
|
+
# A logo alone has no text unless asked for
|
|
27
|
+
text = opts.get("text") if opts.get("text") is not None else (None if logo else DEFAULT_TEXT)
|
|
28
|
+
defaults = share.Watermark(None)
|
|
29
|
+
# Composed, so a font draws an accented letter as one glyph and the check sees what is drawn
|
|
30
|
+
text = unicodedata.normalize("NFC", text.replace("\\n", "\n")) if text else None
|
|
31
|
+
font = Path(opts["font"]) if opts.get("font") else None
|
|
32
|
+
if text:
|
|
33
|
+
share.require_glyphs(text, font)
|
|
34
|
+
mark = share.Watermark(
|
|
35
|
+
text=text,
|
|
36
|
+
text_layer=_layer(opts, "", defaults.text_layer),
|
|
37
|
+
font=font,
|
|
38
|
+
color=copies.color(opts.get("color"), defaults.color),
|
|
39
|
+
logo=logo,
|
|
40
|
+
logo_layer=_layer(opts, "logo_", defaults.logo_layer),
|
|
41
|
+
auto_place=bool(opts.get("auto_place")),
|
|
42
|
+
auto_tone=bool(opts.get("auto_tone")),
|
|
43
|
+
)
|
|
44
|
+
return WatermarkSettings(copies.resolve(opts, ctx), mark, bool(opts.get("keep_metadata")))
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _layer(opts: dict[str, Any], prefix: str, default: share.Layer) -> share.Layer:
|
|
48
|
+
def value(key: str, valid: Callable[[float], bool]) -> float:
|
|
49
|
+
given = opts.get(prefix + key)
|
|
50
|
+
if given is None:
|
|
51
|
+
return float(getattr(default, key))
|
|
52
|
+
if not valid(given):
|
|
53
|
+
raise errors.NaiError(errors.BAD_ARGS, f"--{(prefix + key).replace('_', '-')} is out of range: {given}")
|
|
54
|
+
return float(given)
|
|
55
|
+
|
|
56
|
+
return share.Layer(
|
|
57
|
+
anchor=opts.get(prefix + "position") or default.anchor,
|
|
58
|
+
size=value("size", lambda v: 0 < v <= 1),
|
|
59
|
+
margin=value("margin", lambda v: 0 <= v < 0.5),
|
|
60
|
+
opacity=value("opacity", lambda v: 0 < v <= 1),
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def run(s: WatermarkSettings, ctx: Context) -> dict[str, Any]:
|
|
65
|
+
warnings: list[str] = []
|
|
66
|
+
|
|
67
|
+
def make(source: Path) -> bytes:
|
|
68
|
+
return copies.finish(source, share.watermark(images.open_still(source), s.mark), s.keep_metadata, warnings)
|
|
69
|
+
|
|
70
|
+
return copies.write(s.targets, "watermarked", make, warnings)
|
nai_cli/config.py
ADDED
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
"""The config file and the data directory: limits, defaults, accounts and local models."""
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
import sys
|
|
5
|
+
import tomllib
|
|
6
|
+
from dataclasses import dataclass, field
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
from typing import Any
|
|
9
|
+
|
|
10
|
+
from nai_cli import api, errors, fs, values
|
|
11
|
+
|
|
12
|
+
TOKEN_ENV = "NOVELAI_TOKEN"
|
|
13
|
+
# The account of novelai_token, which the [accounts] tables cannot take the name of
|
|
14
|
+
DEFAULT_ACCOUNT = "default"
|
|
15
|
+
# The longest wait between batch jobs, in seconds
|
|
16
|
+
MAX_INTERVAL = 86400
|
|
17
|
+
|
|
18
|
+
TEMPLATE = """\
|
|
19
|
+
# Persistent API token from NovelAI account settings (starts with pst-). NOVELAI_TOKEN takes precedence when set
|
|
20
|
+
novelai_token = ""
|
|
21
|
+
|
|
22
|
+
# More accounts, chosen with --account NAME or nai account use NAME. A relay that speaks the NovelAI API takes
|
|
23
|
+
# its own token and address; image_url defaults to api_url. Email logins are added with nai account login.
|
|
24
|
+
# [accounts.second]
|
|
25
|
+
# token = "pst-..."
|
|
26
|
+
# [accounts.relay]
|
|
27
|
+
# token = "..."
|
|
28
|
+
# api_url = "https://relay.example.com"
|
|
29
|
+
|
|
30
|
+
[output]
|
|
31
|
+
# Where images are saved; empty means images/ under the data directory
|
|
32
|
+
dir = ""
|
|
33
|
+
# Folders under dir in the same syntax as filename, such as "{year}/{month}" or "{model}"; empty saves in dir
|
|
34
|
+
folder = "{date}"
|
|
35
|
+
# File names in the official client's template syntax, such as "{prompt} s-{seed}"; empty keeps HHMMSS_seed.
|
|
36
|
+
# Tokens: prompt seed date time year month day model modelname resolution width height index id type characters
|
|
37
|
+
# steps cfg sampler
|
|
38
|
+
filename = ""
|
|
39
|
+
|
|
40
|
+
[cost]
|
|
41
|
+
# Requests estimated above this many Anlas return need_confirm and only run with --yes
|
|
42
|
+
max_per_request = 100
|
|
43
|
+
|
|
44
|
+
[batch]
|
|
45
|
+
# Seconds to wait between jobs that sent requests, or [min, max] for a random wait
|
|
46
|
+
interval = 0
|
|
47
|
+
|
|
48
|
+
[local]
|
|
49
|
+
# Models run on this computer need pip install "nai-cli[local]"; nothing is downloaded unless asked.
|
|
50
|
+
# The ONNX tagger nai interrogate uses, and its tag list; empty labels looks beside the model
|
|
51
|
+
tagger = ""
|
|
52
|
+
tagger_labels = ""
|
|
53
|
+
# wd14, cl, cl-v2, eva02 or pixai; empty guesses from the file names
|
|
54
|
+
tagger_kind = ""
|
|
55
|
+
# Where nai local download sam puts the model object: regions use; empty means models/sam under the data directory
|
|
56
|
+
sam_dir = ""
|
|
57
|
+
|
|
58
|
+
[defaults]
|
|
59
|
+
# Generation defaults for all models. Command-line options win; anything unset here uses the model's default
|
|
60
|
+
# model = "v5"
|
|
61
|
+
# V5 Full at "high" or "medium" effort when --effort is not given; the official client now defaults to medium
|
|
62
|
+
# effort = "high"
|
|
63
|
+
# size = "832x1216"
|
|
64
|
+
# steps = 28
|
|
65
|
+
# cfg = 4
|
|
66
|
+
# sampler = "k_euler_ancestral"
|
|
67
|
+
# cfg_rescale = 0
|
|
68
|
+
# noise_schedule = "karras"
|
|
69
|
+
# quality = "standard"
|
|
70
|
+
# uc_preset = "heavy"
|
|
71
|
+
# n = 1
|
|
72
|
+
# true follows the official client: prepend nsfw to the negative prompt unless the prompt mentions nsfw.
|
|
73
|
+
# false (default) never adds it, and removes nsfw from the negative prompt when the prompt mentions nsfw
|
|
74
|
+
official_nsfw = false
|
|
75
|
+
# Split ComfyUI multi-character prompts (COUPLE, AND with MASK or AREA, a character per | line) into characters.
|
|
76
|
+
# Off by default: a single | also divides official prompts
|
|
77
|
+
comfyui_characters = false
|
|
78
|
+
# Let V5 Full and V5 Curated take a noise schedule and Variety+, which the official client hides for V5
|
|
79
|
+
v5_hidden_options = false
|
|
80
|
+
# Transparent images (V5) use premultiplied alpha instead of straight alpha, the official client's other mode
|
|
81
|
+
premultiplied_alpha = false
|
|
82
|
+
# Tidy prompts before sending: full-width commas, extra whitespace, empty tags.
|
|
83
|
+
# Quoted text, text: segments, <markers> and kaomoji are left untouched
|
|
84
|
+
format_prompt = false
|
|
85
|
+
# Also replace underscores with spaces when tidying
|
|
86
|
+
format_underscores = false
|
|
87
|
+
# Insert a space before a closing :: that follows a number (1.2::year 2025:: -> 1.2::year 2025 ::);
|
|
88
|
+
# otherwise the number is read as a new weight
|
|
89
|
+
space_numeric_close = false
|
|
90
|
+
# Convert SD weights such as (tag:1.2) to 1.2::tag:: before sending
|
|
91
|
+
convert_sd_weights = false
|
|
92
|
+
# Fixed tags joined around every prompt typed for a run with ", "; prompts taken from an image are left as stored
|
|
93
|
+
prompt_prefix = ""
|
|
94
|
+
prompt_suffix = ""
|
|
95
|
+
negative_prefix = ""
|
|
96
|
+
negative_suffix = ""
|
|
97
|
+
"""
|
|
98
|
+
|
|
99
|
+
# One folder per day
|
|
100
|
+
DEFAULT_FOLDER = "{date}"
|
|
101
|
+
|
|
102
|
+
# Target type of each value; ranges are checked per model by the CLI
|
|
103
|
+
DEFAULT_KEYS: dict[str, type] = {
|
|
104
|
+
"model": str,
|
|
105
|
+
"effort": str,
|
|
106
|
+
"size": str,
|
|
107
|
+
"steps": int,
|
|
108
|
+
"cfg": float,
|
|
109
|
+
"sampler": str,
|
|
110
|
+
"cfg_rescale": float,
|
|
111
|
+
"noise_schedule": str,
|
|
112
|
+
"quality": str,
|
|
113
|
+
"uc_preset": str,
|
|
114
|
+
"n": int,
|
|
115
|
+
"official_nsfw": bool,
|
|
116
|
+
"v5_hidden_options": bool,
|
|
117
|
+
"comfyui_characters": bool,
|
|
118
|
+
"premultiplied_alpha": bool,
|
|
119
|
+
"format_prompt": bool,
|
|
120
|
+
"format_underscores": bool,
|
|
121
|
+
"space_numeric_close": bool,
|
|
122
|
+
"convert_sd_weights": bool,
|
|
123
|
+
"prompt_prefix": str,
|
|
124
|
+
"prompt_suffix": str,
|
|
125
|
+
"negative_prefix": str,
|
|
126
|
+
"negative_suffix": str,
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def config_dir() -> Path:
|
|
131
|
+
if sys.platform == "win32":
|
|
132
|
+
return _base("APPDATA", "AppData", "Roaming") / "nai-cli"
|
|
133
|
+
return _base("XDG_CONFIG_HOME", ".config") / "nai-cli"
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def data_dir() -> Path:
|
|
137
|
+
if sys.platform == "win32":
|
|
138
|
+
return _base("LOCALAPPDATA", "AppData", "Local") / "nai-cli"
|
|
139
|
+
return _base("XDG_DATA_HOME", ".local", "share") / "nai-cli"
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def _base(variable: str, *under_home: str) -> Path:
|
|
143
|
+
"""The folder a variable names, else the usual one under the home folder. An empty or relative value is
|
|
144
|
+
ignored, as the XDG specification says, and the home folder is only looked up when needed."""
|
|
145
|
+
value = os.environ.get(variable, "")
|
|
146
|
+
if value and Path(value).is_absolute():
|
|
147
|
+
return Path(value)
|
|
148
|
+
try:
|
|
149
|
+
return Path.home().joinpath(*under_home)
|
|
150
|
+
except RuntimeError as e:
|
|
151
|
+
raise errors.NaiError(
|
|
152
|
+
errors.CONFIG_MISSING, f"no home folder is known; set {variable} to the folder to use"
|
|
153
|
+
) from e
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
@dataclass(frozen=True)
|
|
157
|
+
class AccountConfig:
|
|
158
|
+
token: str
|
|
159
|
+
endpoint: api.Endpoint = api.OFFICIAL
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def default_sam_dir() -> Path:
|
|
163
|
+
return data_dir() / "models" / "sam"
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
@dataclass(frozen=True)
|
|
167
|
+
class LocalModels:
|
|
168
|
+
tagger: Path | None = None
|
|
169
|
+
tagger_labels: Path | None = None
|
|
170
|
+
tagger_kind: str = ""
|
|
171
|
+
sam_dir: Path = field(default_factory=default_sam_dir)
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
@dataclass(frozen=True)
|
|
175
|
+
class Config:
|
|
176
|
+
path: Path
|
|
177
|
+
novelai_token: str
|
|
178
|
+
token_source: str | None
|
|
179
|
+
output_dir: Path
|
|
180
|
+
max_per_request: int
|
|
181
|
+
# As written in the file; empty names files HHMMSS_seed
|
|
182
|
+
filename: str = ""
|
|
183
|
+
folder: str = DEFAULT_FOLDER
|
|
184
|
+
defaults: dict[str, Any] = field(default_factory=dict)
|
|
185
|
+
# Seconds between batch jobs that sent requests, drawn between the two
|
|
186
|
+
interval: tuple[float, float] = (0.0, 0.0)
|
|
187
|
+
accounts: dict[str, AccountConfig] = field(default_factory=dict)
|
|
188
|
+
local: LocalModels = field(default_factory=LocalModels)
|
|
189
|
+
|
|
190
|
+
def require_novelai(self) -> str:
|
|
191
|
+
if not self.novelai_token:
|
|
192
|
+
raise errors.NaiError(
|
|
193
|
+
errors.CONFIG_MISSING, f"no NovelAI token: set {TOKEN_ENV} or novelai_token in {self.path}"
|
|
194
|
+
)
|
|
195
|
+
return self.novelai_token
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
def default_path() -> Path:
|
|
199
|
+
return config_dir() / "config.toml"
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def _table(raw: dict[str, Any], key: str, path: Path) -> dict[str, Any]:
|
|
203
|
+
value = raw.get(key, {})
|
|
204
|
+
if not isinstance(value, dict):
|
|
205
|
+
raise errors.NaiError(errors.CONFIG_MISSING, f"{path}: [{key}] must be a table")
|
|
206
|
+
return value
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
def interval(pair: list[Any]) -> tuple[float, float]:
|
|
210
|
+
"""A wait as [seconds] twice or [min, max], each from 0 to a day."""
|
|
211
|
+
if len(pair) != 2 or not all(_seconds(v) for v in pair) or pair[0] > pair[1]:
|
|
212
|
+
raise ValueError(f"needs seconds up to {MAX_INTERVAL}, or [min, max] with min <= max: {pair!r}")
|
|
213
|
+
return float(pair[0]), float(pair[1])
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
def _seconds(value: Any) -> bool:
|
|
217
|
+
return values.is_number(value) and 0 <= value <= MAX_INTERVAL
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
def load(path: Path) -> Config:
|
|
221
|
+
raw: dict = {}
|
|
222
|
+
try:
|
|
223
|
+
# Notepad saves UTF-8 with a byte order mark
|
|
224
|
+
raw = tomllib.loads(path.read_text(encoding="utf-8-sig"))
|
|
225
|
+
except FileNotFoundError:
|
|
226
|
+
pass
|
|
227
|
+
except OSError as e:
|
|
228
|
+
raise errors.NaiError(errors.CONFIG_MISSING, f"{path}: cannot read: {e}") from e
|
|
229
|
+
except UnicodeDecodeError as e:
|
|
230
|
+
raise errors.NaiError(errors.CONFIG_MISSING, f"{path}: not UTF-8 text, so it cannot be read: {e}") from e
|
|
231
|
+
except tomllib.TOMLDecodeError as e:
|
|
232
|
+
raise errors.NaiError(errors.CONFIG_MISSING, f"{path}: cannot parse: {e}") from e
|
|
233
|
+
if _holds_nul(raw):
|
|
234
|
+
raise errors.NaiError(errors.CONFIG_MISSING, f"{path}: a value holds a NUL character")
|
|
235
|
+
env_token = os.environ.get(TOKEN_ENV, "").strip()
|
|
236
|
+
file_token = raw.get("novelai_token", "")
|
|
237
|
+
if not isinstance(file_token, str):
|
|
238
|
+
raise errors.NaiError(errors.CONFIG_MISSING, f"{path}: novelai_token must be a string")
|
|
239
|
+
file_token = file_token.strip()
|
|
240
|
+
return Config(
|
|
241
|
+
path=path,
|
|
242
|
+
novelai_token=env_token or file_token,
|
|
243
|
+
token_source="env" if env_token else ("config" if file_token else None),
|
|
244
|
+
output_dir=_output_dir(raw, path),
|
|
245
|
+
filename=_output_text(raw, path, "filename", ""),
|
|
246
|
+
folder=_output_text(raw, path, "folder", DEFAULT_FOLDER),
|
|
247
|
+
max_per_request=_limit(raw, path, "max_per_request", 100, " of Anlas"),
|
|
248
|
+
defaults=_defaults(raw, path),
|
|
249
|
+
interval=_interval(raw, path),
|
|
250
|
+
accounts=_accounts(raw, path),
|
|
251
|
+
local=_local(raw, path),
|
|
252
|
+
)
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
def _output_dir(raw: dict[str, Any], path: Path) -> Path:
|
|
256
|
+
value = _table(raw, "output", path).get("dir", "")
|
|
257
|
+
if not isinstance(value, str):
|
|
258
|
+
raise errors.NaiError(errors.CONFIG_MISSING, f"{path}: [output] dir must be a string: {value!r}")
|
|
259
|
+
return values.path(value or data_dir() / "images")
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
def _local(raw: dict[str, Any], path: Path) -> LocalModels:
|
|
263
|
+
table = _table(raw, "local", path)
|
|
264
|
+
given = {}
|
|
265
|
+
for key in ("tagger", "tagger_labels", "tagger_kind", "sam_dir"):
|
|
266
|
+
value = table.get(key, "")
|
|
267
|
+
if not isinstance(value, str):
|
|
268
|
+
raise errors.NaiError(errors.CONFIG_MISSING, f"{path}: [local] {key} must be a string: {value!r}")
|
|
269
|
+
given[key] = value.strip()
|
|
270
|
+
|
|
271
|
+
def path_of(key: str) -> Path | None:
|
|
272
|
+
return values.path(given[key]) if given[key] else None
|
|
273
|
+
|
|
274
|
+
return LocalModels(
|
|
275
|
+
tagger=path_of("tagger"),
|
|
276
|
+
tagger_labels=path_of("tagger_labels"),
|
|
277
|
+
tagger_kind=given["tagger_kind"],
|
|
278
|
+
sam_dir=path_of("sam_dir") or default_sam_dir(),
|
|
279
|
+
)
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
def _output_text(raw: dict[str, Any], path: Path, key: str, default: str) -> str:
|
|
283
|
+
value = _table(raw, "output", path).get(key, default)
|
|
284
|
+
if not isinstance(value, str):
|
|
285
|
+
raise errors.NaiError(errors.CONFIG_MISSING, f"{path}: [output] {key} must be a string: {value!r}")
|
|
286
|
+
return value
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
def _limit(raw: dict[str, Any], path: Path, key: str, default: int, unit: str) -> int:
|
|
290
|
+
value = _table(raw, "cost", path).get(key, default)
|
|
291
|
+
if not values.is_whole(value) or value < 0:
|
|
292
|
+
raise errors.NaiError(errors.CONFIG_MISSING, f"{path}: [cost] {key} must be a whole number{unit}: {value!r}")
|
|
293
|
+
return value
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
def _defaults(raw: dict[str, Any], path: Path) -> dict[str, Any]:
|
|
297
|
+
raw = _table(raw, "defaults", path)
|
|
298
|
+
unknown = set(raw) - set(DEFAULT_KEYS)
|
|
299
|
+
if unknown:
|
|
300
|
+
raise errors.NaiError(
|
|
301
|
+
errors.CONFIG_MISSING,
|
|
302
|
+
f"{path}: unknown keys in [defaults]: {', '.join(sorted(unknown))}",
|
|
303
|
+
choices=list(DEFAULT_KEYS),
|
|
304
|
+
)
|
|
305
|
+
out: dict[str, Any] = {}
|
|
306
|
+
for key, value in raw.items():
|
|
307
|
+
kind = DEFAULT_KEYS[key]
|
|
308
|
+
# A float would be cut silently to a whole number, and 1 must not pass as a switch
|
|
309
|
+
right = {
|
|
310
|
+
bool: isinstance(value, bool),
|
|
311
|
+
str: isinstance(value, str),
|
|
312
|
+
int: values.is_whole(value),
|
|
313
|
+
float: values.is_number(value),
|
|
314
|
+
}[kind]
|
|
315
|
+
if not right:
|
|
316
|
+
raise errors.NaiError(errors.CONFIG_MISSING, f"{path}: [defaults] {key} has the wrong type: {value!r}")
|
|
317
|
+
out[key] = kind(value)
|
|
318
|
+
return out
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
def _interval(raw: dict[str, Any], path: Path) -> tuple[float, float]:
|
|
322
|
+
value = _table(raw, "batch", path).get("interval", 0)
|
|
323
|
+
pair = value if isinstance(value, list) else [value, value]
|
|
324
|
+
try:
|
|
325
|
+
return interval(pair)
|
|
326
|
+
except ValueError as e:
|
|
327
|
+
raise errors.NaiError(errors.CONFIG_MISSING, f"{path}: [batch] interval: {e}") from None
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
def _accounts(raw: dict[str, Any], path: Path) -> dict[str, AccountConfig]:
|
|
331
|
+
out = {}
|
|
332
|
+
for name, table in _table(raw, "accounts", path).items():
|
|
333
|
+
where = f"{path}: [accounts.{name}]"
|
|
334
|
+
if name == DEFAULT_ACCOUNT:
|
|
335
|
+
raise errors.NaiError(errors.CONFIG_MISSING, f"{where}: {name} is the account of novelai_token")
|
|
336
|
+
if not isinstance(table, dict) or set(table) - {"token", "api_url", "image_url"}:
|
|
337
|
+
raise errors.NaiError(errors.CONFIG_MISSING, f"{where} takes token, api_url and image_url")
|
|
338
|
+
token, api_url, image_url = (table.get(k, "") for k in ("token", "api_url", "image_url"))
|
|
339
|
+
if not all(isinstance(v, str) for v in (token, api_url, image_url)) or not token.strip():
|
|
340
|
+
raise errors.NaiError(errors.CONFIG_MISSING, f"{where} needs a token, and its values must be strings")
|
|
341
|
+
try:
|
|
342
|
+
endpoint = api.endpoint(api_url, image_url) if api_url else api.OFFICIAL
|
|
343
|
+
except ValueError as e:
|
|
344
|
+
raise errors.NaiError(errors.CONFIG_MISSING, f"{where}: {e}") from None
|
|
345
|
+
if image_url and not api_url:
|
|
346
|
+
raise errors.NaiError(errors.CONFIG_MISSING, f"{where}: image_url needs api_url")
|
|
347
|
+
out[name] = AccountConfig(token.strip(), endpoint)
|
|
348
|
+
return out
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
def _holds_nul(value: Any) -> bool:
|
|
352
|
+
if isinstance(value, str):
|
|
353
|
+
return "\x00" in value
|
|
354
|
+
if isinstance(value, dict):
|
|
355
|
+
return any(_holds_nul(k) or _holds_nul(v) for k, v in value.items())
|
|
356
|
+
return isinstance(value, list) and any(map(_holds_nul, value))
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
def init(path: Path, overwrite: bool) -> bool:
|
|
360
|
+
"""Write the template, readable only by the user since it holds the token; returns whether a file was
|
|
361
|
+
written. An existing file is kept unless overwrite is given."""
|
|
362
|
+
if overwrite:
|
|
363
|
+
fs.replace(path, TEMPLATE, private=True)
|
|
364
|
+
return True
|
|
365
|
+
try:
|
|
366
|
+
fs.create(path, TEMPLATE, private=True)
|
|
367
|
+
except FileExistsError:
|
|
368
|
+
return False
|
|
369
|
+
return True
|