HirCoir commited on
Commit
79df6e6
1 Parent(s): 6d1fe44

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -41
app.py CHANGED
@@ -12,58 +12,50 @@ app = Flask(__name__)
12
  # Define the folder where files are saved
13
  file_folder = '/home/app/'
14
 
15
-
16
- import logging
17
-
18
- model_names = {
19
- "es_MX-claude-14947-epoch-high.onnx": {
20
- "replacements": {
21
- ('(', ','), (')', ','), ('?', ','), ('¿', ','), (':', ','), ('\n', ' ')
22
- }
23
  },
24
- "kamora": {
25
- "replacements": {
26
- # Define los reemplazos específicos del modelo 2 aquí
27
- }
28
- },
29
- # Agrega más modelos si es necesario
30
  }
31
 
32
- def filter_text(text, model_name):
33
- if model_name in model_names:
34
- replacements = model_names[model_name]["replacements"]
35
- # Realizar reemplazos específicos del modelo
36
- filtered_text = multiple_replace(text, replacements)
37
- # Escapar todos los caracteres especiales dentro de las comillas
38
- filtered_text = re.sub(r'(["\'])', lambda m: "\\" + m.group(0), filtered_text)
39
- return filtered_text
40
- else:
41
- logging.error(f"No se encontró el modelo '{model_name}'.")
42
- return None
43
-
44
 
45
  def convert_text_to_speech(parrafo, model):
46
  # Limit text to 500 characters
47
  parrafo = parrafo[:100000]
48
 
49
- parrafo_filtrado = filter_text(parrafo)
50
- random_name = ''.join(random.choices(string.ascii_letters + string.digits, k=8)) + '.wav'
51
- output_file = os.path.join(file_folder, random_name)
52
- app.logger.info("Audio file created at: %s", output_file)
53
- piper_exe = os.path.join(file_folder, 'piper') # Adjusted the path for piper
54
-
55
- if os.path.isfile(piper_exe):
56
- comando = f'echo {parrafo_filtrado} | "{piper_exe}" -m {model} -f {output_file}'
57
- subprocess.run(comando, shell=True)
58
- return output_file
 
 
 
 
 
 
59
  else:
60
- return "The piper.exe file was not found in the correct directory."
61
-
62
 
63
  @app.route('/')
64
  def index():
65
- model_folder = file_folder # Adjusted the model folder to file_folder
66
- model_options = [file for file in os.listdir(model_folder) if file.endswith('.onnx')]
67
  # Log the contents of the current folder
68
  app.logger.info("Contents of current folder: %s", os.listdir(file_folder))
69
  return render_template('index.html', model_options=model_options)
@@ -93,4 +85,4 @@ def convert_text():
93
  return response
94
 
95
  if __name__ == '__main__':
96
- app.run(host='0.0.0.0', port=7860, debug=False)
 
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 | Kamora Neuronal": {
18
+ "model_path": "es_ES-kamora-7539-high.onnx",
19
+ "replacements": [('\n', '. ')]
 
 
 
20
  },
21
+ "Español México | Claude": {
22
+ "model_path": "es_MX-claude-14947-epoch-high.onnx",
23
+ "replacements": [('(', ','), (')', ','), ('?', ','), ('¿', ','), (':', ','), ('\n', ' ')]
24
+ }
 
 
25
  }
26
 
27
+ def filter_text(text, replacements):
28
+ filtered_text = re.sub(r'[^\w\s,.\(\):\u00C0-\u00FF]', '', text)
29
+ for replacement in replacements:
30
+ filtered_text = filtered_text.replace(replacement[0], replacement[1])
31
+ return filtered_text
 
 
 
 
 
 
 
32
 
33
  def convert_text_to_speech(parrafo, model):
34
  # Limit text to 500 characters
35
  parrafo = parrafo[:100000]
36
 
37
+ model_info = models_replacements.get(model)
38
+ if model_info:
39
+ model_path = model_info.get("model_path")
40
+ replacements = model_info.get("replacements")
41
+ parrafo_filtrado = filter_text(parrafo, replacements)
42
+ random_name = ''.join(random.choices(string.ascii_letters + string.digits, k=8)) + '.wav'
43
+ output_file = os.path.join(file_folder, random_name)
44
+ app.logger.info("Audio file created at: %s", output_file)
45
+ piper_exe = os.path.join(file_folder, 'piper') # Adjusted the path for piper
46
+
47
+ if os.path.isfile(piper_exe):
48
+ comando = f'echo {parrafo_filtrado} | "{piper_exe}" -m {model_path} -f {output_file}'
49
+ subprocess.run(comando, shell=True)
50
+ return output_file
51
+ else:
52
+ return "The piper.exe file was not found in the correct directory."
53
  else:
54
+ return "Model not found."
 
55
 
56
  @app.route('/')
57
  def index():
58
+ model_options = list(models_replacements.keys())
 
59
  # Log the contents of the current folder
60
  app.logger.info("Contents of current folder: %s", os.listdir(file_folder))
61
  return render_template('index.html', model_options=model_options)
 
85
  return response
86
 
87
  if __name__ == '__main__':
88
+ app.run(host='0.0.0.0', port=7860, debug=False)