NuMessiah commited on
Commit
3775e9f
·
1 Parent(s): 36e6932

Add return_timestamps=True to whisper pipeline

Browse files
Files changed (1) hide show
  1. app.py +12 -4
app.py CHANGED
@@ -29,10 +29,18 @@ def transcribe_audio(audio_file):
29
  if audio.shape[0] > 1: # Check if multi-channel
30
  audio = torch.mean(audio, dim=0, keepdim=True) # Average channels
31
 
32
- # Transcribe the audio
33
- transcription = whisper_pipeline(audio.squeeze().numpy())["text"] # .squeeze() removes extra dimensions
34
-
35
- return transcription
 
 
 
 
 
 
 
 
36
 
37
  except Exception as e:
38
  return f"An error occurred: {e}"
 
29
  if audio.shape[0] > 1: # Check if multi-channel
30
  audio = torch.mean(audio, dim=0, keepdim=True) # Average channels
31
 
32
+ # Long-Form Transcription with Timestamps
33
+ transcription = whisper_pipeline(audio.squeeze().numpy(), return_timestamps=True)
34
+
35
+ # Format the output with timestamps (Improved)
36
+ formatted_transcription = ""
37
+ for segment in transcription["chunks"]:
38
+ start = segment["timestamp"][0]
39
+ end = segment["timestamp"][1]
40
+ text = segment["text"]
41
+ formatted_transcription += f"[{start:.2f} - {end:.2f}] {text}\n" # Nicer formatting
42
+
43
+ return formatted_transcription
44
 
45
  except Exception as e:
46
  return f"An error occurred: {e}"