amharic-tts / app.py
snackshell's picture
Update app.py
19293b4 verified
import tempfile
import edge_tts
import gradio as gr
import asyncio
language_dict = {
"Amharic": {
"Ameha": "am-ET-AmehaNeural",
"Mekdes": "am-ET-MekdesNeural"
}
}
async def text_to_speech_edge(text, speaker):
voice = language_dict["Amharic"][speaker]
try:
communicate = edge_tts.Communicate(text, voice)
# Create temp file with increased timeout
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
tmp_path = tmp_file.name
await asyncio.wait_for(communicate.save(tmp_path), timeout=30)
return tmp_path
except asyncio.TimeoutError:
error_msg = "αˆ΅αˆ…α‰°α‰΅: αŒŠα‹œ αŠ αˆα‰‹αˆα’ αŠ₯α‰£αŠ­α‹Ž αŠ₯αŠ•α‹°αŒˆαŠ“ α‹­αˆžαŠ­αˆ©α’ (Timeout)"
raise gr.Error(error_msg)
except Exception as e:
error_msg = f"αˆ΅αˆ…α‰°α‰΅: α‹΅αˆα… መፍጠር αŠ αˆα‰°α‰»αˆˆαˆα’\nError: {str(e)}"
raise gr.Error(error_msg)
with gr.Blocks(title="Amharic TTS") as demo:
gr.HTML("""
<style>
h1 { color: #FF007F; text-align: center; }
.gradio-button { background-color: #FF007F !important; color: white !important; }
.gradio-textbox, .gradio-dropdown { border-color: #FF007F !important; }
</style>
<center><h1>Amharic Text-to-Speech</h1></center>
""")
with gr.Row():
with gr.Column():
input_text = gr.Textbox(lines=5, label="α‹¨αŠ αˆ›αˆ­αŠ› αŒ½αˆ‘α",
placeholder="α‹΅αˆα… ለመፍጠር αŒ½αˆ‘α α‹«αˆ΅αŒˆα‰‘...")
speaker = gr.Dropdown(
choices=["Ameha", "Mekdes"],
value="Ameha",
label="αŠ αˆ­α‰²αˆ΅α‰΅"
)
run_btn = gr.Button(value="α‹΅αˆα… ፍጠር", variant="primary")
with gr.Column():
output_audio = gr.Audio(type="filepath", label="α‹¨α‹΅αˆα… α‹αŒ€α‰΅")
run_btn.click(
text_to_speech_edge,
inputs=[input_text, speaker],
outputs=output_audio
)
if __name__ == "__main__":
demo.launch(server_port=7860, share=False)