HirCoir commited on
Commit
6450a0b
1 Parent(s): ec43347

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -9
app.py CHANGED
@@ -1,49 +1,75 @@
1
- import os
2
- import re
 
3
  import subprocess
 
4
  import random
5
  import string
6
- import base64
7
- from flask import Flask, render_template, request, jsonify, after_this_request
8
 
9
  app = Flask(__name__)
10
 
11
- # Define the folder where files are saved (use relative path for portability)
12
- file_folder = 'files/'
13
 
14
  # Models with specific character replacements
15
  models_replacements = {
16
  "Español México | Voz HirCoir": {
17
  "model_path": "es_MX-locutor-18488-epoch-high.onnx",
 
18
  },
19
  "Español México | Kamora Neuronal": {
20
  "model_path": "kamora.onnx",
 
21
  },
22
  "Español México | Claude": {
23
  "model_path": "es_MX-claude-14947-epoch-high.onnx",
 
24
  },
25
  "Español México | Cortana Infinnity": {
26
  "model_path": "es_MX-cortana-19669-epoch-high.onnx",
 
27
  },
28
  "Español México | TheGevy": {
29
  "model_path": "es_MX-gevy-10196-epoch-high.onnx",
 
30
  },
31
  "English US | Voice": {
32
  "model_path": "en_US-ljspeech-high.onnx",
 
33
  }
34
  }
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  def convert_text_to_speech(parrafo, model):
37
- parrafo = parrafo.replace('\n', ' ') # Eliminar saltos de línea
 
 
38
  model_info = models_replacements.get(model)
39
  if model_info:
40
  model_path = model_info.get("model_path")
 
 
41
  random_name = ''.join(random.choices(string.ascii_letters + string.digits, k=8)) + '.wav'
42
  output_file = os.path.join(file_folder, random_name)
43
- piper_exe = os.path.join(os.path.dirname(__file__), 'piper') # Ruta relativa al archivo piper.exe
 
44
 
45
  if os.path.isfile(piper_exe):
46
- comando = f'echo {parrafo} | "{piper_exe}" -m {model_path} -f {output_file}'
47
  subprocess.run(comando, shell=True)
48
  return output_file
49
  else:
@@ -54,6 +80,8 @@ def convert_text_to_speech(parrafo, model):
54
  @app.route('/')
55
  def index():
56
  model_options = list(models_replacements.keys())
 
 
57
  return render_template('index.html', model_options=model_options)
58
 
59
  @app.route('/convert', methods=['POST'])
@@ -66,6 +94,7 @@ def convert_text():
66
  def remove_file(response):
67
  try:
68
  os.remove(output_file)
 
69
  except Exception as error:
70
  app.logger.error("Error deleting file: %s", error)
71
  return response
 
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
 
12
+ # Define the folder where files are saved
13
+ file_folder = '/home/app/'
14
 
15
  # Models with specific character replacements
16
  models_replacements = {
17
  "Español México | Voz HirCoir": {
18
  "model_path": "es_MX-locutor-18488-epoch-high.onnx",
19
+ "replacements": [('(', ','), (')', ','), ('?', ','), ('¿', ','), (':', ','), ('\n', ' ')]
20
  },
21
  "Español México | Kamora Neuronal": {
22
  "model_path": "kamora.onnx",
23
+ "replacements": [('\n', '')] # Modified to remove newlines
24
  },
25
  "Español México | Claude": {
26
  "model_path": "es_MX-claude-14947-epoch-high.onnx",
27
+ "replacements": [('(', ','), (')', ','), ('?', ','), ('¿', ','), (':', ','), ('\n', ' ')]
28
  },
29
  "Español México | Cortana Infinnity": {
30
  "model_path": "es_MX-cortana-19669-epoch-high.onnx",
31
+ "replacements": [('(', ','), (')', ','), ('?', ','), ('¿', ','), (':', ','), ('\n', ' ')]
32
  },
33
  "Español México | TheGevy": {
34
  "model_path": "es_MX-gevy-10196-epoch-high.onnx",
35
+ "replacements": [('(', ','), (')', ','), ('?', ','), ('¿', ','), (':', ','), ('\n', ' ')]
36
  },
37
  "English US | Voice": {
38
  "model_path": "en_US-ljspeech-high.onnx",
39
+ "replacements": [('(', ','), (')', ','), ('?', ','), ('¿', ','), (':', ','), ('\n', ' ')]
40
  }
41
  }
42
 
43
+ def filter_text(text, replacements):
44
+ for replacement in replacements:
45
+ text = text.replace(replacement[0], replacement[1])
46
+ # Elimina los saltos de línea
47
+ text = text.replace('\n', '')
48
+ # Define una función de reemplazo usando una función lambda
49
+ replace_func = lambda m: "\\" + m.group(0)
50
+ # Crea una expresión regular con los caracteres que deseas reemplazar
51
+ regex_pattern = r'(["\'])'
52
+ # Realiza el reemplazo utilizando la función sub de re
53
+ filtered_text = re.sub(regex_pattern, replace_func, text)
54
+ return filtered_text
55
+
56
+
57
  def convert_text_to_speech(parrafo, model):
58
+ # Limit text to 500 characters
59
+ parrafo = parrafo[:10000]
60
+
61
  model_info = models_replacements.get(model)
62
  if model_info:
63
  model_path = model_info.get("model_path")
64
+ replacements = model_info.get("replacements")
65
+ parrafo_filtrado = filter_text(parrafo, replacements)
66
  random_name = ''.join(random.choices(string.ascii_letters + string.digits, k=8)) + '.wav'
67
  output_file = os.path.join(file_folder, random_name)
68
+ app.logger.info("Audio file created at: %s", output_file)
69
+ piper_exe = os.path.join(file_folder, 'piper') # Adjusted the path for piper
70
 
71
  if os.path.isfile(piper_exe):
72
+ comando = f'echo {parrafo_filtrado} | "{piper_exe}" -m {model_path} -f {output_file}'
73
  subprocess.run(comando, shell=True)
74
  return output_file
75
  else:
 
80
  @app.route('/')
81
  def index():
82
  model_options = list(models_replacements.keys())
83
+ # Log the contents of the current folder
84
+ app.logger.info("Contents of current folder: %s", os.listdir(file_folder))
85
  return render_template('index.html', model_options=model_options)
86
 
87
  @app.route('/convert', methods=['POST'])
 
94
  def remove_file(response):
95
  try:
96
  os.remove(output_file)
97
+ app.logger.info("Audio file deleted: %s", output_file)
98
  except Exception as error:
99
  app.logger.error("Error deleting file: %s", error)
100
  return response