Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -88,7 +88,7 @@ VOICES: Dict[str, str] = {
|
|
88 |
"InJoon (Male, KR)": "ko-KR-InJoonNeural",
|
89 |
|
90 |
# Russian
|
91 |
-
"Svetlana (Female, RU)": "ru-RU-
|
92 |
"Dmitry (Male, RU)": "ru-RU-DmitryNeural",
|
93 |
|
94 |
# Arabic
|
@@ -108,9 +108,12 @@ VOICES: Dict[str, str] = {
|
|
108 |
"Sofie (Female, NL)": "nl-NL-SofieNeural",
|
109 |
}
|
110 |
|
111 |
-
def text_to_speech(text: str, voice: str) ->
|
112 |
-
"""Wrapper function to run async code"""
|
113 |
-
|
|
|
|
|
|
|
114 |
|
115 |
def get_languages() -> list:
|
116 |
"""Extract unique languages from voice names"""
|
@@ -127,13 +130,12 @@ def filter_voices(language: str) -> Dict[str, str]:
|
|
127 |
return VOICES
|
128 |
return {name: voice for name, voice in VOICES.items() if f"({language})" in name}
|
129 |
|
130 |
-
def update_voice_dropdown(language: str) ->
|
131 |
-
"""
|
132 |
filtered_voices = filter_voices(language)
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
)
|
137 |
|
138 |
with gr.Blocks(title="Multi-Voice Text-to-Speech", theme="soft") as demo:
|
139 |
gr.Markdown("""
|
@@ -146,7 +148,7 @@ with gr.Blocks(title="Multi-Voice Text-to-Speech", theme="soft") as demo:
|
|
146 |
text_input = gr.Textbox(
|
147 |
label="Enter your text",
|
148 |
placeholder="Type or paste your text here...",
|
149 |
-
lines=5,
|
150 |
max_lines=10
|
151 |
)
|
152 |
|
@@ -173,21 +175,22 @@ with gr.Blocks(title="Multi-Voice Text-to-Speech", theme="soft") as demo:
|
|
173 |
|
174 |
# Interactive components
|
175 |
language_filter.change(
|
176 |
-
update_voice_dropdown,
|
177 |
inputs=language_filter,
|
178 |
-
outputs=voice_dropdown
|
179 |
)
|
180 |
|
181 |
generate_btn.click(
|
182 |
-
text_to_speech,
|
183 |
inputs=[text_input, voice_dropdown],
|
184 |
outputs=[audio_output, download_btn]
|
185 |
)
|
186 |
|
187 |
audio_output.change(
|
188 |
-
lambda: gr.DownloadButton.update(visible=
|
|
|
189 |
outputs=download_btn
|
190 |
)
|
191 |
|
192 |
if __name__ == "__main__":
|
193 |
-
demo.launch()
|
|
|
88 |
"InJoon (Male, KR)": "ko-KR-InJoonNeural",
|
89 |
|
90 |
# Russian
|
91 |
+
"Svetlana (Female, RU)": "ru-RU-Svetlaneural",
|
92 |
"Dmitry (Male, RU)": "ru-RU-DmitryNeural",
|
93 |
|
94 |
# Arabic
|
|
|
108 |
"Sofie (Female, NL)": "nl-NL-SofieNeural",
|
109 |
}
|
110 |
|
111 |
+
def text_to_speech(text: str, voice: str) -> tuple:
|
112 |
+
"""Wrapper function to run async code and return audio file and download button state"""
|
113 |
+
if not text or not voice:
|
114 |
+
return None, gr.DownloadButton.update(visible=False)
|
115 |
+
output_file = asyncio.run(generate_speech(text, VOICES.get(voice, VOICES["Jenny (Female, US)"])))
|
116 |
+
return output_file, gr.DownloadButton.update(visible=True) if output_file else (None, gr.DownloadButton.update(visible=False))
|
117 |
|
118 |
def get_languages() -> list:
|
119 |
"""Extract unique languages from voice names"""
|
|
|
130 |
return VOICES
|
131 |
return {name: voice for name, voice in VOICES.items() if f"({language})" in name}
|
132 |
|
133 |
+
def update_voice_dropdown(language: str) -> tuple:
|
134 |
+
"""Return new choices and default value for voice dropdown"""
|
135 |
filtered_voices = filter_voices(language)
|
136 |
+
choices = list(filtered_voices.keys())
|
137 |
+
default_value = choices[0] if choices else None
|
138 |
+
return choices, default_value
|
|
|
139 |
|
140 |
with gr.Blocks(title="Multi-Voice Text-to-Speech", theme="soft") as demo:
|
141 |
gr.Markdown("""
|
|
|
148 |
text_input = gr.Textbox(
|
149 |
label="Enter your text",
|
150 |
placeholder="Type or paste your text here...",
|
151 |
+
lines mortal=5,
|
152 |
max_lines=10
|
153 |
)
|
154 |
|
|
|
175 |
|
176 |
# Interactive components
|
177 |
language_filter.change(
|
178 |
+
fn=update_voice_dropdown,
|
179 |
inputs=language_filter,
|
180 |
+
outputs=[voice_dropdown]
|
181 |
)
|
182 |
|
183 |
generate_btn.click(
|
184 |
+
fn=text_to_speech,
|
185 |
inputs=[text_input, voice_dropdown],
|
186 |
outputs=[audio_output, download_btn]
|
187 |
)
|
188 |
|
189 |
audio_output.change(
|
190 |
+
fn=lambda x: gr.DownloadButton.update(visible=bool(x)),
|
191 |
+
inputs=audio_output,
|
192 |
outputs=download_btn
|
193 |
)
|
194 |
|
195 |
if __name__ == "__main__":
|
196 |
+
demo.launch(share=True) # Set share=True for public link (requires internet)
|