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:", bundle_dir) random_name = '.'.join(random.choices(string.ascii_letters + string.digits, k=8)) + '.wav' output_file = os.path.join(bundle_dir, random_name) piper_exe = '/app/piper' print("Ejecutando piper 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, check=True) return output_file else: return "El archivo piper no se encontrĂ³ en el directorio correcto." @app.route('/') def index(): model_folder = '.' bundle_dir = os.path.abspath(os.path.dirname(__file__)) print("Cargando carpeta Modelos desde:", bundle_dir) 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)