Update app.py
Browse files
app.py
CHANGED
|
@@ -81,47 +81,21 @@ def format_transcript_with_speakers(transcript, diarization):
|
|
| 81 |
return "".join(formatted_transcript)
|
| 82 |
|
| 83 |
def transcribe_audio(audio_file):
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
audio_input, sr = librosa.load(audio_file, sr=16000)
|
| 87 |
-
audio_input = audio_input.astype(np.float32)
|
| 88 |
-
print(f"Audio duration: {len(audio_input) / sr:.2f} seconds")
|
| 89 |
-
|
| 90 |
-
# Apply speaker diarization
|
| 91 |
-
if pipeline:
|
| 92 |
-
print("Applying speaker diarization...")
|
| 93 |
-
diarization = pipeline(audio_file)
|
| 94 |
-
print("Speaker diarization complete.")
|
| 95 |
-
else:
|
| 96 |
-
diarization = None
|
| 97 |
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
transcriptions = []
|
| 101 |
-
|
| 102 |
-
print("Starting transcription...")
|
| 103 |
-
for i in range(0, len(audio_input), chunk_length - overlap):
|
| 104 |
-
chunk = audio_input[i:i+chunk_length]
|
| 105 |
-
input_features = processor(chunk, sampling_rate=16000, return_tensors="pt").input_features.to(device)
|
| 106 |
-
predicted_ids = model.generate(input_features)
|
| 107 |
-
transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)
|
| 108 |
-
transcriptions.extend(transcription)
|
| 109 |
-
print(f"Processed {i / sr:.2f} to {(i + chunk_length) / sr:.2f} seconds")
|
| 110 |
-
|
| 111 |
-
full_transcription = " ".join(transcriptions)
|
| 112 |
-
print(f"Transcription complete. Full transcription length: {len(full_transcription)} characters")
|
| 113 |
-
|
| 114 |
-
if diarization:
|
| 115 |
-
print("Applying formatting with speaker diarization...")
|
| 116 |
-
formatted_transcription = format_transcript_with_speakers(full_transcription, diarization)
|
| 117 |
-
else:
|
| 118 |
-
print("Applying formatting without speaker diarization...")
|
| 119 |
-
formatted_transcription = format_transcript_with_breaks(full_transcription)
|
| 120 |
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
def format_transcript_with_breaks(transcript):
|
| 127 |
sentences = re.split('(?<=[.!?]) +', transcript)
|
|
|
|
| 81 |
return "".join(formatted_transcript)
|
| 82 |
|
| 83 |
def transcribe_audio(audio_file):
|
| 84 |
+
# Perform diarization on the entire audio file
|
| 85 |
+
diarization = pipeline(audio_file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
+
# Load the audio
|
| 88 |
+
audio_input, sr = librosa.load(audio_file, sr=16000)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
+
# Transcribe the entire audio (or use chunking with time tracking if necessary)
|
| 91 |
+
input_features = processor(audio_input, sampling_rate=16000, return_tensors="pt").input_features.to(device)
|
| 92 |
+
predicted_ids = model.generate(input_features)
|
| 93 |
+
full_transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)[0]
|
| 94 |
+
|
| 95 |
+
# Apply diarization to the full transcription
|
| 96 |
+
formatted_transcription = format_transcript_with_speakers(full_transcription, diarization)
|
| 97 |
+
|
| 98 |
+
return formatted_transcription
|
| 99 |
|
| 100 |
def format_transcript_with_breaks(transcript):
|
| 101 |
sentences = re.split('(?<=[.!?]) +', transcript)
|