File size: 2,858 Bytes
0dd2770
 
 
54412ba
 
 
 
0dd2770
631c13e
 
811ad8f
e5aa50a
811ad8f
0dd2770
54412ba
631c13e
0dd2770
 
54412ba
0dd2770
6daaf10
0dd2770
811ad8f
 
 
2bb9c7e
631c13e
5e3bfe7
54412ba
 
 
 
 
 
 
5e3bfe7
811ad8f
5e3bfe7
631c13e
 
2bb9c7e
631c13e
0fc3b2e
 
631c13e
 
 
 
54412ba
 
 
 
 
51e2649
54412ba
 
 
 
 
 
 
 
 
 
631c13e
54412ba
 
 
 
 
 
 
 
 
51e2649
631c13e
77a39e7
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
import os
import random
import re
import string
import subprocess
import base64
from flask import Flask, render_template, request, jsonify, after_this_request

app = Flask(__name__)

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

def filter_text(text):
    # Filtra caracteres no deseados y limita la longitud del texto
    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[:500]

def convert_text_to_speech(parrafo, model):
    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} -f {output_file}'
        try:
            subprocess.run(comando, shell=True, check=True)
            return output_file
        except subprocess.CalledProcessError as e:
            app.logger.error("Error executing command: %s", e)
            return None
    else:
        return "The piper.exe file was not found in the correct directory."

@app.route('/')
def index():
    model_folder = file_folder  # Adjusted the model folder to file_folder
    model_options = [file for file in os.listdir(model_folder) if file.endswith('.onnx')]
    # 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.get('text')
    model = request.form.get('model')

    if not text or not model:
        return jsonify({'error': 'Invalid request'}), 400
    
    output_file = convert_text_to_speech(text, model)
    if output_file:
        @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
    else:
        return jsonify({'error': 'Conversion failed'}), 500

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