Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,17 +11,22 @@ language_dict = {
|
|
| 11 |
"English": {
|
| 12 |
"Ryan": "en-GB-RyanNeural",
|
| 13 |
"Clara": "en-CA-ClaraNeural"
|
| 14 |
-
}
|
| 15 |
-
"Tigrinya": {} # Empty because we'll use Amharic voices with labels
|
| 16 |
}
|
| 17 |
|
| 18 |
async def text_to_speech_edge(text, language, speaker):
|
|
|
|
| 19 |
if language == "Tigrinya":
|
| 20 |
-
|
| 21 |
-
voice = language_dict["Amharic"][clean_speaker]
|
| 22 |
-
else:
|
| 23 |
-
voice = language_dict[language][speaker]
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
try:
|
| 26 |
communicate = edge_tts.Communicate(text, voice)
|
| 27 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
|
|
@@ -31,104 +36,58 @@ async def text_to_speech_edge(text, language, speaker):
|
|
| 31 |
|
| 32 |
except asyncio.TimeoutError:
|
| 33 |
error_msg = ("α΅α
α°α΅: αα α αααα’ α₯α£αα α₯αα°αα αααα©α’"
|
| 34 |
-
if language
|
| 35 |
else "Error: Timeout. Please try again.")
|
| 36 |
raise gr.Error(error_msg)
|
| 37 |
except Exception as e:
|
| 38 |
error_msg = (f"α΅α
α°α΅: {str(e)}"
|
| 39 |
-
if language
|
| 40 |
else f"Error: {str(e)}")
|
| 41 |
raise gr.Error(error_msg)
|
| 42 |
|
| 43 |
def update_speakers(language):
|
| 44 |
if language == "Tigrinya":
|
| 45 |
-
speakers =
|
| 46 |
else:
|
| 47 |
-
speakers = list(language_dict[
|
| 48 |
|
| 49 |
-
return gr.Dropdown(
|
| 50 |
-
choices=speakers,
|
| 51 |
-
value=speakers[0],
|
| 52 |
-
label=f"Select Speaker {'(Amharic Voices)' if language == 'Tigrinya' else ''}"
|
| 53 |
-
)
|
| 54 |
-
|
| 55 |
-
with gr.Blocks(title="Amharic, English & Tigrinya TTS", theme=gr.themes.Soft()) as demo:
|
| 56 |
-
gr.HTML("""
|
| 57 |
-
<style>
|
| 58 |
-
h1 {
|
| 59 |
-
color: #2E86C1;
|
| 60 |
-
text-align: center;
|
| 61 |
-
background: linear-gradient(45deg, #FF007F, #2E86C1);
|
| 62 |
-
-webkit-background-clip: text;
|
| 63 |
-
-webkit-text-fill-color: transparent;
|
| 64 |
-
margin-bottom: 20px;
|
| 65 |
-
}
|
| 66 |
-
.notice {
|
| 67 |
-
font-size: 0.9em;
|
| 68 |
-
color: #666;
|
| 69 |
-
text-align: center;
|
| 70 |
-
margin: 10px 0;
|
| 71 |
-
font-style: italic;
|
| 72 |
-
}
|
| 73 |
-
.gradio-button {
|
| 74 |
-
background: linear-gradient(45deg, #FF007F, #2E86C1) !important;
|
| 75 |
-
color: white !important;
|
| 76 |
-
}
|
| 77 |
-
</style>
|
| 78 |
-
<center>
|
| 79 |
-
<h1>Amharic, English & Tigrinya Text-to-Speech</h1>
|
| 80 |
-
<div class="notice">
|
| 81 |
-
Note: Tigrinya uses Amharic-accented voices until dedicated models become available
|
| 82 |
-
</div>
|
| 83 |
-
</center>
|
| 84 |
-
""")
|
| 85 |
|
|
|
|
|
|
|
|
|
|
| 86 |
with gr.Row():
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
run_btn = gr.Button(
|
| 103 |
-
value="Generate Audio / α΅αα
αα α",
|
| 104 |
-
variant="primary"
|
| 105 |
-
)
|
| 106 |
-
|
| 107 |
-
with gr.Column():
|
| 108 |
-
output_audio = gr.Audio(
|
| 109 |
-
type="filepath",
|
| 110 |
-
label="Generated Audio / α¨α°αα α¨ α΅αα
",
|
| 111 |
-
interactive=False
|
| 112 |
-
)
|
| 113 |
-
|
| 114 |
-
# Initialize speakers dropdown
|
| 115 |
demo.load(
|
| 116 |
-
fn=lambda: gr.
|
| 117 |
outputs=speaker
|
| 118 |
)
|
| 119 |
-
|
| 120 |
-
# Update speakers when language changes
|
| 121 |
language.change(
|
| 122 |
update_speakers,
|
| 123 |
inputs=language,
|
| 124 |
outputs=speaker
|
| 125 |
)
|
| 126 |
-
|
| 127 |
-
|
| 128 |
text_to_speech_edge,
|
| 129 |
inputs=[input_text, language, speaker],
|
| 130 |
outputs=output_audio
|
| 131 |
)
|
| 132 |
|
| 133 |
if __name__ == "__main__":
|
| 134 |
-
demo.launch(server_port=7860
|
|
|
|
| 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 |
+
# Handle Tigrinya as Amharic with different label
|
| 19 |
if language == "Tigrinya":
|
| 20 |
+
language = "Amharic"
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
try:
|
| 23 |
+
voice = language_dict[language][speaker]
|
| 24 |
+
except KeyError:
|
| 25 |
+
error_msg = (f"α΅α
α°α΅: α΅αα
'{speaker}' α αα°αααα’"
|
| 26 |
+
if language == "Amharic"
|
| 27 |
+
else f"Error: Voice '{speaker}' not found")
|
| 28 |
+
raise gr.Error(error_msg)
|
| 29 |
+
|
| 30 |
try:
|
| 31 |
communicate = edge_tts.Communicate(text, voice)
|
| 32 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
|
|
|
|
| 36 |
|
| 37 |
except asyncio.TimeoutError:
|
| 38 |
error_msg = ("α΅α
α°α΅: αα α αααα’ α₯α£αα α₯αα°αα αααα©α’"
|
| 39 |
+
if language == "Amharic"
|
| 40 |
else "Error: Timeout. Please try again.")
|
| 41 |
raise gr.Error(error_msg)
|
| 42 |
except Exception as e:
|
| 43 |
error_msg = (f"α΅α
α°α΅: {str(e)}"
|
| 44 |
+
if language == "Amharic"
|
| 45 |
else f"Error: {str(e)}")
|
| 46 |
raise gr.Error(error_msg)
|
| 47 |
|
| 48 |
def update_speakers(language):
|
| 49 |
if language == "Tigrinya":
|
| 50 |
+
speakers = list(language_dict["Amharic"].keys())
|
| 51 |
else:
|
| 52 |
+
speakers = list(language_dict.get(language, []))
|
| 53 |
|
| 54 |
+
return gr.Dropdown(choices=speakers, value=speakers[0] if speakers else None)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
+
with gr.Blocks(title="SelamGPT TTS") as demo:
|
| 57 |
+
gr.Markdown("## Amharic, English & Tigrinya Text-to-Speech")
|
| 58 |
+
|
| 59 |
with gr.Row():
|
| 60 |
+
language = gr.Dropdown(
|
| 61 |
+
choices=["Amharic", "English", "Tigrinya"],
|
| 62 |
+
value="Amharic",
|
| 63 |
+
label="Language"
|
| 64 |
+
)
|
| 65 |
+
speaker = gr.Dropdown(
|
| 66 |
+
label="Speaker",
|
| 67 |
+
allow_custom_value=False
|
| 68 |
+
)
|
| 69 |
+
input_text = gr.Textbox(label="Input Text")
|
| 70 |
+
|
| 71 |
+
generate_btn = gr.Button("Generate Audio")
|
| 72 |
+
output_audio = gr.Audio(label="Output")
|
| 73 |
+
|
| 74 |
+
# API endpoints
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
demo.load(
|
| 76 |
+
fn=lambda: gr.Dropdown(choices=language_dict["Amharic"].keys()),
|
| 77 |
outputs=speaker
|
| 78 |
)
|
| 79 |
+
|
|
|
|
| 80 |
language.change(
|
| 81 |
update_speakers,
|
| 82 |
inputs=language,
|
| 83 |
outputs=speaker
|
| 84 |
)
|
| 85 |
+
|
| 86 |
+
generate_btn.click(
|
| 87 |
text_to_speech_edge,
|
| 88 |
inputs=[input_text, language, speaker],
|
| 89 |
outputs=output_audio
|
| 90 |
)
|
| 91 |
|
| 92 |
if __name__ == "__main__":
|
| 93 |
+
demo.launch(server_port=7860)
|