Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ import tempfile
|
|
| 5 |
import os
|
| 6 |
|
| 7 |
async def get_voices():
|
|
|
|
| 8 |
voices = await edge_tts.list_voices()
|
| 9 |
return {f"{v['ShortName']} - {v['Locale']} ({v['Gender']})": v['ShortName'] for v in voices}
|
| 10 |
|
|
@@ -14,28 +15,31 @@ async def text_to_speech(text, voice, rate, pitch):
|
|
| 14 |
if not voice:
|
| 15 |
return None, "Please select a voice."
|
| 16 |
|
|
|
|
| 17 |
voice_short_name = voice.split(" - ")[0]
|
| 18 |
rate_str = f"{rate:+d}%"
|
| 19 |
pitch_str = f"{pitch:+d}Hz"
|
|
|
|
| 20 |
communicate = edge_tts.Communicate(text, voice_short_name, rate=rate_str, pitch=pitch_str)
|
|
|
|
| 21 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
|
| 22 |
tmp_path = tmp_file.name
|
| 23 |
await communicate.save(tmp_path)
|
|
|
|
| 24 |
return tmp_path, None
|
| 25 |
|
| 26 |
async def tts_interface(text, voice, rate, pitch):
|
| 27 |
audio, warning = await text_to_speech(text, voice, rate, pitch)
|
| 28 |
if warning:
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
async def create_demo():
|
| 33 |
voices = await get_voices()
|
| 34 |
-
|
| 35 |
-
description = """
|
| 36 |
-
Convert text to speech using Free-TTS_unlimited-words Adjust speech rate and pitch: 0 is default, positive values increase, negative values decrease.
|
| 37 |
-
|
| 38 |
-
"""
|
| 39 |
|
| 40 |
demo = gr.Interface(
|
| 41 |
fn=tts_interface,
|
|
@@ -47,20 +51,19 @@ async def create_demo():
|
|
| 47 |
],
|
| 48 |
outputs=[
|
| 49 |
gr.Audio(label="Generated Audio", type="filepath"),
|
| 50 |
-
gr.Markdown(label="
|
| 51 |
],
|
| 52 |
-
title="Free-
|
| 53 |
description=description,
|
| 54 |
-
article="Experience the power of Edge TTS for text-to-speech conversion, and explore our advanced Text-to-Video Converter for even more creative possibilities!",
|
| 55 |
analytics_enabled=False,
|
| 56 |
-
allow_flagging="manual",
|
| 57 |
api_name=None
|
| 58 |
)
|
| 59 |
return demo
|
| 60 |
|
| 61 |
async def main():
|
| 62 |
demo = await create_demo()
|
| 63 |
-
|
|
|
|
| 64 |
demo.launch(show_api=False)
|
| 65 |
|
| 66 |
if __name__ == "__main__":
|
|
|
|
| 5 |
import os
|
| 6 |
|
| 7 |
async def get_voices():
|
| 8 |
+
# Obtiene la lista de voces disponibles
|
| 9 |
voices = await edge_tts.list_voices()
|
| 10 |
return {f"{v['ShortName']} - {v['Locale']} ({v['Gender']})": v['ShortName'] for v in voices}
|
| 11 |
|
|
|
|
| 15 |
if not voice:
|
| 16 |
return None, "Please select a voice."
|
| 17 |
|
| 18 |
+
# Corregida la identaci贸n y extracci贸n del nombre
|
| 19 |
voice_short_name = voice.split(" - ")[0]
|
| 20 |
rate_str = f"{rate:+d}%"
|
| 21 |
pitch_str = f"{pitch:+d}Hz"
|
| 22 |
+
|
| 23 |
communicate = edge_tts.Communicate(text, voice_short_name, rate=rate_str, pitch=pitch_str)
|
| 24 |
+
|
| 25 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
|
| 26 |
tmp_path = tmp_file.name
|
| 27 |
await communicate.save(tmp_path)
|
| 28 |
+
|
| 29 |
return tmp_path, None
|
| 30 |
|
| 31 |
async def tts_interface(text, voice, rate, pitch):
|
| 32 |
audio, warning = await text_to_speech(text, voice, rate, pitch)
|
| 33 |
if warning:
|
| 34 |
+
# Lanzar la advertencia y devolver None para el audio
|
| 35 |
+
gr.Warning(warning)
|
| 36 |
+
return None, warning
|
| 37 |
+
# Devolver el audio y limpiar el campo de Markdown
|
| 38 |
+
return audio, ""
|
| 39 |
|
| 40 |
async def create_demo():
|
| 41 |
voices = await get_voices()
|
| 42 |
+
description = "Convert text to speech using Edge TTS. Adjust speech rate and pitch."
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
demo = gr.Interface(
|
| 45 |
fn=tts_interface,
|
|
|
|
| 51 |
],
|
| 52 |
outputs=[
|
| 53 |
gr.Audio(label="Generated Audio", type="filepath"),
|
| 54 |
+
gr.Markdown(label="Status") # Cambiado para recibir el mensaje de texto/error
|
| 55 |
],
|
| 56 |
+
title="Free-TTS Unlimited",
|
| 57 |
description=description,
|
|
|
|
| 58 |
analytics_enabled=False,
|
|
|
|
| 59 |
api_name=None
|
| 60 |
)
|
| 61 |
return demo
|
| 62 |
|
| 63 |
async def main():
|
| 64 |
demo = await create_demo()
|
| 65 |
+
# Gradio 5: queue ya no requiere obligatoriamente default_concurrency_limit
|
| 66 |
+
demo.queue()
|
| 67 |
demo.launch(show_api=False)
|
| 68 |
|
| 69 |
if __name__ == "__main__":
|