Chat / app.py
Asad235's picture
Update app.py
f2af4a4 verified
Raw
History Blame Contribute Delete
1.76 kB
import gradio as gr
import edge_tts
import asyncio
import tempfile
# Voice options
VOICES = {
"πŸ‘© Professional Female": "en-US-JennyNeural",
"πŸ‘¨ Energetic Male": "en-US-GuyNeural",
"πŸ‡¬πŸ‡§ British Female": "en-GB-SoniaNeural",
"πŸ‡¬πŸ‡§ British Male": "en-GB-RyanNeural",
"🎬 Deep Documentary": "en-US-ChristopherNeural",
"😊 Friendly Female": "en-US-AnaNeural",
"🎀 Energetic Announcer": "en-US-DavisNeural",
"πŸ“° News Anchor": "en-US-SaraNeural",
}
async def generate_speech(text, voice_name):
"""Generate speech using Edge TTS"""
if not text or not text.strip():
return None
voice_id = VOICES[voice_name]
output_file = tempfile.NamedTemporaryFile(
delete=False,
suffix=".mp3"
).name
communicate = edge_tts.Communicate(text, voice_id)
await communicate.save(output_file)
return output_file
def tts_wrapper(text, voice):
return asyncio.run(generate_speech(text, voice))
# UI
with gr.Blocks(title="Simple TTS", theme=gr.themes.Soft()) as demo:
gr.Markdown("# πŸŽ™οΈ Simple Text-to-Speech Generator")
text_input = gr.Textbox(
label="Enter your text",
placeholder="Type something here...",
lines=3
)
voice_select = gr.Dropdown(
choices=list(VOICES.keys()),
label="Select Voice",
value="πŸ‘© Professional Female"
)
generate_btn = gr.Button("πŸ”Š Generate Speech", variant="primary")
audio_output = gr.Audio(label="Audio Output")
generate_btn.click(
fn=tts_wrapper,
inputs=[text_input, voice_select],
outputs=audio_output
)
if __name__ == "__main__":
demo.launch(
server_name="0.0.0.0",
server_port=7860
)