Oviya commited on
Commit
35698c9
·
1 Parent(s): 05d6ff0

add app.py

Browse files
Files changed (2) hide show
  1. app.py +76 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import uuid
3
+ import gradio as gr
4
+ from wyn_wav2lip.wav2lip import Wav2Lip
5
+
6
+ MEDIA_DIR = "media"
7
+ os.makedirs(MEDIA_DIR, exist_ok=True)
8
+
9
+ wav2lip = Wav2Lip()
10
+ wav2lip.setup()
11
+
12
+ def lipsync_func(image, audio):
13
+ # image: PIL.Image
14
+ # audio: tuple (sample_rate, data) or filepath depending on Gradio component
15
+
16
+ # Save inputs
17
+ image_path = os.path.join(MEDIA_DIR, f"{uuid.uuid4()}.png")
18
+ audio_path = os.path.join(MEDIA_DIR, f"{uuid.uuid4()}.wav")
19
+
20
+ image.save(image_path)
21
+
22
+ import soundfile as sf
23
+ sr, data = audio # if using gr.Audio(type="numpy")
24
+ sf.write(audio_path, data, sr)
25
+
26
+ # Run Wav2Lip like we did before
27
+ # (Use the same logic as in the Colab example)
28
+ video_path = run_wav2lip(image_path, audio_path)
29
+
30
+ return video_path
31
+
32
+ def run_wav2lip(image_path, audio_path):
33
+ # similar to the Colab sample before
34
+ existing_mp4 = {
35
+ f for f in os.listdir(MEDIA_DIR)
36
+ if f.lower().endswith(".mp4")
37
+ }
38
+
39
+ import os
40
+ from os import getcwd, chdir
41
+
42
+ old = getcwd()
43
+ chdir(MEDIA_DIR)
44
+ try:
45
+ wav2lip.run(
46
+ video_file=os.path.basename(image_path),
47
+ vocal_file=os.path.basename(audio_path),
48
+ )
49
+ finally:
50
+ chdir(old)
51
+
52
+ new_mp4 = [
53
+ f for f in os.listdir(MEDIA_DIR)
54
+ if f.lower().endswith(".mp4") and f not in existing_mp4
55
+ ]
56
+ if not new_mp4:
57
+ mp4_candidates = [
58
+ os.path.join(MEDIA_DIR, f)
59
+ for f in os.listdir(MEDIA_DIR)
60
+ if f.lower().endswith(".mp4")
61
+ ]
62
+ if not mp4_candidates:
63
+ raise RuntimeError("No video created")
64
+ return max(mp4_candidates, key=os.path.getmtime)
65
+ return os.path.join(MEDIA_DIR, new_mp4[0])
66
+
67
+
68
+ demo = gr.Interface(
69
+ fn=lipsync_func,
70
+ inputs=[gr.Image(type="pil"), gr.Audio(type="numpy")],
71
+ outputs=gr.Video(),
72
+ title="Wav2Lip Lipsync Service"
73
+ )
74
+
75
+ if __name__ == "__main__":
76
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ wyn-wav2lip
2
+ gradio