snackshell commited on
Commit
51e442c
Β·
verified Β·
1 Parent(s): f88a8c0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -34
app.py CHANGED
@@ -3,6 +3,7 @@ import edge_tts
3
  import gradio as gr
4
  import asyncio
5
 
 
6
  language_dict = {
7
  "Amharic": {
8
  "Ameha": "am-ET-AmehaNeural",
@@ -13,18 +14,24 @@ language_dict = {
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:
@@ -33,25 +40,25 @@ async def text_to_speech_edge(text, language, speaker):
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:
55
  gr.HTML("""
56
  <style>
57
  h1 {
@@ -60,16 +67,26 @@ with gr.Blocks(title="Amharic, English & Tigrinya TTS") as demo:
60
  background: linear-gradient(45deg, #FF007F, #2E86C1);
61
  -webkit-background-clip: text;
62
  -webkit-text-fill-color: transparent;
 
63
  }
64
- .gradio-button {
65
- background: linear-gradient(45deg, #FF007F, #2E86C1) !important;
66
- color: white !important;
 
 
 
67
  }
68
- .gradio-textbox, .gradio-dropdown {
69
- border-color: #2E86C1 !important;
 
70
  }
71
  </style>
72
- <center><h1>Amharic, English & Tigrinya Text-to-Speech</h1></center>
 
 
 
 
 
73
  """)
74
 
75
  with gr.Row():
@@ -80,25 +97,33 @@ with gr.Blocks(title="Amharic, English & Tigrinya TTS") as demo:
80
  label="Select Language / α‰‹αŠ•α‰‹ α‹­αˆαˆ¨αŒ‘"
81
  )
82
  input_text = gr.Textbox(
83
- lines=5,
84
  label="Enter Text / αŒ½αˆ‘α α‹«αˆ΅αŒˆα‰‘",
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 / αŠ αˆ­α‰²αˆ΅α‰΅ α‹­αˆαˆ¨αŒ‘"
 
 
92
  )
93
- run_btn = gr.Button(value="Generate Audio / α‹΅αˆα… ፍጠር", variant="primary")
94
 
95
  with gr.Column():
96
  output_audio = gr.Audio(
97
  type="filepath",
98
- label="Generated Audio / α‹¨α‰°αˆαŒ αˆ¨ α‹΅αˆα…"
 
99
  )
100
 
101
- # Update the speaker dropdown when language changes.
 
 
 
 
 
 
102
  language.change(
103
  update_speakers,
104
  inputs=language,
@@ -112,4 +137,4 @@ with gr.Blocks(title="Amharic, English & Tigrinya TTS") as demo:
112
  )
113
 
114
  if __name__ == "__main__":
115
- demo.launch(server_port=7860, share=False)
 
3
  import gradio as gr
4
  import asyncio
5
 
6
+ # Updated language dictionary with clear voice labeling
7
  language_dict = {
8
  "Amharic": {
9
  "Ameha": "am-ET-AmehaNeural",
 
14
  "Clara": "en-CA-ClaraNeural"
15
  },
16
  "Tigrinya": {
17
+ "Ameha (Amharic Voice)": "am-ET-AmehaNeural",
18
+ "Mekdes (Amharic Voice)": "am-ET-MekdesNeural"
19
  }
20
  }
21
 
22
  async def text_to_speech_edge(text, language, speaker):
23
+ # Clean speaker name if it's a Tigrinya selection
24
  if language == "Tigrinya":
25
+ speaker = speaker.replace(" (Amharic Voice)", "")
 
 
26
 
27
+ try:
28
+ voice = language_dict[language][speaker]
29
+ except KeyError:
30
+ error_msg = (f"αˆ΅αˆ…α‰°α‰΅: α‹΅αˆα… '{speaker}' αˆˆα‰‹αŠ•α‰‹ '{language}' αŠ αˆα‰°αŒˆαŠ˜αˆα’"
31
+ if language in ["Amharic", "Tigrinya"]
32
+ else f"Error: Voice '{speaker}' not available for '{language}'")
33
+ raise gr.Error(error_msg)
34
+
35
  try:
36
  communicate = edge_tts.Communicate(text, voice)
37
  with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
 
40
  return tmp_path
41
 
42
  except asyncio.TimeoutError:
43
+ error_msg = ("αˆ΅αˆ…α‰°α‰΅: αŒŠα‹œ αŠ αˆα‰‹αˆα’ αŠ₯α‰£αŠ­α‹Ž αŠ₯αŠ•α‹°αŒˆαŠ“ α‹­αˆžαŠ­αˆ©α’"
44
+ if language in ["Amharic", "Tigrinya"]
45
+ else "Error: Timeout. Please try again.")
46
  raise gr.Error(error_msg)
47
  except Exception as e:
48
+ error_msg = (f"αˆ΅αˆ…α‰°α‰΅: {str(e)}"
49
+ if language in ["Amharic", "Tigrinya"]
50
+ else f"Error: {str(e)}")
51
  raise gr.Error(error_msg)
52
 
53
  def update_speakers(language):
54
+ speakers = list(language_dict[language].keys())
55
+ return gr.Dropdown(
56
+ choices=speakers,
57
+ value=speakers[0],
58
+ label=f"Select Speaker {'(Amharic Voices)' if language == 'Tigrinya' else ''}"
59
+ )
60
 
61
+ with gr.Blocks(title="Amharic, English & Tigrinya TTS", theme=gr.themes.Soft()) as demo:
62
  gr.HTML("""
63
  <style>
64
  h1 {
 
67
  background: linear-gradient(45deg, #FF007F, #2E86C1);
68
  -webkit-background-clip: text;
69
  -webkit-text-fill-color: transparent;
70
+ margin-bottom: 20px;
71
  }
72
+ .notice {
73
+ font-size: 0.9em;
74
+ color: #666;
75
+ text-align: center;
76
+ margin: 10px 0;
77
+ font-style: italic;
78
  }
79
+ .gradio-button {
80
+ background: linear-gradient(45deg, #FF007F, #2E86C1) !important;
81
+ color: white !important;
82
  }
83
  </style>
84
+ <center>
85
+ <h1>Amharic, English & Tigrinya Text-to-Speech</h1>
86
+ <div class="notice">
87
+ Note: Tigrinya uses Amharic-accented voices until dedicated models become available
88
+ </div>
89
+ </center>
90
  """)
91
 
92
  with gr.Row():
 
97
  label="Select Language / α‰‹αŠ•α‰‹ α‹­αˆαˆ¨αŒ‘"
98
  )
99
  input_text = gr.Textbox(
100
+ lines=5,
101
  label="Enter Text / αŒ½αˆ‘α α‹«αˆ΅αŒˆα‰‘",
102
  placeholder="Type your text here... / αŒ½αˆ‘αα‹ŽαŠ• α‹­αŒ»α‰..."
103
  )
104
  speaker = gr.Dropdown(
105
+ label="Select Speaker / αŠ αˆ­α‰²αˆ΅α‰΅ α‹­αˆαˆ¨αŒ‘",
106
+ interactive=True
107
+ )
108
+ run_btn = gr.Button(
109
+ value="Generate Audio / α‹΅αˆα… ፍጠር",
110
+ variant="primary"
111
  )
 
112
 
113
  with gr.Column():
114
  output_audio = gr.Audio(
115
  type="filepath",
116
+ label="Generated Audio / α‹¨α‰°αˆαŒ αˆ¨ α‹΅αˆα…",
117
+ interactive=False
118
  )
119
 
120
+ # Initialize speakers dropdown
121
+ demo.load(
122
+ fn=lambda: gr.Dropdown(choices=list(language_dict["Amharic"].keys()),
123
+ outputs=speaker
124
+ )
125
+
126
+ # Update speakers when language changes
127
  language.change(
128
  update_speakers,
129
  inputs=language,
 
137
  )
138
 
139
  if __name__ == "__main__":
140
+ demo.launch(server_port=7860, share=False)