duongthienz commited on
Commit
0da24ba
·
verified ·
1 Parent(s): a79d4c7

update timespoken for multi

Browse files
Files changed (1) hide show
  1. state.py +15 -84
state.py CHANGED
@@ -715,97 +715,24 @@ def analyze(inFileName):
715
  printV("Loaded results", 4)
716
 
717
  pipeline = st.session_state.pipeline
718
- # For annotation-only files (RTTM/TXT/CSV), annotationToNoiseList
719
- # misclassifies almost everything as multiVoice because the window-based
720
- # classifier sees >2 speakers in every window. Instead derive voice
721
- # categories directly from the annotation's segment gaps — more accurate
722
- # and consistent for demo/pre-labeled files.
723
- _is_annotation_file = inFileName.lower().endswith((".rttm", ".txt", ".csv"))
724
- if _is_annotation_file:
725
- from pyannote.core import Annotation, Segment
726
- from collections import defaultdict
727
  noVoice = Annotation()
728
  multiVoice = Annotation()
729
  oneVoice = Annotation()
730
- all_segs = sorted(
731
- [(seg.start, seg.end, label)
732
- for label in currAnnotation.labels()
733
- if label is not None and str(label).strip() != ""
734
- for seg in currAnnotation.subset([label]).itersegments()],
735
- key=lambda x: x[0]
736
- )
737
- # Detect multi-voice: pairwise overlaps between speakers
738
- speaker_segs = defaultdict(list)
739
- for start, end, label in all_segs:
740
- speaker_segs[label].append((start, end))
741
- multi_intervals = []
742
- labels_list = list(speaker_segs.keys())
743
- for i in range(len(labels_list)):
744
- for j in range(i+1, len(labels_list)):
745
- for s1, e1 in speaker_segs[labels_list[i]]:
746
- for s2, e2 in speaker_segs[labels_list[j]]:
747
- ov_s, ov_e = max(s1, s2), min(e1, e2)
748
- if ov_e > ov_s + 0.05:
749
- multi_intervals.append((ov_s, ov_e))
750
- multi_intervals.sort()
751
- merged_multi = []
752
- for s, e in multi_intervals:
753
- if merged_multi and s <= merged_multi[-1][1]:
754
- merged_multi[-1] = (merged_multi[-1][0], max(merged_multi[-1][1], e))
755
- else:
756
- merged_multi.append([s, e])
757
- for s, e in merged_multi:
758
- active = sorted({label for start, end, label in all_segs
759
- if start < e and end > s})
760
- mv_label = '+'.join(active) if active else 'overlap'
761
- multiVoice[Segment(s, e)] = mv_label
762
- # No Voice: gaps in the union of all speech
763
- speech_union = []
764
- for start, end, _ in all_segs:
765
- if speech_union and start <= speech_union[-1][1]:
766
- speech_union[-1] = (speech_union[-1][0], max(speech_union[-1][1], end))
767
- else:
768
- speech_union.append([start, end])
769
- prev_end = 0.0
770
- for s, e in speech_union:
771
- if s > prev_end + 0.1:
772
- noVoice[Segment(prev_end, s)] = 'silence'
773
- prev_end = e
774
- if currTotalTime > prev_end + 0.1:
775
- noVoice[Segment(prev_end, currTotalTime)] = 'silence'
776
- # Single Voice: segments not overlapping any multi-voice region
777
- for start, end, label in all_segs:
778
- if not any(ms < end and me > start for ms, me in merged_multi):
779
- oneVoice[Segment(start, end)] = label
780
- else:
781
- try:
782
- noVoice, oneVoice, multiVoice = su.calcSpeakingTypes(pipeline, currAnnotation, currTotalTime)
783
- except Exception as e:
784
- print(f"calcSpeakingTypes failed ({e}), falling back to annotation-based voice split")
785
- from pyannote.core import Annotation, Segment
786
- noVoice = Annotation()
787
- multiVoice = Annotation()
788
- oneVoice = Annotation()
789
- all_segs = sorted(
790
- [(seg.start, seg.end, label)
791
- for label in currAnnotation.labels()
792
- if label is not None and str(label).strip() != ""
793
- for seg in currAnnotation.subset([label]).itersegments()],
794
- key=lambda x: x[0]
795
- )
796
- prev_end = 0.0
797
- for start, end, label in all_segs:
798
- if start > prev_end + 0.1:
799
- noVoice[Segment(prev_end, start)] = 'silence'
800
- oneVoice[Segment(start, end)] = label
801
- prev_end = max(prev_end, end)
802
- if currTotalTime > prev_end + 0.1:
803
- noVoice[Segment(prev_end, currTotalTime)] = 'silence'
804
  sumNoVoice = su.sumTimes(noVoice)
805
  sumOneVoice = su.sumTimes(oneVoice)
806
  sumMultiVoice = su.sumTimes(multiVoice)
807
 
808
-
809
  # df3
810
  df3 = utils.build_df3(noVoice, oneVoice, multiVoice)
811
  st.session_state.summaries[inFileName]["df3"] = df3
@@ -838,6 +765,10 @@ def analyze(inFileName):
838
  currTotalTime,
839
  )
840
  st.session_state.summaries[inFileName]["df2"] = df2
 
 
 
 
841
  printV("Set df2", 4)
842
 
843
  except Exception as e:
 
715
  printV("Loaded results", 4)
716
 
717
  pipeline = st.session_state.pipeline
718
+ try:
719
+ noVoice, oneVoice, multiVoice = su.calcSpeakingTypes(pipeline, currAnnotation, currTotalTime)
720
+ except Exception as e:
721
+ print(f"calcSpeakingTypes failed ({e}), falling back to annotation-based voice split")
722
+ # Fall back: build a clean annotation containing only properly named
723
+ # speakers (non-None, non-empty) so build_df5 never receives None labels.
724
+ from pyannote.core import Annotation
 
 
725
  noVoice = Annotation()
726
  multiVoice = Annotation()
727
  oneVoice = Annotation()
728
+ for label in currAnnotation.labels():
729
+ if label is not None and str(label).strip() != "":
730
+ for seg in currAnnotation.subset([label]).itersegments():
731
+ oneVoice[seg] = label
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
732
  sumNoVoice = su.sumTimes(noVoice)
733
  sumOneVoice = su.sumTimes(oneVoice)
734
  sumMultiVoice = su.sumTimes(multiVoice)
735
 
 
736
  # df3
737
  df3 = utils.build_df3(noVoice, oneVoice, multiVoice)
738
  st.session_state.summaries[inFileName]["df3"] = df3
 
765
  currTotalTime,
766
  )
767
  st.session_state.summaries[inFileName]["df2"] = df2
768
+
769
+ # Per-speaker multi-voice seconds — stored for the bar chart overlay
770
+ mv_speakers, mv_times = su.sumMultiTimesPerSpeaker(multiVoice)
771
+ st.session_state.summaries[inFileName]["mv_per_speaker"] = dict(zip(mv_speakers, mv_times))
772
  printV("Set df2", 4)
773
 
774
  except Exception as e: