Spaces:
Paused
Paused
Update app.py
Browse files12344 unmodified lines910111112131414151611 unmodified lines2829303132333435333637381 unmodified line40414243444543444546474848495051525054515253545955565758645966676061696263646566677668787980816970713 unmodified lines75767791787980958182831 unmodified line8586871038889904 unmodified lines95969711411511611711898991001011021031041051061071081091101111121 unmodified line1141151161301171181191202 unmodified lines1231241251391261271282 unmodified lines131132133148134135136137import osimport subprocessimport threadingfrom pathlib import Path4 unmodified linesOUTPUT_DIR = Path("outputs")OUTPUT_DIR.mkdir(exist_ok=True)
# Hugging Face mirror containing the checkpoint files (fast + reliable)HF_BASE = "https://huggingface.co/camenduru/video-retalking/resolve/main"
# Files needed by Video-Retalking pipeline (common minimal set)CHECKPOINT_FILES = [ "30_net_gen.pth", "BFM.zip",11 unmodified lines
_setup_lock = threading.Lock()
def run(cmd, cwd=None, extra_env=None): env = os.environ.copy() if extra_env: env.update(extra_env)
def run(cmd, cwd=None): p = subprocess.run( cmd, cwd=str(cwd) if cwd else None,1 unmodified line stderr=subprocess.STDOUT, text=True, check=False, env=env, ) if p.returncode != 0: raise RuntimeError( f"Command failed ({p.returncode}): {' '.join(cmd)}\n\n{p.stdout}" ) raise RuntimeError(f"Command failed ({p.returncode}): {' '.join(cmd)}\n\n{p.stdout}") return p.stdout
def download_file(url: str, dest: Path): """ Download using curl with resume support (-C -) and follow redirects (-L). """ dest.parent.mkdir(parents=True, exist_ok=True) # Skip if already present if dest.exists() and dest.stat().st_size > 0: return run(["bash", "-lc", f'curl -L -C - --retry 5 --retry-delay 2 -o "{dest}" "{url}"'])
def ensure_checkpoints(): ckpt_dir = REPO_DIR / "checkpoints" ckpt_dir.mkdir(parents=True, exist_ok=True)
# Download required checkpoint files for fname in CHECKPOINT_FILES: url = f"{HF_BASE}/{fname}" download_file(url, ckpt_dir / fname) download_file(f"{HF_BASE}/{fname}", ckpt_dir / fname)
# Unzip BFM.zip -> checkpoints/BFM/ bfm_zip = ckpt_dir / "BFM.zip" bfm_dir = ckpt_dir / "BFM" if not bfm_dir.exists(): bfm_dir.mkdir(parents=True, exist_ok=True) run(["unzip", "-q", str(bfm_zip), "-d", str(bfm_dir)])
def setup(): """ One-time setup: clone repo, pull LFS (best effort), download checkpoints. Guarded by a lock to prevent multiple concurrent setup runs. """ with _setup_lock: if SETUP_FLAG.exists() and REPO_DIR.exists(): return3 unmodified lines if not REPO_DIR.exists(): run(["git", "clone", "https://github.com/OpenTalker/video-retalking.git", str(REPO_DIR)])
# Best-effort: fetch any git-lfs files if repo uses them try: run(["git", "lfs", "pull"], cwd=REPO_DIR) except Exception: # Not fatal; we'll fetch checkpoints from HF mirror anyway pass
ensure_checkpoints()1 unmodified line SETUP_FLAG.touch() print("β
Setup complete!")
def generate(image_path, audio_path): if not image_path or not audio_path: return None, "β Upload both image and audio!"4 unmodified lines image_path = Path(image_path).resolve() audio_path = Path(audio_path).resolve()
if not image_path.exists(): return None, f"β Image not found: {image_path}" if not audio_path.exists(): return None, f"β Audio not found: {audio_path}"
out_path = (OUTPUT_DIR / "result.mp4").resolve() if out_path.exists(): out_path.unlink()
# Fix OMP warning by forcing a valid integer value safe_env = { "OMP_NUM_THREADS": "1", "MKL_NUM_THREADS": "1", "OPENBLAS_NUM_THREADS": "1", "NUMEXPR_NUM_THREADS": "1", }
cmd = [ "python", "inference.py",1 unmodified line "--audio", str(audio_path), "--outfile", str(out_path), ] run(cmd, cwd=REPO_DIR) run(cmd, cwd=REPO_DIR, extra_env=safe_env)
if out_path.exists(): return str(out_path), "β
Video generated successfully!"2 unmodified lines except Exception as e: return None, f"β Error: {e}"
demo = gr.Interface( fn=generate, inputs=[2 unmodified lines ], outputs=[ gr.Video(label="πΉ Generated Video"), gr.Textbox(label="Status", lines=4), gr.Textbox(label="Status", lines=5), ], title="π¬ Video-Retalking Lip Sync", description="Upload a face image and audio to generate a lip-synced video.",app.py+18-32