NickVerri commited on
Commit
bc12787
·
verified ·
1 Parent(s): e9c1162

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -9
app.py CHANGED
@@ -10,8 +10,6 @@ from datetime import timedelta
10
  from pyannote.audio import Pipeline
11
  from huggingface_hub import hf_hub_download
12
  # Import ALL required classes for Safe Globals whitelisting
13
- # Note: Some internal classes might not be directly importable, so we use string references if needed
14
- # but these are the main ones exposed by the library.
15
  from pyannote.audio.core.task import Specifications, Problem, Resolution
16
 
17
  # --- Configuration & Tokens ---
@@ -188,15 +186,17 @@ if uploaded_file:
188
  model = whisper.load_model("base")
189
  result = model.transcribe("audio_optimized.m4a", word_timestamps=True)
190
 
191
- # 3. Alignment
192
  final_segments = []
193
 
194
- # Pre-calculate speaker turns for faster lookup
195
  speaker_turns = []
196
  if diarization:
197
  try:
198
- for turn, _, speaker_id in diarization.itertracks(yield_label=True):
199
- speaker_turns.append({"start": turn.start, "end": turn.end, "speaker": speaker_id})
 
 
200
  except Exception as e:
201
  print(f"Error iterating tracks: {e}")
202
 
@@ -204,7 +204,7 @@ if uploaded_file:
204
  mid_time = (segment['start'] + segment['end']) / 2
205
  speaker = "Unknown"
206
 
207
- # Improved matching logic
208
  if speaker_turns:
209
  # 1. Try strict containment
210
  for turn in speaker_turns:
@@ -216,7 +216,12 @@ if uploaded_file:
216
  if speaker == "Unknown":
217
  best_dist = 1.0 # Max distance 1 second
218
  for turn in speaker_turns:
219
- dist = min(abs(turn["start"] - mid_time), abs(turn["end"] - mid_time))
 
 
 
 
 
220
  if dist < best_dist:
221
  best_dist = dist
222
  speaker = turn["speaker"]
@@ -238,7 +243,6 @@ if uploaded_file:
238
  # Diarization Preview to help the user identify roles
239
  with st.expander("Transcript Preview (Diarized)"):
240
  for seg in st.session_state.transcript[:20]: # Show first 20 segments
241
- # FIXED: Simple string concatenation to avoid syntax errors
242
  display_text = f"**{seg['speaker']}:** {seg['text']}"
243
  st.markdown(display_text)
244
 
 
10
  from pyannote.audio import Pipeline
11
  from huggingface_hub import hf_hub_download
12
  # Import ALL required classes for Safe Globals whitelisting
 
 
13
  from pyannote.audio.core.task import Specifications, Problem, Resolution
14
 
15
  # --- Configuration & Tokens ---
 
186
  model = whisper.load_model("base")
187
  result = model.transcribe("audio_optimized.m4a", word_timestamps=True)
188
 
189
+ # 3. Alignment with Distance Fallback
190
  final_segments = []
191
 
192
+ # Pre-calculate speaker turns
193
  speaker_turns = []
194
  if diarization:
195
  try:
196
+ # Safely extract tracks
197
+ if hasattr(diarization, 'itertracks'):
198
+ for turn, _, speaker_id in diarization.itertracks(yield_label=True):
199
+ speaker_turns.append({"start": turn.start, "end": turn.end, "speaker": speaker_id})
200
  except Exception as e:
201
  print(f"Error iterating tracks: {e}")
202
 
 
204
  mid_time = (segment['start'] + segment['end']) / 2
205
  speaker = "Unknown"
206
 
207
+ # Improved matching logic with fallback
208
  if speaker_turns:
209
  # 1. Try strict containment
210
  for turn in speaker_turns:
 
216
  if speaker == "Unknown":
217
  best_dist = 1.0 # Max distance 1 second
218
  for turn in speaker_turns:
219
+ # Distance to start or end of turn
220
+ dist_start = abs(turn["start"] - mid_time)
221
+ dist_end = abs(turn["end"] - mid_time)
222
+ # Also check if mid_time is just slightly outside
223
+ dist = min(dist_start, dist_end)
224
+
225
  if dist < best_dist:
226
  best_dist = dist
227
  speaker = turn["speaker"]
 
243
  # Diarization Preview to help the user identify roles
244
  with st.expander("Transcript Preview (Diarized)"):
245
  for seg in st.session_state.transcript[:20]: # Show first 20 segments
 
246
  display_text = f"**{seg['speaker']}:** {seg['text']}"
247
  st.markdown(display_text)
248