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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -21
app.py CHANGED
@@ -14,35 +14,40 @@ forced_decoder_ids = processor.get_decoder_prompt_ids(language="es", task="trans
14
 
15
  # Function to process and translate audio
16
  def translate_audio(filepath):
17
- if filepath is None:
18
- gr.Warning("No audio found, please retry.")
19
- return ""
20
 
21
- audio = AudioSegment.from_file(filepath)
22
- chunk_length_ms = 30 * 1000 # 30 seconds
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():
35
- generated_ids = model.generate(
36
- inputs["input_features"],
37
- forced_decoder_ids=forced_decoder_ids
38
- )
39
 
40
- translation = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
41
- full_translation += translation + " "
 
42
 
43
- os.remove(chunk_path)
 
 
 
 
 
44
 
45
- return full_translation.strip()
46
 
47
  # Gradio UI components
48
  mic_transcribe = gr.Interface(
 
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():
35
+ generated_ids = model.generate(
36
+ inputs["input_features"],
37
+ forced_decoder_ids=forced_decoder_ids
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(