File size: 2,042 Bytes
631c13e
 
 
0dd2770
 
 
 
 
 
631c13e
 
0dd2770
631c13e
0dd2770
 
 
 
6daaf10
0dd2770
631c13e
 
0dd2770
 
631c13e
 
 
5e3bfe7
 
 
 
 
 
 
631c13e
 
 
 
 
 
 
 
 
 
 
 
 
 
30bf56e
 
631c13e
 
 
0dd2770
631c13e
 
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
from flask import Flask, render_template, request, jsonify
from io import BytesIO
import base64
import subprocess
import os
import random
import string
import re

app = Flask(__name__)

def filter_text(text):
    filtered_text = re.sub(r'[^\w\s,.\(\):\u00C0-\u00FF]', '', text)
    filtered_text = filtered_text.replace('\n', ' ')
    filtered_text = filtered_text.replace('(', ',').replace(')', ',')
    return filtered_text

def convert_text_to_speech(parrafo, model):
    parrafo_filtrado = filter_text(parrafo)
    bundle_dir = os.path.abspath(os.path.dirname(__file__))
    print("Cargando carpeta Modelos desde:", os.path.join(bundle_dir, 'Models'))
    random_name = ''.join(random.choices(string.ascii_letters + string.digits, k=8)) + '.wav'
    output_file = os.path.join('.', random_name)
    piper_exe = os.path.join(bundle_dir, './piper.exe')
    print("Ejecutando piper.exe desde:", 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."

@app.route('/')
def index():
    model_folder = 'Models'
    bundle_dir = os.path.abspath(os.path.dirname(__file__))
    print("Cargando carpeta Modelos desde:", os.path.join(bundle_dir, model_folder))
    model_options = [file for file in os.listdir(model_folder) if file.endswith('.onnx')]
    return render_template('index.html', model_options=model_options)

@app.route('/convert', methods=['POST'])
def convert_text():
    text = request.form['text']
    model = request.form['model']
    output_file = convert_text_to_speech(text, model)
    
    with open(output_file, 'rb') as audio_file:
        audio_content = audio_file.read()
        
    audio_base64 = base64.b64encode(audio_content).decode('utf-8')
    return jsonify({'audio_base64': audio_base64})

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=7860)