Spaces:
Sleeping
Sleeping
File size: 9,808 Bytes
110fc0e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 | # app.py
# AI Video Enhancer 4K - Gradio app for Hugging Face Spaces
# With batched GPU processing to avoid ZeroGPU timeout
import os
import shutil
import subprocess
import tempfile
import time
from pathlib import Path
from typing import Tuple, List
import gradio as gr
import spaces
import torch
import numpy as np
from PIL import Image
import cv2
from huggingface_hub import hf_hub_download
# Config
TEMP_DIR = Path(tempfile.gettempdir()) / "hf_video_enhancer"
TEMP_DIR.mkdir(parents=True, exist_ok=True)
# Cache model path globally so we don't re-download
_model_cache = {}
def run_cmd(cmd):
p = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if p.returncode != 0:
raise RuntimeError(f"Command failed: {p.stderr.decode()}")
return p.stdout.decode()
def probe_video(video_path: str) -> Tuple[float, int, int, float]:
cmd = [
"ffprobe", "-v", "error",
"-select_streams", "v:0",
"-show_entries", "stream=width,height,duration,r_frame_rate",
"-of", "default=noprint_wrappers=1:nokey=0",
video_path
]
p = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out = p.stdout.decode()
width = height = 0
duration = 0.0
fps = 30.0
for line in out.splitlines():
if line.startswith("width="):
width = int(line.split("=")[1])
elif line.startswith("height="):
height = int(line.split("=")[1])
elif line.startswith("duration="):
try:
duration = float(line.split("=")[1])
except:
pass
elif line.startswith("r_frame_rate="):
try:
fps_str = line.split("=")[1]
if "/" in fps_str:
num, den = fps_str.split("/")
fps = float(num) / float(den)
else:
fps = float(fps_str)
except:
pass
return duration, width, height, fps
def extract_frames(video_path: str, frames_dir: Path):
frames_dir.mkdir(parents=True, exist_ok=True)
run_cmd([
"ffmpeg", "-y", "-i", video_path,
"-vsync", "0",
str(frames_dir / "%06d.png")
])
def reassemble_video(frames_dir: Path, audio_src: str, out_path: str, fps: float = 30.0):
tmp_video = str(frames_dir.parent / "tmp_video.mp4")
run_cmd([
"ffmpeg", "-y", "-framerate", str(fps),
"-i", str(frames_dir / "%06d.png"),
"-c:v", "libx264", "-preset", "veryfast", "-pix_fmt", "yuv420p",
"-crf", "18", tmp_video
])
p = subprocess.run(
["ffprobe", "-v", "error", "-select_streams", "a", "-show_entries",
"stream=codec_type", "-of", "default=noprint_wrappers=1", audio_src],
stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
if p.stdout.decode().strip():
run_cmd([
"ffmpeg", "-y", "-i", tmp_video, "-i", audio_src,
"-c:v", "copy", "-c:a", "aac",
"-map", "0:v:0", "-map", "1:a:0", out_path
])
os.remove(tmp_video)
else:
shutil.move(tmp_video, out_path)
def simple_upscale(img: np.ndarray, scale: int) -> np.ndarray:
"""Simple bicubic upscaling using OpenCV"""
h, w = img.shape[:2]
return cv2.resize(img, (w * scale, h * scale), interpolation=cv2.INTER_CUBIC)
def get_model_path(scale: int) -> str:
"""Download model weights (cached)"""
global _model_cache
if scale not in _model_cache:
if scale == 2:
_model_cache[scale] = hf_hub_download(repo_id="ai-forever/Real-ESRGAN", filename="RealESRGAN_x2.pth")
else:
_model_cache[scale] = hf_hub_download(repo_id="ai-forever/Real-ESRGAN", filename="RealESRGAN_x4.pth")
return _model_cache[scale]
@spaces.GPU(duration=120)
def enhance_batch(frame_paths: List[str], scale: int = 4) -> int:
"""
Enhance a SMALL BATCH of frames using Real-ESRGAN.
Called multiple times for different batches to avoid timeout.
"""
from spandrel import ImageModelDescriptor, ModelLoader
if not frame_paths:
return 0
model_path = get_model_path(scale)
model = ModelLoader().load_from_file(model_path)
assert isinstance(model, ImageModelDescriptor)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = model.to(device).eval()
processed = 0
for frame_path in frame_paths:
img = cv2.imread(frame_path)
if img is None:
continue
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
tensor = torch.from_numpy(img_rgb).permute(2, 0, 1).float().div(255.0)
tensor = tensor.unsqueeze(0).to(device)
with torch.no_grad():
output = model(tensor)
output = output.squeeze(0).cpu().clamp(0, 1).mul(255).byte()
output = output.permute(1, 2, 0).numpy()
output_bgr = cv2.cvtColor(output, cv2.COLOR_RGB2BGR)
cv2.imwrite(frame_path, output_bgr)
processed += 1
return processed
def process_video(video_file, scale: int = 4, progress=gr.Progress()) -> Tuple[str, str]:
"""Main video processing - handles file I/O outside GPU function"""
if video_file is None:
return "⚠️ Please upload a video file.", None
ts = int(time.time() * 1000)
base_dir = TEMP_DIR / f"job_{ts}"
base_dir.mkdir(parents=True, exist_ok=True)
in_path = base_dir / "input_video"
try:
shutil.copy(video_file, in_path)
except Exception as e:
return f"Error: {e}", None
try:
duration, w, h, fps = probe_video(str(in_path))
except Exception as e:
shutil.rmtree(base_dir, ignore_errors=True)
return f"Error probing video: {e}", None
if duration <= 0:
shutil.rmtree(base_dir, ignore_errors=True)
return "Could not determine video duration.", None
# Limit for ZeroGPU - process max ~15 seconds of video
max_frames = int(fps * 15)
progress(0.05, f"Video: {w}x{h}, {duration:.1f}s")
frames_dir = base_dir / "frames"
try:
extract_frames(str(in_path), frames_dir)
except Exception as e:
shutil.rmtree(base_dir, ignore_errors=True)
return f"Failed extracting frames: {e}", None
frame_files = sorted(frames_dir.glob("*.png"))
num_frames = len(frame_files)
# Limit frames if too many
if num_frames > max_frames:
progress(0.1, f"Limiting to {max_frames} frames...")
for f in frame_files[max_frames:]:
f.unlink()
frame_files = frame_files[:max_frames]
num_frames = max_frames
progress(0.15, f"Enhancing {num_frames} frames...")
# Pre-download model (outside GPU call)
try:
get_model_path(scale)
except Exception as e:
print(f"Model download failed: {e}")
# Process in SMALL BATCHES (10 frames per GPU call to avoid timeout)
batch_size = 10
total_enhanced = 0
use_fallback = False
for batch_start in range(0, num_frames, batch_size):
batch_end = min(batch_start + batch_size, num_frames)
batch_paths = [str(f) for f in frame_files[batch_start:batch_end]]
if not use_fallback:
try:
enhanced = enhance_batch(batch_paths, scale)
total_enhanced += enhanced
print(f"Batch {batch_start}-{batch_end}: enhanced {enhanced} frames")
except Exception as e:
print(f"GPU batch failed: {e}, switching to fallback...")
use_fallback = True
if use_fallback:
# Fallback to CPU upscaling for this batch
for fp in batch_paths:
img = cv2.imread(fp)
if img is not None:
upscaled = simple_upscale(img, scale)
cv2.imwrite(fp, upscaled)
total_enhanced += 1
# Update progress
pct = 0.15 + 0.75 * (batch_end / num_frames)
progress(pct, f"Processed {batch_end}/{num_frames} frames")
progress(0.92, "Creating video...")
out_video = base_dir / "enhanced_output.mp4"
try:
reassemble_video(frames_dir, str(in_path), str(out_video), fps)
except Exception as e:
shutil.rmtree(base_dir, ignore_errors=True)
return f"Failed reassembling: {e}", None
shutil.rmtree(frames_dir, ignore_errors=True)
try:
_, out_w, out_h, _ = probe_video(str(out_video))
method = "bicubic" if use_fallback else "Real-ESRGAN"
progress(1.0, "Done!")
return f"✅ Done! {w}x{h} → {out_w}x{out_h} ({method})", str(out_video)
except:
return "✅ Done!", str(out_video)
# Gradio UI
with gr.Blocks(title="AI Video Enhancer", theme=gr.themes.Soft()) as demo:
gr.Markdown("# 🎬 AI Video Enhancer")
gr.Markdown("Upscale videos using Real-ESRGAN AI enhancement.")
# LOGIN BUTTON - This allows ZeroGPU to recognize your Pro account
gr.LoginButton()
with gr.Row():
with gr.Column(scale=2):
video_in = gr.File(label="Upload video", file_types=[".mp4", ".avi", ".mov", ".mkv", ".webm"])
scale_choice = gr.Radio(choices=[2, 4], value=4, label="Upscale Factor")
btn = gr.Button("🚀 Enhance", variant="primary")
status = gr.Textbox(label="Status", interactive=False)
with gr.Column(scale=1):
out_video = gr.Video(label="Result")
gr.Markdown("**Note:** Limited to ~15 seconds for ZeroGPU. Longer videos will be truncated.")
btn.click(fn=process_video, inputs=[video_in, scale_choice], outputs=[status, out_video])
if __name__ == "__main__":
demo.launch()
|