snackshell commited on
Commit
2bbabc3
Β·
verified Β·
1 Parent(s): a23d291

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -0
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)