Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import subprocess
|
| 2 |
import threading
|
| 3 |
from pathlib import Path
|
|
@@ -8,10 +9,8 @@ SETUP_FLAG = Path("setup_done.txt")
|
|
| 8 |
OUTPUT_DIR = Path("outputs")
|
| 9 |
OUTPUT_DIR.mkdir(exist_ok=True)
|
| 10 |
|
| 11 |
-
# Hugging Face mirror containing the checkpoint files (fast + reliable)
|
| 12 |
HF_BASE = "https://huggingface.co/camenduru/video-retalking/resolve/main"
|
| 13 |
|
| 14 |
-
# Files needed by Video-Retalking pipeline (common minimal set)
|
| 15 |
CHECKPOINT_FILES = [
|
| 16 |
"30_net_gen.pth",
|
| 17 |
"BFM.zip",
|
|
@@ -29,8 +28,11 @@ CHECKPOINT_FILES = [
|
|
| 29 |
|
| 30 |
_setup_lock = threading.Lock()
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
def run(cmd, cwd=None):
|
| 34 |
p = subprocess.run(
|
| 35 |
cmd,
|
| 36 |
cwd=str(cwd) if cwd else None,
|
|
@@ -38,47 +40,32 @@ def run(cmd, cwd=None):
|
|
| 38 |
stderr=subprocess.STDOUT,
|
| 39 |
text=True,
|
| 40 |
check=False,
|
|
|
|
| 41 |
)
|
| 42 |
if p.returncode != 0:
|
| 43 |
-
raise RuntimeError(
|
| 44 |
-
f"Command failed ({p.returncode}): {' '.join(cmd)}\n\n{p.stdout}"
|
| 45 |
-
)
|
| 46 |
return p.stdout
|
| 47 |
|
| 48 |
-
|
| 49 |
def download_file(url: str, dest: Path):
|
| 50 |
-
"""
|
| 51 |
-
Download using curl with resume support (-C -) and follow redirects (-L).
|
| 52 |
-
"""
|
| 53 |
dest.parent.mkdir(parents=True, exist_ok=True)
|
| 54 |
-
# Skip if already present
|
| 55 |
if dest.exists() and dest.stat().st_size > 0:
|
| 56 |
return
|
| 57 |
run(["bash", "-lc", f'curl -L -C - --retry 5 --retry-delay 2 -o "{dest}" "{url}"'])
|
| 58 |
|
| 59 |
-
|
| 60 |
def ensure_checkpoints():
|
| 61 |
ckpt_dir = REPO_DIR / "checkpoints"
|
| 62 |
ckpt_dir.mkdir(parents=True, exist_ok=True)
|
| 63 |
|
| 64 |
-
# Download required checkpoint files
|
| 65 |
for fname in CHECKPOINT_FILES:
|
| 66 |
-
|
| 67 |
-
download_file(url, ckpt_dir / fname)
|
| 68 |
|
| 69 |
-
# Unzip BFM.zip -> checkpoints/BFM/
|
| 70 |
bfm_zip = ckpt_dir / "BFM.zip"
|
| 71 |
bfm_dir = ckpt_dir / "BFM"
|
| 72 |
if not bfm_dir.exists():
|
| 73 |
bfm_dir.mkdir(parents=True, exist_ok=True)
|
| 74 |
run(["unzip", "-q", str(bfm_zip), "-d", str(bfm_dir)])
|
| 75 |
|
| 76 |
-
|
| 77 |
def setup():
|
| 78 |
-
"""
|
| 79 |
-
One-time setup: clone repo, pull LFS (best effort), download checkpoints.
|
| 80 |
-
Guarded by a lock to prevent multiple concurrent setup runs.
|
| 81 |
-
"""
|
| 82 |
with _setup_lock:
|
| 83 |
if SETUP_FLAG.exists() and REPO_DIR.exists():
|
| 84 |
return
|
|
@@ -88,11 +75,9 @@ def setup():
|
|
| 88 |
if not REPO_DIR.exists():
|
| 89 |
run(["git", "clone", "https://github.com/OpenTalker/video-retalking.git", str(REPO_DIR)])
|
| 90 |
|
| 91 |
-
# Best-effort: fetch any git-lfs files if repo uses them
|
| 92 |
try:
|
| 93 |
run(["git", "lfs", "pull"], cwd=REPO_DIR)
|
| 94 |
except Exception:
|
| 95 |
-
# Not fatal; we'll fetch checkpoints from HF mirror anyway
|
| 96 |
pass
|
| 97 |
|
| 98 |
ensure_checkpoints()
|
|
@@ -100,7 +85,6 @@ def setup():
|
|
| 100 |
SETUP_FLAG.touch()
|
| 101 |
print("β
Setup complete!")
|
| 102 |
|
| 103 |
-
|
| 104 |
def generate(image_path, audio_path):
|
| 105 |
if not image_path or not audio_path:
|
| 106 |
return None, "β Upload both image and audio!"
|
|
@@ -111,15 +95,18 @@ def generate(image_path, audio_path):
|
|
| 111 |
image_path = Path(image_path).resolve()
|
| 112 |
audio_path = Path(audio_path).resolve()
|
| 113 |
|
| 114 |
-
if not image_path.exists():
|
| 115 |
-
return None, f"β Image not found: {image_path}"
|
| 116 |
-
if not audio_path.exists():
|
| 117 |
-
return None, f"β Audio not found: {audio_path}"
|
| 118 |
-
|
| 119 |
out_path = (OUTPUT_DIR / "result.mp4").resolve()
|
| 120 |
if out_path.exists():
|
| 121 |
out_path.unlink()
|
| 122 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
cmd = [
|
| 124 |
"python",
|
| 125 |
"inference.py",
|
|
@@ -127,7 +114,7 @@ def generate(image_path, audio_path):
|
|
| 127 |
"--audio", str(audio_path),
|
| 128 |
"--outfile", str(out_path),
|
| 129 |
]
|
| 130 |
-
run(cmd, cwd=REPO_DIR)
|
| 131 |
|
| 132 |
if out_path.exists():
|
| 133 |
return str(out_path), "β
Video generated successfully!"
|
|
@@ -136,7 +123,6 @@ def generate(image_path, audio_path):
|
|
| 136 |
except Exception as e:
|
| 137 |
return None, f"β Error: {e}"
|
| 138 |
|
| 139 |
-
|
| 140 |
demo = gr.Interface(
|
| 141 |
fn=generate,
|
| 142 |
inputs=[
|
|
@@ -145,7 +131,7 @@ demo = gr.Interface(
|
|
| 145 |
],
|
| 146 |
outputs=[
|
| 147 |
gr.Video(label="πΉ Generated Video"),
|
| 148 |
-
gr.Textbox(label="Status", lines=
|
| 149 |
],
|
| 150 |
title="π¬ Video-Retalking Lip Sync",
|
| 151 |
description="Upload a face image and audio to generate a lip-synced video.",
|
|
|
|
| 1 |
+
import os
|
| 2 |
import subprocess
|
| 3 |
import threading
|
| 4 |
from pathlib import Path
|
|
|
|
| 9 |
OUTPUT_DIR = Path("outputs")
|
| 10 |
OUTPUT_DIR.mkdir(exist_ok=True)
|
| 11 |
|
|
|
|
| 12 |
HF_BASE = "https://huggingface.co/camenduru/video-retalking/resolve/main"
|
| 13 |
|
|
|
|
| 14 |
CHECKPOINT_FILES = [
|
| 15 |
"30_net_gen.pth",
|
| 16 |
"BFM.zip",
|
|
|
|
| 28 |
|
| 29 |
_setup_lock = threading.Lock()
|
| 30 |
|
| 31 |
+
def run(cmd, cwd=None, extra_env=None):
|
| 32 |
+
env = os.environ.copy()
|
| 33 |
+
if extra_env:
|
| 34 |
+
env.update(extra_env)
|
| 35 |
|
|
|
|
| 36 |
p = subprocess.run(
|
| 37 |
cmd,
|
| 38 |
cwd=str(cwd) if cwd else None,
|
|
|
|
| 40 |
stderr=subprocess.STDOUT,
|
| 41 |
text=True,
|
| 42 |
check=False,
|
| 43 |
+
env=env,
|
| 44 |
)
|
| 45 |
if p.returncode != 0:
|
| 46 |
+
raise RuntimeError(f"Command failed ({p.returncode}): {' '.join(cmd)}\n\n{p.stdout}")
|
|
|
|
|
|
|
| 47 |
return p.stdout
|
| 48 |
|
|
|
|
| 49 |
def download_file(url: str, dest: Path):
|
|
|
|
|
|
|
|
|
|
| 50 |
dest.parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
| 51 |
if dest.exists() and dest.stat().st_size > 0:
|
| 52 |
return
|
| 53 |
run(["bash", "-lc", f'curl -L -C - --retry 5 --retry-delay 2 -o "{dest}" "{url}"'])
|
| 54 |
|
|
|
|
| 55 |
def ensure_checkpoints():
|
| 56 |
ckpt_dir = REPO_DIR / "checkpoints"
|
| 57 |
ckpt_dir.mkdir(parents=True, exist_ok=True)
|
| 58 |
|
|
|
|
| 59 |
for fname in CHECKPOINT_FILES:
|
| 60 |
+
download_file(f"{HF_BASE}/{fname}", ckpt_dir / fname)
|
|
|
|
| 61 |
|
|
|
|
| 62 |
bfm_zip = ckpt_dir / "BFM.zip"
|
| 63 |
bfm_dir = ckpt_dir / "BFM"
|
| 64 |
if not bfm_dir.exists():
|
| 65 |
bfm_dir.mkdir(parents=True, exist_ok=True)
|
| 66 |
run(["unzip", "-q", str(bfm_zip), "-d", str(bfm_dir)])
|
| 67 |
|
|
|
|
| 68 |
def setup():
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
with _setup_lock:
|
| 70 |
if SETUP_FLAG.exists() and REPO_DIR.exists():
|
| 71 |
return
|
|
|
|
| 75 |
if not REPO_DIR.exists():
|
| 76 |
run(["git", "clone", "https://github.com/OpenTalker/video-retalking.git", str(REPO_DIR)])
|
| 77 |
|
|
|
|
| 78 |
try:
|
| 79 |
run(["git", "lfs", "pull"], cwd=REPO_DIR)
|
| 80 |
except Exception:
|
|
|
|
| 81 |
pass
|
| 82 |
|
| 83 |
ensure_checkpoints()
|
|
|
|
| 85 |
SETUP_FLAG.touch()
|
| 86 |
print("β
Setup complete!")
|
| 87 |
|
|
|
|
| 88 |
def generate(image_path, audio_path):
|
| 89 |
if not image_path or not audio_path:
|
| 90 |
return None, "β Upload both image and audio!"
|
|
|
|
| 95 |
image_path = Path(image_path).resolve()
|
| 96 |
audio_path = Path(audio_path).resolve()
|
| 97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
out_path = (OUTPUT_DIR / "result.mp4").resolve()
|
| 99 |
if out_path.exists():
|
| 100 |
out_path.unlink()
|
| 101 |
|
| 102 |
+
# Fix OMP warning by forcing a valid integer value
|
| 103 |
+
safe_env = {
|
| 104 |
+
"OMP_NUM_THREADS": "1",
|
| 105 |
+
"MKL_NUM_THREADS": "1",
|
| 106 |
+
"OPENBLAS_NUM_THREADS": "1",
|
| 107 |
+
"NUMEXPR_NUM_THREADS": "1",
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
cmd = [
|
| 111 |
"python",
|
| 112 |
"inference.py",
|
|
|
|
| 114 |
"--audio", str(audio_path),
|
| 115 |
"--outfile", str(out_path),
|
| 116 |
]
|
| 117 |
+
run(cmd, cwd=REPO_DIR, extra_env=safe_env)
|
| 118 |
|
| 119 |
if out_path.exists():
|
| 120 |
return str(out_path), "β
Video generated successfully!"
|
|
|
|
| 123 |
except Exception as e:
|
| 124 |
return None, f"β Error: {e}"
|
| 125 |
|
|
|
|
| 126 |
demo = gr.Interface(
|
| 127 |
fn=generate,
|
| 128 |
inputs=[
|
|
|
|
| 131 |
],
|
| 132 |
outputs=[
|
| 133 |
gr.Video(label="πΉ Generated Video"),
|
| 134 |
+
gr.Textbox(label="Status", lines=5),
|
| 135 |
],
|
| 136 |
title="π¬ Video-Retalking Lip Sync",
|
| 137 |
description="Upload a face image and audio to generate a lip-synced video.",
|