multimodalart HF Staff commited on
Commit
01d8a55
Β·
verified Β·
1 Parent(s): b4ba969

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +56 -2
app.py CHANGED
@@ -16,11 +16,13 @@ import os
16
 
17
  os.environ.setdefault("PYTORCH_CUDA_ALLOC_CONF", "expandable_segments:True")
18
 
 
19
  import sys
20
  import tempfile
21
  import time
22
  from pathlib import Path
23
 
 
24
  import spaces # MUST come before torch
25
  import gradio as gr
26
 
@@ -35,7 +37,43 @@ def log(message):
35
  print(f"[viddub] {message}", flush=True)
36
 
37
 
38
- @spaces.GPU(duration=240)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  def dub(video_path: str, audio_path: str, seed: int = 0, harmonize: bool = True,
40
  progress=gr.Progress(track_tqdm=True)):
41
  """Dub a source video with replacement speech.
@@ -51,6 +89,19 @@ def dub(video_path: str, audio_path: str, seed: int = 0, harmonize: bool = True,
51
  start = time.perf_counter()
52
  if not video_path or not audio_path:
53
  raise ValueError("Provide both a source video and replacement speech.")
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  out_path = str(Path(tempfile.mkdtemp()) / "dubbed.mp4")
55
  rt.infer_file(video_path, audio_path, out_path,
56
  seed=int(seed), harmonize=bool(harmonize))
@@ -70,7 +121,10 @@ with gr.Blocks(theme=gr.themes.Citrus(), css=CSS, title="VidDub") as demo:
70
  "# VidDub: Replacement-Aware Distillation for Two-Step Video Dubbing\n"
71
  "Upload a source video and replacement speech β€” the full-context "
72
  "two-step editor re-articulates the mouth while preserving identity, "
73
- "pose, lighting, and background."
 
 
 
74
  )
75
  with gr.Row():
76
  with gr.Column():
 
16
 
17
  os.environ.setdefault("PYTORCH_CUDA_ALLOC_CONF", "expandable_segments:True")
18
 
19
+ import math
20
  import sys
21
  import tempfile
22
  import time
23
  from pathlib import Path
24
 
25
+ import decord
26
  import spaces # MUST come before torch
27
  import gradio as gr
28
 
 
37
  print(f"[viddub] {message}", flush=True)
38
 
39
 
40
+ # Measured on this Space (zero-a10g / ZeroGPU large): one 81-frame window of the
41
+ # two-step sampler costs ~19.5s; the VAE encode/decode and paste-back inside a
42
+ # window dominate. The duration estimator below scales with the number of
43
+ # 81-frame windows the input will need (stride 64, overlap 17, tail re-anchored).
44
+ WINDOW_SECONDS = 19.5
45
+ OVERHEAD_SECONDS = 15
46
+ # ~4.4x the free-visitor quota margin: PRO visitors can run longer clips.
47
+ MAX_SECONDS = 110
48
+ MAX_WINDOWS = 4 # 4 windows covers up to 273 frames (~11.4s at 24 fps)
49
+ MIN_FRAMES, WINDOW_LEN, WINDOW_STRIDE = 81, 81, 64
50
+
51
+
52
+ def _windows(n_frames):
53
+ """Mirror viddub.full_context.inference.windows (overlap 17)."""
54
+ if n_frames < MIN_FRAMES:
55
+ return 0
56
+ starts = list(range(0, n_frames - WINDOW_LEN + 1, WINDOW_STRIDE))
57
+ if starts[-1] + WINDOW_LEN < n_frames:
58
+ starts.append(n_frames - WINDOW_LEN)
59
+ return len(starts)
60
+
61
+
62
+ def _estimate_duration(video_path, *args, **kwargs):
63
+ """ZeroGPU dynamic duration: scale the reservation with the window count.
64
+
65
+ Swallows extra args (audio, seed, harmonize, progress) β€” Gradio passes the
66
+ Progress object positionally and a strict signature would raise.
67
+ """
68
+ try:
69
+ frames = len(decord.VideoReader(str(video_path), num_threads=2))
70
+ except Exception:
71
+ return MAX_SECONDS
72
+ return min(MAX_SECONDS,
73
+ OVERHEAD_SECONDS + math.ceil(_windows(frames) * WINDOW_SECONDS * 1.4))
74
+
75
+
76
+ @spaces.GPU(duration=_estimate_duration)
77
  def dub(video_path: str, audio_path: str, seed: int = 0, harmonize: bool = True,
78
  progress=gr.Progress(track_tqdm=True)):
79
  """Dub a source video with replacement speech.
 
89
  start = time.perf_counter()
90
  if not video_path or not audio_path:
91
  raise ValueError("Provide both a source video and replacement speech.")
92
+ frames = len(decord.VideoReader(str(video_path), num_threads=2))
93
+ windows = _windows(frames)
94
+ if frames < MIN_FRAMES:
95
+ raise ValueError(
96
+ f"Source video has {frames} frames; VidDub needs at least "
97
+ f"{MIN_FRAMES} (a ~3.4s clip at 24 fps).")
98
+ if windows > MAX_WINDOWS:
99
+ # 3 strides + one full window is the longest clip that stays <= 4 windows.
100
+ longest = 3 * WINDOW_STRIDE + WINDOW_LEN
101
+ raise ValueError(
102
+ f"Source video needs {windows} windows (> {MAX_WINDOWS}). Please "
103
+ f"trim to at most {longest} frames "
104
+ f"(~{longest / 24:.1f}s at 24 fps).")
105
  out_path = str(Path(tempfile.mkdtemp()) / "dubbed.mp4")
106
  rt.infer_file(video_path, audio_path, out_path,
107
  seed=int(seed), harmonize=bool(harmonize))
 
121
  "# VidDub: Replacement-Aware Distillation for Two-Step Video Dubbing\n"
122
  "Upload a source video and replacement speech β€” the full-context "
123
  "two-step editor re-articulates the mouth while preserving identity, "
124
+ "pose, lighting, and background.\n\n"
125
+ "**Constraints**: one mostly front-facing face, **81–273 frames** "
126
+ "(β‰ˆ3.4–11.4s at 24 fps β€” the editor runs up to four 81-frame "
127
+ "windows on ZeroGPU). Longer clips: trim first."
128
  )
129
  with gr.Row():
130
  with gr.Column():