Spaces:
Paused
Paused
Update app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061import gradio as grimport subprocessimport osfrom pathlib import Pathdef setup():if Path("setup_done.txt").exists():returnprint("Setting up Video-Retalking...")# Clone repoos.system("git clone https://github.com/OpenTalker/video-retalking.git vrt")# Download modelsos.chdir("vrt")os.system("bash scripts/download_models.sh")os.chdir("..")Path("setup_done.txt").touch()print("β Setup complete!")def generate(image, audio):if not image or not audio:return None, "β Upload both image and audio!"try:setup()output = "result.mp4"# Run inferencecmd = f"cd vrt && python inference.py --face ../{image} --audio ../{audio} --outfile ../{output}"result = os.system(cmd)if Path(output).exists():return output, "β Video generated successfully!"else:return None, "β Failed to generate video"except Exception as e:return None, f"β Error: {str(e)}"# Gradio Interfacedemo = gr.Interface(fn=generate,inputs=[gr.Image(type="filepath", label="π· Face Image"),gr.Audio(type="filepath", label="π΅ Audio File")],outputs=[gr.Video(label="πΉ Generated Video"),gr.Textbox(label="Status", lines=2)],title="π¬ Video-Retalking Lip Sync",description="Upload a face image and audio to generate lip-synced video")if __name__ == "__main__":demo.launch()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101import osimport shutilimport subprocessfrom pathlib import Pathimport gradio as grREPO_DIR = Path("vrt")SETUP_FLAG = Path("setup_done.txt")OUTPUT_DIR = Path("outputs")OUTPUT_DIR.mkdir(exist_ok=True)def run(cmd, cwd=None):"""Run a shell command safely and raise a helpful error if it fails."""p = subprocess.run(cmd,cwd=str(cwd) if cwd else None,stdout=subprocess.PIPE,stderr=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 setup():"""One-time setup: clone repo + download models.Uses a flag file so it doesn't redo work every request."""if SETUP_FLAG.exists() and REPO_DIR.exists():returnprint("Setting up Video-Retalking...")if not REPO_DIR.exists():run(["git", "clone", "https://github.com/OpenTalker/video-retalking.git", str(REPO_DIR)])# Download models (idempotent in most cases)run(["bash", "scripts/download_models.sh"], cwd=REPO_DIR)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!"try:setup()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}"# Unique output name per runout_path = (OUTPUT_DIR / "result.mp4").resolve()# Clean old output if anyif out_path.exists():out_path.unlink()# Run inferencecmd = ["python","inference.py","--face", str(image_path),"--audio", str(audio_path),"--outfile", str(out_path),]run(cmd, cwd=REPO_DIR)if out_path.exists():return str(out_path), "β Video generated successfully!"return None, "β Failed to generate video (no output file created)."except Exception as e:return None, f"β Error: {e}"demo = gr.Interface(fn=generate,inputs=[gr.Image(type="filepath", label="π· Face Image"),gr.Audio(type="filepath", label="π΅ Audio File"),],outputs=[gr.Video(label="πΉ Generated Video"),gr.Textbox(label="Status", lines=3),],title="π¬ Video-Retalking Lip Sync",description="Upload a face image and audio to generate a lip-synced video.",)if __name__ == "__main__":demo.launch()
app.py+75-35