Spaces:
Sleeping
Sleeping
| 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() | |