duongthienz commited on
Commit
2205402
·
verified ·
1 Parent(s): 9979934

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -20
app.py CHANGED
@@ -114,32 +114,25 @@ def processFile(filePath):
114
  print("Speakers Detected")
115
  totalTimeInSeconds = int(waveform_gain_adjusted.shape[-1]/sampleRate)
116
  print("Time in seconds calculated")
117
- return annotations, totalTimeInSeconds
118
 
119
- def generate_speaker_clips(annotations, audio_path, file_index):
120
  """
121
  For each unique speaker in `annotations`, find their longest contiguous segment.
122
  If that segment is >= 5s, clip exactly 5s from its start.
123
  If < 5s, take the whole segment.
 
 
124
  Saves clips as WAV bytes in st.session_state.speakerClips[file_index].
125
- Only works when a real audio file is provided (skips silently otherwise).
126
  """
127
- if audio_path is None or not os.path.exists(audio_path):
128
- return
129
 
130
  # Extend speakerClips list to cover this file index
131
  while len(st.session_state.speakerClips) <= file_index:
132
  st.session_state.speakerClips.append({})
133
 
134
- try:
135
- waveform, sample_rate = torchaudio.load(audio_path)
136
- except Exception as e:
137
- print(f"generate_speaker_clips: could not load audio: {e}")
138
- return
139
-
140
  clips = {}
141
  for speaker in annotations.labels():
142
- # Collect all segments for this speaker
143
  speaker_segments = [
144
  segment for segment, _, label in annotations.itertracks(yield_label=True)
145
  if label == speaker
@@ -147,22 +140,17 @@ def generate_speaker_clips(annotations, audio_path, file_index):
147
  if not speaker_segments:
148
  continue
149
 
150
- # Find the longest segment
151
  longest = max(speaker_segments, key=lambda s: s.duration)
152
 
153
- # Determine clip boundaries
154
  clip_start = longest.start
155
  clip_duration = min(longest.duration, 5.0)
156
  clip_end = clip_start + clip_duration
157
 
158
  start_sample = int(clip_start * sample_rate)
159
- end_sample = int(clip_end * sample_rate)
160
- end_sample = min(end_sample, waveform.shape[-1])
161
 
162
  clip_waveform = waveform[:, start_sample:end_sample]
163
 
164
- # Write to an in-memory bytes buffer as WAV
165
- import io
166
  buffer = io.BytesIO()
167
  torchaudio.save(buffer, clip_waveform, sample_rate, format="wav")
168
  buffer.seek(0)
@@ -556,7 +544,7 @@ else:
556
  st.session_state.unusedSpeakers[i] = speakerNames
557
  else:
558
  with st.spinner(text=f'Processing File {i+1} of {totalFiles}'):
559
- annotations, totalSeconds = processFile(file_paths[i])
560
  print(f"Finished processing {file_paths[i]}")
561
  st.session_state.results[i] = (annotations, totalSeconds)
562
  print("Results saved")
@@ -566,7 +554,8 @@ else:
566
  st.session_state.unusedSpeakers[i] = speakerNames
567
  print("Speakers saved")
568
  with st.spinner(text=f'Generating speaker clips for File {i+1} of {totalFiles}'):
569
- generate_speaker_clips(annotations, file_paths[i], i)
 
570
  print(f"Speaker clips generated for {file_paths[i]}")
571
  with st.spinner(text=f'Analyzing File {i+1} of {totalFiles}'):
572
  analyze(file_names[i])
 
114
  print("Speakers Detected")
115
  totalTimeInSeconds = int(waveform_gain_adjusted.shape[-1]/sampleRate)
116
  print("Time in seconds calculated")
117
+ return annotations, totalTimeInSeconds, waveform_gain_adjusted, sampleRate
118
 
119
+ def generate_speaker_clips(annotations, waveform, sample_rate, file_index):
120
  """
121
  For each unique speaker in `annotations`, find their longest contiguous segment.
122
  If that segment is >= 5s, clip exactly 5s from its start.
123
  If < 5s, take the whole segment.
124
+ Accepts the already-loaded waveform tensor and sample_rate from processFile,
125
+ so this never needs to re-decode the audio file (avoids torchcodec/FFmpeg issues).
126
  Saves clips as WAV bytes in st.session_state.speakerClips[file_index].
 
127
  """
128
+ import io
 
129
 
130
  # Extend speakerClips list to cover this file index
131
  while len(st.session_state.speakerClips) <= file_index:
132
  st.session_state.speakerClips.append({})
133
 
 
 
 
 
 
 
134
  clips = {}
135
  for speaker in annotations.labels():
 
136
  speaker_segments = [
137
  segment for segment, _, label in annotations.itertracks(yield_label=True)
138
  if label == speaker
 
140
  if not speaker_segments:
141
  continue
142
 
 
143
  longest = max(speaker_segments, key=lambda s: s.duration)
144
 
 
145
  clip_start = longest.start
146
  clip_duration = min(longest.duration, 5.0)
147
  clip_end = clip_start + clip_duration
148
 
149
  start_sample = int(clip_start * sample_rate)
150
+ end_sample = min(int(clip_end * sample_rate), waveform.shape[-1])
 
151
 
152
  clip_waveform = waveform[:, start_sample:end_sample]
153
 
 
 
154
  buffer = io.BytesIO()
155
  torchaudio.save(buffer, clip_waveform, sample_rate, format="wav")
156
  buffer.seek(0)
 
544
  st.session_state.unusedSpeakers[i] = speakerNames
545
  else:
546
  with st.spinner(text=f'Processing File {i+1} of {totalFiles}'):
547
+ annotations, totalSeconds, waveform, sample_rate = processFile(file_paths[i])
548
  print(f"Finished processing {file_paths[i]}")
549
  st.session_state.results[i] = (annotations, totalSeconds)
550
  print("Results saved")
 
554
  st.session_state.unusedSpeakers[i] = speakerNames
555
  print("Speakers saved")
556
  with st.spinner(text=f'Generating speaker clips for File {i+1} of {totalFiles}'):
557
+ generate_speaker_clips(annotations, waveform, sample_rate, i)
558
+ del waveform # free memory after clipping
559
  print(f"Speaker clips generated for {file_paths[i]}")
560
  with st.spinner(text=f'Analyzing File {i+1} of {totalFiles}'):
561
  analyze(file_names[i])