CoolFace
Apppublic

Aadharsh3008/Trump-LipSync

sourceHugging Facemitupdated 2y agoView on Hugging Face
1likes
app.py119 linesDownload Raw Back to root
1import os2import zipfile3import subprocess4import gradio as gr5from tortoise.api import TextToSpeech6from tortoise.utils.audio import load_audio7import torchaudio  # Added import for torchaudio8 9# Paths10ZIP_FILE = "Trump-LipSync.zip"  11EXTRACTED_FOLDER = "Trump-LipSync"12TRUMP_VIDEO_PATH = os.path.join(EXTRACTED_FOLDER,EXTRACTED_FOLDER, "videos", "trump.mp4")  13TRUMP_AUDIO_PATH = os.path.join(EXTRACTED_FOLDER,EXTRACTED_FOLDER, "audios", "Trump_WEF_2018.mp3")  14CHECKPOINT_PATH = os.path.join(EXTRACTED_FOLDER,EXTRACTED_FOLDER, "Wav2Lip", "checkpoints", "wav2lip_gan.pth")  15 16# Function to extract the zip file17def extract_zip():18    try:19        # Check if the zip file exists20        if not os.path.exists(ZIP_FILE):21            return f"Error: {ZIP_FILE} not found."22 23        # Check if the folder is already extracted24        if os.path.exists(EXTRACTED_FOLDER):25            return f"Folder {EXTRACTED_FOLDER} already exists."26 27        # Extract the zip file28        with zipfile.ZipFile(ZIP_FILE, 'r') as zip_ref:29            zip_ref.extractall(EXTRACTED_FOLDER)30 31        return f"Zip file extracted successfully to {EXTRACTED_FOLDER}!"32    except Exception as e:33        return f"Error extracting zip file: {str(e)}"34 35# Function to generate speech using Tortoise-TTS36def generate_speech(text, output_wav):37    try:38        voice_sample = load_audio(TRUMP_AUDIO_PATH, 22050)39        voice_samples = [voice_sample]40        conditioning_latents = None41        preset = "fast"42        tts=TextToSpeech(kv_cache=True)43        gen = tts.tts_with_preset(text, voice_samples=voice_samples, conditioning_latents=conditioning_latents, preset=preset)44        torchaudio.save(output_wav, gen.squeeze(0).cpu(), 24000)45        return True46    except Exception as e:47        print(f"Error generating speech: {str(e)}")48        return False49 50# Function to run Wav2Lip for lip-syncing51def run_wav2lip(video_path, audio_path, output_video):52    try:53        command = [54            "python", os.path.join(EXTRACTED_FOLDER,EXTRACTED_FOLDER, "Wav2Lip", "inference.py"),55            "--checkpoint_path", CHECKPOINT_PATH,56            "--face", video_path,57            "--audio", audio_path,58            "--outfile", output_video59        ]60        subprocess.run(command, check=True)61        return True62    except Exception as e:63        print(f"Error running Wav2Lip: {str(e)}")64        return False65 66def process_lipsync(text):67    try:68        # Check if the zip file is extracted69        if not os.path.exists(EXTRACTED_FOLDER):70            return "Error: Zip file not extracted. Please check the logs.", None71 72        # Check if Trump's video exists73        if not os.path.exists(TRUMP_VIDEO_PATH):74            return "Error: Trump's video not found.", None75 76        # Check if Wav2Lip checkpoint exists77        if not os.path.exists(CHECKPOINT_PATH):78            return "Error: Wav2Lip checkpoint not found.", None79 80        # Define output file paths81        output_wav = "generated_speech.wav"82        output_video = "lip_synced_output.mp4"83 84        # Generate speech using Tortoise-TTS85        if not generate_speech(text, output_wav):86            return "Error: Failed to generate speech.", None87 88        # Run Wav2Lip to sync the generated speech with Trump's video89        if not run_wav2lip(TRUMP_VIDEO_PATH,EXTRACTED_FOLDER, output_wav, output_video):90            return "Error: Failed to run Wav2Lip.", None91 92        # Return success message and the output video93        return "Lip-synced video generated!", output_video94    except Exception as e:95        return f"Error processing lip-sync: {str(e)}", None96 97# Gradio interface98def main():99    # Extract the zip file first100    extract_status = extract_zip()101    print(extract_status)102 103    # Create Gradio interface104    iface = gr.Interface(105        fn=process_lipsync,106        inputs=[107            gr.Textbox(label="Enter text for speech synthesis")108        ],109        outputs=[110            gr.Textbox(label="Status"),111            gr.Video(label="Generated Lip-Synced Video")112        ],113        title="TTS & Lip Sync Generator (Trump Only)",114        description="Enter text to generate a lip-synced video using Trump's voice and video."115    )116    iface.launch()117 118if __name__ == "__main__":119    main()