Debdeep30 commited on
Commit
bbe6bab
·
verified ·
1 Parent(s): eb34f96

Upload pipeline/talking_head.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. pipeline/talking_head.py +58 -0
pipeline/talking_head.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ SadTalker client — sends portrait + audio to the MI300X server, gets MP4 back.
3
+
4
+ Server (droplet port 8003):
5
+ POST /generate multipart: audio file + profile_id form field → MP4 bytes
6
+ GET /health → {"status":"ok"}
7
+ """
8
+
9
+ import os
10
+ import tempfile
11
+ import httpx
12
+
13
+ TALKING_HEAD_URL = os.getenv("TALKING_HEAD_URL", "")
14
+ TIMEOUT = float(os.getenv("TALKING_HEAD_TIMEOUT", "120"))
15
+
16
+
17
+ def generate_talking_video(audio_path: str, profile_id: str) -> str | None:
18
+ """
19
+ Animate the profile portrait to match the given audio.
20
+ Returns path to a temp MP4 file, or None if unavailable/failed.
21
+ """
22
+ if not TALKING_HEAD_URL or not audio_path:
23
+ return None
24
+ if not os.path.exists(audio_path):
25
+ return None
26
+ try:
27
+ with open(audio_path, "rb") as f:
28
+ audio_bytes = f.read()
29
+
30
+ suffix = os.path.splitext(audio_path)[1] or ".mp3"
31
+ mime = "audio/mpeg" if suffix == ".mp3" else "audio/wav"
32
+
33
+ r = httpx.post(
34
+ f"{TALKING_HEAD_URL.rstrip('/')}/generate",
35
+ files={"audio": (f"audio{suffix}", audio_bytes, mime)},
36
+ data={"profile_id": profile_id},
37
+ timeout=TIMEOUT,
38
+ )
39
+ r.raise_for_status()
40
+
41
+ tmp = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False)
42
+ tmp.write(r.content)
43
+ tmp.close()
44
+ return tmp.name
45
+
46
+ except Exception as e:
47
+ print(f"[TalkingHead] {e}")
48
+ return None
49
+
50
+
51
+ def is_available() -> bool:
52
+ if not TALKING_HEAD_URL:
53
+ return False
54
+ try:
55
+ r = httpx.get(f"{TALKING_HEAD_URL.rstrip('/')}/health", timeout=5.0)
56
+ return r.status_code == 200
57
+ except Exception:
58
+ return False