Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,61 +1,101 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import subprocess
|
| 3 |
import os
|
|
|
|
|
|
|
| 4 |
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
def setup():
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
return
|
| 9 |
-
|
| 10 |
print("Setting up Video-Retalking...")
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
# Download models
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
Path("setup_done.txt").touch()
|
| 21 |
print("β
Setup complete!")
|
| 22 |
|
| 23 |
-
def generate(
|
| 24 |
-
if not
|
| 25 |
return None, "β Upload both image and audio!"
|
| 26 |
-
|
| 27 |
try:
|
| 28 |
setup()
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
# Run inference
|
| 33 |
-
cmd =
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
except Exception as e:
|
| 43 |
-
return None, f"β Error: {
|
| 44 |
|
| 45 |
-
# Gradio Interface
|
| 46 |
demo = gr.Interface(
|
| 47 |
fn=generate,
|
| 48 |
inputs=[
|
| 49 |
gr.Image(type="filepath", label="π· Face Image"),
|
| 50 |
-
gr.Audio(type="filepath", label="π΅ Audio File")
|
| 51 |
],
|
| 52 |
outputs=[
|
| 53 |
gr.Video(label="πΉ Generated Video"),
|
| 54 |
-
gr.Textbox(label="Status", lines=
|
| 55 |
],
|
| 56 |
title="π¬ Video-Retalking Lip Sync",
|
| 57 |
-
description="Upload a face image and audio to generate lip-synced video"
|
| 58 |
)
|
| 59 |
|
| 60 |
if __name__ == "__main__":
|
| 61 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import shutil
|
| 3 |
+
import subprocess
|
| 4 |
from pathlib import Path
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
REPO_DIR = Path("vrt")
|
| 8 |
+
SETUP_FLAG = Path("setup_done.txt")
|
| 9 |
+
OUTPUT_DIR = Path("outputs")
|
| 10 |
+
OUTPUT_DIR.mkdir(exist_ok=True)
|
| 11 |
+
|
| 12 |
+
def run(cmd, cwd=None):
|
| 13 |
+
"""
|
| 14 |
+
Run a shell command safely and raise a helpful error if it fails.
|
| 15 |
+
"""
|
| 16 |
+
p = subprocess.run(
|
| 17 |
+
cmd,
|
| 18 |
+
cwd=str(cwd) if cwd else None,
|
| 19 |
+
stdout=subprocess.PIPE,
|
| 20 |
+
stderr=subprocess.STDOUT,
|
| 21 |
+
text=True,
|
| 22 |
+
check=False
|
| 23 |
+
)
|
| 24 |
+
if p.returncode != 0:
|
| 25 |
+
raise RuntimeError(f"Command failed ({p.returncode}): {' '.join(cmd)}\n\n{p.stdout}")
|
| 26 |
+
return p.stdout
|
| 27 |
|
| 28 |
def setup():
|
| 29 |
+
"""
|
| 30 |
+
One-time setup: clone repo + download models.
|
| 31 |
+
Uses a flag file so it doesn't redo work every request.
|
| 32 |
+
"""
|
| 33 |
+
if SETUP_FLAG.exists() and REPO_DIR.exists():
|
| 34 |
return
|
| 35 |
+
|
| 36 |
print("Setting up Video-Retalking...")
|
| 37 |
+
|
| 38 |
+
if not REPO_DIR.exists():
|
| 39 |
+
run(["git", "clone", "https://github.com/OpenTalker/video-retalking.git", str(REPO_DIR)])
|
| 40 |
+
|
| 41 |
+
# Download models (idempotent in most cases)
|
| 42 |
+
run(["bash", "scripts/download_models.sh"], cwd=REPO_DIR)
|
| 43 |
+
|
| 44 |
+
SETUP_FLAG.touch()
|
|
|
|
|
|
|
| 45 |
print("β
Setup complete!")
|
| 46 |
|
| 47 |
+
def generate(image_path, audio_path):
|
| 48 |
+
if not image_path or not audio_path:
|
| 49 |
return None, "β Upload both image and audio!"
|
| 50 |
+
|
| 51 |
try:
|
| 52 |
setup()
|
| 53 |
+
|
| 54 |
+
image_path = Path(image_path).resolve()
|
| 55 |
+
audio_path = Path(audio_path).resolve()
|
| 56 |
+
|
| 57 |
+
if not image_path.exists():
|
| 58 |
+
return None, f"β Image not found: {image_path}"
|
| 59 |
+
if not audio_path.exists():
|
| 60 |
+
return None, f"β Audio not found: {audio_path}"
|
| 61 |
+
|
| 62 |
+
# Unique output name per run
|
| 63 |
+
out_path = (OUTPUT_DIR / "result.mp4").resolve()
|
| 64 |
+
|
| 65 |
+
# Clean old output if any
|
| 66 |
+
if out_path.exists():
|
| 67 |
+
out_path.unlink()
|
| 68 |
+
|
| 69 |
# Run inference
|
| 70 |
+
cmd = [
|
| 71 |
+
"python",
|
| 72 |
+
"inference.py",
|
| 73 |
+
"--face", str(image_path),
|
| 74 |
+
"--audio", str(audio_path),
|
| 75 |
+
"--outfile", str(out_path),
|
| 76 |
+
]
|
| 77 |
+
run(cmd, cwd=REPO_DIR)
|
| 78 |
+
|
| 79 |
+
if out_path.exists():
|
| 80 |
+
return str(out_path), "β
Video generated successfully!"
|
| 81 |
+
return None, "β Failed to generate video (no output file created)."
|
| 82 |
+
|
| 83 |
except Exception as e:
|
| 84 |
+
return None, f"β Error: {e}"
|
| 85 |
|
|
|
|
| 86 |
demo = gr.Interface(
|
| 87 |
fn=generate,
|
| 88 |
inputs=[
|
| 89 |
gr.Image(type="filepath", label="π· Face Image"),
|
| 90 |
+
gr.Audio(type="filepath", label="π΅ Audio File"),
|
| 91 |
],
|
| 92 |
outputs=[
|
| 93 |
gr.Video(label="πΉ Generated Video"),
|
| 94 |
+
gr.Textbox(label="Status", lines=3),
|
| 95 |
],
|
| 96 |
title="π¬ Video-Retalking Lip Sync",
|
| 97 |
+
description="Upload a face image and audio to generate a lip-synced video.",
|
| 98 |
)
|
| 99 |
|
| 100 |
if __name__ == "__main__":
|
| 101 |
+
demo.launch()
|