Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,79 +1,66 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import edge_tts
|
3 |
-
import asyncio
|
4 |
-
import tempfile
|
5 |
-
import os
|
6 |
-
|
7 |
-
#
|
8 |
-
async def get_voices():
|
9 |
-
voices = await edge_tts.list_voices()
|
10 |
-
return {f"{v['ShortName']} - {v['Locale']} ({v['Gender']})": v['ShortName'] for v in voices}
|
11 |
-
|
12 |
-
#
|
13 |
-
async def text_to_speech(text, voice, rate, pitch):
|
14 |
-
if not text.strip():
|
15 |
-
return None, gr.Warning("
|
16 |
-
if not voice:
|
17 |
-
return None, gr.Warning("
|
18 |
-
|
19 |
-
voice_short_name = voice.split(" - ")[0]
|
20 |
-
rate_str = f"{rate:+d}%"
|
21 |
-
pitch_str = f"{pitch:+d}Hz"
|
22 |
-
communicate = edge_tts.Communicate(text, voice_short_name, rate=rate_str, pitch=pitch_str)
|
23 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
|
24 |
-
tmp_path = tmp_file.name
|
25 |
-
await communicate.save(tmp_path)
|
26 |
-
return tmp_path, None
|
27 |
-
|
28 |
-
#
|
29 |
-
def tts_interface(text, voice, rate, pitch):
|
30 |
-
audio, warning = asyncio.run(text_to_speech(text, voice, rate, pitch))
|
31 |
-
return audio, warning
|
32 |
-
|
33 |
-
#
|
34 |
-
import gradio as gr
|
35 |
-
|
36 |
-
async def create_demo():
|
37 |
-
voices = await get_voices()
|
38 |
-
|
39 |
-
description = """
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
],
|
68 |
-
title="Edge TTS Text-to-Speech",
|
69 |
-
description=description,
|
70 |
-
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!",
|
71 |
-
analytics_enabled=False,
|
72 |
-
allow_flagging=False
|
73 |
-
)
|
74 |
-
return demo
|
75 |
-
|
76 |
-
# Run the application
|
77 |
-
if __name__ == "__main__":
|
78 |
-
demo = asyncio.run(create_demo())
|
79 |
-
demo.launch()
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import edge_tts
|
3 |
+
import asyncio
|
4 |
+
import tempfile
|
5 |
+
import os
|
6 |
+
|
7 |
+
# Obtener todas las voces disponibles
|
8 |
+
async def get_voices():
|
9 |
+
voices = await edge_tts.list_voices()
|
10 |
+
return {f"{v['ShortName']} - {v['Locale']} ({v['Gender']})": v['ShortName'] for v in voices}
|
11 |
+
|
12 |
+
# Función de texto a voz
|
13 |
+
async def text_to_speech(text, voice, rate, pitch):
|
14 |
+
if not text.strip():
|
15 |
+
return None, gr.Warning("Por favor, ingresa texto para convertir.") # Warning si no se ingresa texto
|
16 |
+
if not voice:
|
17 |
+
return None, gr.Warning("Por favor, selecciona una voz.") # Warning si no se selecciona una voz
|
18 |
+
|
19 |
+
voice_short_name = voice.split(" - ")[0]
|
20 |
+
rate_str = f"{rate:+d}%" # Ajustar velocidad en formato adecuado
|
21 |
+
pitch_str = f"{pitch:+d}Hz" # Ajustar tono en formato adecuado
|
22 |
+
communicate = edge_tts.Communicate(text, voice_short_name, rate=rate_str, pitch=pitch_str)
|
23 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
|
24 |
+
tmp_path = tmp_file.name
|
25 |
+
await communicate.save(tmp_path) # Guardar archivo temporal con el audio generado
|
26 |
+
return tmp_path, None
|
27 |
+
|
28 |
+
# Función para la interfaz de Gradio
|
29 |
+
def tts_interface(text, voice, rate, pitch):
|
30 |
+
audio, warning = asyncio.run(text_to_speech(text, voice, rate, pitch)) # Ejecutar función asíncrona
|
31 |
+
return audio, warning
|
32 |
+
|
33 |
+
# Crear aplicación Gradio
|
34 |
+
import gradio as gr
|
35 |
+
|
36 |
+
async def create_demo():
|
37 |
+
voices = await get_voices() # Obtener lista de voces disponibles
|
38 |
+
|
39 |
+
description = """
|
40 |
+
Convierte texto en audio utilizando Microsoft Edge TTS. Ajusta la velocidad y el tono del habla: 0 es el valor predeterminado, valores positivos aumentan, valores negativos disminuyen.
|
41 |
+
"""
|
42 |
+
|
43 |
+
demo = gr.Interface(
|
44 |
+
fn=tts_interface,
|
45 |
+
inputs=[
|
46 |
+
gr.Textbox(label="Texto de entrada", lines=5), # Entrada para texto
|
47 |
+
gr.Dropdown(choices=[""] + list(voices.keys()), label="Seleccionar voz", value=""), # Selección de voz
|
48 |
+
gr.Slider(minimum=-50, maximum=50, value=0, label="Ajuste de velocidad (%)", step=1), # Ajuste de velocidad
|
49 |
+
gr.Slider(minimum=-20, maximum=20, value=0, label="Ajuste de tono (Hz)", step=1) # Ajuste de tono
|
50 |
+
],
|
51 |
+
outputs=[
|
52 |
+
gr.Audio(label="Audio generado", type="filepath"), # Salida del audio generado
|
53 |
+
gr.Markdown(label="Advertencia", visible=False) # Salida de advertencias
|
54 |
+
],
|
55 |
+
title="Edge TTS Texto a Voz",
|
56 |
+
description=description,
|
57 |
+
# article="Experimenta el poder de Edge TTS para la conversión de texto a voz y explora nuestro avanzado Convertidor de Texto a Video para posibilidades creativas aún mayores.",
|
58 |
+
analytics_enabled=False,
|
59 |
+
allow_flagging=False
|
60 |
+
)
|
61 |
+
return demo
|
62 |
+
|
63 |
+
# Ejecutar la aplicación
|
64 |
+
if __name__ == "__main__":
|
65 |
+
demo = asyncio.run(create_demo())
|
66 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|