Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -17,11 +17,8 @@ app = Flask(__name__)
|
|
17 |
|
18 |
# Define el directorio donde se guardan los archivos
|
19 |
file_folder = getattr(sys, '_MEIPASS', os.path.abspath(os.path.dirname(__file__)))
|
20 |
-
|
21 |
-
temp_audio_folder= "/home/app/temp_audio/"
|
22 |
-
#model_folder = os.path.join(file_folder, 'models')
|
23 |
model_folder = "/home/app/"
|
24 |
-
#piper_binary_path = os.path.join(file_folder, 'piper', 'piper')
|
25 |
piper_binary_path = "/home/app/piper"
|
26 |
|
27 |
# Define los nombres asignados a modelos espec铆ficos
|
@@ -33,9 +30,8 @@ model_names = {
|
|
33 |
}
|
34 |
|
35 |
def filter_text(text):
|
36 |
-
filtered_text =
|
37 |
-
filtered_text =
|
38 |
-
filtered_text = filtered_text.replace('\n', ' ')
|
39 |
return filtered_text
|
40 |
|
41 |
def convert_text_to_speech(text, model):
|
@@ -43,27 +39,21 @@ def convert_text_to_speech(text, model):
|
|
43 |
random_name = ''.join(random.choices(string.ascii_letters + string.digits, k=8)) + '.wav'
|
44 |
output_file = os.path.join(temp_audio_folder, random_name)
|
45 |
|
46 |
-
if os.path.isfile(piper_binary_path):
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
logging.error(f"Error al ejecutar el comando: {e}")
|
57 |
-
return None
|
58 |
-
else:
|
59 |
-
logging.error(f"Modelo '{model}' no encontrado en la ubicaci贸n especificada.")
|
60 |
-
return None
|
61 |
else:
|
62 |
-
logging.error(f"
|
63 |
-
return None
|
64 |
else:
|
65 |
-
logging.error(f"No se encontr贸 el binario de Piper en la ubicaci贸n especificada.")
|
66 |
-
|
67 |
|
68 |
# Define una funci贸n decoradora para restringir el acceso a la ruta /convert
|
69 |
def restrict_access(func):
|
@@ -89,31 +79,33 @@ def index():
|
|
89 |
@app.route('/convert', methods=['POST'])
|
90 |
@restrict_access
|
91 |
def convert_text():
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
|
|
|
|
116 |
|
117 |
if __name__ == '__main__':
|
118 |
logging.info("Se est谩 iniciando la aplicaci贸n.")
|
119 |
-
app.run(host='0.0.0.0', port=7860, debug=False)
|
|
|
17 |
|
18 |
# Define el directorio donde se guardan los archivos
|
19 |
file_folder = getattr(sys, '_MEIPASS', os.path.abspath(os.path.dirname(__file__)))
|
20 |
+
temp_audio_folder = "/home/app/temp_audio/"
|
|
|
|
|
21 |
model_folder = "/home/app/"
|
|
|
22 |
piper_binary_path = "/home/app/piper"
|
23 |
|
24 |
# Define los nombres asignados a modelos espec铆ficos
|
|
|
30 |
}
|
31 |
|
32 |
def filter_text(text):
|
33 |
+
filtered_text = re.sub(r'[^\w\s,.\(\):\u00C0-\u00FF]', '', text) # Filtra caracteres no deseados
|
34 |
+
filtered_text = filtered_text.replace('\n', ' ') # Reemplaza saltos de l铆nea con espacios
|
|
|
35 |
return filtered_text
|
36 |
|
37 |
def convert_text_to_speech(text, model):
|
|
|
39 |
random_name = ''.join(random.choices(string.ascii_letters + string.digits, k=8)) + '.wav'
|
40 |
output_file = os.path.join(temp_audio_folder, random_name)
|
41 |
|
42 |
+
if os.path.isfile(piper_binary_path) and model in model_names:
|
43 |
+
model_path = os.path.join(model_folder, model_names[model])
|
44 |
+
if os.path.isfile(model_path):
|
45 |
+
# Construye el comando para ejecutar Piper
|
46 |
+
command = f'echo "{filtered_text}" | "{piper_binary_path}" -m {model_path} -f {output_file}'
|
47 |
+
try:
|
48 |
+
subprocess.run(command, shell=True, check=True)
|
49 |
+
return output_file
|
50 |
+
except subprocess.CalledProcessError as e:
|
51 |
+
logging.error(f"Error al ejecutar el comando: {e}")
|
|
|
|
|
|
|
|
|
|
|
52 |
else:
|
53 |
+
logging.error(f"Modelo '{model}' no encontrado en la ubicaci贸n especificada.")
|
|
|
54 |
else:
|
55 |
+
logging.error(f"No se encontr贸 el binario de Piper en la ubicaci贸n especificada o modelo no asignado.")
|
56 |
+
return None
|
57 |
|
58 |
# Define una funci贸n decoradora para restringir el acceso a la ruta /convert
|
59 |
def restrict_access(func):
|
|
|
79 |
@app.route('/convert', methods=['POST'])
|
80 |
@restrict_access
|
81 |
def convert_text():
|
82 |
+
try:
|
83 |
+
text = request.form['text']
|
84 |
+
model = request.form['model']
|
85 |
+
output_file = convert_text_to_speech(text, model)
|
86 |
+
|
87 |
+
@after_this_request
|
88 |
+
def remove_file(response):
|
89 |
+
try:
|
90 |
+
if output_file and os.path.exists(output_file):
|
91 |
+
os.remove(output_file)
|
92 |
+
logging.info("Audio file deleted: %s", output_file)
|
93 |
+
except Exception as error:
|
94 |
+
logging.error("Error deleting file: %s", error)
|
95 |
+
return response
|
96 |
+
|
97 |
+
if output_file:
|
98 |
+
with open(output_file, 'rb') as audio_file:
|
99 |
+
audio_content = audio_file.read()
|
100 |
+
|
101 |
+
audio_base64 = base64.b64encode(audio_content).decode('utf-8')
|
102 |
+
return jsonify({'audio_base64': audio_base64})
|
103 |
+
else:
|
104 |
+
return jsonify({'error': 'Error al convertir texto a voz'}), 500 # Internal Server Error
|
105 |
+
except Exception as e:
|
106 |
+
logging.error("Error processing request: %s", e)
|
107 |
+
return jsonify({'error': 'Error interno del servidor'}), 500 # Internal Server Error
|
108 |
|
109 |
if __name__ == '__main__':
|
110 |
logging.info("Se est谩 iniciando la aplicaci贸n.")
|
111 |
+
app.run(host='0.0.0.0', port=7860, debug=False)
|