helloWorld199 commited on
Commit
4936aba
1 Parent(s): 09c8fbd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -0
app.py CHANGED
@@ -167,7 +167,57 @@ def add_voice_label(json_file, audio_path):
167
  with open(json_file, 'w') as f:
168
  json.dump(data, f, indent=4)
169
 
 
 
 
 
 
 
 
 
 
 
170
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
171
 
172
  with gr.Blocks() as demo:
173
  gr.HTML(HEADER)
 
167
  with open(json_file, 'w') as f:
168
  json.dump(data, f, indent=4)
169
 
170
+ def add_voice_labelv2(json_file, audio_path):
171
+ # Load the JSON file
172
+ with open(json_file, 'r') as f:
173
+ data = json.load(f)
174
+
175
+ # Create VAD object
176
+ vad_iterator = VADIterator(model)
177
+
178
+ # Read input audio file
179
+ wav, _ = librosa.load(audio_path, sr=SAMPLING_RATE, mono=True)
180
 
181
+ speech_probs = []
182
+ # Size of the window we compute the probability on
183
+ window_size_samples = SAMPLING_RATE/4
184
+ for i in range(0, len(wav), window_size_samples):
185
+ chunk = torch.from_numpy(wav[i: i+ window_size_samples])
186
+ if len(chunk) < window_size_samples:
187
+ break
188
+ speech_prob = model(chunk, SAMPLING_RATE).item()
189
+ speech_probs.append(speech_prob)
190
+ vad_iterator.reset_states() # reset model states after each audio
191
+
192
+ voice_idxs = np.where(speech_probs >= 0.7)
193
+ if len(voice_idxs) == 0:
194
+ print("NO VOICE SEGMENTS DETECTED!")
195
+ try:
196
+ begin_seq = True
197
+ start_idx = 0
198
+
199
+ for i in range(len(voice_idxs)):
200
+ if begin_seq:
201
+ start_idx = voice_idxs[i]
202
+ begin_seq = False
203
+ if voice_idxs[i+1] == voice_idxs[i] + 1
204
+ continue
205
+
206
+ start_time = float((start_idx*window_size_samples)/SAMPLING_RATE)
207
+ end_time = float((voice_idxs[i]*window_size_samples)/SAMPLING_RATE)
208
+
209
+ start_minutes = int(start_time)
210
+ end_minutes = int(end_time)
211
+ start_seconds = (start_time - start_minutes) * 60
212
+ end_seconds = (end_time - end_minutes) * 60
213
+
214
+ data['vocal_times'] = {
215
+ "start_time": f"{start_minutes}.{start_seconds}",
216
+ "end_time": f"{end_minutes}.{end_seconds}"
217
+ }
218
+
219
+ except Exception as e:
220
+ print(f"An exception occurred: {e}")
221
 
222
  with gr.Blocks() as demo:
223
  gr.HTML(HEADER)