HirCoir commited on
Commit
51e2649
1 Parent(s): 0371a5c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -14
app.py CHANGED
@@ -1,4 +1,4 @@
1
- from flask import Flask, render_template, request, jsonify
2
  from io import BytesIO
3
  import base64
4
  import subprocess
@@ -18,24 +18,21 @@ def filter_text(text):
18
  def convert_text_to_speech(parrafo, model):
19
  parrafo_filtrado = filter_text(parrafo)
20
  bundle_dir = os.path.abspath(os.path.dirname(__file__))
21
- print("Cargando carpeta Modelos desde:", bundle_dir)
22
- random_name = '.'.join(random.choices(string.ascii_letters + string.digits, k=8)) + '.wav'
23
- output_file = os.path.join(bundle_dir, random_name)
24
- piper_exe = '/app/piper'
25
- print("Ejecutando piper desde:", piper_exe)
26
 
27
  if os.path.isfile(piper_exe):
28
- comando = f'echo {parrafo_filtrado} | {piper_exe} -m {model} -f {output_file}'
29
- subprocess.run(comando, shell=True, check=True)
30
  return output_file
31
  else:
32
- return "El archivo piper no se encontró en el directorio correcto."
33
 
34
  @app.route('/')
35
  def index():
36
- model_folder = '.'
37
- bundle_dir = os.path.abspath(os.path.dirname(__file__))
38
- print("Cargando carpeta Modelos desde:", bundle_dir)
39
  model_options = [file for file in os.listdir(model_folder) if file.endswith('.onnx')]
40
  return render_template('index.html', model_options=model_options)
41
 
@@ -45,11 +42,24 @@ def convert_text():
45
  model = request.form['model']
46
  output_file = convert_text_to_speech(text, model)
47
 
 
 
 
 
 
 
 
 
 
48
  with open(output_file, 'rb') as audio_file:
49
  audio_content = audio_file.read()
50
 
51
  audio_base64 = base64.b64encode(audio_content).decode('utf-8')
52
- return jsonify({'audio_base64': audio_base64})
 
 
 
 
53
 
54
  if __name__ == '__main__':
55
- app.run(debug=True, host='0.0.0.0', port=7860)
 
1
+ from flask import Flask, render_template, request, jsonify, after_this_request
2
  from io import BytesIO
3
  import base64
4
  import subprocess
 
18
  def convert_text_to_speech(parrafo, model):
19
  parrafo_filtrado = filter_text(parrafo)
20
  bundle_dir = os.path.abspath(os.path.dirname(__file__))
21
+ random_name = ''.join(random.choices(string.ascii_letters + string.digits, k=8)) + '.wav'
22
+ output_file = os.path.join('.', random_name)
23
+ app.logger.info("Archivo de audio creado en: %s", output_file)
24
+ piper_exe = os.path.join(bundle_dir, './piper')
 
25
 
26
  if os.path.isfile(piper_exe):
27
+ comando = f'echo {parrafo_filtrado} | "{piper_exe}" -m {model} -f {output_file}'
28
+ subprocess.run(comando, shell=True)
29
  return output_file
30
  else:
31
+ return "El archivo piper.exe no se encontró en el directorio correcto."
32
 
33
  @app.route('/')
34
  def index():
35
+ model_folder = os.path.join('./')
 
 
36
  model_options = [file for file in os.listdir(model_folder) if file.endswith('.onnx')]
37
  return render_template('index.html', model_options=model_options)
38
 
 
42
  model = request.form['model']
43
  output_file = convert_text_to_speech(text, model)
44
 
45
+ @after_this_request
46
+ def remove_file(response):
47
+ try:
48
+ os.remove(output_file)
49
+ app.logger.info("Archivo de audio eliminado: %s", output_file)
50
+ except Exception as error:
51
+ app.logger.error("Error eliminando el archivo: %s", error)
52
+ return response
53
+
54
  with open(output_file, 'rb') as audio_file:
55
  audio_content = audio_file.read()
56
 
57
  audio_base64 = base64.b64encode(audio_content).decode('utf-8')
58
+
59
+ response = jsonify({'audio_base64': audio_base64})
60
+
61
+ return response
62
+
63
 
64
  if __name__ == '__main__':
65
+ app.run(host='0.0.0.0', debug=False)