HirCoir commited on
Commit
ec43347
1 Parent(s): ac08e3e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -36
app.py CHANGED
@@ -1,73 +1,49 @@
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', ',')]
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
- # Define una función de reemplazo usando una función lambda
47
- replace_func = lambda m: "\\" + m.group(0)
48
- # Crea una expresión regular con los caracteres que deseas reemplazar
49
- regex_pattern = r'(["\'])'
50
- # Realiza el reemplazo utilizando la función sub de re
51
- filtered_text = re.sub(regex_pattern, replace_func, text)
52
- return filtered_text
53
-
54
-
55
  def convert_text_to_speech(parrafo, model):
56
- # Limit text to 500 characters
57
- parrafo = parrafo[:10000]
58
-
59
  model_info = models_replacements.get(model)
60
  if model_info:
61
  model_path = model_info.get("model_path")
62
- replacements = model_info.get("replacements")
63
- parrafo_filtrado = filter_text(parrafo, replacements)
64
  random_name = ''.join(random.choices(string.ascii_letters + string.digits, k=8)) + '.wav'
65
  output_file = os.path.join(file_folder, random_name)
66
- app.logger.info("Audio file created at: %s", output_file)
67
- piper_exe = os.path.join(file_folder, 'piper') # Adjusted the path for piper
68
 
69
  if os.path.isfile(piper_exe):
70
- comando = f'echo {parrafo_filtrado} | "{piper_exe}" -m {model_path} -f {output_file}'
71
  subprocess.run(comando, shell=True)
72
  return output_file
73
  else:
@@ -78,8 +54,6 @@ def convert_text_to_speech(parrafo, model):
78
  @app.route('/')
79
  def index():
80
  model_options = list(models_replacements.keys())
81
- # Log the contents of the current folder
82
- app.logger.info("Contents of current folder: %s", os.listdir(file_folder))
83
  return render_template('index.html', model_options=model_options)
84
 
85
  @app.route('/convert', methods=['POST'])
@@ -92,7 +66,6 @@ def convert_text():
92
  def remove_file(response):
93
  try:
94
  os.remove(output_file)
95
- app.logger.info("Audio file deleted: %s", output_file)
96
  except Exception as error:
97
  app.logger.error("Error deleting file: %s", error)
98
  return response
 
 
 
 
 
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
  @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
  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