-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecognition.py
More file actions
440 lines (350 loc) · 14.8 KB
/
Copy pathrecognition.py
File metadata and controls
440 lines (350 loc) · 14.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
import numpy as np
import threading
from loguru import logger
import torch
import whisperx
SAMPLING_RATE = 16000 # Hz
MIN_AUDIO_BUFFER_DURATION = 1 # seconds
MAX_AUDIO_BUFFER_DURATION = 15 # seconds
FAST_FORWARD_TIME_MARGIN = 0.1 # seconds
ASR_CONTEXT_LENGTH = 200 # words
VAD_SILENCE_STT_THRESHOLD = 0.2
CONDITIONING_TEXT = {
"en": "He thoughtfuly said:",
"ru": "Он задумчиво сказал:"
}
cuda_lock = threading.Lock()
class AudioBuffer:
def __init__(
self,
sampling_rate=SAMPLING_RATE,
min_duration=MIN_AUDIO_BUFFER_DURATION,
max_duration=MAX_AUDIO_BUFFER_DURATION
):
self.sampling_rate = sampling_rate
self.min_duration = min_duration
self.max_duration = max_duration
self.min_size = int(min_duration * sampling_rate)
self.max_size = int(max_duration * sampling_rate)
self.buffer = None
self.offset = None # global time of the buffer's first element
self.clear()
def push(self, data):
if data is not None:
assert isinstance(data, np.ndarray), f"Invalid data type: {type(data)}"
self.buffer = np.concatenate([self.buffer, data])
if len(self.buffer) < self.min_size:
logger.debug("Audio buffer ({:.2f}s) is shorter than {}s", len(self.buffer) / self.sampling_rate, self.min_duration)
return None, None
elif len(self.buffer) > self.max_size:
o_offset = self.offset
logger.debug("Audio buffer ({:.2f}s) is longer than {}s, trimming.", len(self.buffer) / self.sampling_rate, self.max_duration)
self.offset += (len(self.buffer) - self.max_size) / self.sampling_rate
self.buffer = self.buffer[-self.max_size:]
logger.debug("New buffer length: {:.2f}s. Offset: {:.2f}s -> {:.2f}s", len(self.buffer) / self.sampling_rate, o_offset, self.offset)
if data is None:
return self.clear()
return self.buffer, self.offset
def clear(self):
buffer, offset = self.buffer, self.offset
self.buffer = np.empty((0,), dtype=np.float32)
self.offset = 0
return buffer, offset
def fast_forward(self, time):
if time < self.offset:
logger.debug("Ignoring negative fast forward: {:.2f} -> {:.2f}s. Maybe the buffer was front-trimmed?", self.offset, time)
return
ff = min(time - self.offset, len(self.buffer) / self.sampling_rate)
ff_size = int(ff * self.sampling_rate)
self.buffer = self.buffer[ff_size:]
self.offset += ff
def empty(self):
return len(self.buffer) == 0
class Word:
def __init__(self, word, start, end, sep=""):
self.word = word
self.start = start
self.end = end
self.sep = sep
@staticmethod
def none():
return Word(word=None, start=None, end=None)
@staticmethod
def to_text(words):
words = words if isinstance(words, list) else [words]
if not words:
return ""
return words[0].sep.join([w.word for w in words])
@staticmethod
def apply_offset(words, offset):
words = words if isinstance(words, list) else [words]
for i in range(len(words)):
words[i].start += offset
words[i].end += offset
return words
def __repr__(self):
return f"{self.start}:{self.end} {self.word}"
class HypothesisBuffer:
def __init__(self):
self.clear()
def clear(self):
self.confirmed_words = []
self.unconfirmed_words = []
def update(self, current_words):
if self.confirmed_words:
current_words = self._fast_forward(current_words, self.confirmed_words[-1].end)
self.unconfirmed_words = self._fast_forward(self.unconfirmed_words, self.confirmed_words[-1].end)
if self.unconfirmed_words:
lcp = self._longest_common_prefix(self.unconfirmed_words, current_words)
self.confirmed_words.extend(lcp)
if lcp:
current_words = self._fast_forward(current_words, self.confirmed_words[-1].end)
self.unconfirmed_words = current_words
return self.confirmed_words, self.unconfirmed_words
@staticmethod
def _longest_common_prefix(s1, s2):
min_len = min(len(s1), len(s2))
logger.debug("comparing:")
logger.debug("unconf: {}", Word.to_text(s1))
logger.debug("currnt: {}", Word.to_text(s2))
def compare(w1, w2):
w1_lower = ''.join(c for c in w1.word.lower() if c.isalpha())
w2_lower = ''.join(c for c in w2.word.lower() if c.isalpha())
return w1_lower == w2_lower
index = 0
while index < min_len and compare(s1[index], s2[index]):
index += 1
return s1[:index]
@staticmethod
def _fast_forward(words, time):
# fast forward `words` till the first word which starts after `time`
while words and words[0].start < time:
words.pop(0)
return words
class VoiceActivityDetector:
def __init__(self, sampling_rate=16000, voice_threshold=0.5):
assert sampling_rate in [8000, 16000], f"Unsupported sampling rate {sampling_rate}"
self.model, _ = torch.hub.load(repo_or_dir='snakers4/silero-vad', model='silero_vad')
self.sampling_rate = sampling_rate
self.voice_threshold = voice_threshold
self.silence_threshold = max(self.voice_threshold - 0.15, 0.1)
self._sample_width = {8000: 256, 16000: 512}[self.sampling_rate]
self._trailing_silence = 0.0
self._trailing_voice = 0.0
self._has_voice = None
self._remainder = None
self.reset()
def reset(self):
trailing_silence, trailing_voice, has_voice = (
self._trailing_silence, self._trailing_voice, self._has_voice
)
self._trailing_silence = 0.0
self._trailing_voice = 0.0
self._has_voice = False
self._remainder = np.empty((0,), dtype=np.float32)
return {
"trailing_silence": trailing_silence,
"trailing_voice": trailing_voice,
"has_voice": has_voice
}
def clear_voice(self):
self._trailing_voice = 0.0
self._has_voice = False
def process_chunk(self, chunk):
if chunk is None:
return self.reset()
chunk = np.concatenate([self._remainder, chunk])
for i in range(len(chunk) // self._sample_width):
sample = chunk[i * self._sample_width:(i + 1) * self._sample_width]
self._process_sample(sample)
self._remainder = chunk[-(len(chunk) % self._sample_width):]
return {
"trailing_silence": self._trailing_silence,
"trailing_voice": self._trailing_voice,
"has_voice": self._has_voice
}
def _process_sample(self, chunk):
prob = self.model(torch.from_numpy(chunk), self.sampling_rate)
if prob > self.voice_threshold:
self._trailing_voice += self._get_duration(chunk)
self._trailing_silence = 0.0
self._has_voice = True
elif prob < self.silence_threshold:
self._trailing_voice = 0.0
self._trailing_silence += self._get_duration(chunk)
def _get_duration(self, chunk):
return len(chunk) / self.sampling_rate
class OfflineASR:
word_sep = " "
_cached_model_params = None
@staticmethod
def get_model(language="en", device="cuda", cached=True):
if (cached and OfflineASR._cached_model_params and
OfflineASR._cached_model_params["language"] == language):
return OfflineASR._cached_model_params
if OfflineASR._cached_model_params:
OfflineASR._cached_model_params["model"].model.model.unload_model(to_cpu=True)
OfflineASR._cached_model_params["model_a"].cpu()
del OfflineASR._cached_model_params["model"]
del OfflineASR._cached_model_params["model_a"]
torch.cuda.empty_cache()
whisper_model = whisperx.load_model(
"large-v2",
device=device,
compute_type="float16",
language=language,
asr_options={"suppress_numerals": True}
# vad_options={'vad_onset': 0.8, 'vad_offset': 0.8}
)
align_model, align_metadata = whisperx.load_align_model(
language_code=language,
device=device,
model_name="WAV2VEC2_ASR_LARGE_LV60K_960H" if language == "en" else None
)
OfflineASR._cached_model_params = {
"model": whisper_model,
"model_a": align_model,
"align_metadata": align_metadata,
"language": language
}
return OfflineASR._cached_model_params
def __init__(self, language="en", cached=False):
self.language = language
self.batch_size = 1 # reduce if low on GPU mem
self.device = "cuda"
model_params = OfflineASR.get_model(language=language, cached=cached)
self.model = model_params["model"]
self.model_a = model_params["model_a"]
self.align_metadata = model_params["align_metadata"]
def transcribe(self, audio, previous_text=None):
with cuda_lock:
transcript = self.model.transcribe(
audio,
batch_size=self.batch_size,
language=self.language,
previous_text=previous_text
)
with cuda_lock:
result = whisperx.align(
transcript=transcript["segments"],
model=self.model_a,
align_model_metadata=self.align_metadata,
audio=audio,
device=self.device,
return_char_alignments=False
)
return self._to_words(result)
def _to_words(self, result):
words = []
for w in result["word_segments"]:
if "start" in w and "end" in w: # ignore unaligned words
word = Word(
word=w["word"],
start=w["start"],
end=w["end"],
sep=self.word_sep
)
words.append(word)
else:
logger.warning("Skipping unaligned word: {}", w)
return words
class OnlineASR:
def __init__(self, context_length=ASR_CONTEXT_LENGTH, language='en', cached=False):
"""
context_length: number of words to condition on
"""
self.context_length = context_length
self.audio_buffer = None
self.h_buffer = None
self.asr = OfflineASR(language, cached=cached)
self.reset()
def reset(self):
self.audio_buffer = AudioBuffer()
self.h_buffer = HypothesisBuffer()
@logger.catch
def process_chunk(self, chunk, finalize=False, return_audio=False):
sr = self.sample_rate
if finalize:
audio, buffer_offset = self.audio_buffer.clear()
logger.debug("Flushing audio buffer of length: {:.2f}", len(audio) / sr)
if not audio.any(): # buffer is empty
return None
else:
audio, buffer_offset = self.audio_buffer.push(chunk)
if audio is None: # buffer is not filled yet
return None
buffer_duration = len(audio) / sr
buffer_end_time = buffer_offset + buffer_duration
logger.debug("Transcribing audio of length: {:.2f}, buffer offset: {:.2f}", buffer_duration, buffer_offset)
if self.context_length > 0:
context = Word.to_text(self.h_buffer.confirmed_words)[-self.context_length:]
logger.debug("Conditioning on: {}", context)
else:
context = None
words = self.asr.transcribe(audio, previous_text=context)
words = Word.apply_offset(words, buffer_offset)
logger.opt(colors=True).debug("<g>Buffer transcription: {}</g>", Word.to_text(words))
# TODO: it relays on the last word end time, which is not accurate.
# better to track speech activity detection (vad)
if words:
silence_time = buffer_end_time - words[-1].end
else:
silence_time = buffer_duration
confirmed_words, unconfirmed_words = self.h_buffer.update(words)
if finalize:
self.h_buffer.clear()
if confirmed_words:
# fast forward to the middle of the last confirmed word and the first unconfirmed word
# it's less likely to cut the current utterance by mistake
start = confirmed_words[-1].end
end = unconfirmed_words[0].start if unconfirmed_words else buffer_end_time
ff_time = (start + end) / 2
self.audio_buffer.fast_forward(ff_time)
logger.debug("Fast forwarding audio buffer to: {:.2f}", ff_time)
result = {
"confirmed_text": Word.to_text(confirmed_words),
"unconfirmed_text": Word.to_text(unconfirmed_words),
"silence_time": silence_time
}
if return_audio:
result["audio"] = audio
logger.debug("Confirmed text: {}", result["confirmed_text"])
logger.debug("Unconfirmed text: {}", result["unconfirmed_text"])
logger.debug("Silence time: {:.2f}", silence_time)
return result
@property
def sample_rate(self):
return self.audio_buffer.sampling_rate
class ASRWithVAD:
def __init__(self, language='en', cached=False, vad_silence_threshold=VAD_SILENCE_STT_THRESHOLD):
self.asr = OfflineASR(language=language, cached=cached)
self.vad_silence_threshold = vad_silence_threshold
self.vad = VoiceActivityDetector()
self.audio_buffer = AudioBuffer(min_duration=1.0, max_duration=60)
def process_chunk(self, chunk, context=None):
vad_stats = self.vad.process_chunk(chunk)
audio_buffer, _ = self.audio_buffer.push(chunk)
if (audio_buffer is not None and
vad_stats["has_voice"] and
vad_stats["trailing_silence"] > self.vad_silence_threshold
):
logger.debug("STT silence threshold triggered, transcribing.")
buffer_text = self._transcribe(audio_buffer, context=context)
self.audio_buffer.clear()
self.vad.clear_voice()
else:
buffer_text = None
return {
"text": buffer_text,
"vad_stats": vad_stats
}
def _transcribe(self, audio_buffer, context=None):
if context is not None and context.last_message():
cond_text = context.last_message()["content"][0]["text"]
else:
cond_text = CONDITIONING_TEXT[self.asr.language]
words = self.asr.transcribe(audio_buffer, previous_text=cond_text)
return Word.to_text(words)
@property
def sample_rate(self):
return SAMPLING_RATE