HirCoir commited on
Commit
8b9fcd0
1 Parent(s): 0c3edc9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -21
app.py CHANGED
@@ -6,41 +6,65 @@ 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
- 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[:100000]
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)
28
  app.logger.info("Audio file created at: %s", output_file)
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
42
- model_options = [file for file in os.listdir(model_folder) if file.endswith('.onnx')]
43
- # Log the contents of the current folder
44
  app.logger.info("Contents of current folder: %s", os.listdir(file_folder))
45
  return render_template('index.html', model_options=model_options)
46
 
@@ -59,6 +83,9 @@ def convert_text():
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
 
@@ -69,4 +96,4 @@ def convert_text():
69
  return response
70
 
71
  if __name__ == '__main__':
72
- app.run(host='0.0.0.0', port=7860, debug=False)
 
6
  import random
7
  import string
8
  import re
9
+ import logging
10
 
11
  app = Flask(__name__)
12
 
13
+ # Define los nombres asignados a modelos específicos, en caso de no existir no se muestran
14
+ model_names = {
15
+ "Español México | Kamora Neuronal": {
16
+ "model_path": "es_ES-kamora-7539-high.onnx",
17
+ "replacements": [('\n', '. ')]
18
+ },
19
+ "Español México | Claude": {
20
+ "model_path": "es_MX-claude-14947-epoch-high.onnx",
21
+ "replacements": [('(', ','), (')', ','), ('?', ','), ('¿', ','), (':', ','), ('\n', ' ')]
22
+ }
23
+ }
24
 
25
+ # Comprueba si los modelos definidos existen en la carpeta de modelos
26
+ model_folder = '/home/app/' # Asegúrate de definir la ruta correcta a la carpeta de modelos
27
+ existing_models = [model_name for model_name in model_names.keys() if os.path.isfile(os.path.join(model_folder, model_names[model_name]["model_path"]))]
28
+
29
+ def multiple_replace(text, replacements):
30
+ # Iterar sobre cada par de remplazo
31
+ for old, new in replacements:
32
+ text = text.replace(old, new)
33
+ return text
34
+
35
+ def filter_text(text, model_name):
36
+ if model_name in model_names:
37
+ replacements = model_names[model_name]["replacements"]
38
+ # Realizar reemplazos específicos del modelo
39
+ filtered_text = multiple_replace(text, replacements)
40
+ # Escapar todos los caracteres especiales dentro de las comillas
41
+ filtered_text = re.sub(r'(["\'])', lambda m: "\\" + m.group(0), filtered_text)
42
+ return filtered_text
43
+ else:
44
+ logging.error(f"No se encontró el modelo '{model_name}'.")
45
+ return None
46
+
47
+ # Define una función para convertir texto a voz
48
+ def convert_text_to_speech(text, model_name):
49
+ filtered_text = filter_text(text, model_name)[:500] # Limitar el texto a 500 caracteres
50
+ if filtered_text is None:
51
+ return None
52
 
 
 
 
 
 
53
  random_name = ''.join(random.choices(string.ascii_letters + string.digits, k=8)) + '.wav'
54
  output_file = os.path.join(file_folder, random_name)
55
  app.logger.info("Audio file created at: %s", output_file)
56
+ piper_exe = os.path.join(file_folder, 'piper') # Ajusta la ruta para piper
57
+
58
  if os.path.isfile(piper_exe):
59
+ comando = f'echo {filtered_text} | "{piper_exe}" -m {model_name} -f {output_file}'
60
  subprocess.run(comando, shell=True)
61
  return output_file
62
  else:
63
+ return "No se encontró el archivo piper.exe en el directorio correcto."
 
64
 
65
  @app.route('/')
66
  def index():
67
+ model_options = existing_models
 
 
68
  app.logger.info("Contents of current folder: %s", os.listdir(file_folder))
69
  return render_template('index.html', model_options=model_options)
70
 
 
83
  app.logger.error("Error deleting file: %s", error)
84
  return response
85
 
86
+ if output_file is None:
87
+ return "Error: No se pudo generar el archivo de audio."
88
+
89
  with open(output_file, 'rb') as audio_file:
90
  audio_content = audio_file.read()
91
 
 
96
  return response
97
 
98
  if __name__ == '__main__':
99
+ app.run(host='0.0.0.0', port=7860, debug=False)