Saitama070 commited on
Commit
dbdb02f
·
verified ·
1 Parent(s): d95e48d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -15
app.py CHANGED
@@ -1,6 +1,8 @@
1
  import os
 
2
  import subprocess
3
  import torch
 
4
  from tortoise.api import TextToSpeech
5
  from tortoise.utils.audio import save_wav
6
 
@@ -18,15 +20,10 @@ def setup_wav2lip():
18
 
19
  return None
20
 
21
-
22
  def generate_speech(text, voice_model, output_wav):
23
  """ Uses Tortoise TTS to generate speech. """
24
- print(f"Generating speech for: {text} in {voice_model} voice...")
25
  tts = TextToSpeech()
26
- voice = voice_model # Example: 'trump'
27
-
28
- # Generate speech using Tortoise
29
- speech = tts.tts(text, voice)
30
  save_wav(speech, output_wav)
31
 
32
  def run_wav2lip(video_path, audio_path, output_video):
@@ -40,22 +37,44 @@ def run_wav2lip(video_path, audio_path, output_video):
40
  ]
41
  subprocess.run(command, check=True)
42
 
43
- def main():
44
- setup_wav2lip() # Ensure Wav2Lip is ready
 
 
 
 
 
 
45
 
46
- text = "Hello, this is a test speech."
47
- voice_model = "trump"
48
  output_wav = "generated_speech.wav"
49
- video_path = "trump_video.mp4"
50
  output_video = "lip_synced_output.mp4"
 
51
 
52
- # Step 1: Generate Speech
 
 
 
53
  generate_speech(text, voice_model, output_wav)
54
-
55
- # Step 2: Run Wav2Lip
56
  run_wav2lip(video_path, output_wav, output_video)
57
 
58
- print(f"Lip-synced video saved as {output_video}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
 
60
  if __name__ == "__main__":
61
  main()
 
1
  import os
2
+ import zipfile
3
  import subprocess
4
  import torch
5
+ import gradio as gr
6
  from tortoise.api import TextToSpeech
7
  from tortoise.utils.audio import save_wav
8
 
 
20
 
21
  return None
22
 
 
23
  def generate_speech(text, voice_model, output_wav):
24
  """ Uses Tortoise TTS to generate speech. """
 
25
  tts = TextToSpeech()
26
+ speech = tts.tts(text, voice_model)
 
 
 
27
  save_wav(speech, output_wav)
28
 
29
  def run_wav2lip(video_path, audio_path, output_video):
 
37
  ]
38
  subprocess.run(command, check=True)
39
 
40
+ def process_lipsync(text, voice_model, video_file):
41
+ """ Handles text-to-speech generation and lip-syncing. """
42
+ error = setup_wav2lip()
43
+ if error:
44
+ return error, None
45
+
46
+ if not text or not video_file:
47
+ return "Please provide both text and a video.", None
48
 
 
 
49
  output_wav = "generated_speech.wav"
 
50
  output_video = "lip_synced_output.mp4"
51
+ video_path = "input_video.mp4"
52
 
53
+ # Save the uploaded video file
54
+ with open(video_path, "wb") as f:
55
+ f.write(video_file.read())
56
+
57
  generate_speech(text, voice_model, output_wav)
 
 
58
  run_wav2lip(video_path, output_wav, output_video)
59
 
60
+ return "Lip-synced video generated!", output_video
61
+
62
+ def main():
63
+ iface = gr.Interface(
64
+ fn=process_lipsync,
65
+ inputs=[
66
+ gr.Textbox(label="Enter text for speech synthesis"),
67
+ gr.Dropdown(["trump", "elon", "obama"], label="Choose a voice model"),
68
+ gr.File(label="Upload a video (MP4)")
69
+ ],
70
+ outputs=[
71
+ gr.Textbox(label="Status"),
72
+ gr.Video(label="Generated Lip-Synced Video")
73
+ ],
74
+ title="TTS & Lip Sync Generator",
75
+ description="Enter text, select a voice, and upload a video to generate a lip-synced output."
76
+ )
77
+ iface.launch()
78
 
79
  if __name__ == "__main__":
80
  main()