File size: 2,137 Bytes
0dd2770
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
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)