HirCoir commited on
Commit
54412ba
1 Parent(s): c19844b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -32
app.py CHANGED
@@ -1,11 +1,10 @@
1
- from flask import Flask, render_template, request, jsonify, after_this_request
2
- from io import BytesIO
3
- import base64
4
- import subprocess
5
  import os
6
  import random
7
- import string
8
  import re
 
 
 
 
9
 
10
  app = Flask(__name__)
11
 
@@ -13,15 +12,13 @@ app = Flask(__name__)
13
  file_folder = '/home/app/'
14
 
15
  def filter_text(text):
 
16
  filtered_text = re.sub(r'[^\w\s,.\(\):\u00C0-\u00FF]', '', text)
17
  filtered_text = filtered_text.replace('\n', ' ')
18
  filtered_text = filtered_text.replace('(', ',').replace(')', ',')
19
- return filtered_text
20
 
21
  def convert_text_to_speech(parrafo, model):
22
- # Limit text to 500 characters
23
- parrafo = parrafo[:500]
24
-
25
  parrafo_filtrado = filter_text(parrafo)
26
  random_name = ''.join(random.choices(string.ascii_letters + string.digits, k=8)) + '.wav'
27
  output_file = os.path.join(file_folder, random_name)
@@ -29,13 +26,16 @@ def convert_text_to_speech(parrafo, model):
29
  piper_exe = os.path.join(file_folder, 'piper') # Adjusted the path for piper
30
 
31
  if os.path.isfile(piper_exe):
32
- comando = f'echo {parrafo_filtrado} | "{piper_exe}" -m {model} -f {output_file}'
33
- subprocess.run(comando, shell=True)
34
- return output_file
 
 
 
 
35
  else:
36
  return "The piper.exe file was not found in the correct directory."
37
 
38
-
39
  @app.route('/')
40
  def index():
41
  model_folder = file_folder # Adjusted the model folder to file_folder
@@ -46,27 +46,32 @@ def index():
46
 
47
  @app.route('/convert', methods=['POST'])
48
  def convert_text():
49
- text = request.form['text']
50
- model = request.form['model']
51
- output_file = convert_text_to_speech(text, model)
52
-
53
- @after_this_request
54
- def remove_file(response):
55
- try:
56
- os.remove(output_file)
57
- app.logger.info("Audio file deleted: %s", output_file)
58
- except Exception as error:
59
- app.logger.error("Error deleting file: %s", error)
60
- return response
61
 
62
- with open(output_file, 'rb') as audio_file:
63
- audio_content = audio_file.read()
 
 
 
 
 
 
 
 
64
 
65
- audio_base64 = base64.b64encode(audio_content).decode('utf-8')
66
-
67
- response = jsonify({'audio_base64': audio_base64})
68
-
69
- return response
 
 
 
 
70
 
71
  if __name__ == '__main__':
72
  app.run(host='0.0.0.0', port=7860, debug=False)
 
 
 
 
 
1
  import os
2
  import random
 
3
  import re
4
+ import string
5
+ import subprocess
6
+ import base64
7
+ from flask import Flask, render_template, request, jsonify, after_this_request
8
 
9
  app = Flask(__name__)
10
 
 
12
  file_folder = '/home/app/'
13
 
14
  def filter_text(text):
15
+ # Filtra caracteres no deseados y limita la longitud del texto
16
  filtered_text = re.sub(r'[^\w\s,.\(\):\u00C0-\u00FF]', '', text)
17
  filtered_text = filtered_text.replace('\n', ' ')
18
  filtered_text = filtered_text.replace('(', ',').replace(')', ',')
19
+ return filtered_text[:500]
20
 
21
  def convert_text_to_speech(parrafo, model):
 
 
 
22
  parrafo_filtrado = filter_text(parrafo)
23
  random_name = ''.join(random.choices(string.ascii_letters + string.digits, k=8)) + '.wav'
24
  output_file = os.path.join(file_folder, random_name)
 
26
  piper_exe = os.path.join(file_folder, 'piper') # Adjusted the path for piper
27
 
28
  if os.path.isfile(piper_exe):
29
+ comando = f'echo "{parrafo_filtrado}" | "{piper_exe}" -m {model} -f {output_file}'
30
+ try:
31
+ subprocess.run(comando, shell=True, check=True)
32
+ return output_file
33
+ except subprocess.CalledProcessError as e:
34
+ app.logger.error("Error executing command: %s", e)
35
+ return None
36
  else:
37
  return "The piper.exe file was not found in the correct directory."
38
 
 
39
  @app.route('/')
40
  def index():
41
  model_folder = file_folder # Adjusted the model folder to file_folder
 
46
 
47
  @app.route('/convert', methods=['POST'])
48
  def convert_text():
49
+ text = request.form.get('text')
50
+ model = request.form.get('model')
51
+
52
+ if not text or not model:
53
+ return jsonify({'error': 'Invalid request'}), 400
 
 
 
 
 
 
 
54
 
55
+ output_file = convert_text_to_speech(text, model)
56
+ if output_file:
57
+ @after_this_request
58
+ def remove_file(response):
59
+ try:
60
+ os.remove(output_file)
61
+ app.logger.info("Audio file deleted: %s", output_file)
62
+ except Exception as error:
63
+ app.logger.error("Error deleting file: %s", error)
64
+ return response
65
 
66
+ with open(output_file, 'rb') as audio_file:
67
+ audio_content = audio_file.read()
68
+
69
+ audio_base64 = base64.b64encode(audio_content).decode('utf-8')
70
+
71
+ response = jsonify({'audio_base64': audio_base64})
72
+ return response
73
+ else:
74
+ return jsonify({'error': 'Conversion failed'}), 500
75
 
76
  if __name__ == '__main__':
77
  app.run(host='0.0.0.0', port=7860, debug=False)