Tamerstito commited on
Commit
c97e116
·
verified ·
1 Parent(s): f845b65

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -4
app.py CHANGED
@@ -13,22 +13,32 @@ processor = WhisperProcessor.from_pretrained("openai/whisper-small")
13
  forced_decoder_ids = processor.get_decoder_prompt_ids(language="es", task="translate")
14
 
15
  # Function to process and translate audio
 
 
16
  def translate_audio(filepath):
17
  try:
18
- if filepath is None:
19
- return "No audio file received."
 
 
20
 
21
  audio = AudioSegment.from_file(filepath)
 
 
22
  chunk_length_ms = 30 * 1000
23
  chunks = [audio[i:i + chunk_length_ms] for i in range(0, len(audio), chunk_length_ms)]
 
24
 
25
  full_translation = ""
26
 
27
  for i, chunk in enumerate(chunks):
28
  chunk_path = f"chunk_{i}.wav"
29
  chunk.export(chunk_path, format="wav")
 
30
 
31
  waveform, sample_rate = torchaudio.load(chunk_path)
 
 
32
  inputs = processor(waveform[0], sampling_rate=sample_rate, return_tensors="pt")
33
 
34
  with torch.no_grad():
@@ -38,17 +48,19 @@ def translate_audio(filepath):
38
  )
39
 
40
  translation = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
 
 
41
  full_translation += translation + " "
42
  os.remove(chunk_path)
43
 
 
44
  return full_translation.strip()
45
 
46
  except Exception as e:
47
- # Print error for debugging in HF logs
48
  print("ERROR:", str(e))
 
49
  return f"An error occurred: {str(e)}"
50
 
51
-
52
  # Gradio UI components
53
  mic_transcribe = gr.Interface(
54
  fn=translate_audio,
 
13
  forced_decoder_ids = processor.get_decoder_prompt_ids(language="es", task="translate")
14
 
15
  # Function to process and translate audio
16
+ import traceback
17
+
18
  def translate_audio(filepath):
19
  try:
20
+ print("Received filepath:", filepath)
21
+
22
+ if filepath is None or not os.path.exists(filepath):
23
+ return "No audio file received or file does not exist."
24
 
25
  audio = AudioSegment.from_file(filepath)
26
+ print("Audio loaded. Duration (ms):", len(audio))
27
+
28
  chunk_length_ms = 30 * 1000
29
  chunks = [audio[i:i + chunk_length_ms] for i in range(0, len(audio), chunk_length_ms)]
30
+ print(f"Audio split into {len(chunks)} chunks.")
31
 
32
  full_translation = ""
33
 
34
  for i, chunk in enumerate(chunks):
35
  chunk_path = f"chunk_{i}.wav"
36
  chunk.export(chunk_path, format="wav")
37
+ print(f"Exported chunk {i} to {chunk_path}")
38
 
39
  waveform, sample_rate = torchaudio.load(chunk_path)
40
+ print(f"Loaded chunk {i} with sample rate {sample_rate}")
41
+
42
  inputs = processor(waveform[0], sampling_rate=sample_rate, return_tensors="pt")
43
 
44
  with torch.no_grad():
 
48
  )
49
 
50
  translation = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
51
+ print(f"Chunk {i} translation:", translation)
52
+
53
  full_translation += translation + " "
54
  os.remove(chunk_path)
55
 
56
+ print("Full translation done.")
57
  return full_translation.strip()
58
 
59
  except Exception as e:
 
60
  print("ERROR:", str(e))
61
+ traceback.print_exc()
62
  return f"An error occurred: {str(e)}"
63
 
 
64
  # Gradio UI components
65
  mic_transcribe = gr.Interface(
66
  fn=translate_audio,