fatma812 commited on
Commit
e3173c4
·
verified ·
1 Parent(s): 6ab80cf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +134 -12
app.py CHANGED
@@ -11,19 +11,141 @@ print("PYDANTIC =", pydantic.__version__)
11
  print("HF HUB =", huggingface_hub.__version__)
12
 
13
 
14
-
15
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
- def test(img, audio):
18
- return "test.mp4"
19
 
20
- demo = gr.Interface(
21
- fn=test,
22
- inputs=[
23
- gr.Image(type="numpy"),
24
- gr.Audio(type="numpy")
25
- ],
26
- outputs=gr.Video()
27
- )
28
 
29
- demo.launch(show_api=False)
 
 
 
 
11
  print("HF HUB =", huggingface_hub.__version__)
12
 
13
 
 
14
  import gradio as gr
15
+ import subprocess
16
+ import os
17
+ from PIL import Image
18
+ import numpy as np
19
+ from pydub import AudioSegment
20
+
21
+
22
+ # ----------------------------
23
+ # Save audio (numpy -> mp3)
24
+ # ----------------------------
25
+ def save_audio_mp3(audio_tuple, filename):
26
+ sampling_rate, audio_data = audio_tuple
27
+
28
+ audio_bytes = np.array(audio_data, dtype=np.int16).tobytes()
29
+
30
+ audio_segment = AudioSegment(
31
+ audio_bytes,
32
+ sample_width=2,
33
+ frame_rate=sampling_rate,
34
+ channels=1
35
+ )
36
+
37
+ audio_segment.export(filename, format="mp3")
38
+
39
+
40
+ # ----------------------------
41
+ # Merge video + audio correctly
42
+ # ----------------------------
43
+ def merge_audio_video(video_path, audio_path, output_path):
44
+
45
+ if os.path.exists(output_path):
46
+ os.remove(output_path)
47
+
48
+ cmd = [
49
+ "ffmpeg",
50
+ "-y",
51
+ "-i", video_path,
52
+ "-i", audio_path,
53
+ "-c:v", "copy",
54
+ "-c:a", "aac",
55
+ "-map", "0:v:0",
56
+ "-map", "1:a:0",
57
+ output_path
58
+ ]
59
+
60
+ subprocess.run(cmd, check=True)
61
+
62
+ return output_path
63
+
64
+
65
+ # ----------------------------
66
+ # Main inference
67
+ # ----------------------------
68
+ def run_inference(input_image, input_audio):
69
+
70
+ if input_image is None:
71
+ raise gr.Error("Please upload an image.")
72
+
73
+ if input_audio is None:
74
+ raise gr.Error("Please upload audio.")
75
+
76
+ os.makedirs("sample_data", exist_ok=True)
77
+ os.makedirs("results", exist_ok=True)
78
+
79
+ # Save image
80
+ image_path = "sample_data/uploaded_image.png"
81
+ Image.fromarray(input_image.astype(np.uint8)).save(image_path)
82
+
83
+ # Save audio
84
+ audio_path = "sample_data/uploaded_audio.mp3"
85
+ save_audio_mp3(input_audio, audio_path)
86
+
87
+ # Run Wav2Lip inference
88
+ cmd = [
89
+ "python3",
90
+ "inference.py",
91
+ "--checkpoint_path", "checkpoints/wav2lip_gan.pth",
92
+ "--face", image_path,
93
+ "--audio", audio_path
94
+ ]
95
+
96
+ result = subprocess.run(cmd, capture_output=True, text=True)
97
+
98
+ if result.returncode != 0:
99
+ raise gr.Error(f"Inference failed:\n{result.stderr}")
100
+
101
+ # output from wav2lip
102
+ wav2lip_video = "results/result_voice.mp4"
103
+
104
+ if not os.path.exists(wav2lip_video):
105
+ raise gr.Error("Wav2Lip output video not found!")
106
+
107
+ # merge audio + video
108
+ final_video = merge_audio_video(
109
+ wav2lip_video,
110
+ audio_path,
111
+ "results/final_output.mp4"
112
+ )
113
+
114
+ return final_video
115
+
116
+
117
+ # ----------------------------
118
+ # UI
119
+ # ----------------------------
120
+ def create_demo():
121
+
122
+ with gr.Blocks() as demo:
123
+
124
+ gr.Markdown("# 🎤 Wav2Lip Demo")
125
+
126
+ with gr.Row():
127
+ input_image = gr.Image(type="numpy", label="Image")
128
+ input_audio = gr.Audio(type="numpy", label="Audio")
129
+ output_video = gr.Video(label="Output", type="filepath")
130
+
131
+ btn = gr.Button("Generate")
132
+
133
+ btn.click(
134
+ fn=run_inference,
135
+ inputs=[input_image, input_audio],
136
+ outputs=output_video
137
+ )
138
+
139
+ gr.Markdown("### Sample")
140
+ with gr.Row():
141
+ gr.Image("sample/spark.png")
142
+ gr.Audio("sample/spark_1.1.mp3")
143
+ gr.Video("sample/final_output.mp4")
144
 
145
+ return demo
 
146
 
 
 
 
 
 
 
 
 
147
 
148
+ if __name__ == "__main__":
149
+ demo = create_demo()
150
+ demo.queue()
151
+ demo.launch(show_api=False)