Spaces:
Running
Running
File size: 4,014 Bytes
fc646be dda36f3 fc646be 11fd342 e32ee6c dda36f3 11fd342 8cab254 dda36f3 fc646be dda36f3 fc646be dda36f3 11fd342 dda36f3 11fd342 dda36f3 fc646be dda36f3 fc646be dda36f3 8cab254 dda36f3 fc646be dda36f3 fc646be dda36f3 8cab254 dda36f3 fc646be dda36f3 fc646be |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
import os
import zipfile
import subprocess
import gradio as gr
from tortoise.api import TextToSpeech
import torchaudio # Added import for torchaudio
# Paths
ZIP_FILE = "Trump-LipSync.zip"
EXTRACTED_FOLDER = "Trump-LipSync"
TRUMP_VIDEO_PATH = os.path.join(EXTRACTED_FOLDER,EXTRACTED_FOLDER, "videos", "trump.mp4")
CHECKPOINT_PATH = os.path.join(EXTRACTED_FOLDER,EXTRACTED_FOLDER, "Wav2Lip", "checkpoints", "wav2lip_gan.pth")
# Function to extract the zip file
def extract_zip():
try:
# Check if the zip file exists
if not os.path.exists(ZIP_FILE):
return f"Error: {ZIP_FILE} not found."
# Check if the folder is already extracted
if os.path.exists(EXTRACTED_FOLDER):
return f"Folder {EXTRACTED_FOLDER} already exists."
# Extract the zip file
with zipfile.ZipFile(ZIP_FILE, 'r') as zip_ref:
zip_ref.extractall(EXTRACTED_FOLDER)
return f"Zip file extracted successfully to {EXTRACTED_FOLDER}!"
except Exception as e:
return f"Error extracting zip file: {str(e)}"
# Function to generate speech using Tortoise-TTS
def generate_speech(text, output_wav):
try:
tts = TextToSpeech()
speech = tts.tts(text, "trump")
speech = speech.squeeze()
if speech.dim() == 1:
speech = speech.unsqueeze(0)
sample_rate = 22050
torchaudio.save(output_wav, speech.cpu(), sample_rate)
return True
except Exception as e:
print(f"Error generating speech: {str(e)}")
return False
# Function to run Wav2Lip for lip-syncing
def run_wav2lip(video_path, audio_path, output_video):
try:
command = [
"python", os.path.join(EXTRACTED_FOLDER,EXTRACTED_FOLDER, "Wav2Lip", "inference.py"),
"--checkpoint_path", CHECKPOINT_PATH,
"--face", video_path,
"--audio", audio_path,
"--outfile", output_video
]
subprocess.run(command, check=True)
return True
except Exception as e:
print(f"Error running Wav2Lip: {str(e)}")
return False
def process_lipsync(text):
try:
# Check if the zip file is extracted
if not os.path.exists(EXTRACTED_FOLDER):
return "Error: Zip file not extracted. Please check the logs.", None
# Check if Trump's video exists
if not os.path.exists(TRUMP_VIDEO_PATH):
return "Error: Trump's video not found.", None
# Check if Wav2Lip checkpoint exists
if not os.path.exists(CHECKPOINT_PATH):
return "Error: Wav2Lip checkpoint not found.", None
# Define output file paths
output_wav = "generated_speech.wav"
output_video = "lip_synced_output.mp4"
# Generate speech using Tortoise-TTS
if not generate_speech(text, output_wav):
return "Error: Failed to generate speech.", None
# Run Wav2Lip to sync the generated speech with Trump's video
if not run_wav2lip(TRUMP_VIDEO_PATH,EXTRACTED_FOLDER, output_wav, output_video):
return "Error: Failed to run Wav2Lip.", None
# Return success message and the output video
return "Lip-synced video generated!", output_video
except Exception as e:
return f"Error processing lip-sync: {str(e)}", None
# Gradio interface
def main():
# Extract the zip file first
extract_status = extract_zip()
print(extract_status)
# Create Gradio interface
iface = gr.Interface(
fn=process_lipsync,
inputs=[
gr.Textbox(label="Enter text for speech synthesis")
],
outputs=[
gr.Textbox(label="Status"),
gr.Video(label="Generated Lip-Synced Video")
],
title="TTS & Lip Sync Generator (Trump Only)",
description="Enter text to generate a lip-synced video using Trump's voice and video."
)
iface.launch()
if __name__ == "__main__":
main() |