File size: 3,837 Bytes
6450a0b
 
 
ec43347
6450a0b
0dd2770
54412ba
6450a0b
9bd30eb
0dd2770
631c13e
 
6450a0b
 
8b9fcd0
79df6e6
 
4beb109
 
 
 
cf8407f
 
6450a0b
cf8407f
79df6e6
cb46d56
9bd30eb
6d1fe44
79df6e6
 
6450a0b
cf8407f
 
 
6450a0b
cf8407f
 
 
6450a0b
cf8407f
 
 
6450a0b
79df6e6
6d1fe44
 
9bd30eb
 
 
 
28871b0
ee9c6b6
6450a0b
 
 
79df6e6
 
 
9bd30eb
79df6e6
 
6450a0b
 
79df6e6
 
6450a0b
79df6e6
 
 
 
ec64c3b
79df6e6
5e3bfe7
631c13e
 
79df6e6
6450a0b
 
631c13e
 
 
 
029b0ee
 
 
 
 
 
 
 
6450a0b
029b0ee
ec64c3b
029b0ee
 
 
 
 
 
 
 
 
 
51e2649
631c13e
79df6e6
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from flask import Flask, render_template, request, jsonify, after_this_request
from io import BytesIO
import base64
import subprocess
import os
import random
import string
import re
import shlex

app = Flask(__name__)

# Define the folder where files are saved
file_folder = '/home/app/'

# Models with specific character replacements
models_replacements = {
    "Español México | Sorah": {
        "model_path": "es_MX-sorah.onnx",
        "replacements": [('(', ','), (')', ','), ('?', ','), ('¿', ','), (':', ','), ('\n', ' ')]
    },
    "Español México | Voz HirCoir": {
        "model_path": "es_MX-locutor-18488-epoch-high.onnx",
        "replacements": [('(', ','), (')', ','), ('?', ','), ('¿', ','), (':', ','), ('\n', ' ')]
    },
    "Español México | Kamora Neuronal": {
        "model_path": "kamora.onnx",
        "replacements": [('\n', '')]
    },
    "Español México | Claude": {
        "model_path": "es_MX-claude-14947-epoch-high.onnx",
        "replacements": [('(', ','), (')', ','), ('?', ','), ('¿', ','), (':', ','), ('\n', ' ')]
    },
    "Español México | Cortana Infinnity": {
        "model_path": "es_MX-cortana-19669-epoch-high.onnx",
        "replacements": [('(', ','), (')', ','), ('?', ','), ('¿', ','), (':', ','), ('\n', ' ')]
    },
    "Español México | TheGevy": {
        "model_path": "es_MX-gevy-10196-epoch-high.onnx",
        "replacements": [('(', ','), (')', ','), ('?', ','), ('¿', ','), (':', ','), ('\n', ' ')]
    },
    "English US | Voice": {
        "model_path": "en_US-ljspeech-high.onnx",
        "replacements": [('(', ','), (')', ','), ('?', ','), ('¿', ','), (':', ','), ('\n', ' ')]
    }
}

def filter_text(text):
    # Escapa caracteres especiales
    escaped_text = shlex.quote(text)
    return escaped_text

def convert_text_to_speech(parrafo, model):
    # Limit text to 500 characters
    parrafo = parrafo[:10000]
    
    model_info = models_replacements.get(model)
    if model_info:
        model_path = model_info.get("model_path")
        parrafo_filtrado = filter_text(parrafo)
        random_name = ''.join(random.choices(string.ascii_letters + string.digits, k=8)) + '.wav'
        output_file = os.path.join(file_folder, random_name)
        app.logger.info("Audio file created at: %s", output_file)
        piper_exe = os.path.join(file_folder, 'piper')  # Adjusted the path for piper
        
        if os.path.isfile(piper_exe):
            comando = f'echo {parrafo_filtrado} | "{piper_exe}" -m {model_path} -f {output_file}'
            subprocess.run(comando, shell=True)
            return output_file
        else:
            return "The piper.exe file was not found in the correct directory."
    else:
        return "Model not found."

@app.route('/')
def index():
    model_options = list(models_replacements.keys())
    # Log the contents of the current folder
    app.logger.info("Contents of current folder: %s", os.listdir(file_folder))
    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)
    
    @after_this_request
    def remove_file(response):
        try:
            os.remove(output_file)
            app.logger.info("Audio file deleted: %s", output_file)
        except Exception as error:
            app.logger.error("Error deleting file: %s", error)
        return response
    
    with open(output_file, 'rb') as audio_file:
        audio_content = audio_file.read()
        
    audio_base64 = base64.b64encode(audio_content).decode('utf-8')
    
    response = jsonify({'audio_base64': audio_base64})
    
    return response

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