duongthienz commited on
Commit
2da810e
·
1 Parent(s): ac425ab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -3
app.py CHANGED
@@ -115,7 +115,24 @@ def processFile(filePath):
115
  totalTimeInSeconds = int(waveform_gain_adjusted.shape[-1]/sampleRate)
116
  print("Time in seconds calculated")
117
  return annotations, totalTimeInSeconds
118
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
  def addCategory():
120
  newCategory = st.session_state.categoryInput
121
  st.toast(f"Adding {newCategory}")
@@ -608,12 +625,19 @@ try:
608
 
609
  st.sidebar.divider()
610
  st.sidebar.subheader("Rename Speakers")
611
- st.sidebar.caption("Replace SPEAKER_## labels with real names.")
612
  current_renames = st.session_state.speakerRenames[currFileIndex]
 
613
  for sp in speakerNames:
614
  current_label = current_renames.get(sp, "")
 
 
 
 
 
 
615
  new_name = st.sidebar.text_input(
616
- f"{sp}",
617
  value=current_label,
618
  placeholder=f"e.g. John",
619
  key=f"rename_{currFileIndex}_{sp}"
 
115
  totalTimeInSeconds = int(waveform_gain_adjusted.shape[-1]/sampleRate)
116
  print("Time in seconds calculated")
117
  return annotations, totalTimeInSeconds
118
+
119
+ @st.cache_data
120
+ def extract_speaker_clip(file_path, speaker, _annotation, clip_duration=5.0):
121
+ """Extract a short audio clip for a speaker from their first segment."""
122
+ waveform, sample_rate = torchaudio.load(file_path)
123
+ for segment, _, label in _annotation.itertracks(yield_label=True):
124
+ if label == speaker and segment.duration >= 1.0:
125
+ start_sample = int(segment.start * sample_rate)
126
+ end_sample = int(min(segment.start + clip_duration, segment.end) * sample_rate)
127
+ clip = waveform[:, start_sample:end_sample]
128
+ # Convert to wav bytes in memory
129
+ import io
130
+ buf = io.BytesIO()
131
+ torchaudio.save(buf, clip, sample_rate, format="wav")
132
+ buf.seek(0)
133
+ return buf.read()
134
+ return None
135
+
136
  def addCategory():
137
  newCategory = st.session_state.categoryInput
138
  st.toast(f"Adding {newCategory}")
 
625
 
626
  st.sidebar.divider()
627
  st.sidebar.subheader("Rename Speakers")
628
+ st.sidebar.caption("Replace SPEAKER_## labels with real names. Listen to a sample clip to help identify each speaker.")
629
  current_renames = st.session_state.speakerRenames[currFileIndex]
630
+ currFilePath = st.session_state.file_paths[currFileIndex]
631
  for sp in speakerNames:
632
  current_label = current_renames.get(sp, "")
633
+ st.sidebar.markdown(f"**{sp}**")
634
+ # Only show audio player for real audio files (not rttm/csv/txt)
635
+ if currFilePath.lower().endswith(('.wav', '.mp3', '.mp4')):
636
+ clip_bytes = extract_speaker_clip(currFilePath, sp, currAnnotation)
637
+ if clip_bytes:
638
+ st.sidebar.audio(clip_bytes, format="audio/wav")
639
  new_name = st.sidebar.text_input(
640
+ f"Rename {sp}",
641
  value=current_label,
642
  placeholder=f"e.g. John",
643
  key=f"rename_{currFileIndex}_{sp}"