Spaces:
Sleeping
Sleeping
update
Browse files
app.py
CHANGED
|
@@ -4,82 +4,100 @@ import gdown
|
|
| 4 |
import gradio as gr
|
| 5 |
import zipfile
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
# DOWNLOAD
|
| 9 |
-
#
|
| 10 |
|
| 11 |
-
|
|
|
|
| 12 |
|
| 13 |
if not os.path.exists("SadTalker"):
|
| 14 |
-
print("⬇️ Downloading SadTalker
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
output="SadTalker",
|
| 20 |
-
quiet=False,
|
| 21 |
-
use_cookies=False
|
| 22 |
-
)
|
| 23 |
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
|
|
|
| 26 |
os.chdir("SadTalker")
|
| 27 |
|
| 28 |
-
#
|
| 29 |
-
#
|
| 30 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
def generate_video(image, audio):
|
| 33 |
-
|
| 34 |
-
image.save(image_path)
|
| 35 |
-
audio_path = audio
|
| 36 |
|
| 37 |
-
# Run SadTalker (GFPGAN OFF for CPU Spaces)
|
| 38 |
cmd = [
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
]
|
| 48 |
|
| 49 |
-
|
| 50 |
-
print("▶ Running SadTalker inference...")
|
| 51 |
subprocess.run(cmd)
|
| 52 |
|
| 53 |
-
# Auto
|
| 54 |
-
|
| 55 |
for root, dirs, files in os.walk("results"):
|
| 56 |
for f in files:
|
| 57 |
if f.endswith(".mp4"):
|
| 58 |
-
|
| 59 |
|
| 60 |
-
if not
|
| 61 |
return "❌ No video generated."
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
final_video = mp4_list[0]
|
| 66 |
-
|
| 67 |
-
print("🎉 Final video:", final_video)
|
| 68 |
-
return final_video
|
| 69 |
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
-
# -----------------------------
|
| 72 |
-
# GRADIO UI
|
| 73 |
-
# -----------------------------
|
| 74 |
demo = gr.Interface(
|
| 75 |
fn=generate_video,
|
| 76 |
-
inputs=[
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
outputs=gr.Video(label="Generated Video"),
|
| 81 |
-
title="SadTalker (Google Drive Version)",
|
| 82 |
-
description="Loads SadTalker directly from your Google Drive (No GitHub cloning)"
|
| 83 |
)
|
| 84 |
|
| 85 |
demo.launch()
|
|
|
|
| 4 |
import gradio as gr
|
| 5 |
import zipfile
|
| 6 |
|
| 7 |
+
# ------------------------------------------------------
|
| 8 |
+
# 1) DOWNLOAD SadTalker ZIP FROM GOOGLE DRIVE
|
| 9 |
+
# ------------------------------------------------------
|
| 10 |
|
| 11 |
+
# ← Put your ZIP FILE ID here
|
| 12 |
+
SADTALKER_ZIP_ID = "1fsB8ug8zSabdtFecCpL20ApGLjz2xgvy"
|
| 13 |
|
| 14 |
if not os.path.exists("SadTalker"):
|
| 15 |
+
print("⬇️ Downloading SadTalker.zip...")
|
| 16 |
+
gdown.download(f"https://drive.google.com/uc?id={SADTALKER_ZIP_ID}", "sad.zip", quiet=False)
|
| 17 |
|
| 18 |
+
print("📦 Extracting ZIP...")
|
| 19 |
+
with zipfile.ZipFile("sad.zip", 'r') as zip_ref:
|
| 20 |
+
zip_ref.extractall("./")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
# Detect extracted folder name
|
| 23 |
+
if os.path.exists("SadTalker-main"):
|
| 24 |
+
os.rename("SadTalker-main", "SadTalker")
|
| 25 |
+
elif os.path.exists("SadTalker"):
|
| 26 |
+
pass
|
| 27 |
|
| 28 |
+
print("✔ SadTalker ready!")
|
| 29 |
os.chdir("SadTalker")
|
| 30 |
|
| 31 |
+
# ------------------------------------------------------
|
| 32 |
+
# 2) DOWNLOAD CHECKPOINT MODELS (YOUR OLD CODE)
|
| 33 |
+
# ------------------------------------------------------
|
| 34 |
+
|
| 35 |
+
def gd(file_id, out):
|
| 36 |
+
url = f"https://drive.google.com/uc?id={file_id}"
|
| 37 |
+
if not os.path.exists(out):
|
| 38 |
+
print(f"⬇️ Downloading: {out}")
|
| 39 |
+
gdown.download(url, out, quiet=False)
|
| 40 |
+
else:
|
| 41 |
+
print(f"✔ Already exists: {out}")
|
| 42 |
+
|
| 43 |
+
os.makedirs("checkpoints", exist_ok=True)
|
| 44 |
+
|
| 45 |
+
# Your original model IDs (replace with yours)
|
| 46 |
+
gd("1g3VIpU3yhpITMZtrWU2mbmyzLoF9K3Gz", "checkpoints/audio2exp_00300-model.pth")
|
| 47 |
+
gd("1Jp4i_Qc-6qCms7v1kN61RE3qnT_9J8vg", "checkpoints/audio2pose_00140-model.pth")
|
| 48 |
+
gd("1pTbKmpOeWRSA1NYQ3DnWe3W-9Qeyy7PK", "checkpoints/mapping_00109-model.pth.tar")
|
| 49 |
+
gd("1QJins0e_hvvZM4d47gR9eoCxa0L4LpWj", "checkpoints/mapping_00229-model.pth.tar")
|
| 50 |
+
gd("1caz3ESiy-aEzI0ptff8qpjcxdTf-L26E", "checkpoints/SadTalker_V0.0.2_256.safetensors")
|
| 51 |
+
gd("12mj9EEs4KIS6-9FjF-PjEegF-TDKvvi6", "checkpoints/SadTalker_V0.0.2_512.safetensors")
|
| 52 |
+
gd("1cag0u7e5RgdKoxBa7exY599VNGtYgNQb", "checkpoints/shape_predictor_68_face_landmarks.dat")
|
| 53 |
+
gd("1aUrFSdMHpBbH1CBMz-Y5bw0kCXGhs0Mu", "checkpoints/hubert-soft.pt") # optional
|
| 54 |
+
|
| 55 |
+
print("✔ All checkpoints ready!")
|
| 56 |
+
|
| 57 |
+
# ------------------------------------------------------
|
| 58 |
+
# 3) GENERATE VIDEO (Auto-detect MP4)
|
| 59 |
+
# ------------------------------------------------------
|
| 60 |
|
| 61 |
def generate_video(image, audio):
|
| 62 |
+
image.save("input.png")
|
|
|
|
|
|
|
| 63 |
|
|
|
|
| 64 |
cmd = [
|
| 65 |
+
"python3", "inference.py",
|
| 66 |
+
"--driven_audio", audio,
|
| 67 |
+
"--source_image", "input.png",
|
| 68 |
+
"--result_dir", "results",
|
| 69 |
+
"--still",
|
| 70 |
+
"--enhancer", "None",
|
| 71 |
+
"--expression_scale", "1.8",
|
| 72 |
+
"--preprocess", "full"
|
| 73 |
]
|
| 74 |
|
| 75 |
+
print("▶ Running inference...")
|
|
|
|
| 76 |
subprocess.run(cmd)
|
| 77 |
|
| 78 |
+
# Auto find latest mp4
|
| 79 |
+
mp4s = []
|
| 80 |
for root, dirs, files in os.walk("results"):
|
| 81 |
for f in files:
|
| 82 |
if f.endswith(".mp4"):
|
| 83 |
+
mp4s.append(os.path.join(root, f))
|
| 84 |
|
| 85 |
+
if not mp4s:
|
| 86 |
return "❌ No video generated."
|
| 87 |
|
| 88 |
+
mp4s.sort(key=lambda x: os.path.getmtime(x), reverse=True)
|
| 89 |
+
return mp4s[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
+
# ------------------------------------------------------
|
| 92 |
+
# 4) GRADIO UI
|
| 93 |
+
# ------------------------------------------------------
|
| 94 |
|
|
|
|
|
|
|
|
|
|
| 95 |
demo = gr.Interface(
|
| 96 |
fn=generate_video,
|
| 97 |
+
inputs=[gr.Image(type="pil"), gr.Audio(type="filepath")],
|
| 98 |
+
outputs=gr.Video(),
|
| 99 |
+
title="SadTalker from Google Drive (Full Version)",
|
| 100 |
+
description="Loads SadTalker + all checkpoints + auto video detection"
|
|
|
|
|
|
|
|
|
|
| 101 |
)
|
| 102 |
|
| 103 |
demo.launch()
|