import gradio as gr import asyncio import edge_tts import os # ✅ Async function to generate speech async def generate_speech(text, voice): communicate = edge_tts.Communicate(text, voice) await communicate.save("output.mp3") # ✅ Wrapper for Gradio (since Gradio is sync, and edge-tts is async) def tts_wrapper(text, voice): asyncio.run(generate_speech(text, voice)) return "output.mp3" # ✅ Voice options VOICE_OPTIONS = { "Aria (Female, US)": "en-US-AriaNeural", "Ryan (Male, UK)": "en-GB-RyanNeural", "Willem (Male, ZA)": "af-ZA-WillemNeural", "Salma (Female, EG)": "ar-EG-SalmaNeural", "Guy (Male, US)": "en-US-GuyNeural" } # ✅ Gradio Interface demo = gr.Interface( fn=tts_wrapper, inputs=[ gr.Textbox(lines=4, placeholder="Enter text to convert to speech"), gr.Dropdown(choices=list(VOICE_OPTIONS.values()), label="Select Voice") ], outputs=gr.Audio(type="filepath", label="Generated Speech"), title="🎤 Text-to-Speech App with Microsoft Edge TTS", description="Type text and select a voice. The app will speak it using edge-tts." ) if __name__ == "__main__": demo.launch()