Spaces:
Running
Running
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 | |
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 | Kamora Neuronal": { | |
"model_path": "es_ES-kamora-7539-high.onnx", | |
"replacements": [('\n', '. ')] | |
}, | |
"Español México | Claude": { | |
"model_path": "es_MX-claude-14947-epoch-high.onnx", | |
"replacements": [('(', ','), (')', ','), ('?', ','), ('¿', ','), (':', ','), ('\n', ' ')] | |
} | |
} | |
def filter_text(text, replacements): | |
filtered_text = re.sub(r'[^\w\s,.\(\):\u00C0-\u00FF]', '', text) | |
for replacement in replacements: | |
filtered_text = filtered_text.replace(replacement[0], replacement[1]) | |
return filtered_text | |
def convert_text_to_speech(parrafo, model): | |
# Limit text to 500 characters | |
parrafo = parrafo[:100000] | |
model_info = models_replacements.get(model) | |
if model_info: | |
model_path = model_info.get("model_path") | |
replacements = model_info.get("replacements") | |
parrafo_filtrado = filter_text(parrafo, replacements) | |
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." | |
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) | |
def convert_text(): | |
text = request.form['text'] | |
model = request.form['model'] | |
output_file = convert_text_to_speech(text, model) | |
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) | |