Saitama070 commited on
Commit
fc646be
·
verified ·
1 Parent(s): e32ee6c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -1
app.py CHANGED
@@ -1,4 +1,91 @@
 
 
 
 
 
1
  import subprocess
2
 
3
  # Run the extract script
4
- subprocess.run(["python", "extract.py"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import torch
3
+ import gradio as gr
4
+ from tortoise.api import TextToSpeech
5
+ from tortoise.utils.audio import save_wav
6
  import subprocess
7
 
8
  # Run the extract script
9
+ subprocess.run(["python", "extract.py"])
10
+
11
+ # Path to Trump's pre-stored video
12
+ TRUMP_VIDEO_PATH = "videos/trump.mp4"
13
+
14
+ def setup_wav2lip():
15
+ """Ensures Wav2Lip model checkpoint is set up."""
16
+ checkpoint_path = "Wav2Lip/checkpoints/wav2lip_gan.pth"
17
+
18
+ # Check if Wav2Lip folder exists
19
+ if not os.path.exists("Wav2Lip"):
20
+ return "Error: Wav2Lip folder not found. Please ensure it is set up."
21
+
22
+ # Check if the checkpoint file exists
23
+ if not os.path.exists(checkpoint_path):
24
+ return "Error: Wav2Lip checkpoint not found. Please download and place it in Wav2Lip/checkpoints/"
25
+
26
+ return None
27
+
28
+ def generate_speech(text, output_wav):
29
+ """Uses Tortoise TTS to generate speech."""
30
+ tts = TextToSpeech()
31
+ # Use Trump's voice model
32
+ speech = tts.tts(text, "trump")
33
+ save_wav(speech, output_wav)
34
+
35
+ def run_wav2lip(video_path, audio_path, output_video):
36
+ """Runs Wav2Lip to sync the audio with the video."""
37
+ command = [
38
+ "python", "Wav2Lip/inference.py",
39
+ "--checkpoint_path", "Wav2Lip/checkpoints/wav2lip_gan.pth",
40
+ "--face", video_path,
41
+ "--audio", audio_path,
42
+ "--outfile", output_video
43
+ ]
44
+ subprocess.run(command, check=True)
45
+
46
+ def process_lipsync(text):
47
+ """Handles text-to-speech generation and lip-syncing."""
48
+ # Check if Wav2Lip is set up
49
+ error = setup_wav2lip()
50
+ if error:
51
+ return error, None
52
+
53
+ # Check if text is provided
54
+ if not text:
55
+ return "Please provide text for speech synthesis.", None
56
+
57
+ # Check if Trump's video exists
58
+ if not os.path.exists(TRUMP_VIDEO_PATH):
59
+ return "Error: Trump's video not found. Please ensure it is placed in the 'videos' folder.", None
60
+
61
+ # Define output file paths
62
+ output_wav = "generated_speech.wav"
63
+ output_video = "lip_synced_output.mp4"
64
+
65
+ # Generate speech using Trump's voice model
66
+ generate_speech(text, output_wav)
67
+
68
+ # Run Wav2Lip to sync the generated speech with Trump's video
69
+ run_wav2lip(TRUMP_VIDEO_PATH, output_wav, output_video)
70
+
71
+ # Return success message and the output video
72
+ return "Lip-synced video generated!", output_video
73
+
74
+ def main():
75
+ # Create Gradio interface
76
+ iface = gr.Interface(
77
+ fn=process_lipsync,
78
+ inputs=[
79
+ gr.Textbox(label="Enter text for speech synthesis")
80
+ ],
81
+ outputs=[
82
+ gr.Textbox(label="Status"),
83
+ gr.Video(label="Generated Lip-Synced Video")
84
+ ],
85
+ title="TTS & Lip Sync Generator (Trump Only)",
86
+ description="Enter text to generate a lip-synced video using Trump's voice and video."
87
+ )
88
+ iface.launch()
89
+
90
+ if __name__ == "__main__":
91
+ main()