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
nai_cli/cost.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"""Anlas estimates using the official client's pricing formulas. Actual spend is the balance difference."""
|
|
2
|
+
|
|
3
|
+
import math
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
|
|
6
|
+
from nai_cli import errors, presets
|
|
7
|
+
from nai_cli.models import Model
|
|
8
|
+
|
|
9
|
+
OPUS_FREE_MAX_PIXELS = 1024 * 1024
|
|
10
|
+
OPUS_FREE_MAX_STEPS = 28
|
|
11
|
+
MAX_PER_SAMPLE = 140
|
|
12
|
+
_AREA = 2.951823174884865e-6
|
|
13
|
+
_STEP_AREA = 5.753298233447344e-7
|
|
14
|
+
# Medium is priced at a fixed 23 steps divided by 1.75, without rounding
|
|
15
|
+
MEDIUM_COST_STEPS = 23
|
|
16
|
+
MEDIUM_COST_DIVISOR = 1.75
|
|
17
|
+
VIBE_ENCODE_COST = 2
|
|
18
|
+
VIBE_EXTRA_COST = 2
|
|
19
|
+
VIBE_FREE_COUNT = 4
|
|
20
|
+
PRECISE_REFERENCE_COST = 5
|
|
21
|
+
# The main generation page's tiers; the Director tools page offers only the first
|
|
22
|
+
_UPSCALE_TIERS = [(1048576, 1), (1747627, 2), (2446678, 3), (3145728, 4)]
|
|
23
|
+
UPSCALE_MAX_PIXELS = 3145728
|
|
24
|
+
TOOL_STEPS = 28
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
@dataclass
|
|
28
|
+
class Estimate:
|
|
29
|
+
anlas: int
|
|
30
|
+
per_sample: int
|
|
31
|
+
free_by_opus: bool
|
|
32
|
+
# Free only while the V5 quota pool lasts; afterwards if_quota_exhausted is charged
|
|
33
|
+
if_quota_exhausted: int
|
|
34
|
+
valid: bool = True
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def require_valid(estimate: Estimate) -> None:
|
|
38
|
+
if not estimate.valid:
|
|
39
|
+
raise errors.NaiError(
|
|
40
|
+
errors.BAD_ARGS, "size and steps exceed the NovelAI per-image limit", per_sample=estimate.per_sample
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def per_sample(model: Model, width: int, height: int, steps: int, strength: float = 1.0) -> int:
|
|
45
|
+
pixels = width * height
|
|
46
|
+
if model.medium_effort:
|
|
47
|
+
base = (_AREA * pixels + _STEP_AREA * pixels * MEDIUM_COST_STEPS) / MEDIUM_COST_DIVISOR
|
|
48
|
+
else:
|
|
49
|
+
base = _area_price(pixels, steps)
|
|
50
|
+
return max(math.ceil(base * model.anlas_multiplier * strength), 2)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def generation(
|
|
54
|
+
model: Model,
|
|
55
|
+
width: int,
|
|
56
|
+
height: int,
|
|
57
|
+
steps: int,
|
|
58
|
+
n_samples: int = 1,
|
|
59
|
+
strength: float = 1.0,
|
|
60
|
+
opus: bool = True,
|
|
61
|
+
quota_exhausted: bool = False,
|
|
62
|
+
precise_references: int = 0,
|
|
63
|
+
vibes: int = 0,
|
|
64
|
+
vibes_to_encode: int = 0,
|
|
65
|
+
) -> Estimate:
|
|
66
|
+
"""model is the model that actually runs (the inpainting model for infill); vibes counts only those sent."""
|
|
67
|
+
sample = per_sample(model, width, height, steps, strength)
|
|
68
|
+
if sample > MAX_PER_SAMPLE:
|
|
69
|
+
return Estimate(anlas=-1, per_sample=sample, free_by_opus=False, if_quota_exhausted=-1, valid=False)
|
|
70
|
+
effective_steps = model.fixed.steps if model.fixed else steps
|
|
71
|
+
# The server keeps the image free with precise references and charges only them (measured)
|
|
72
|
+
eligible = opus and effective_steps <= OPUS_FREE_MAX_STEPS and width * height <= OPUS_FREE_MAX_PIXELS
|
|
73
|
+
blocked = model.opus_quota and quota_exhausted
|
|
74
|
+
free = eligible and not blocked
|
|
75
|
+
extra_per_sample = precise_references * PRECISE_REFERENCE_COST
|
|
76
|
+
extra_per_request = max(vibes - VIBE_FREE_COUNT, 0) * VIBE_EXTRA_COST
|
|
77
|
+
one_time = vibes_to_encode * VIBE_ENCODE_COST
|
|
78
|
+
|
|
79
|
+
def total(discount: int) -> int:
|
|
80
|
+
samples = sample * max(n_samples - discount, 0) + extra_per_sample * n_samples
|
|
81
|
+
return samples + extra_per_request + one_time
|
|
82
|
+
|
|
83
|
+
return Estimate(
|
|
84
|
+
anlas=total(1 if free else 0),
|
|
85
|
+
per_sample=sample,
|
|
86
|
+
free_by_opus=free,
|
|
87
|
+
if_quota_exhausted=total(0) if (eligible and model.opus_quota) else total(1 if free else 0),
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def upscale(width: int, height: int) -> int:
|
|
92
|
+
"""Priced by the source's area alone, with no Opus discount; returns -1 above 3145728 pixels."""
|
|
93
|
+
pixels = width * height
|
|
94
|
+
if pixels > UPSCALE_MAX_PIXELS:
|
|
95
|
+
return -1
|
|
96
|
+
return next(price for limit, price in _UPSCALE_TIERS if pixels <= limit)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def augment(width: int, height: int, bg_removal: bool = False, opus: bool = True) -> int:
|
|
100
|
+
"""Scales the size into [TOOL_MIN_PIXELS, TOOL_MAX_PIXELS], then prices one 28-step image of
|
|
101
|
+
nai-diffusion-3, which the official client uses for every tool; returns -1 above the per-image limit."""
|
|
102
|
+
w, h = presets.grow_to(*presets.shrink_to(width, height, presets.TOOL_MAX_PIXELS), presets.TOOL_MIN_PIXELS)
|
|
103
|
+
price = max(_area_price(w * h, TOOL_STEPS), 2)
|
|
104
|
+
if price > MAX_PER_SAMPLE:
|
|
105
|
+
return -1
|
|
106
|
+
if bg_removal:
|
|
107
|
+
return price * 3 + 5
|
|
108
|
+
if opus and w * h <= OPUS_FREE_MAX_PIXELS:
|
|
109
|
+
return 0
|
|
110
|
+
return price
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def _area_price(pixels: int, steps: int) -> int:
|
|
114
|
+
return math.ceil(_AREA * pixels + _STEP_AREA * pixels * steps)
|