HirCoir's picture
Create app.py
0dd2770 verified
raw
history blame
No virus
2.14 kB
import argparse
import subprocess
import os
import sys
import random
import string
import gradio as gr
import tempfile
import re
import zipfile
from pathlib import Path
def filter_text(text):
filtered_text = re.sub(r'[^A-Za-zÁÉÍÓÚÜáéíóúü0-9,.\(\):\s]', '', text)
filtered_text = filtered_text.replace('\n', ' ')
filtered_text = filtered_text.replace('(', ',').replace(')', ',')
return filtered_text
def extract_zip(zip_file, output_dir, password):
with zipfile.ZipFile(zip_file, 'r') as zip_ref:
zip_ref.extractall(output_dir, pwd=password.encode('utf-8'))
def convert_text_to_speech(parrafo, model, password):
parrafo_filtrado = filter_text(parrafo)
bundle_dir = Path(__file__).resolve().parent
random_name = ''.join(random.choices(string.ascii_letters + string.digits, k=8)) + '.wav'
output_file = os.path.join('.', random_name)
zip_file = os.path.join(bundle_dir, 'models.zip')
extract_dir = os.path.join(bundle_dir, 'extracted_models')
# Extract models from the zip file
extract_zip(zip_file, extract_dir, password)
# Use the extracted models
piper_exe = os.path.join(extract_dir, 'piper.exe')
if os.path.isfile(piper_exe):
comando = f'echo {parrafo_filtrado} | "{piper_exe}" -m {model} -f {output_file}'
subprocess.run(comando, shell=True)
return output_file
else:
return "El archivo piper.exe no se encontró en el directorio correcto."
def play_audio(parrafo, model, password):
output_file = convert_text_to_speech(parrafo, model, password)
with open(output_file, 'rb') as audio_file:
audio_content = audio_file.read()
return audio_content
input_text = gr.Textbox(label="Introduce el texto")
model_options = [
"es_MX-locutor-voice-11400-epoch-high.onnx",
"es_MX-cortanav3-high.onnx",
]
select_model = gr.Dropdown(model_options, label="Selecciona el modelo")
# Set your secret key for password protection
secret_key = "your_secret_key"
gr.Interface(play_audio, inputs=[input_text, select_model], outputs=gr.Audio(), flagging_options=None).launch(secret_key=secret_key)