Merlintxu commited on
Commit
8f59a41
verified
1 Parent(s): 007d6a1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -7
app.py CHANGED
@@ -9,6 +9,7 @@ import warnings
9
  from transformers import logging
10
  import math
11
  import json
 
12
 
13
  # Suprimir advertencias
14
  warnings.filterwarnings("ignore")
@@ -118,20 +119,24 @@ def detect_and_select_model(audio):
118
 
119
  def save_transcription(transcriptions, file_format):
120
  if file_format == "JSON":
121
- file_path = "transcription.json"
122
- with open(file_path, 'w') as f:
123
- json.dump(transcriptions, f, ensure_ascii=False, indent=4)
124
  elif file_format == "TXT":
125
- file_path = "transcription.txt"
126
- with open(file_path, 'w') as f:
127
  for entry in transcriptions:
128
- f.write(f"{entry['start_time']:.2f},{entry['end_time']:.2f},{entry['text']}\n")
 
 
129
  return file_path
130
 
131
  def combined_interface(audio, file_format):
132
  try:
 
133
  language, model_options = detect_and_select_model(audio)
134
  selected_model = model_options[0]
 
 
135
 
136
  # Primer yield: A帽adir None para la s茅ptima salida (Archivo de Descarga)
137
  yield language, model_options, selected_model, "", 0, "Initializing...", None
@@ -142,19 +147,32 @@ def combined_interface(audio, file_format):
142
  full_transcription = " ".join([t["text"] for t in transcriptions])
143
  progress_int = math.floor(progress)
144
  status = f"Transcribing... {progress_int}% complete"
 
145
  # Yield con None para el archivo de descarga hasta que est茅 completo
146
  yield language, model_options, selected_model, full_transcription.strip(), progress_int, status, None
147
 
 
148
  # Guardar transcripci贸n
149
  file_path = save_transcription(transcriptions, file_format)
 
 
 
 
 
 
 
 
 
150
 
151
  # Limpiar archivos temporales
152
  os.remove("converted_audio.wav")
 
153
 
154
  # Yield final con el archivo de descarga
155
- yield language, model_options, selected_model, full_transcription.strip(), 100, f"Transcription complete! Download {file_path}", file_path
156
 
157
  except Exception as e:
 
158
  # Asegurarse de que el yield de error tambi茅n devuelva 7 valores
159
  yield str(e), [], "", "An error occurred during processing.", 0, "Error", ""
160
 
 
9
  from transformers import logging
10
  import math
11
  import json
12
+ import tempfile
13
 
14
  # Suprimir advertencias
15
  warnings.filterwarnings("ignore")
 
119
 
120
  def save_transcription(transcriptions, file_format):
121
  if file_format == "JSON":
122
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".json") as tmp:
123
+ json.dump(transcriptions, tmp, ensure_ascii=False, indent=4)
124
+ file_path = tmp.name
125
  elif file_format == "TXT":
126
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".txt") as tmp:
 
127
  for entry in transcriptions:
128
+ tmp.write(f"{entry['start_time']:.2f},{entry['end_time']:.2f},{entry['text']}\n".encode())
129
+ file_path = tmp.name
130
+ print(f"Archivo de transcripci贸n guardado en: {file_path}")
131
  return file_path
132
 
133
  def combined_interface(audio, file_format):
134
  try:
135
+ print(f"Ruta del archivo de audio subido: {audio}")
136
  language, model_options = detect_and_select_model(audio)
137
  selected_model = model_options[0]
138
+ print(f"Idioma detectado: {language}")
139
+ print(f"Modelos disponibles: {model_options}")
140
 
141
  # Primer yield: A帽adir None para la s茅ptima salida (Archivo de Descarga)
142
  yield language, model_options, selected_model, "", 0, "Initializing...", None
 
147
  full_transcription = " ".join([t["text"] for t in transcriptions])
148
  progress_int = math.floor(progress)
149
  status = f"Transcribing... {progress_int}% complete"
150
+ print(f"Progreso: {progress_int}%")
151
  # Yield con None para el archivo de descarga hasta que est茅 completo
152
  yield language, model_options, selected_model, full_transcription.strip(), progress_int, status, None
153
 
154
+ print("Guardando transcripci贸n.")
155
  # Guardar transcripci贸n
156
  file_path = save_transcription(transcriptions, file_format)
157
+ print(f"Transcripci贸n guardada en: {file_path}")
158
+
159
+ # Verificar que file_path no es un directorio
160
+ if os.path.isdir(file_path):
161
+ raise ValueError(f"El archivo de transcripci贸n deber铆a ser un archivo, pero es un directorio: {file_path}")
162
+
163
+ # Verificar que el archivo existe
164
+ if not os.path.isfile(file_path):
165
+ raise ValueError(f"El archivo de transcripci贸n no existe: {file_path}")
166
 
167
  # Limpiar archivos temporales
168
  os.remove("converted_audio.wav")
169
+ print("Archivos temporales limpiados.")
170
 
171
  # Yield final con el archivo de descarga
172
+ yield language, model_options, selected_model, full_transcription.strip(), 100, "Transcription complete! Download the file below.", file_path
173
 
174
  except Exception as e:
175
+ print(f"Error: {e}")
176
  # Asegurarse de que el yield de error tambi茅n devuelva 7 valores
177
  yield str(e), [], "", "An error occurred during processing.", 0, "Error", ""
178