Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,7 +15,7 @@ async def generate_speech(text: str, voice: str) -> str:
|
|
| 15 |
print(f"Error: {e}")
|
| 16 |
return None
|
| 17 |
|
| 18 |
-
#
|
| 19 |
VOICES: Dict[str, str] = {
|
| 20 |
# English (US)
|
| 21 |
"Jenny (Female, US)": "en-US-JennyNeural",
|
|
@@ -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
|
|
@@ -100,7 +100,7 @@ VOICES: Dict[str, str] = {
|
|
| 100 |
"Swara (Female, IN)": "hi-IN-SwaraNeural",
|
| 101 |
"Madhur (Male, IN)": "hi-IN-MadhurNeural",
|
| 102 |
|
| 103 |
-
#
|
| 104 |
"Brigitte (Female, BE)": "fr-BE-BrigitteNeural",
|
| 105 |
"Gerard (Male, BE)": "fr-BE-GerardNeural",
|
| 106 |
"Finn (Male, NL)": "nl-NL-FinnNeural",
|
|
@@ -111,36 +111,14 @@ VOICES: Dict[str, str] = {
|
|
| 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,
|
| 115 |
output_file = asyncio.run(generate_speech(text, VOICES.get(voice, VOICES["Jenny (Female, US)"])))
|
| 116 |
-
return output_file,
|
| 117 |
-
|
| 118 |
-
def get_languages() -> list:
|
| 119 |
-
"""Extract unique languages from voice names"""
|
| 120 |
-
languages = set()
|
| 121 |
-
for name in VOICES.keys():
|
| 122 |
-
if '(' in name and ')' in name:
|
| 123 |
-
lang = name.split('(')[1].split(')')[0]
|
| 124 |
-
languages.add(lang)
|
| 125 |
-
return sorted(languages)
|
| 126 |
-
|
| 127 |
-
def filter_voices(language: str) -> Dict[str, str]:
|
| 128 |
-
"""Filter voices by selected language"""
|
| 129 |
-
if not language or language == "All":
|
| 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("""
|
| 142 |
# 🎤 Advanced Text-to-Speech Converter
|
| 143 |
-
### With 100+ Voices
|
| 144 |
""")
|
| 145 |
|
| 146 |
with gr.Row():
|
|
@@ -148,21 +126,15 @@ with gr.Blocks(title="Multi-Voice Text-to-Speech", theme="soft") as demo:
|
|
| 148 |
text_input = gr.Textbox(
|
| 149 |
label="Enter your text",
|
| 150 |
placeholder="Type or paste your text here...",
|
| 151 |
-
lines
|
| 152 |
max_lines=10
|
| 153 |
)
|
| 154 |
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
)
|
| 161 |
-
voice_dropdown = gr.Dropdown(
|
| 162 |
-
list(VOICES.keys()),
|
| 163 |
-
label="Select Voice",
|
| 164 |
-
value="Jenny (Female, US)"
|
| 165 |
-
)
|
| 166 |
|
| 167 |
generate_btn = gr.Button("Generate Speech", variant="primary")
|
| 168 |
|
|
@@ -174,12 +146,6 @@ with gr.Blocks(title="Multi-Voice Text-to-Speech", theme="soft") as demo:
|
|
| 174 |
)
|
| 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],
|
|
@@ -187,10 +153,10 @@ with gr.Blocks(title="Multi-Voice Text-to-Speech", theme="soft") as demo:
|
|
| 187 |
)
|
| 188 |
|
| 189 |
audio_output.change(
|
| 190 |
-
fn=lambda 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 (
|
|
|
|
| 15 |
print(f"Error: {e}")
|
| 16 |
return None
|
| 17 |
|
| 18 |
+
# List of voices
|
| 19 |
VOICES: Dict[str, str] = {
|
| 20 |
# English (US)
|
| 21 |
"Jenny (Female, US)": "en-US-JennyNeural",
|
|
|
|
| 88 |
"InJoon (Male, KR)": "ko-KR-InJoonNeural",
|
| 89 |
|
| 90 |
# Russian
|
| 91 |
+
"Svetlana (Female, RU)": "ru-RU-SvetlanaNeural",
|
| 92 |
"Dmitry (Male, RU)": "ru-RU-DmitryNeural",
|
| 93 |
|
| 94 |
# Arabic
|
|
|
|
| 100 |
"Swara (Female, IN)": "hi-IN-SwaraNeural",
|
| 101 |
"Madhur (Male, IN)": "hi-IN-MadhurNeural",
|
| 102 |
|
| 103 |
+
# Others
|
| 104 |
"Brigitte (Female, BE)": "fr-BE-BrigitteNeural",
|
| 105 |
"Gerard (Male, BE)": "fr-BE-GerardNeural",
|
| 106 |
"Finn (Male, NL)": "nl-NL-FinnNeural",
|
|
|
|
| 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, {"visible": False}
|
| 115 |
output_file = asyncio.run(generate_speech(text, VOICES.get(voice, VOICES["Jenny (Female, US)"])))
|
| 116 |
+
return output_file, {"visible": True} if output_file else (None, {"visible": False})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
with gr.Blocks(title="Multi-Voice Text-to-Speech", theme="soft") as demo:
|
| 119 |
gr.Markdown("""
|
| 120 |
# 🎤 Advanced Text-to-Speech Converter
|
| 121 |
+
### With 100+ Voices
|
| 122 |
""")
|
| 123 |
|
| 124 |
with gr.Row():
|
|
|
|
| 126 |
text_input = gr.Textbox(
|
| 127 |
label="Enter your text",
|
| 128 |
placeholder="Type or paste your text here...",
|
| 129 |
+
lines=5,
|
| 130 |
max_lines=10
|
| 131 |
)
|
| 132 |
|
| 133 |
+
voice_dropdown = gr.Dropdown(
|
| 134 |
+
choices=list(VOICES.keys()),
|
| 135 |
+
label="Select Voice",
|
| 136 |
+
value="Jenny (Female, US)"
|
| 137 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
generate_btn = gr.Button("Generate Speech", variant="primary")
|
| 140 |
|
|
|
|
| 146 |
)
|
| 147 |
|
| 148 |
# Interactive components
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
generate_btn.click(
|
| 150 |
fn=text_to_speech,
|
| 151 |
inputs=[text_input, voice_dropdown],
|
|
|
|
| 153 |
)
|
| 154 |
|
| 155 |
audio_output.change(
|
| 156 |
+
fn=lambda x: {"visible": bool(x)},
|
| 157 |
inputs=audio_output,
|
| 158 |
outputs=download_btn
|
| 159 |
)
|
| 160 |
|
| 161 |
if __name__ == "__main__":
|
| 162 |
+
demo.launch(share=True) # Set share=True for public link (remove for local only)
|