Edge-TT / app.py
aheedsajid's picture
Update app.py
7e6edcc verified
import edge_tts
import gradio as gr
import tempfile
import asyncio
language_dict = {
'Abeo': 'en-NG-AbeoNeural',
'Ana': 'en-US-AnaNeural',
'Andrew': 'en-US-AndrewNeural',
'Aria': 'en-US-AriaNeural',
'Asilia': 'en-KE-AsiliaNeural',
'Ava': 'en-US-AvaNeural',
'Brian': 'en-US-BrianNeural',
'Christopher': 'en-US-ChristopherNeural',
'Chilemba': 'en-KE-ChilembaNeural',
'Clara': 'en-CA-ClaraNeural',
'Connor': 'en-IE-ConnorNeural',
'Emily': 'en-IE-EmilyNeural',
'Elimu': 'en-TZ-ElimuNeural',
'Emma': 'en-US-EmmaMultilingualNeural',
'Eric': 'en-US-EricNeural',
'Ezinne': 'en-NG-EzinneNeural',
'Guy': 'en-US-GuyNeural',
'Imani': 'en-TZ-ImaniNeural',
'James': 'en-PH-JamesNeural',
'Jenny': 'en-US-JennyNeural',
'Leah': 'en-ZA-LeahNeural',
'Libby': 'en-GB-LibbyNeural',
'Liam': 'en-CA-LiamNeural',
'Luke': 'en-ZA-LukeNeural',
'Luna': 'en-SG-LunaNeural',
'Maisie': 'en-GB-MaisieNeural',
'Michelle': 'en-US-MichelleNeural',
'Mitchell': 'en-NZ-MitchellNeural',
'Molly': 'en-NZ-MollyNeural',
'Natasha': 'en-AU-NatashaNeural',
'Neerja': 'en-IN-NeerjaExpressiveNeural',
'Prabhat': 'en-IN-PrabhatNeural',
'Roger': 'en-US-RogerNeural',
'Rosa': 'en-PH-RosaNeural',
'Ryan': 'en-GB-RyanNeural',
'Sam': 'en-HK-SamNeural',
'Sonia': 'en-GB-SoniaNeural',
'Steffan': 'en-US-SteffanNeural',
'Thomas': 'en-GB-ThomasNeural',
'Wayne': 'en-SG-WayneNeural',
'William': 'en-AU-WilliamNeural',
'Yan': 'en-HK-YanNeural'
}
async def text_to_speech_edge(text, language_code):
"""Convert text to speech using Edge TTS"""
if not text:
return "Please enter some text", None
if not language_code:
return "Please select a voice model", None
voice = language_dict.get(language_code, "en-US-AriaNeural")
communicate = edge_tts.Communicate(text, voice)
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
tmp_path = tmp_file.name
await communicate.save(tmp_path)
return f"Speech synthesis completed for: {text[:50]}...", tmp_path
def text_to_speech_sync(text, language_code):
"""Synchronous wrapper for the async function"""
return asyncio.run(text_to_speech_edge(text, language_code))
# Create Gradio interface
iface = gr.Interface(
fn=text_to_speech_sync,
inputs=[
gr.Textbox(lines=5, label="Input Text", placeholder="Enter the text you want to convert to speech..."),
gr.Dropdown(choices=list(language_dict.keys()), label="Choose the Voice Model", value="Aria")
],
outputs=[
gr.Textbox(label="Status"),
gr.Audio(label="Generated Audio")
],
title="Edge TTS - Text to Speech",
description="Microsoft Edge Text-To-Speech (Forked & Fixed Ilaria TTS)",
theme=gr.themes.Soft(),
examples=[
["Hello, this is a test of the text to speech system.", "Aria"],
["Welcome to Edge TTS! This is an example.", "Brian"],
]
)
if __name__ == "__main__":
iface.launch()