Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import tempfile
|
2 |
+
import edge_tts
|
3 |
+
import gradio as gr
|
4 |
+
import asyncio
|
5 |
+
|
6 |
+
language_dict = {
|
7 |
+
"Amharic": {
|
8 |
+
"Ameha": "am-ET-AmehaNeural",
|
9 |
+
"Mekdes": "am-ET-MekdesNeural"
|
10 |
+
},
|
11 |
+
"English": {
|
12 |
+
"Ryan": "en-GB-RyanNeural",
|
13 |
+
"Clara": "en-CA-ClaraNeural"
|
14 |
+
}
|
15 |
+
}
|
16 |
+
|
17 |
+
async def text_to_speech_edge(text, language, speaker):
|
18 |
+
voice = language_dict[language][speaker]
|
19 |
+
|
20 |
+
try:
|
21 |
+
communicate = edge_tts.Communicate(text, voice)
|
22 |
+
|
23 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
|
24 |
+
tmp_path = tmp_file.name
|
25 |
+
await asyncio.wait_for(communicate.save(tmp_path), timeout=30)
|
26 |
+
|
27 |
+
return tmp_path
|
28 |
+
|
29 |
+
except asyncio.TimeoutError:
|
30 |
+
error_msg = "α΅α
α°α΅: αα α αααα’ α₯α£αα α₯αα°αα αααα©α’ (Timeout)" if language == "Amharic" else "Error: Timeout. Please try again."
|
31 |
+
raise gr.Error(error_msg)
|
32 |
+
except Exception as e:
|
33 |
+
error_msg = f"α΅α
α°α΅: α΅αα
ααα α α αα°α»ααα’\nError: {str(e)}" if language == "Amharic" else f"Error: Failed to generate audio.\nDetails: {str(e)}"
|
34 |
+
raise gr.Error(error_msg)
|
35 |
+
|
36 |
+
def update_speakers(language):
|
37 |
+
speakers = list(language_dict[language].keys())
|
38 |
+
return gr.Dropdown(choices=speakers, value=speakers[0])
|
39 |
+
|
40 |
+
with gr.Blocks(title="Amharic & English TTS") as demo:
|
41 |
+
gr.HTML("""
|
42 |
+
<style>
|
43 |
+
h1 {
|
44 |
+
color: #2E86C1;
|
45 |
+
text-align: center;
|
46 |
+
background: linear-gradient(45deg, #FF007F, #2E86C1);
|
47 |
+
-webkit-background-clip: text;
|
48 |
+
-webkit-text-fill-color: transparent;
|
49 |
+
}
|
50 |
+
.gradio-button {
|
51 |
+
background: linear-gradient(45deg, #FF007F, #2E86C1) !important;
|
52 |
+
color: white !important;
|
53 |
+
}
|
54 |
+
.gradio-textbox, .gradio-dropdown {
|
55 |
+
border-color: #2E86C1 !important;
|
56 |
+
}
|
57 |
+
</style>
|
58 |
+
<center><h1>Amharic & English Text-to-Speech</h1></center>
|
59 |
+
""")
|
60 |
+
|
61 |
+
with gr.Row():
|
62 |
+
with gr.Column():
|
63 |
+
language = gr.Dropdown(
|
64 |
+
choices=["Amharic", "English"],
|
65 |
+
value="Amharic",
|
66 |
+
label="Select Language / ααα ααα¨α‘"
|
67 |
+
)
|
68 |
+
input_text = gr.Textbox(
|
69 |
+
lines=5,
|
70 |
+
label="Enter Text / α½αα α«α΅αα‘",
|
71 |
+
placeholder="Type your text here... / α½αααα αα»α..."
|
72 |
+
)
|
73 |
+
speaker = gr.Dropdown(
|
74 |
+
choices=["Ameha", "Mekdes"],
|
75 |
+
value="Ameha",
|
76 |
+
label="Select Speaker / α αα²α΅α΅ ααα¨α‘"
|
77 |
+
)
|
78 |
+
run_btn = gr.Button(value="Generate Audio / α΅αα
αα α", variant="primary")
|
79 |
+
|
80 |
+
with gr.Column():
|
81 |
+
output_audio = gr.Audio(
|
82 |
+
type="filepath",
|
83 |
+
label="Generated Audio / α¨α°αα α¨ α΅αα
"
|
84 |
+
)
|
85 |
+
|
86 |
+
language.change(
|
87 |
+
update_speakers,
|
88 |
+
inputs=language,
|
89 |
+
outputs=speaker
|
90 |
+
)
|
91 |
+
|
92 |
+
run_btn.click(
|
93 |
+
text_to_speech_edge,
|
94 |
+
inputs=[input_text, language, speaker],
|
95 |
+
outputs=output_audio
|
96 |
+
)
|
97 |
+
|
98 |
+
if __name__ == "__main__":
|
99 |
+
demo.launch(server_port=7860, share=False)
|