Spaces:
Sleeping
Sleeping
Add return_timestamps=True to whisper pipeline
Browse files
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 |
-
#
|
| 33 |
-
transcription = whisper_pipeline(audio.squeeze().numpy()
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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}"
|