Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -13,37 +13,42 @@ language_dict = {
|
|
13 |
"Clara": "en-CA-ClaraNeural"
|
14 |
},
|
15 |
"Tigrinya": {
|
16 |
-
"Ameha": "
|
17 |
-
"Mekdes": "
|
18 |
}
|
19 |
}
|
20 |
|
21 |
async def text_to_speech_edge(text, language, speaker):
|
|
|
|
|
|
|
|
|
22 |
voice = language_dict[language][speaker]
|
23 |
|
24 |
try:
|
25 |
communicate = edge_tts.Communicate(text, voice)
|
26 |
-
|
27 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
|
28 |
tmp_path = tmp_file.name
|
29 |
await asyncio.wait_for(communicate.save(tmp_path), timeout=30)
|
30 |
-
|
31 |
return tmp_path
|
32 |
|
33 |
except asyncio.TimeoutError:
|
34 |
error_msg = ("α΅α
α°α΅: αα α αααα’ α₯α£αα α₯αα°αα αααα©α’ (Timeout)"
|
35 |
-
if language == "Amharic"
|
36 |
else "Error: Timeout. Please try again.")
|
37 |
raise gr.Error(error_msg)
|
38 |
except Exception as e:
|
39 |
error_msg = (f"α΅α
α°α΅: α΅αα
ααα α α αα°α»ααα’\nError: {str(e)}"
|
40 |
-
if language == "Amharic"
|
41 |
else f"Error: Failed to generate audio.\nDetails: {str(e)}")
|
42 |
raise gr.Error(error_msg)
|
43 |
|
44 |
def update_speakers(language):
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
47 |
return gr.update(choices=speakers, value=speakers[0])
|
48 |
|
49 |
with gr.Blocks(title="Amharic, English & Tigrinya TTS") as demo:
|
@@ -80,6 +85,7 @@ with gr.Blocks(title="Amharic, English & Tigrinya TTS") as demo:
|
|
80 |
placeholder="Type your text here... / α½αααα αα»α..."
|
81 |
)
|
82 |
speaker = gr.Dropdown(
|
|
|
83 |
choices=["Ameha", "Mekdes"],
|
84 |
value="Ameha",
|
85 |
label="Select Speaker / α αα²α΅α΅ ααα¨α‘"
|
@@ -92,7 +98,7 @@ with gr.Blocks(title="Amharic, English & Tigrinya TTS") as demo:
|
|
92 |
label="Generated Audio / α¨α°αα α¨ α΅αα
"
|
93 |
)
|
94 |
|
95 |
-
#
|
96 |
language.change(
|
97 |
update_speakers,
|
98 |
inputs=language,
|
|
|
13 |
"Clara": "en-CA-ClaraNeural"
|
14 |
},
|
15 |
"Tigrinya": {
|
16 |
+
"Ameha": "am-ET-AmehaNeural",
|
17 |
+
"Mekdes": "am-ET-MekdesNeural"
|
18 |
}
|
19 |
}
|
20 |
|
21 |
async def text_to_speech_edge(text, language, speaker):
|
22 |
+
# If Tigrinya is selected, override to use Amharic voices.
|
23 |
+
if language == "Tigrinya":
|
24 |
+
language = "Amharic"
|
25 |
+
|
26 |
voice = language_dict[language][speaker]
|
27 |
|
28 |
try:
|
29 |
communicate = edge_tts.Communicate(text, voice)
|
|
|
30 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
|
31 |
tmp_path = tmp_file.name
|
32 |
await asyncio.wait_for(communicate.save(tmp_path), timeout=30)
|
|
|
33 |
return tmp_path
|
34 |
|
35 |
except asyncio.TimeoutError:
|
36 |
error_msg = ("α΅α
α°α΅: αα α αααα’ α₯α£αα α₯αα°αα αααα©α’ (Timeout)"
|
37 |
+
if language == "Amharic"
|
38 |
else "Error: Timeout. Please try again.")
|
39 |
raise gr.Error(error_msg)
|
40 |
except Exception as e:
|
41 |
error_msg = (f"α΅α
α°α΅: α΅αα
ααα α α αα°α»ααα’\nError: {str(e)}"
|
42 |
+
if language == "Amharic"
|
43 |
else f"Error: Failed to generate audio.\nDetails: {str(e)}")
|
44 |
raise gr.Error(error_msg)
|
45 |
|
46 |
def update_speakers(language):
|
47 |
+
# For Tigrinya, return Amharic speakers.
|
48 |
+
if language == "Tigrinya":
|
49 |
+
speakers = list(language_dict["Amharic"].keys())
|
50 |
+
else:
|
51 |
+
speakers = list(language_dict[language].keys())
|
52 |
return gr.update(choices=speakers, value=speakers[0])
|
53 |
|
54 |
with gr.Blocks(title="Amharic, English & Tigrinya TTS") as demo:
|
|
|
85 |
placeholder="Type your text here... / α½αααα αα»α..."
|
86 |
)
|
87 |
speaker = gr.Dropdown(
|
88 |
+
# For Tigrinya, this will be updated to Amharic speakers.
|
89 |
choices=["Ameha", "Mekdes"],
|
90 |
value="Ameha",
|
91 |
label="Select Speaker / α αα²α΅α΅ ααα¨α‘"
|
|
|
98 |
label="Generated Audio / α¨α°αα α¨ α΅αα
"
|
99 |
)
|
100 |
|
101 |
+
# Update the speaker dropdown when language changes.
|
102 |
language.change(
|
103 |
update_speakers,
|
104 |
inputs=language,
|