Update app.py
Browse files
app.py
CHANGED
|
@@ -46,6 +46,71 @@ from basicsr.utils.download_util import load_file_from_url
|
|
| 46 |
from realesrgan import RealESRGANer
|
| 47 |
from realesrgan.archs.srvgg_arch import SRVGGNetCompact
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
# ─────────────────────────────────────────────────────────────
|
| 50 |
# System checks & deps
|
| 51 |
# ─────────────────────────────────────────────────────────────
|
|
@@ -58,12 +123,12 @@ FFMPEG = _which("ffmpeg")
|
|
| 58 |
FFPROBE = _which("ffprobe")
|
| 59 |
|
| 60 |
if not FFMPEG or not FFPROBE:
|
| 61 |
-
MISSING_MSG = (
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
)
|
| 68 |
else:
|
| 69 |
MISSING_MSG = ""
|
|
@@ -299,9 +364,13 @@ def realesrgan(img, model_name, denoise_strength, face_enhance, outscale):
|
|
| 299 |
channel_multiplier=2,
|
| 300 |
bg_upsampler=upsampler
|
| 301 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 302 |
|
| 303 |
# ----- PIL -> cv2 -----
|
| 304 |
-
cv_img =
|
| 305 |
if cv_img.ndim == 3 and cv_img.shape[2] == 4:
|
| 306 |
cv_img = cv2.cvtColor(cv_img, cv2.COLOR_RGBA2BGRA)
|
| 307 |
else:
|
|
@@ -417,9 +486,7 @@ def step1_extract(
|
|
| 417 |
err = proc.stderr.read() if proc.stderr else ""
|
| 418 |
except Exception:
|
| 419 |
err = ""
|
| 420 |
-
return None, None, f"FFmpeg error or no frames produced
|
| 421 |
-
|
| 422 |
-
{err}", cmd_preview, last_html, None, None, None
|
| 423 |
|
| 424 |
gallery = sample_paths(frames, 30)
|
| 425 |
zip_path = work / "frames.zip"
|
|
@@ -475,7 +542,7 @@ def step2_upscale(
|
|
| 475 |
|
| 476 |
device = "cuda" if os.environ.get("CUDA_VISIBLE_DEVICES") else "cpu"
|
| 477 |
half = (precision == "half") and (device == "cuda")
|
| 478 |
-
upsampler = get_realesrganer(
|
| 479 |
|
| 480 |
work = Path(tempfile.mkdtemp(prefix="up_"))
|
| 481 |
out_dir = work / "upscaled"; out_dir.mkdir(parents=True, exist_ok=True)
|
|
@@ -595,9 +662,8 @@ def step3_encode(frames_dir_state: str | None, prefix_state: str | None, orig_vi
|
|
| 595 |
err = proc.stderr.read() if proc.stderr else ""
|
| 596 |
except Exception:
|
| 597 |
err = ""
|
| 598 |
-
return None, None, f"Encoding failed
|
| 599 |
-
|
| 600 |
-
{err}", last_html
|
| 601 |
return str(out_file), f"Video created: {out_file.name}", render_progress(100.0, "Encoding complete")
|
| 602 |
|
| 603 |
# ───────────────── Quick Mode — one click: All frames → Upscale ×4 → MP4 (audio)
|
|
|
|
| 46 |
from realesrgan import RealESRGANer
|
| 47 |
from realesrgan.archs.srvgg_arch import SRVGGNetCompact
|
| 48 |
|
| 49 |
+
# Flag so UI can know if realesrgan is importable
|
| 50 |
+
HAVE_REALESRGAN = True
|
| 51 |
+
|
| 52 |
+
def build_rrdb(scale: int, num_block: int):
|
| 53 |
+
# simple wrapper to the imported RRDBNet class
|
| 54 |
+
return _RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=num_block, num_grow_ch=32, scale=scale)
|
| 55 |
+
|
| 56 |
+
def _weights_dir() -> str:
|
| 57 |
+
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 58 |
+
wdir = os.path.join(ROOT_DIR, "weights")
|
| 59 |
+
os.makedirs(wdir, exist_ok=True)
|
| 60 |
+
return wdir
|
| 61 |
+
|
| 62 |
+
def get_realesrganer(model_id: str, scale: int, tile: int, half: bool, device: str = "cpu") -> RealESRGANer:
|
| 63 |
+
"""
|
| 64 |
+
model_id: one of {"x4plus", "x4plus-anime", "x2plus"}
|
| 65 |
+
Downloads weights if missing, builds the proper arch, and returns a RealESRGANer.
|
| 66 |
+
"""
|
| 67 |
+
wdir = _weights_dir()
|
| 68 |
+
|
| 69 |
+
if model_id == "x4plus":
|
| 70 |
+
model = build_rrdb(scale=4, num_block=23)
|
| 71 |
+
netscale = 4
|
| 72 |
+
urls = ["https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth"]
|
| 73 |
+
model_path = os.path.join(wdir, "RealESRGAN_x4plus.pth")
|
| 74 |
+
dni_weight = None
|
| 75 |
+
elif model_id == "x4plus-anime":
|
| 76 |
+
model = build_rrdb(scale=4, num_block=6)
|
| 77 |
+
netscale = 4
|
| 78 |
+
urls = ["https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.2.4/RealESRGAN_x4plus_anime_6B.pth"]
|
| 79 |
+
model_path = os.path.join(wdir, "RealESRGAN_x4plus_anime_6B.pth")
|
| 80 |
+
dni_weight = None
|
| 81 |
+
elif model_id == "x2plus":
|
| 82 |
+
model = build_rrdb(scale=2, num_block=23)
|
| 83 |
+
netscale = 2
|
| 84 |
+
urls = ["https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.1/RealESRGAN_x2plus.pth"]
|
| 85 |
+
model_path = os.path.join(wdir, "RealESRGAN_x2plus.pth")
|
| 86 |
+
dni_weight = None
|
| 87 |
+
else:
|
| 88 |
+
raise ValueError(f"Unknown model_id: {model_id}")
|
| 89 |
+
|
| 90 |
+
# ensure weights on disk
|
| 91 |
+
for url in urls:
|
| 92 |
+
fname = os.path.basename(url)
|
| 93 |
+
local_path = os.path.join(wdir, fname)
|
| 94 |
+
if not os.path.isfile(local_path):
|
| 95 |
+
load_file_from_url(url=url, model_dir=wdir, progress=True)
|
| 96 |
+
|
| 97 |
+
# Use GPU if visible; otherwise CPU
|
| 98 |
+
gpu_id = 0 if (device == "cuda") else None
|
| 99 |
+
|
| 100 |
+
upsampler = RealESRGANer(
|
| 101 |
+
scale=netscale,
|
| 102 |
+
model_path=model_path,
|
| 103 |
+
dni_weight=dni_weight,
|
| 104 |
+
model=model,
|
| 105 |
+
tile=tile or 256,
|
| 106 |
+
tile_pad=10,
|
| 107 |
+
pre_pad=10,
|
| 108 |
+
half=bool(half and device == "cuda"),
|
| 109 |
+
gpu_id=gpu_id
|
| 110 |
+
)
|
| 111 |
+
return upsampler
|
| 112 |
+
|
| 113 |
+
|
| 114 |
# ─────────────────────────────────────────────────────────────
|
| 115 |
# System checks & deps
|
| 116 |
# ─────────────────────────────────────────────────────────────
|
|
|
|
| 123 |
FFPROBE = _which("ffprobe")
|
| 124 |
|
| 125 |
if not FFMPEG or not FFPROBE:
|
| 126 |
+
MISSING_MSG = (
|
| 127 |
+
"⚠️ FFmpeg not found. Add a 'packages.txt' with exactly:\n"
|
| 128 |
+
"ffmpeg\n"
|
| 129 |
+
"libsm6\n"
|
| 130 |
+
"libxext6\n"
|
| 131 |
+
"Then restart the Space."
|
| 132 |
)
|
| 133 |
else:
|
| 134 |
MISSING_MSG = ""
|
|
|
|
| 364 |
channel_multiplier=2,
|
| 365 |
bg_upsampler=upsampler
|
| 366 |
)
|
| 367 |
+
import random, string
|
| 368 |
+
|
| 369 |
+
def rnd_string(n: int = 8) -> str:
|
| 370 |
+
return "".join(random.choice(string.ascii_lowercase + string.digits) for _ in range(n))
|
| 371 |
|
| 372 |
# ----- PIL -> cv2 -----
|
| 373 |
+
cv_img = np.array(img)
|
| 374 |
if cv_img.ndim == 3 and cv_img.shape[2] == 4:
|
| 375 |
cv_img = cv2.cvtColor(cv_img, cv2.COLOR_RGBA2BGRA)
|
| 376 |
else:
|
|
|
|
| 486 |
err = proc.stderr.read() if proc.stderr else ""
|
| 487 |
except Exception:
|
| 488 |
err = ""
|
| 489 |
+
return None, None, f"FFmpeg error or no frames produced.\\n\\n{err}", cmd_preview, last_html, None, None, None
|
|
|
|
|
|
|
| 490 |
|
| 491 |
gallery = sample_paths(frames, 30)
|
| 492 |
zip_path = work / "frames.zip"
|
|
|
|
| 542 |
|
| 543 |
device = "cuda" if os.environ.get("CUDA_VISIBLE_DEVICES") else "cpu"
|
| 544 |
half = (precision == "half") and (device == "cuda")
|
| 545 |
+
upsampler = get_realesrganer(model_id, scale, tile, half, device=device)
|
| 546 |
|
| 547 |
work = Path(tempfile.mkdtemp(prefix="up_"))
|
| 548 |
out_dir = work / "upscaled"; out_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
| 662 |
err = proc.stderr.read() if proc.stderr else ""
|
| 663 |
except Exception:
|
| 664 |
err = ""
|
| 665 |
+
return None, None, f"Encoding failed.\\n\\n{err}", last_html
|
| 666 |
+
|
|
|
|
| 667 |
return str(out_file), f"Video created: {out_file.name}", render_progress(100.0, "Encoding complete")
|
| 668 |
|
| 669 |
# ───────────────── Quick Mode — one click: All frames → Upscale ×4 → MP4 (audio)
|