aiwithshweta commited on
Commit
73d63fb
·
verified ·
1 Parent(s): 1f173a8
Files changed (1) hide show
  1. app.py +64 -9
app.py CHANGED
@@ -1,20 +1,34 @@
1
  import os
 
2
  import gdown
3
  import gradio as gr
4
 
5
- # Make checkpoints dir
6
- os.makedirs("checkpoints", exist_ok=True)
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
- # Google Drive Download Helper
9
  def gd(file_id, out):
10
  url = f"https://drive.google.com/uc?id={file_id}"
11
  if not os.path.exists(out):
12
- print(f"Downloading {out}...")
13
  gdown.download(url, out, quiet=False)
14
  else:
15
  print(f"{out} already exists.")
16
 
17
- # ---- DOWNLOAD ALL MODELS ----
 
 
18
  gd("1g3VIpU3yhpITMZtrWU2mbmyzLoF9K3Gz", "checkpoints/auido2exp_00300-model.pth")
19
  gd("1Jp4i_Qc-6qCms7v1kN61RE3qnT_9J8vg", "checkpoints/auido2pose_00140-model.pth")
20
  gd("1pTbKmpOeWRSA1NYQ3DnWe3W-9Qeyy7PK", "checkpoints/mapping_00109-model.pth.tar")
@@ -23,10 +37,51 @@ gd("1caz3ESiy-aEzI0ptff8qpjcxdTf-L26E", "checkpoints/SadTalker_256.safetensors")
23
  gd("12mj9EEs4KIS6-9FjF-PjEegF-TDKvvi6", "checkpoints/SadTalker_512.safetensors")
24
  gd("1cag0u7e5RgdKoxBa7exY599VNGtYgNQb", "checkpoints/shape_predictor_68_face_landmarks.dat")
25
 
26
- # -------------------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
- def greet(name):
29
- return "Hello " + name + "!!"
 
 
 
 
 
 
 
 
30
 
31
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
32
  demo.launch()
 
1
  import os
2
+ import subprocess
3
  import gdown
4
  import gradio as gr
5
 
6
+ # ---------------------------------------------------
7
+ # AUTO CLONE SADTALKER FROM GITHUB (only first time)
8
+ # ---------------------------------------------------
9
+
10
+ if not os.path.exists("SadTalker"):
11
+ print("Cloning SadTalker from GitHub...")
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"{out} already exists.")
28
 
29
+ os.makedirs("checkpoints", exist_ok=True)
30
+
31
+ # Download all required model files
32
  gd("1g3VIpU3yhpITMZtrWU2mbmyzLoF9K3Gz", "checkpoints/auido2exp_00300-model.pth")
33
  gd("1Jp4i_Qc-6qCms7v1kN61RE3qnT_9J8vg", "checkpoints/auido2pose_00140-model.pth")
34
  gd("1pTbKmpOeWRSA1NYQ3DnWe3W-9Qeyy7PK", "checkpoints/mapping_00109-model.pth.tar")
 
37
  gd("12mj9EEs4KIS6-9FjF-PjEegF-TDKvvi6", "checkpoints/SadTalker_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
+ # remove previous output
46
+ if os.path.exists("result.mp4"):
47
+ os.remove("result.mp4")
48
+
49
+ # Save input image & audio
50
+ image.save("input.png")
51
+ audio_path = "input.wav"
52
+ audio.save(audio_path)
53
+
54
+ # Run SadTalker inference
55
+ cmd = [
56
+ "python3", "inference.py",
57
+ "--driven_audio", audio_path,
58
+ "--source_image", "input.png",
59
+ "--result_dir", "./",
60
+ "--enhancer", "gfpgan",
61
+ "--still", # input stability
62
+ ]
63
+
64
+ subprocess.run(cmd)
65
+
66
+ # Return output
67
+ if os.path.exists("result.mp4"):
68
+ return "result.mp4"
69
+ else:
70
+ return "Error: video not generated"
71
+
72
+ # ---------------------------------------------------
73
+ # GRADIO UI
74
+ # ---------------------------------------------------
75
 
76
+ demo = gr.Interface(
77
+ fn=generate_video,
78
+ inputs=[
79
+ gr.Image(type="pil", label="Source Image"),
80
+ gr.Audio(type="filepath", label="Input Voice")
81
+ ],
82
+ outputs=gr.Video(label="Generated Talking Video"),
83
+ title="SadTalker Online API (Auto-Clone + Google Drive Models)",
84
+ description="Upload image + audio → Generate talking video"
85
+ )
86
 
 
87
  demo.launch()