tileupscale 0.6.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.
- tileupscale-0.6.0.dist-info/METADATA +172 -0
- tileupscale-0.6.0.dist-info/RECORD +52 -0
- tileupscale-0.6.0.dist-info/WHEEL +5 -0
- tileupscale-0.6.0.dist-info/entry_points.txt +2 -0
- tileupscale-0.6.0.dist-info/licenses/LICENSE +21 -0
- tileupscale-0.6.0.dist-info/licenses/NOTICE +6 -0
- tileupscale-0.6.0.dist-info/top_level.txt +1 -0
- upscale/__init__.py +30 -0
- upscale/__main__.py +3 -0
- upscale/about.py +170 -0
- upscale/algorithms/__init__.py +166 -0
- upscale/algorithms/classical.py +82 -0
- upscale/algorithms/plugins/__init__.py +52 -0
- upscale/algorithms/plugins/texture_residual.py +117 -0
- upscale/algorithms/plugins/vectorize_scale.py +450 -0
- upscale/alpha_policy.py +140 -0
- upscale/api/__init__.py +19 -0
- upscale/api/compare_routes.py +256 -0
- upscale/api/deps.py +18 -0
- upscale/api/jobs.py +535 -0
- upscale/api/media_routes.py +257 -0
- upscale/api/models_routes.py +107 -0
- upscale/api/system_routes.py +210 -0
- upscale/backends/__init__.py +27 -0
- upscale/backends/onnx_rt.py +291 -0
- upscale/benchmark.py +96 -0
- upscale/checkpoint.py +259 -0
- upscale/cli.py +634 -0
- upscale/compare.py +204 -0
- upscale/config.py +56 -0
- upscale/engine.py +321 -0
- upscale/estimates.py +338 -0
- upscale/filters.py +180 -0
- upscale/help_content.py +95 -0
- upscale/infer_util.py +22 -0
- upscale/io_utils.py +622 -0
- upscale/job_prep.py +318 -0
- upscale/mcp_server.py +287 -0
- upscale/memmap_util.py +70 -0
- upscale/models.py +628 -0
- upscale/naming.py +63 -0
- upscale/pipeline.py +482 -0
- upscale/presets.py +127 -0
- upscale/quality.py +36 -0
- upscale/schema.py +315 -0
- upscale/server.py +893 -0
- upscale/speed_stats.py +273 -0
- upscale/suggest.py +93 -0
- upscale/system.py +102 -0
- upscale/tiling.py +430 -0
- upscale/web.py +118 -0
- upscale/weight_blend.py +75 -0
upscale/cli.py
ADDED
|
@@ -0,0 +1,634 @@
|
|
|
1
|
+
"""CLI entry point for the upscale."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import shlex
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
import click
|
|
9
|
+
|
|
10
|
+
from .config import UpscaleConfig
|
|
11
|
+
from .engine import execute
|
|
12
|
+
from .models import DEFAULT_WEIGHTS_DIR, MODELS, add_external_model, all_models, model_status
|
|
13
|
+
from .naming import resolve_output_path
|
|
14
|
+
from .pipeline import DEFAULT_DISK_INTERMEDIATE_THRESHOLD
|
|
15
|
+
from .schema import FilterSettings, NamePattern, OnExists
|
|
16
|
+
|
|
17
|
+
IMAGE_GLOBS = ("*.png", "*.jpg", "*.jpeg", "*.tif", "*.tiff", "*.webp", "*.bmp")
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def settings_to_cli(cfg: UpscaleConfig) -> str:
|
|
21
|
+
"""Build an equivalent CLI command string for the UI."""
|
|
22
|
+
parts = ["upscale", "upscale", str(cfg.input or "INPUT"), str(cfg.output or "OUTPUT")]
|
|
23
|
+
if cfg.scale is not None:
|
|
24
|
+
parts += ["--scale", str(cfg.scale)]
|
|
25
|
+
if cfg.target_size is not None:
|
|
26
|
+
parts += ["--target-size", str(cfg.target_size)]
|
|
27
|
+
if cfg.algorithm:
|
|
28
|
+
parts += ["--algorithm", cfg.algorithm]
|
|
29
|
+
else:
|
|
30
|
+
parts += ["--model", cfg.model]
|
|
31
|
+
if cfg.crop:
|
|
32
|
+
x, y, w, h = cfg.crop
|
|
33
|
+
parts += ["--crop", f"{x},{y},{w},{h}"]
|
|
34
|
+
if cfg.strength != 1.0:
|
|
35
|
+
parts += ["--strength", str(cfg.strength)]
|
|
36
|
+
if cfg.denoise != 0.5:
|
|
37
|
+
parts += ["--denoise", str(cfg.denoise)]
|
|
38
|
+
if cfg.blend_model:
|
|
39
|
+
parts += ["--blend-model", cfg.blend_model, "--blend", str(cfg.blend)]
|
|
40
|
+
if cfg.format != "auto":
|
|
41
|
+
parts += ["--format", cfg.format]
|
|
42
|
+
if cfg.quality != 90:
|
|
43
|
+
parts += ["--quality", str(cfg.quality)]
|
|
44
|
+
f = cfg.filters or FilterSettings()
|
|
45
|
+
if f.pre_denoise:
|
|
46
|
+
parts += ["--pre-denoise", str(f.pre_denoise)]
|
|
47
|
+
if f.jpeg_clean:
|
|
48
|
+
parts += ["--jpeg-clean", str(f.jpeg_clean)]
|
|
49
|
+
if f.deblur:
|
|
50
|
+
parts += ["--deblur", str(f.deblur)]
|
|
51
|
+
if f.edge:
|
|
52
|
+
parts += ["--edge", str(f.edge)]
|
|
53
|
+
if f.grain:
|
|
54
|
+
parts += ["--grain", str(f.grain)]
|
|
55
|
+
if f.color_contrast:
|
|
56
|
+
parts += ["--color-contrast", str(f.color_contrast)]
|
|
57
|
+
if f.halo_reduce:
|
|
58
|
+
parts += ["--halo-reduce", str(f.halo_reduce)]
|
|
59
|
+
if cfg.half:
|
|
60
|
+
parts.append("--half")
|
|
61
|
+
if cfg.cpu:
|
|
62
|
+
parts.append("--cpu")
|
|
63
|
+
if cfg.fast:
|
|
64
|
+
parts.append("--fast")
|
|
65
|
+
if cfg.sr_alpha:
|
|
66
|
+
parts.append("--sr-alpha")
|
|
67
|
+
if cfg.tile != "auto":
|
|
68
|
+
parts += ["--tile", str(cfg.tile)]
|
|
69
|
+
if cfg.overlap != 32:
|
|
70
|
+
parts += ["--overlap", str(cfg.overlap)]
|
|
71
|
+
if cfg.tiff_compression and cfg.tiff_compression != "lzw":
|
|
72
|
+
parts += ["--tiff-compression", cfg.tiff_compression]
|
|
73
|
+
if cfg.device not in ("auto", None) and not cfg.cpu:
|
|
74
|
+
parts += ["--device", str(cfg.device)]
|
|
75
|
+
if f.seamless:
|
|
76
|
+
parts.append("--seamless")
|
|
77
|
+
v = cfg.vectorize
|
|
78
|
+
if v is not None and (
|
|
79
|
+
cfg.algorithm == "vectorize_scale"
|
|
80
|
+
or v.engine != "vtracer"
|
|
81
|
+
or v.color_precision != 6
|
|
82
|
+
or v.filter_speckle != 4
|
|
83
|
+
or v.corner_threshold != 60
|
|
84
|
+
or v.mode != "spline"
|
|
85
|
+
or getattr(v, "max_trace_edge", 1024) != 1024
|
|
86
|
+
):
|
|
87
|
+
if v.engine != "vtracer":
|
|
88
|
+
parts += ["--vectorize-engine", v.engine]
|
|
89
|
+
if v.color_precision != 6:
|
|
90
|
+
parts += ["--vectorize-color-precision", str(v.color_precision)]
|
|
91
|
+
if v.filter_speckle != 4:
|
|
92
|
+
parts += ["--vectorize-filter-speckle", str(v.filter_speckle)]
|
|
93
|
+
if v.corner_threshold != 60:
|
|
94
|
+
parts += ["--vectorize-corner-threshold", str(v.corner_threshold)]
|
|
95
|
+
if v.mode != "spline":
|
|
96
|
+
parts += ["--vectorize-mode", v.mode]
|
|
97
|
+
mte = getattr(v, "max_trace_edge", 1024)
|
|
98
|
+
if mte != 1024:
|
|
99
|
+
parts += ["--vectorize-max-trace-edge", str(mte)]
|
|
100
|
+
if cfg.name_pattern is not None:
|
|
101
|
+
parts += ["--name-pattern", cfg.name_pattern.pattern]
|
|
102
|
+
on_ex = getattr(cfg.name_pattern, "on_exists", None)
|
|
103
|
+
if on_ex is not None and str(on_ex.value if hasattr(on_ex, "value") else on_ex) != "number":
|
|
104
|
+
parts += ["--on-exists", str(on_ex.value if hasattr(on_ex, "value") else on_ex)]
|
|
105
|
+
return " ".join(shlex.quote(part) for part in parts)
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def _common_options(fn):
|
|
109
|
+
opts = [
|
|
110
|
+
click.option("--scale", "-s", type=float, default=None),
|
|
111
|
+
click.option("--target-size", "-t", type=int, default=None),
|
|
112
|
+
click.option("--model", "-m", default="RealESRGAN_x4plus", show_default=True),
|
|
113
|
+
click.option(
|
|
114
|
+
"--algorithm",
|
|
115
|
+
default=None,
|
|
116
|
+
type=str,
|
|
117
|
+
help=(
|
|
118
|
+
"Classical / plugin resize instead of a neural model. "
|
|
119
|
+
"Built-in: nearest, bilinear, bicubic, lanczos2, lanczos3. "
|
|
120
|
+
"Paper: texture_residual, vectorize_scale (needs upscale[vectorize]). "
|
|
121
|
+
"Or any name registered via register_algorithm."
|
|
122
|
+
),
|
|
123
|
+
),
|
|
124
|
+
click.option("--tile", default="auto", show_default=True),
|
|
125
|
+
click.option("--overlap", default=32, show_default=True, type=int),
|
|
126
|
+
click.option(
|
|
127
|
+
"--device",
|
|
128
|
+
default="auto",
|
|
129
|
+
show_default=True,
|
|
130
|
+
type=click.Choice(["auto", "cuda", "cpu", "mps"], case_sensitive=False),
|
|
131
|
+
),
|
|
132
|
+
click.option("--cpu", is_flag=True, default=False),
|
|
133
|
+
click.option("--half", is_flag=True, default=False),
|
|
134
|
+
click.option("--weights-dir", type=click.Path(path_type=Path), default=None),
|
|
135
|
+
click.option(
|
|
136
|
+
"--tiff-compression",
|
|
137
|
+
default="lzw",
|
|
138
|
+
show_default=True,
|
|
139
|
+
type=click.Choice(
|
|
140
|
+
["lzw", "jpeg", "none", "deflate", "zstd"], case_sensitive=False
|
|
141
|
+
),
|
|
142
|
+
),
|
|
143
|
+
click.option("--quiet", "-q", is_flag=True, default=False),
|
|
144
|
+
click.option("--fast", is_flag=True, default=False),
|
|
145
|
+
click.option("--sr-alpha", is_flag=True, default=False),
|
|
146
|
+
click.option(
|
|
147
|
+
"--disk-intermediate-threshold",
|
|
148
|
+
type=int,
|
|
149
|
+
default=DEFAULT_DISK_INTERMEDIATE_THRESHOLD,
|
|
150
|
+
show_default=True,
|
|
151
|
+
),
|
|
152
|
+
click.option("--crop", default=None, help="Crop region x,y,w,h"),
|
|
153
|
+
click.option("--strength", type=float, default=1.0, show_default=True),
|
|
154
|
+
click.option("--denoise", type=float, default=0.5, show_default=True),
|
|
155
|
+
click.option("--blend-model", default=None),
|
|
156
|
+
click.option("--blend", type=float, default=0.5, show_default=True),
|
|
157
|
+
click.option(
|
|
158
|
+
"--vectorize-engine",
|
|
159
|
+
type=click.Choice(["vtracer", "autotrace"], case_sensitive=False),
|
|
160
|
+
default="vtracer",
|
|
161
|
+
show_default=True,
|
|
162
|
+
help="vectorize_scale: tracing engine (vtracer or PyAutoTrace)",
|
|
163
|
+
),
|
|
164
|
+
click.option(
|
|
165
|
+
"--vectorize-color-precision",
|
|
166
|
+
type=click.IntRange(1, 8),
|
|
167
|
+
default=6,
|
|
168
|
+
show_default=True,
|
|
169
|
+
help="vectorize_scale: color bit depth (lower = flatter)",
|
|
170
|
+
),
|
|
171
|
+
click.option(
|
|
172
|
+
"--vectorize-filter-speckle",
|
|
173
|
+
type=click.IntRange(0, 64),
|
|
174
|
+
default=4,
|
|
175
|
+
show_default=True,
|
|
176
|
+
help="vectorize_scale: drop speckles smaller than N px",
|
|
177
|
+
),
|
|
178
|
+
click.option(
|
|
179
|
+
"--vectorize-corner-threshold",
|
|
180
|
+
type=click.IntRange(0, 180),
|
|
181
|
+
default=60,
|
|
182
|
+
show_default=True,
|
|
183
|
+
help="vectorize_scale: corner sensitivity (higher = sharper)",
|
|
184
|
+
),
|
|
185
|
+
click.option(
|
|
186
|
+
"--vectorize-mode",
|
|
187
|
+
type=click.Choice(["spline", "polygon"], case_sensitive=False),
|
|
188
|
+
default="spline",
|
|
189
|
+
show_default=True,
|
|
190
|
+
help="vectorize_scale: path style",
|
|
191
|
+
),
|
|
192
|
+
click.option(
|
|
193
|
+
"--vectorize-max-trace-edge",
|
|
194
|
+
type=click.IntRange(256, 4096),
|
|
195
|
+
default=1024,
|
|
196
|
+
show_default=True,
|
|
197
|
+
help="vectorize_scale: long-edge cap before tracing",
|
|
198
|
+
),
|
|
199
|
+
click.option(
|
|
200
|
+
"--format",
|
|
201
|
+
"fmt",
|
|
202
|
+
default="auto",
|
|
203
|
+
show_default=True,
|
|
204
|
+
type=click.Choice(
|
|
205
|
+
[
|
|
206
|
+
"auto",
|
|
207
|
+
"png",
|
|
208
|
+
"jpeg",
|
|
209
|
+
"jpeg_exif",
|
|
210
|
+
"webp",
|
|
211
|
+
"tiff",
|
|
212
|
+
"bmp",
|
|
213
|
+
"tga",
|
|
214
|
+
"jp2",
|
|
215
|
+
"psd",
|
|
216
|
+
"psb",
|
|
217
|
+
],
|
|
218
|
+
case_sensitive=False,
|
|
219
|
+
),
|
|
220
|
+
),
|
|
221
|
+
click.option(
|
|
222
|
+
"--quality",
|
|
223
|
+
type=click.IntRange(1, 100),
|
|
224
|
+
default=90,
|
|
225
|
+
show_default=True,
|
|
226
|
+
),
|
|
227
|
+
click.option(
|
|
228
|
+
"--name-pattern",
|
|
229
|
+
default=None,
|
|
230
|
+
help="Output name pattern (default: none for single upscale; "
|
|
231
|
+
"batch defaults to {stem}_{scale}x_{model}.{ext})",
|
|
232
|
+
),
|
|
233
|
+
click.option(
|
|
234
|
+
"--on-exists",
|
|
235
|
+
default="number",
|
|
236
|
+
show_default=True,
|
|
237
|
+
type=click.Choice(["number", "overwrite", "skip"], case_sensitive=False),
|
|
238
|
+
),
|
|
239
|
+
click.option("--pre-denoise", type=click.FloatRange(0.0, 1.0), default=0.0),
|
|
240
|
+
click.option("--jpeg-clean", type=click.FloatRange(0.0, 1.0), default=0.0),
|
|
241
|
+
click.option("--deblur", type=click.FloatRange(0.0, 1.0), default=0.0),
|
|
242
|
+
click.option("--edge", type=click.FloatRange(0.0, 1.0), default=0.0),
|
|
243
|
+
click.option("--grain", type=click.FloatRange(0.0, 1.0), default=0.0),
|
|
244
|
+
click.option("--color-contrast", type=click.FloatRange(0.0, 1.0), default=0.0),
|
|
245
|
+
click.option("--halo-reduce", type=click.FloatRange(0.0, 1.0), default=0.0),
|
|
246
|
+
click.option("--seamless", is_flag=True, default=False),
|
|
247
|
+
]
|
|
248
|
+
for opt in reversed(opts):
|
|
249
|
+
fn = opt(fn)
|
|
250
|
+
return fn
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
def _parse_crop(crop: str | None) -> tuple[int, int, int, int] | None:
|
|
254
|
+
if not crop:
|
|
255
|
+
return None
|
|
256
|
+
parts = [int(x.strip()) for x in crop.split(",")]
|
|
257
|
+
if len(parts) != 4:
|
|
258
|
+
raise click.UsageError("--crop must be x,y,w,h")
|
|
259
|
+
if parts[2] <= 0 or parts[3] <= 0 or parts[0] < 0 or parts[1] < 0:
|
|
260
|
+
raise click.UsageError("--crop requires non-negative x,y and positive w,h")
|
|
261
|
+
return parts[0], parts[1], parts[2], parts[3]
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
def _cfg_from_opts(input: Path | None, output: Path | None, **kw) -> UpscaleConfig:
|
|
265
|
+
"""Build UpscaleConfig after validating shared fields via JobSettings."""
|
|
266
|
+
from .schema import CropRect, JobSettings, VectorizeSettings
|
|
267
|
+
|
|
268
|
+
crop = _parse_crop(kw.pop("crop", None))
|
|
269
|
+
fmt = kw.pop("fmt", "auto")
|
|
270
|
+
name_pattern = kw.pop("name_pattern")
|
|
271
|
+
on_exists = kw.pop("on_exists")
|
|
272
|
+
filters = FilterSettings(
|
|
273
|
+
pre_denoise=kw.pop("pre_denoise"),
|
|
274
|
+
jpeg_clean=kw.pop("jpeg_clean"),
|
|
275
|
+
deblur=kw.pop("deblur"),
|
|
276
|
+
edge=kw.pop("edge"),
|
|
277
|
+
grain=kw.pop("grain"),
|
|
278
|
+
color_contrast=kw.pop("color_contrast"),
|
|
279
|
+
halo_reduce=kw.pop("halo_reduce"),
|
|
280
|
+
seamless=kw.pop("seamless"),
|
|
281
|
+
)
|
|
282
|
+
vectorize = VectorizeSettings(
|
|
283
|
+
engine=str(kw.pop("vectorize_engine") or "vtracer").lower(),
|
|
284
|
+
color_precision=kw.pop("vectorize_color_precision"),
|
|
285
|
+
filter_speckle=kw.pop("vectorize_filter_speckle"),
|
|
286
|
+
corner_threshold=kw.pop("vectorize_corner_threshold"),
|
|
287
|
+
mode=str(kw.pop("vectorize_mode") or "spline").lower(),
|
|
288
|
+
max_trace_edge=kw.pop("vectorize_max_trace_edge"),
|
|
289
|
+
)
|
|
290
|
+
pattern = None
|
|
291
|
+
if name_pattern:
|
|
292
|
+
pattern = NamePattern(pattern=name_pattern, on_exists=OnExists(on_exists))
|
|
293
|
+
|
|
294
|
+
# Validate bounds / enums through the canonical pydantic model.
|
|
295
|
+
try:
|
|
296
|
+
JobSettings.model_validate(
|
|
297
|
+
{
|
|
298
|
+
"scale": kw.get("scale"),
|
|
299
|
+
"target_size": kw.get("target_size"),
|
|
300
|
+
"model": kw.get("model") or "RealESRGAN_x4plus",
|
|
301
|
+
"algorithm": kw.get("algorithm"),
|
|
302
|
+
"tile": kw.get("tile", "auto"),
|
|
303
|
+
"overlap": kw.get("overlap", 32),
|
|
304
|
+
"device": "cpu" if kw.get("cpu") else (kw.get("device") or "auto"),
|
|
305
|
+
"half": bool(kw.get("half")),
|
|
306
|
+
"format": fmt,
|
|
307
|
+
"quality": kw.get("quality", 90),
|
|
308
|
+
"tiff_compression": kw.get("tiff_compression", "lzw"),
|
|
309
|
+
"crop": CropRect(x=crop[0], y=crop[1], w=crop[2], h=crop[3])
|
|
310
|
+
if crop
|
|
311
|
+
else None,
|
|
312
|
+
"strength": kw.get("strength", 1.0),
|
|
313
|
+
"denoise": kw.get("denoise", 0.5),
|
|
314
|
+
"blend_model": kw.get("blend_model"),
|
|
315
|
+
"blend": kw.get("blend", 0.5),
|
|
316
|
+
"filters": filters,
|
|
317
|
+
"vectorize": vectorize,
|
|
318
|
+
"name_pattern": pattern,
|
|
319
|
+
"sr_alpha": bool(kw.get("sr_alpha")),
|
|
320
|
+
"fast": bool(kw.get("fast")),
|
|
321
|
+
"quiet": bool(kw.get("quiet")),
|
|
322
|
+
}
|
|
323
|
+
)
|
|
324
|
+
except Exception as exc: # noqa: BLE001
|
|
325
|
+
raise click.UsageError(str(exc)) from exc
|
|
326
|
+
|
|
327
|
+
return UpscaleConfig(
|
|
328
|
+
input=input,
|
|
329
|
+
output=output,
|
|
330
|
+
format=fmt,
|
|
331
|
+
crop=crop,
|
|
332
|
+
filters=filters,
|
|
333
|
+
vectorize=vectorize,
|
|
334
|
+
name_pattern=pattern,
|
|
335
|
+
**kw,
|
|
336
|
+
)
|
|
337
|
+
|
|
338
|
+
|
|
339
|
+
@click.group(context_settings={"help_option_names": ["-h", "--help"]})
|
|
340
|
+
def cli() -> None:
|
|
341
|
+
"""AI image upscaler — Real-ESRGAN with feather-blended tiles."""
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
@cli.command("upscale")
|
|
345
|
+
@click.argument("input", type=click.Path(exists=True, dir_okay=False, path_type=Path))
|
|
346
|
+
@click.argument("output", type=click.Path(dir_okay=False, path_type=Path))
|
|
347
|
+
@_common_options
|
|
348
|
+
def upscale_cmd(input: Path, output: Path, **kw) -> None:
|
|
349
|
+
"""Upscale a single image."""
|
|
350
|
+
cfg = _cfg_from_opts(input, output, **kw)
|
|
351
|
+
execute(cfg)
|
|
352
|
+
|
|
353
|
+
|
|
354
|
+
@cli.command("batch")
|
|
355
|
+
@click.argument("input_dir", type=click.Path(exists=True, file_okay=False, path_type=Path))
|
|
356
|
+
@click.argument("output_dir", type=click.Path(file_okay=False, path_type=Path))
|
|
357
|
+
@_common_options
|
|
358
|
+
def batch_cmd(input_dir: Path, output_dir: Path, **kw) -> None:
|
|
359
|
+
"""Upscale all images in INPUT_DIR into OUTPUT_DIR (non-recursive)."""
|
|
360
|
+
output_dir.mkdir(parents=True, exist_ok=True)
|
|
361
|
+
# Batch always uses a name pattern (explicit or default).
|
|
362
|
+
if kw.get("name_pattern") is None:
|
|
363
|
+
kw["name_pattern"] = "{stem}_{scale}x_{model}.{ext}"
|
|
364
|
+
files: list[Path] = []
|
|
365
|
+
for pattern in IMAGE_GLOBS:
|
|
366
|
+
files.extend(sorted(input_dir.glob(pattern)))
|
|
367
|
+
files.extend(sorted(input_dir.glob(pattern.upper())))
|
|
368
|
+
seen: set[Path] = set()
|
|
369
|
+
unique = []
|
|
370
|
+
for f in files:
|
|
371
|
+
if f not in seen:
|
|
372
|
+
seen.add(f)
|
|
373
|
+
unique.append(f)
|
|
374
|
+
if not unique:
|
|
375
|
+
raise click.UsageError(f"No images found in {input_dir}")
|
|
376
|
+
|
|
377
|
+
errors: list[str] = []
|
|
378
|
+
for src in unique:
|
|
379
|
+
cfg = _cfg_from_opts(src, output_dir / src.name, **kw)
|
|
380
|
+
# Apply naming pattern into output_dir
|
|
381
|
+
if cfg.name_pattern:
|
|
382
|
+
# Classical / plugin jobs should stamp the algorithm name, not the unused neural model.
|
|
383
|
+
model_token = cfg.algorithm if cfg.algorithm else cfg.model
|
|
384
|
+
resolved = resolve_output_path(
|
|
385
|
+
output_dir,
|
|
386
|
+
stem=src.stem,
|
|
387
|
+
scale=cfg.scale or "auto",
|
|
388
|
+
model=model_token,
|
|
389
|
+
ext=cfg.format if cfg.format != "auto" else src.suffix.lstrip(".") or "png",
|
|
390
|
+
pattern=cfg.name_pattern,
|
|
391
|
+
)
|
|
392
|
+
if resolved is None:
|
|
393
|
+
click.echo(f"Skip {src.name} (exists)")
|
|
394
|
+
continue
|
|
395
|
+
cfg.output = resolved
|
|
396
|
+
click.echo(f"=== {src.name} ===")
|
|
397
|
+
try:
|
|
398
|
+
execute(cfg)
|
|
399
|
+
except Exception as exc: # noqa: BLE001 — one bad file must not abort the batch
|
|
400
|
+
errors.append(f"{src.name}: {exc}")
|
|
401
|
+
click.echo(f"Failed {src.name}: {exc}", err=True)
|
|
402
|
+
if errors:
|
|
403
|
+
click.echo(f"Batch finished with {len(errors)} failure(s).", err=True)
|
|
404
|
+
raise click.exceptions.Exit(1)
|
|
405
|
+
|
|
406
|
+
|
|
407
|
+
@cli.command("compare")
|
|
408
|
+
@click.argument("input", type=click.Path(exists=True, dir_okay=False, path_type=Path))
|
|
409
|
+
@click.argument("output_dir", type=click.Path(file_okay=False, path_type=Path))
|
|
410
|
+
@click.option("--crop", required=True, help="x,y,w,h")
|
|
411
|
+
@click.option("--models", required=True, help="Comma-separated model names")
|
|
412
|
+
@click.option("--scale", "-s", type=float, default=4.0, show_default=True)
|
|
413
|
+
@click.option("--device", default="auto")
|
|
414
|
+
@click.option("--half", is_flag=True, default=False)
|
|
415
|
+
def compare_cmd(
|
|
416
|
+
input: Path,
|
|
417
|
+
output_dir: Path,
|
|
418
|
+
crop: str,
|
|
419
|
+
models: str,
|
|
420
|
+
scale: float,
|
|
421
|
+
device: str,
|
|
422
|
+
half: bool,
|
|
423
|
+
) -> None:
|
|
424
|
+
"""Compare models on a crop region."""
|
|
425
|
+
from .compare import compare_models
|
|
426
|
+
from .schema import CropRect
|
|
427
|
+
|
|
428
|
+
parts = [int(x) for x in crop.split(",")]
|
|
429
|
+
rect = CropRect(x=parts[0], y=parts[1], w=parts[2], h=parts[3])
|
|
430
|
+
names = [m.strip() for m in models.split(",") if m.strip()]
|
|
431
|
+
results = compare_models(
|
|
432
|
+
input, output_dir, models=names, crop=rect, scale=scale, device=device, half=half
|
|
433
|
+
)
|
|
434
|
+
for r in results:
|
|
435
|
+
status = r["error"] or r["path"]
|
|
436
|
+
click.echo(f"{r['model']}: {r['seconds']}s → {status}")
|
|
437
|
+
|
|
438
|
+
|
|
439
|
+
@cli.group("models")
|
|
440
|
+
def models_group() -> None:
|
|
441
|
+
"""Manage built-in and external models."""
|
|
442
|
+
|
|
443
|
+
|
|
444
|
+
@models_group.command("list")
|
|
445
|
+
@click.option("--weights-dir", type=click.Path(path_type=Path), default=None)
|
|
446
|
+
def models_list(weights_dir: Path | None) -> None:
|
|
447
|
+
wdir = weights_dir or DEFAULT_WEIGHTS_DIR
|
|
448
|
+
catalog = all_models(wdir)
|
|
449
|
+
col_w = max(len(k) for k in catalog) + 2
|
|
450
|
+
for name, meta in catalog.items():
|
|
451
|
+
st = model_status(name, wdir)
|
|
452
|
+
desc = meta.get("description", "")
|
|
453
|
+
click.echo(f" {name:<{col_w}} [{st}] {desc}")
|
|
454
|
+
|
|
455
|
+
|
|
456
|
+
@models_group.command("add")
|
|
457
|
+
@click.argument("name")
|
|
458
|
+
@click.argument("url_or_path")
|
|
459
|
+
@click.option("--scale", type=int, default=4, show_default=True)
|
|
460
|
+
@click.option("--sha256", default=None)
|
|
461
|
+
@click.option("--weights-dir", type=click.Path(path_type=Path), default=None)
|
|
462
|
+
def models_add(
|
|
463
|
+
name: str,
|
|
464
|
+
url_or_path: str,
|
|
465
|
+
scale: int,
|
|
466
|
+
sha256: str | None,
|
|
467
|
+
weights_dir: Path | None,
|
|
468
|
+
) -> None:
|
|
469
|
+
wdir = weights_dir or DEFAULT_WEIGHTS_DIR
|
|
470
|
+
p = Path(url_or_path)
|
|
471
|
+
if p.exists():
|
|
472
|
+
add_external_model(name, path=str(p), scale=scale, sha256=sha256, weights_dir=wdir)
|
|
473
|
+
else:
|
|
474
|
+
add_external_model(name, url=url_or_path, scale=scale, sha256=sha256, weights_dir=wdir)
|
|
475
|
+
click.echo(f"Added external model {name}")
|
|
476
|
+
|
|
477
|
+
|
|
478
|
+
@models_group.command("verify")
|
|
479
|
+
@click.argument("name")
|
|
480
|
+
@click.option("--weights-dir", type=click.Path(path_type=Path), default=None)
|
|
481
|
+
def models_verify(name: str, weights_dir: Path | None) -> None:
|
|
482
|
+
wdir = weights_dir or DEFAULT_WEIGHTS_DIR
|
|
483
|
+
click.echo(f"{name}: {model_status(name, wdir)}")
|
|
484
|
+
|
|
485
|
+
|
|
486
|
+
# Keep bare `upscale models` listing for backward compat
|
|
487
|
+
@cli.command("models-legacy", hidden=True)
|
|
488
|
+
def list_models_legacy() -> None:
|
|
489
|
+
for name, meta in MODELS.items():
|
|
490
|
+
click.echo(f" {name} {meta.get('description', '')}")
|
|
491
|
+
|
|
492
|
+
|
|
493
|
+
@cli.command("suggest")
|
|
494
|
+
@click.argument("input", type=click.Path(exists=True, dir_okay=False, path_type=Path))
|
|
495
|
+
@click.option("--fast", is_flag=True, default=False)
|
|
496
|
+
def suggest_cmd(input: Path, fast: bool) -> None:
|
|
497
|
+
"""Suggest a model from image statistics."""
|
|
498
|
+
import json
|
|
499
|
+
|
|
500
|
+
from .suggest import suggest_model
|
|
501
|
+
|
|
502
|
+
click.echo(json.dumps(suggest_model(input, fast=fast), indent=2))
|
|
503
|
+
|
|
504
|
+
|
|
505
|
+
@cli.group("presets")
|
|
506
|
+
def presets_group() -> None:
|
|
507
|
+
"""List target and option presets."""
|
|
508
|
+
|
|
509
|
+
|
|
510
|
+
@presets_group.command("list")
|
|
511
|
+
def presets_list_cmd() -> None:
|
|
512
|
+
"""Print FHD/4K/print targets and Photo 4× options."""
|
|
513
|
+
import json
|
|
514
|
+
|
|
515
|
+
from .presets import list_presets
|
|
516
|
+
|
|
517
|
+
click.echo(json.dumps(list_presets(), indent=2))
|
|
518
|
+
|
|
519
|
+
|
|
520
|
+
@cli.command("benchmark")
|
|
521
|
+
@click.argument("input", type=click.Path(exists=True, dir_okay=False, path_type=Path))
|
|
522
|
+
@click.option("--model", "-m", default="RealESRGAN_x4plus", show_default=True)
|
|
523
|
+
@click.option("--device", default="cpu", show_default=True)
|
|
524
|
+
@click.option("--crop-size", default=64, show_default=True, type=int)
|
|
525
|
+
@click.option("--scale", "-s", default=2.0, show_default=True, type=float)
|
|
526
|
+
def benchmark_cmd(input: Path, model: str, device: str, crop_size: int, scale: float) -> None:
|
|
527
|
+
"""Run a tiny crop timing benchmark (saved under user config)."""
|
|
528
|
+
import json
|
|
529
|
+
|
|
530
|
+
from .benchmark import load_benchmark_history, run_benchmark
|
|
531
|
+
|
|
532
|
+
entry = run_benchmark(
|
|
533
|
+
input, model=model, device=device, crop_size=crop_size, scale=scale
|
|
534
|
+
)
|
|
535
|
+
click.echo(json.dumps({"last": entry, "history": load_benchmark_history()[-5:]}, indent=2))
|
|
536
|
+
|
|
537
|
+
|
|
538
|
+
@cli.command("serve")
|
|
539
|
+
@click.option("--host", default="127.0.0.1", show_default=True)
|
|
540
|
+
@click.option("--port", default=8787, show_default=True, type=int)
|
|
541
|
+
def serve_cmd(host: str, port: int) -> None:
|
|
542
|
+
"""Launch the FastAPI web UI and job API.
|
|
543
|
+
|
|
544
|
+
Default bind is 127.0.0.1. Non-loopback hosts require UPSCALE_ALLOW_REMOTE=1
|
|
545
|
+
because the job/model APIs are unauthenticated.
|
|
546
|
+
"""
|
|
547
|
+
try:
|
|
548
|
+
from .server import run_server
|
|
549
|
+
except ImportError as exc:
|
|
550
|
+
raise click.ClickException(
|
|
551
|
+
"Server extras required: pip install 'tileupscale[server]'"
|
|
552
|
+
) from exc
|
|
553
|
+
try:
|
|
554
|
+
run_server(host=host, port=port)
|
|
555
|
+
except RuntimeError as exc:
|
|
556
|
+
raise click.ClickException(str(exc)) from exc
|
|
557
|
+
|
|
558
|
+
|
|
559
|
+
@cli.command("mcp")
|
|
560
|
+
def mcp_cmd() -> None:
|
|
561
|
+
"""Run the MCP stdio server for AI agents."""
|
|
562
|
+
try:
|
|
563
|
+
from .mcp_server import run_stdio
|
|
564
|
+
except ImportError as exc:
|
|
565
|
+
raise click.ClickException(
|
|
566
|
+
"MCP extras required: pip install 'tileupscale[mcp]'"
|
|
567
|
+
) from exc
|
|
568
|
+
run_stdio()
|
|
569
|
+
|
|
570
|
+
|
|
571
|
+
@cli.command("ui")
|
|
572
|
+
@click.option("--host", default="127.0.0.1", show_default=True)
|
|
573
|
+
@click.option("--port", default=7860, show_default=True, type=int)
|
|
574
|
+
@click.option("--share", is_flag=True, default=False)
|
|
575
|
+
@click.option(
|
|
576
|
+
"--gradio",
|
|
577
|
+
is_flag=True,
|
|
578
|
+
default=False,
|
|
579
|
+
help="Launch legacy Gradio UI instead of redirecting to `upscale serve`.",
|
|
580
|
+
)
|
|
581
|
+
def ui_cmd(host: str, port: int, share: bool, gradio: bool) -> None:
|
|
582
|
+
"""Open the design UI via `upscale serve` (Gradio kept behind --gradio)."""
|
|
583
|
+
import ipaddress
|
|
584
|
+
import os
|
|
585
|
+
|
|
586
|
+
if not gradio:
|
|
587
|
+
if share:
|
|
588
|
+
raise click.ClickException(
|
|
589
|
+
"`upscale ui` no longer uses Gradio share; run `upscale serve` "
|
|
590
|
+
"or pass --gradio for the legacy UI."
|
|
591
|
+
)
|
|
592
|
+
# Default port for serve is 8787; keep 7860 only for --gradio compat.
|
|
593
|
+
serve_port = 8787 if port == 7860 else port
|
|
594
|
+
click.echo(
|
|
595
|
+
f"Redirecting to `upscale serve --host {host} --port {serve_port}` "
|
|
596
|
+
"(pass --gradio for the legacy Gradio UI).",
|
|
597
|
+
err=True,
|
|
598
|
+
)
|
|
599
|
+
from .server import run_server
|
|
600
|
+
|
|
601
|
+
run_server(host=host, port=serve_port)
|
|
602
|
+
return
|
|
603
|
+
|
|
604
|
+
click.echo("Note: Gradio UI is deprecated; prefer `upscale serve`.", err=True)
|
|
605
|
+
allow_remote = os.environ.get("UPSCALE_ALLOW_REMOTE", "").strip() in (
|
|
606
|
+
"1",
|
|
607
|
+
"true",
|
|
608
|
+
"yes",
|
|
609
|
+
)
|
|
610
|
+
loopback = False
|
|
611
|
+
try:
|
|
612
|
+
loopback = ipaddress.ip_address(host).is_loopback
|
|
613
|
+
except ValueError:
|
|
614
|
+
loopback = host in ("localhost",)
|
|
615
|
+
if share and not allow_remote:
|
|
616
|
+
raise click.ClickException(
|
|
617
|
+
"Refusing --share without UPSCALE_ALLOW_REMOTE=1 "
|
|
618
|
+
"(Gradio share exposes an unauthenticated UI)."
|
|
619
|
+
)
|
|
620
|
+
if not loopback and not allow_remote:
|
|
621
|
+
raise click.ClickException(
|
|
622
|
+
f"Refusing to bind non-loopback host {host!r}. "
|
|
623
|
+
"Set UPSCALE_ALLOW_REMOTE=1 to allow."
|
|
624
|
+
)
|
|
625
|
+
try:
|
|
626
|
+
from .web import launch
|
|
627
|
+
except ImportError as exc:
|
|
628
|
+
raise click.ClickException(
|
|
629
|
+
"Gradio required: pip install 'tileupscale[ui]'"
|
|
630
|
+
) from exc
|
|
631
|
+
launch(host=host, port=port, share=share)
|
|
632
|
+
|
|
633
|
+
|
|
634
|
+
main = upscale_cmd
|