Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,58 +7,60 @@ import string
|
|
| 7 |
import gradio as gr
|
| 8 |
import tempfile
|
| 9 |
import re
|
| 10 |
-
import zipfile
|
| 11 |
-
from pathlib import Path
|
| 12 |
|
| 13 |
def filter_text(text):
|
|
|
|
|
|
|
| 14 |
filtered_text = re.sub(r'[^A-Za-zÁÉÍÓÚÜáéíóúü0-9,.\(\):\s]', '', text)
|
|
|
|
| 15 |
filtered_text = filtered_text.replace('\n', ' ')
|
| 16 |
filtered_text = filtered_text.replace('(', ',').replace(')', ',')
|
| 17 |
return filtered_text
|
| 18 |
|
| 19 |
-
def
|
| 20 |
-
with zipfile.ZipFile(zip_file, 'r') as zip_ref:
|
| 21 |
-
zip_ref.extractall(output_dir, pwd=password.encode('utf-8'))
|
| 22 |
-
|
| 23 |
-
def convert_text_to_speech(parrafo, model, password):
|
| 24 |
parrafo_filtrado = filter_text(parrafo)
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
random_name = ''.join(random.choices(string.ascii_letters + string.digits, k=8)) + '.wav'
|
| 28 |
output_file = os.path.join('.', random_name)
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
# Extract models from the zip file
|
| 34 |
-
extract_zip(zip_file, extract_dir, password)
|
| 35 |
-
|
| 36 |
-
# Use the extracted models
|
| 37 |
-
piper_exe = os.path.join(extract_dir, 'piper.exe')
|
| 38 |
|
|
|
|
| 39 |
if os.path.isfile(piper_exe):
|
|
|
|
| 40 |
comando = f'echo {parrafo_filtrado} | "{piper_exe}" -m {model} -f {output_file}'
|
| 41 |
subprocess.run(comando, shell=True)
|
|
|
|
|
|
|
| 42 |
return output_file
|
| 43 |
else:
|
| 44 |
return "El archivo piper.exe no se encontró en el directorio correcto."
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
|
|
|
| 48 |
with open(output_file, 'rb') as audio_file:
|
| 49 |
audio_content = audio_file.read()
|
| 50 |
return audio_content
|
| 51 |
|
|
|
|
| 52 |
input_text = gr.Textbox(label="Introduce el texto")
|
| 53 |
-
|
| 54 |
model_options = [
|
| 55 |
"es_MX-locutor-voice-11400-epoch-high.onnx",
|
|
|
|
| 56 |
"es_MX-cortanav3-high.onnx",
|
| 57 |
-
|
| 58 |
-
|
| 59 |
select_model = gr.Dropdown(model_options, label="Selecciona el modelo")
|
| 60 |
|
| 61 |
-
|
| 62 |
-
secret_key = "your_secret_key"
|
| 63 |
-
|
| 64 |
-
gr.Interface(play_audio, inputs=[input_text, select_model], outputs=gr.Audio(), flagging_options=None).launch(secret_key=secret_key)
|
|
|
|
| 7 |
import gradio as gr
|
| 8 |
import tempfile
|
| 9 |
import re
|
|
|
|
|
|
|
| 10 |
|
| 11 |
def filter_text(text):
|
| 12 |
+
# Expresión regular para permitir solo letras de la A a la Z en mayúsculas y minúsculas,
|
| 13 |
+
# letras con acentos utilizadas en español México, números, comas, puntos, paréntesis, dos puntos y espacios
|
| 14 |
filtered_text = re.sub(r'[^A-Za-zÁÉÍÓÚÜáéíóúü0-9,.\(\):\s]', '', text)
|
| 15 |
+
# Eliminar saltos de línea y reemplazar paréntesis por comas
|
| 16 |
filtered_text = filtered_text.replace('\n', ' ')
|
| 17 |
filtered_text = filtered_text.replace('(', ',').replace(')', ',')
|
| 18 |
return filtered_text
|
| 19 |
|
| 20 |
+
def convert_text_to_speech(parrafo, model):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
parrafo_filtrado = filter_text(parrafo)
|
| 22 |
|
| 23 |
+
# Determinar el directorio base dependiendo de si se ejecuta desde el código fuente o desde el ejecutable congelado
|
| 24 |
+
if getattr(sys, 'frozen', False):
|
| 25 |
+
# La aplicación se está ejecutando desde un ejecutable congelado
|
| 26 |
+
bundle_dir = getattr(sys, '_MEIPASS', os.path.abspath(os.path.dirname(sys.argv[0])))
|
| 27 |
+
else:
|
| 28 |
+
# La aplicación se está ejecutando desde el código fuente
|
| 29 |
+
bundle_dir = os.path.abspath(os.path.dirname(__file__))
|
| 30 |
+
|
| 31 |
+
# Generar un nombre de archivo aleatorio
|
| 32 |
random_name = ''.join(random.choices(string.ascii_letters + string.digits, k=8)) + '.wav'
|
| 33 |
output_file = os.path.join('.', random_name)
|
| 34 |
|
| 35 |
+
# Construir la ruta al archivo piper.exe
|
| 36 |
+
piper_exe = os.path.join(bundle_dir, 'piper.exe')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
# Verificar si la ruta es válida
|
| 39 |
if os.path.isfile(piper_exe):
|
| 40 |
+
# Ejecutar el comando para generar el archivo de audio
|
| 41 |
comando = f'echo {parrafo_filtrado} | "{piper_exe}" -m {model} -f {output_file}'
|
| 42 |
subprocess.run(comando, shell=True)
|
| 43 |
+
|
| 44 |
+
# Devolver la ruta al archivo de audio generado
|
| 45 |
return output_file
|
| 46 |
else:
|
| 47 |
return "El archivo piper.exe no se encontró en el directorio correcto."
|
| 48 |
|
| 49 |
+
# Función para cargar y reproducir el archivo de audio en Gradio
|
| 50 |
+
def play_audio(parrafo, model):
|
| 51 |
+
output_file = convert_text_to_speech(parrafo, model)
|
| 52 |
with open(output_file, 'rb') as audio_file:
|
| 53 |
audio_content = audio_file.read()
|
| 54 |
return audio_content
|
| 55 |
|
| 56 |
+
# Crear la interfaz de Gradio
|
| 57 |
input_text = gr.Textbox(label="Introduce el texto")
|
|
|
|
| 58 |
model_options = [
|
| 59 |
"es_MX-locutor-voice-11400-epoch-high.onnx",
|
| 60 |
+
"otro_modelo.onnx",
|
| 61 |
"es_MX-cortanav3-high.onnx",
|
| 62 |
+
"es_MX-gafe-12232-epoch-high.onnx"
|
| 63 |
+
] # Agrega aquí más modelos si es necesario
|
| 64 |
select_model = gr.Dropdown(model_options, label="Selecciona el modelo")
|
| 65 |
|
| 66 |
+
gr.Interface(play_audio, inputs=[input_text, select_model], outputs=gr.Audio(), flagging_options=None).launch()
|
|
|
|
|
|
|
|
|