HirCoir commited on
Commit
0dd2770
1 Parent(s): e20b881

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -0
app.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import argparse
2
+ import subprocess
3
+ import os
4
+ import sys
5
+ import random
6
+ 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 extract_zip(zip_file, output_dir, password):
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
+ bundle_dir = Path(__file__).resolve().parent
27
+ random_name = ''.join(random.choices(string.ascii_letters + string.digits, k=8)) + '.wav'
28
+ output_file = os.path.join('.', random_name)
29
+
30
+ zip_file = os.path.join(bundle_dir, 'models.zip')
31
+ extract_dir = os.path.join(bundle_dir, 'extracted_models')
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
+ def play_audio(parrafo, model, password):
47
+ output_file = convert_text_to_speech(parrafo, model, password)
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
+ # Set your secret key for password protection
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)