ma4389's picture
Upload 2 files
44d9727 verified
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()