File size: 1,969 Bytes
631c13e 0dd2770 631c13e 0dd2770 631c13e 0dd2770 6daaf10 0dd2770 631c13e 0371a5c 7f61c2f 0371a5c aa84530 0371a5c 631c13e 5e3bfe7 0371a5c 5e3bfe7 0371a5c 5e3bfe7 631c13e df6cee9 631c13e 0371a5c 631c13e 30bf56e 631c13e 0dd2770 631c13e 0371a5c |
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 |
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)
|