Spaces:
Running
Running
update
Browse files
app.py
CHANGED
|
@@ -3,93 +3,87 @@ import subprocess
|
|
| 3 |
import gdown
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
-
# -----------------------------------------
|
| 7 |
-
# AUTO CLONE SADTALKER
|
| 8 |
-
# -----------------------------------------
|
| 9 |
-
|
| 10 |
if not os.path.exists("SadTalker"):
|
| 11 |
-
print("Cloning SadTalker
|
| 12 |
subprocess.run(["git", "clone", "https://github.com/OpenTalker/SadTalker.git"])
|
| 13 |
|
| 14 |
-
# Move inside SadTalker folder
|
| 15 |
os.chdir("SadTalker")
|
| 16 |
|
| 17 |
-
# -----------------------------------------
|
| 18 |
-
# GOOGLE DRIVE DOWNLOADER
|
| 19 |
-
# -----------------------------------------
|
| 20 |
-
|
| 21 |
def gd(file_id, out):
|
| 22 |
url = f"https://drive.google.com/uc?id={file_id}"
|
| 23 |
if not os.path.exists(out):
|
| 24 |
-
print(f"Downloading {out}
|
| 25 |
gdown.download(url, out, quiet=False)
|
| 26 |
else:
|
| 27 |
-
print(f"
|
| 28 |
|
| 29 |
os.makedirs("checkpoints", exist_ok=True)
|
| 30 |
|
| 31 |
-
# Download
|
| 32 |
-
gd("1g3VIpU3yhpITMZtrWU2mbmyzLoF9K3Gz", "checkpoints/
|
| 33 |
-
gd("1Jp4i_Qc-6qCms7v1kN61RE3qnT_9J8vg", "checkpoints/
|
| 34 |
gd("1pTbKmpOeWRSA1NYQ3DnWe3W-9Qeyy7PK", "checkpoints/mapping_00109-model.pth.tar")
|
| 35 |
gd("1QJins0e_hvvZM4d47gR9eoCxa0L4LpWj", "checkpoints/mapping_00229-model.pth.tar")
|
| 36 |
gd("1caz3ESiy-aEzI0ptff8qpjcxdTf-L26E", "checkpoints/SadTalker_V0.0.2_256.safetensors")
|
| 37 |
gd("12mj9EEs4KIS6-9FjF-PjEegF-TDKvvi6", "checkpoints/SadTalker_V0.0.2_512.safetensors")
|
| 38 |
-
gd("1cag0u7e5RgdKoxBa7exY599VNGtYgNQb", "checkpoints/shape_predictor_68_face_landmarks.dat")
|
| 39 |
-
|
| 40 |
-
# ---------------------------------------------------
|
| 41 |
-
# SADTALKER INFERENCE FUNCTION
|
| 42 |
-
# ---------------------------------------------------
|
| 43 |
|
|
|
|
|
|
|
|
|
|
| 44 |
def generate_video(image, audio):
|
| 45 |
-
|
|
|
|
| 46 |
image_path = "input.png"
|
| 47 |
image.save(image_path)
|
| 48 |
-
|
| 49 |
audio_path = audio
|
| 50 |
|
| 51 |
-
# Run SadTalker
|
| 52 |
cmd = [
|
| 53 |
"python3", "inference.py",
|
| 54 |
"--driven_audio", audio_path,
|
| 55 |
"--source_image", image_path,
|
| 56 |
-
"--result_dir", "
|
| 57 |
-
"--enhancer", "gfpgan",
|
| 58 |
"--still",
|
|
|
|
| 59 |
]
|
|
|
|
|
|
|
| 60 |
subprocess.run(cmd)
|
| 61 |
|
| 62 |
-
#
|
| 63 |
mp4_files = []
|
| 64 |
-
for root, dirs, files in os.walk("
|
| 65 |
for f in files:
|
| 66 |
if f.endswith(".mp4"):
|
| 67 |
mp4_files.append(os.path.join(root, f))
|
| 68 |
|
| 69 |
if not mp4_files:
|
| 70 |
-
return "Error: No video generated"
|
| 71 |
|
| 72 |
-
# 💯 Latest generated video choose
|
| 73 |
mp4_files.sort(key=lambda x: os.path.getmtime(x), reverse=True)
|
| 74 |
-
|
| 75 |
|
| 76 |
-
print("
|
| 77 |
-
return
|
| 78 |
|
| 79 |
-
|
| 80 |
-
# ---------------------------------------------------
|
| 81 |
# GRADIO UI
|
| 82 |
-
# -----------------------------------------
|
| 83 |
-
|
| 84 |
demo = gr.Interface(
|
| 85 |
fn=generate_video,
|
| 86 |
inputs=[
|
| 87 |
gr.Image(type="pil", label="Source Image"),
|
| 88 |
-
gr.Audio(type="filepath", label="Input
|
| 89 |
],
|
| 90 |
-
outputs=gr.Video(label="Generated
|
| 91 |
-
title="SadTalker
|
| 92 |
-
description="Upload image + audio →
|
| 93 |
)
|
| 94 |
|
| 95 |
demo.launch()
|
|
|
|
| 3 |
import gdown
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
+
# -----------------------------------------
|
| 7 |
+
# AUTO CLONE SADTALKER (only first time)
|
| 8 |
+
# -----------------------------------------
|
|
|
|
| 9 |
if not os.path.exists("SadTalker"):
|
| 10 |
+
print("➡️ Cloning SadTalker...")
|
| 11 |
subprocess.run(["git", "clone", "https://github.com/OpenTalker/SadTalker.git"])
|
| 12 |
|
|
|
|
| 13 |
os.chdir("SadTalker")
|
| 14 |
|
| 15 |
+
# -----------------------------------------
|
| 16 |
+
# GOOGLE DRIVE CHECKPOINT DOWNLOADER
|
| 17 |
+
# -----------------------------------------
|
|
|
|
| 18 |
def gd(file_id, out):
|
| 19 |
url = f"https://drive.google.com/uc?id={file_id}"
|
| 20 |
if not os.path.exists(out):
|
| 21 |
+
print(f"⬇️ Downloading: {out}")
|
| 22 |
gdown.download(url, out, quiet=False)
|
| 23 |
else:
|
| 24 |
+
print(f"✔ Already exists: {out}")
|
| 25 |
|
| 26 |
os.makedirs("checkpoints", exist_ok=True)
|
| 27 |
|
| 28 |
+
# Download your files
|
| 29 |
+
gd("1g3VIpU3yhpITMZtrWU2mbmyzLoF9K3Gz", "checkpoints/audio2exp_00300-model.pth")
|
| 30 |
+
gd("1Jp4i_Qc-6qCms7v1kN61RE3qnT_9J8vg", "checkpoints/audio2pose_00140-model.pth")
|
| 31 |
gd("1pTbKmpOeWRSA1NYQ3DnWe3W-9Qeyy7PK", "checkpoints/mapping_00109-model.pth.tar")
|
| 32 |
gd("1QJins0e_hvvZM4d47gR9eoCxa0L4LpWj", "checkpoints/mapping_00229-model.pth.tar")
|
| 33 |
gd("1caz3ESiy-aEzI0ptff8qpjcxdTf-L26E", "checkpoints/SadTalker_V0.0.2_256.safetensors")
|
| 34 |
gd("12mj9EEs4KIS6-9FjF-PjEegF-TDKvvi6", "checkpoints/SadTalker_V0.0.2_512.safetensors")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
# -----------------------------------------
|
| 37 |
+
# SADTALKER INFERENCE (AUTO-FIND RESULT)
|
| 38 |
+
# -----------------------------------------
|
| 39 |
def generate_video(image, audio):
|
| 40 |
+
|
| 41 |
+
# Save input image
|
| 42 |
image_path = "input.png"
|
| 43 |
image.save(image_path)
|
|
|
|
| 44 |
audio_path = audio
|
| 45 |
|
| 46 |
+
# Run SadTalker (GFPGAN off: no freezing!)
|
| 47 |
cmd = [
|
| 48 |
"python3", "inference.py",
|
| 49 |
"--driven_audio", audio_path,
|
| 50 |
"--source_image", image_path,
|
| 51 |
+
"--result_dir", "results",
|
|
|
|
| 52 |
"--still",
|
| 53 |
+
"--enhancer", "None"
|
| 54 |
]
|
| 55 |
+
|
| 56 |
+
print("🎬 Running SadTalker...")
|
| 57 |
subprocess.run(cmd)
|
| 58 |
|
| 59 |
+
# Auto-find latest MP4 inside results folder
|
| 60 |
mp4_files = []
|
| 61 |
+
for root, dirs, files in os.walk("results"):
|
| 62 |
for f in files:
|
| 63 |
if f.endswith(".mp4"):
|
| 64 |
mp4_files.append(os.path.join(root, f))
|
| 65 |
|
| 66 |
if not mp4_files:
|
| 67 |
+
return "❌ Error: No video generated."
|
| 68 |
|
|
|
|
| 69 |
mp4_files.sort(key=lambda x: os.path.getmtime(x), reverse=True)
|
| 70 |
+
final_mp4 = mp4_files[0]
|
| 71 |
|
| 72 |
+
print("🎉 Generated Video:", final_mp4)
|
| 73 |
+
return final_mp4
|
| 74 |
|
| 75 |
+
# -----------------------------------------
|
|
|
|
| 76 |
# GRADIO UI
|
| 77 |
+
# -----------------------------------------
|
|
|
|
| 78 |
demo = gr.Interface(
|
| 79 |
fn=generate_video,
|
| 80 |
inputs=[
|
| 81 |
gr.Image(type="pil", label="Source Image"),
|
| 82 |
+
gr.Audio(type="filepath", label="Input Audio")
|
| 83 |
],
|
| 84 |
+
outputs=gr.Video(label="Generated Video"),
|
| 85 |
+
title="SadTalker (HF Space Auto-Working)",
|
| 86 |
+
description="Upload image + audio → AI talking video"
|
| 87 |
)
|
| 88 |
|
| 89 |
demo.launch()
|