Update app.py
Browse files
app.py
CHANGED
@@ -3,11 +3,16 @@ import edge_tts
|
|
3 |
import asyncio
|
4 |
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 |
|
|
|
11 |
async def text_to_speech(text, voice, rate, pitch):
|
12 |
if not text.strip():
|
13 |
return None, "Please enter text to convert."
|
@@ -23,11 +28,49 @@ async def text_to_speech(text, voice, rate, pitch):
|
|
23 |
await communicate.save(tmp_path)
|
24 |
return tmp_path, None
|
25 |
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
if warning:
|
29 |
-
return
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
async def create_demo():
|
33 |
voices = await get_voices()
|
@@ -55,10 +98,12 @@ async def create_demo():
|
|
55 |
gr.Textbox(label="Input Text", lines=5),
|
56 |
gr.Dropdown(choices=[""] + list(voices.keys()), label="Select Voice", value=""),
|
57 |
gr.Slider(minimum=-50, maximum=50, value=0, label="Speech Rate Adjustment (%)", step=1),
|
58 |
-
gr.Slider(minimum=-20, maximum=20, value=0, label="Pitch Adjustment (Hz)", step=1)
|
|
|
59 |
],
|
60 |
outputs=[
|
61 |
gr.Audio(label="Generated Audio", type="filepath"),
|
|
|
62 |
gr.Markdown(label="Warning", visible=False)
|
63 |
],
|
64 |
title="Edge TTS Text-to-Speech",
|
@@ -76,4 +121,4 @@ async def main():
|
|
76 |
demo.launch(show_api=False)
|
77 |
|
78 |
if __name__ == "__main__":
|
79 |
-
asyncio.run(main())
|
|
|
3 |
import asyncio
|
4 |
import tempfile
|
5 |
import os
|
6 |
+
from pydub import AudioSegment
|
7 |
+
from pydub.playback import play
|
8 |
+
import math
|
9 |
|
10 |
+
# Funci贸n para obtener las voces disponibles
|
11 |
async def get_voices():
|
12 |
voices = await edge_tts.list_voices()
|
13 |
return {f"{v['ShortName']} - {v['Locale']} ({v['Gender']})": v['ShortName'] for v in voices}
|
14 |
|
15 |
+
# Funci贸n principal de conversi贸n de texto a voz
|
16 |
async def text_to_speech(text, voice, rate, pitch):
|
17 |
if not text.strip():
|
18 |
return None, "Please enter text to convert."
|
|
|
28 |
await communicate.save(tmp_path)
|
29 |
return tmp_path, None
|
30 |
|
31 |
+
# Funci贸n para agregar el fondo musical al speech
|
32 |
+
def add_background_music(speech_file, background_music_file, output_file):
|
33 |
+
# Cargar los archivos de audio
|
34 |
+
speech = AudioSegment.from_mp3(speech_file)
|
35 |
+
background_music = AudioSegment.from_mp3(background_music_file)
|
36 |
+
|
37 |
+
# Ajustar el volumen del fondo musical al 15%
|
38 |
+
background_music = background_music - 16 # Reducci贸n aproximada para 15%
|
39 |
+
|
40 |
+
# Repetir el fondo musical si es m谩s corto que el speech
|
41 |
+
if len(background_music) < len(speech):
|
42 |
+
repetitions = math.ceil(len(speech) / len(background_music))
|
43 |
+
background_music = background_music * repetitions
|
44 |
+
|
45 |
+
# Cortar el fondo musical para que coincida con la duraci贸n del speech
|
46 |
+
background_music = background_music[:len(speech)]
|
47 |
+
|
48 |
+
# Superponer el speech y el fondo musical
|
49 |
+
final_audio = speech.overlay(background_music)
|
50 |
+
|
51 |
+
# Exportar el audio resultante
|
52 |
+
final_audio.export(output_file, format="mp3")
|
53 |
+
print(f"Archivo generado exitosamente: {output_file}")
|
54 |
+
|
55 |
+
# Interfaz Gradio
|
56 |
+
async def tts_interface(text, voice, rate, pitch, background_music):
|
57 |
+
# Generar el speech
|
58 |
+
speech_file, warning = await text_to_speech(text, voice, rate, pitch)
|
59 |
if warning:
|
60 |
+
return None, None, gr.Warning(warning)
|
61 |
+
|
62 |
+
# Verificar si se proporcion贸 un archivo de fondo musical
|
63 |
+
if background_music is None or background_music == "":
|
64 |
+
return speech_file, None, None
|
65 |
+
|
66 |
+
# Agregar el fondo musical
|
67 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
|
68 |
+
output_file = tmp_file.name
|
69 |
+
add_background_music(speech_file, background_music, output_file)
|
70 |
+
|
71 |
+
# Eliminar el archivo temporal del speech original
|
72 |
+
os.remove(speech_file)
|
73 |
+
return output_file, None, None
|
74 |
|
75 |
async def create_demo():
|
76 |
voices = await get_voices()
|
|
|
98 |
gr.Textbox(label="Input Text", lines=5),
|
99 |
gr.Dropdown(choices=[""] + list(voices.keys()), label="Select Voice", value=""),
|
100 |
gr.Slider(minimum=-50, maximum=50, value=0, label="Speech Rate Adjustment (%)", step=1),
|
101 |
+
gr.Slider(minimum=-20, maximum=20, value=0, label="Pitch Adjustment (Hz)", step=1),
|
102 |
+
gr.Audio(label="Background Music", type="filepath", optional=True)
|
103 |
],
|
104 |
outputs=[
|
105 |
gr.Audio(label="Generated Audio", type="filepath"),
|
106 |
+
gr.Image(label="Visualization", visible=False),
|
107 |
gr.Markdown(label="Warning", visible=False)
|
108 |
],
|
109 |
title="Edge TTS Text-to-Speech",
|
|
|
121 |
demo.launch(show_api=False)
|
122 |
|
123 |
if __name__ == "__main__":
|
124 |
+
asyncio.run(main())
|