Update app.py
Browse files
app.py
CHANGED
|
@@ -547,17 +547,45 @@ def save_uploaded_images(files: List[gr.File] | None, prefix: str = "upload") ->
|
|
| 547 |
return paths, in_dir
|
| 548 |
|
| 549 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 550 |
def step2_upscale(
|
| 551 |
frames_list: List[str] | None,
|
| 552 |
-
|
| 553 |
-
|
| 554 |
tile: int,
|
| 555 |
precision: str,
|
| 556 |
prog_html: str,
|
| 557 |
uploaded_imgs: List[gr.File] | None,
|
|
|
|
|
|
|
| 558 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 559 |
|
| 560 |
-
#
|
| 561 |
if uploaded_imgs and len(uploaded_imgs) > 0:
|
| 562 |
img_paths, _ = save_uploaded_images(uploaded_imgs, prefix="up")
|
| 563 |
src_paths = [str(p) for p in img_paths]
|
|
@@ -568,25 +596,27 @@ def step2_upscale(
|
|
| 568 |
yield None, None, "No images provided. Upload files or run Step 1 first.", prog_html
|
| 569 |
return
|
| 570 |
|
| 571 |
-
# Map
|
| 572 |
-
model_id =
|
| 573 |
-
scale =
|
| 574 |
-
|
| 575 |
device = "cuda" if os.environ.get("CUDA_VISIBLE_DEVICES") else "cpu"
|
| 576 |
half = (precision == "half") and (device == "cuda")
|
| 577 |
upsampler = get_realesrganer(model_id, scale, tile, half, device=device)
|
| 578 |
|
| 579 |
work = Path(tempfile.mkdtemp(prefix="up_"))
|
| 580 |
-
out_dir = work / "upscaled"
|
|
|
|
| 581 |
|
| 582 |
total = len(src_paths)
|
| 583 |
done = 0
|
| 584 |
up_paths: List[Path] = []
|
| 585 |
-
|
| 586 |
last_pct = -1
|
|
|
|
| 587 |
for fp in src_paths:
|
| 588 |
try:
|
| 589 |
img = Image.open(fp).convert("RGB")
|
|
|
|
| 590 |
output, _ = upsampler.enhance(np.array(img), outscale=scale)
|
| 591 |
out_img = Image.fromarray(output)
|
| 592 |
out_file = out_dir / (Path(fp).stem + ".jpg")
|
|
@@ -594,6 +624,7 @@ def step2_upscale(
|
|
| 594 |
up_paths.append(out_file)
|
| 595 |
except Exception:
|
| 596 |
pass
|
|
|
|
| 597 |
done += 1
|
| 598 |
if total:
|
| 599 |
pct = int(round((done / total) * 100))
|
|
@@ -601,7 +632,7 @@ def step2_upscale(
|
|
| 601 |
if pct != last_pct:
|
| 602 |
label = f"Upscaling… {pct}% · {remaining}/{total} remaining"
|
| 603 |
prog_html = render_progress(pct, label)
|
| 604 |
-
#
|
| 605 |
yield None, None, label, prog_html
|
| 606 |
last_pct = pct
|
| 607 |
|
|
@@ -610,13 +641,15 @@ def step2_upscale(
|
|
| 610 |
return
|
| 611 |
|
| 612 |
# Final outputs
|
|
|
|
| 613 |
gallery = sample_paths(up_paths, 30)
|
| 614 |
zip_path = work / "upscaled.zip"
|
| 615 |
with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zf:
|
| 616 |
for p in up_paths:
|
| 617 |
zf.write(p, p.name)
|
| 618 |
|
| 619 |
-
final_label = f"Upscaled: {len(up_paths)}"
|
|
|
|
| 620 |
yield gallery, str(zip_path), final_label, render_progress(100.0, "Upscaling complete")
|
| 621 |
|
| 622 |
# ───────────────── Encode (Step 3) — supports uploaded frames/ZIP & optional audio source
|
|
|
|
| 547 |
return paths, in_dir
|
| 548 |
|
| 549 |
|
| 550 |
+
# Map UI model names to internal IDs (make sure MODEL_MAP exists above)
|
| 551 |
+
MODEL_MAP = {
|
| 552 |
+
"RealESRGAN_x4plus": "x4plus",
|
| 553 |
+
"RealESRGAN_x4plus_anime_6B": "x4plus-anime",
|
| 554 |
+
"RealESRGAN_x2plus": "x2plus",
|
| 555 |
+
# fallbacks for entries we don't wire separately here:
|
| 556 |
+
"RealESRNet_x4plus": "x4plus",
|
| 557 |
+
"realesr-general-x4v3": "x4plus",
|
| 558 |
+
}
|
| 559 |
+
|
| 560 |
+
def _clamp_scale_for_model(outscale: int, model_id: str) -> int:
|
| 561 |
+
if model_id == "x2plus":
|
| 562 |
+
return 2
|
| 563 |
+
return 4 # x4 models
|
| 564 |
+
|
| 565 |
def step2_upscale(
|
| 566 |
frames_list: List[str] | None,
|
| 567 |
+
ui_model_name: str,
|
| 568 |
+
outscale: int,
|
| 569 |
tile: int,
|
| 570 |
precision: str,
|
| 571 |
prog_html: str,
|
| 572 |
uploaded_imgs: List[gr.File] | None,
|
| 573 |
+
denoise_strength: float = 0.5, # <- NEW param (to match UI)
|
| 574 |
+
face_enhance: bool = False, # <- NEW param (to match UI)
|
| 575 |
):
|
| 576 |
+
"""Upscale frames with live progress updates.
|
| 577 |
+
Streams updates like: Upscaling… 20% · 80/100 remaining
|
| 578 |
+
Yields: (gallery|None, zip_path|None, details|str, progress_html|str)
|
| 579 |
+
"""
|
| 580 |
+
if not HAVE_REALESRGAN:
|
| 581 |
+
msg = ("Real-ESRGAN not available. Ensure requirements.txt includes (each on its own line):\n"
|
| 582 |
+
"--prefer-binary\n"
|
| 583 |
+
"numpy==1.26.4\nscipy==1.11.4\nscikit-image==0.22.0\nopencv-python-headless==4.10.0.84\n"
|
| 584 |
+
"torch==2.2.2\nrealesrgan==0.3.0\nbasicsr==1.4.2\npillow\ngradio==5.44.1")
|
| 585 |
+
yield None, None, msg, prog_html
|
| 586 |
+
return
|
| 587 |
|
| 588 |
+
# Choose source: uploaded images take priority, else frames from Step 1
|
| 589 |
if uploaded_imgs and len(uploaded_imgs) > 0:
|
| 590 |
img_paths, _ = save_uploaded_images(uploaded_imgs, prefix="up")
|
| 591 |
src_paths = [str(p) for p in img_paths]
|
|
|
|
| 596 |
yield None, None, "No images provided. Upload files or run Step 1 first.", prog_html
|
| 597 |
return
|
| 598 |
|
| 599 |
+
# Map UI -> internal model id and clamp outscale
|
| 600 |
+
model_id = MODEL_MAP.get(ui_model_name, "x4plus")
|
| 601 |
+
scale = _clamp_scale_for_model(int(outscale or 4), model_id)
|
| 602 |
+
|
| 603 |
device = "cuda" if os.environ.get("CUDA_VISIBLE_DEVICES") else "cpu"
|
| 604 |
half = (precision == "half") and (device == "cuda")
|
| 605 |
upsampler = get_realesrganer(model_id, scale, tile, half, device=device)
|
| 606 |
|
| 607 |
work = Path(tempfile.mkdtemp(prefix="up_"))
|
| 608 |
+
out_dir = work / "upscaled"
|
| 609 |
+
out_dir.mkdir(parents=True, exist_ok=True)
|
| 610 |
|
| 611 |
total = len(src_paths)
|
| 612 |
done = 0
|
| 613 |
up_paths: List[Path] = []
|
|
|
|
| 614 |
last_pct = -1
|
| 615 |
+
|
| 616 |
for fp in src_paths:
|
| 617 |
try:
|
| 618 |
img = Image.open(fp).convert("RGB")
|
| 619 |
+
# (denoise_strength & face_enhance are accepted but not wired in this minimal build)
|
| 620 |
output, _ = upsampler.enhance(np.array(img), outscale=scale)
|
| 621 |
out_img = Image.fromarray(output)
|
| 622 |
out_file = out_dir / (Path(fp).stem + ".jpg")
|
|
|
|
| 624 |
up_paths.append(out_file)
|
| 625 |
except Exception:
|
| 626 |
pass
|
| 627 |
+
|
| 628 |
done += 1
|
| 629 |
if total:
|
| 630 |
pct = int(round((done / total) * 100))
|
|
|
|
| 632 |
if pct != last_pct:
|
| 633 |
label = f"Upscaling… {pct}% · {remaining}/{total} remaining"
|
| 634 |
prog_html = render_progress(pct, label)
|
| 635 |
+
# Stream a progress update (gallery/zip are None until the end)
|
| 636 |
yield None, None, label, prog_html
|
| 637 |
last_pct = pct
|
| 638 |
|
|
|
|
| 641 |
return
|
| 642 |
|
| 643 |
# Final outputs
|
| 644 |
+
up_paths = sorted(up_paths, key=_natural_key)
|
| 645 |
gallery = sample_paths(up_paths, 30)
|
| 646 |
zip_path = work / "upscaled.zip"
|
| 647 |
with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zf:
|
| 648 |
for p in up_paths:
|
| 649 |
zf.write(p, p.name)
|
| 650 |
|
| 651 |
+
final_label = (f"Upscaled: {len(up_paths)} | Model: {ui_model_name}→{model_id} | "
|
| 652 |
+
f"Scale: x{scale} | Tile: {tile} | Precision: {precision}")
|
| 653 |
yield gallery, str(zip_path), final_label, render_progress(100.0, "Upscaling complete")
|
| 654 |
|
| 655 |
# ───────────────── Encode (Step 3) — supports uploaded frames/ZIP & optional audio source
|