Spaces:
Paused
Paused
Update app.py
Browse files1234 unmodified lines89101112131415161711 unmodified lines29303132333435361 unmodified line38394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283843 unmodified lines88899091929394959697981 unmodified line1001011021031041051064 unmodified lines1111121131141151161171181191201211221231241251 unmodified line1271281291301311321332 unmodified lines1361371381391401411422 unmodified lines145146147148149150151import 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):p = subprocess.run(cmd,cwd=str(cwd) if cwd else None,1 unmodified linestderr=subprocess.STDOUT,text=True,check=False,)if p.returncode != 0:raise RuntimeError(f"Command failed ({p.returncode}): {' '.join(cmd)}\n\n{p.stdout}")return p.stdoutdef 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 presentif dest.exists() and dest.stat().st_size > 0:returnrun(["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 filesfor fname in CHECKPOINT_FILES:url = f"{HF_BASE}/{fname}"download_file(url, 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 linesif 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 themtry:run(["git", "lfs", "pull"], cwd=REPO_DIR)except Exception:# Not fatal; we'll fetch checkpoints from HF mirror anywaypassensure_checkpoints()1 unmodified lineSETUP_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 linesimage_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()cmd = ["python","inference.py",1 unmodified line"--audio", str(audio_path),"--outfile", str(out_path),]run(cmd, cwd=REPO_DIR)if out_path.exists():return str(out_path), "β Video generated successfully!"2 unmodified linesexcept 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),],title="π¬ Video-Retalking Lip Sync",description="Upload a face image and audio to generate a lip-synced video.",12344 unmodified lines91011121314151611 unmodified lines28293031323334353637381 unmodified line40414243444546474849505152535455565758596061626364656667686970713 unmodified lines7576777879808182831 unmodified line8586878889904 unmodified lines95969798991001011021031041051061071081091101111121 unmodified line1141151161171181191202 unmodified lines1231241251261271282 unmodified lines131132133134135136137import osimport subprocessimport threadingfrom pathlib import Path4 unmodified linesOUTPUT_DIR = Path("outputs")OUTPUT_DIR.mkdir(exist_ok=True)HF_BASE = "https://huggingface.co/camenduru/video-retalking/resolve/main"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)p = subprocess.run(cmd,cwd=str(cwd) if cwd else None,1 unmodified linestderr=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}")return p.stdoutdef download_file(url: str, dest: Path):dest.parent.mkdir(parents=True, exist_ok=True)if dest.exists() and dest.stat().st_size > 0:returnrun(["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)for fname in CHECKPOINT_FILES:download_file(f"{HF_BASE}/{fname}", ckpt_dir / fname)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():with _setup_lock:if SETUP_FLAG.exists() and REPO_DIR.exists():return3 unmodified linesif not REPO_DIR.exists():run(["git", "clone", "https://github.com/OpenTalker/video-retalking.git", str(REPO_DIR)])try:run(["git", "lfs", "pull"], cwd=REPO_DIR)except Exception:passensure_checkpoints()1 unmodified lineSETUP_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 linesimage_path = Path(image_path).resolve()audio_path = Path(audio_path).resolve()out_path = (OUTPUT_DIR / "result.mp4").resolve()if out_path.exists():out_path.unlink()# Fix OMP warning by forcing a valid integer valuesafe_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, extra_env=safe_env)if out_path.exists():return str(out_path), "β Video generated successfully!"2 unmodified linesexcept 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=5),],title="π¬ Video-Retalking Lip Sync",description="Upload a face image and audio to generate a lip-synced video.",
app.py+18-32