duongthienz commited on
Commit
5cff7cf
·
verified ·
1 Parent(s): 452b2bf

Fix a bug with speaker names in left sidebar

Browse files
Files changed (1) hide show
  1. app.py +27 -14
app.py CHANGED
@@ -322,24 +322,37 @@ try:
322
  stem = fname.rsplit(".", 1)[0] # strip extension
323
  return (stem[:5] + "...") if len(stem) > 5 else stem
324
 
325
- token_display_map = {}
 
 
 
 
 
326
  for fn in st.session_state.file_names:
327
  if not (fn in st.session_state.results and len(st.session_state.results[fn]) == 2):
328
  continue
329
  short = _short_fn(fn)
330
- # First pass: collect display labels per file to detect duplicates
331
- sp_labels = {sp: f"{short}: {get_display_name(sp, fn)}"
332
- for sp in st.session_state.results[fn][0].labels()}
333
- label_counts = {}
334
- for disp in sp_labels.values():
335
- label_counts[disp] = label_counts.get(disp, 0) + 1
336
- # Second pass: build map, disambiguating where needed
337
- for sp, disp in sp_labels.items():
338
- raw = f"{fn}: {sp}" # raw token always uses full filename
339
- if label_counts[disp] > 1:
340
- token_display_map[f"{short}: {get_display_name(sp, fn)} ({sp})"] = raw
341
- else:
342
- token_display_map[disp] = raw
 
 
 
 
 
 
 
 
343
  display_speaker_tokens = list(token_display_map.keys())
344
 
345
  # -----------------------------------------------------------------------
 
322
  stem = fname.rsplit(".", 1)[0] # strip extension
323
  return (stem[:5] + "...") if len(stem) > 5 else stem
324
 
325
+ # Build token_display_map in two passes to guarantee globally unique display keys.
326
+ # Pass 1: collect all (display_label -> raw_token) pairs across every file.
327
+ # Pass 2: any display label that appears more than once (collision between
328
+ # files that share the same short prefix AND speaker name) gets the
329
+ # raw speaker ID appended to disambiguate.
330
+ raw_pairs = [] # [(display_label, raw_token), ...]
331
  for fn in st.session_state.file_names:
332
  if not (fn in st.session_state.results and len(st.session_state.results[fn]) == 2):
333
  continue
334
  short = _short_fn(fn)
335
+ for sp in st.session_state.results[fn][0].labels():
336
+ raw = f"{fn}: {sp}"
337
+ disp = f"{short}: {get_display_name(sp, fn)}"
338
+ raw_pairs.append((disp, raw))
339
+
340
+ # Count occurrences of each display label globally
341
+ disp_counts = {}
342
+ for disp, _ in raw_pairs:
343
+ disp_counts[disp] = disp_counts.get(disp, 0) + 1
344
+
345
+ token_display_map = {}
346
+ for disp, raw in raw_pairs:
347
+ fn, sp = raw.split(": ", 1)
348
+ short = _short_fn(fn)
349
+ if disp_counts[disp] > 1:
350
+ # Disambiguate with raw speaker ID
351
+ unique_disp = f"{short}: {get_display_name(sp, fn)} ({sp})"
352
+ else:
353
+ unique_disp = disp
354
+ token_display_map[unique_disp] = raw
355
+
356
  display_speaker_tokens = list(token_display_map.keys())
357
 
358
  # -----------------------------------------------------------------------