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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
|
from datetime import datetime
from faster_whisper import WhisperModel
from functools import partial
from pydub import AudioSegment
from whisper.normalizers import EnglishTextNormalizer
from scipy.optimize import minimize
import app_config
import argparse
import editdistance
import langcodes
import math
import numpy as np
import os
import pyaudio
import time
import typing
class AudioStream():
FORMAT = pyaudio.paInt16
# Size of each frame (audio sample), in bytes. If you change FORMAT, make
# sure this stays up to date!
FRAME_SZ = 2
# Frames per second.
FPS = 16000
CHANNELS = 1
def __init__(self):
pass
def getSamples(self) -> bytes:
raise NotImplementedError("getSamples is not implemented!")
class DiskStream(AudioStream):
def __init__(self, path: str):
fmt = None
if path.endswith(".mp3"):
fmt = "mp3"
elif path.endswith(".wav"):
fmt = "wav"
else:
raise NotImplementedError(f"Requested file type {path} " + \
"is not supported")
print(f"Loading audio data")
audio = AudioSegment.from_file(path, format=fmt)
audio = audio.set_channels(1)
# TODO(yum) replace manual decimation code with this!
audio = audio.set_frame_rate(16000)
frames = np.array(audio.get_array_of_samples())
frames = np.int16(frames).tobytes()
self.frames = frames
print(f"Loaded data")
def getSamples(self) -> bytes:
# Give out samples at a fixed rate to minimize
# noise.
give_s = 0.2
nframes = int(give_s * AudioStream.FPS)
frames = self.frames[0:nframes * AudioStream.FRAME_SZ];
self.frames = self.frames[nframes * AudioStream.FRAME_SZ:]
return frames
class MicStream(AudioStream):
CHUNK_SZ = 1024
def __init__(self, which_mic: str):
self.p = pyaudio.PyAudio()
self.stream = None
self.sample_rate = None
# Each time pyaudio gives us audio data, it's in the form of a chunk of
# samples. We keep these in a list to keep the audio callback as light
# as possible. Whenever downstream layers want data, we collapse the
# list into a single array of data (a bytes object).
self.chunks = []
print(f"Finding mic {which_mic}")
self.dumpMicDevices()
got_match = False
device_index = -1
focusrite_str = "Focusrite"
index_str = "Digital Audio Interface"
if which_mic == "index":
target_str = index_str
elif which_mic == "focusrite":
target_str = focusrite_str
else:
print(f"Mic {which_mic} requested, treating it as a numerical " +
"device ID", file=sys.stderr)
device_index = int(which_mic)
got_match = True
if not got_match:
info = self.p.get_host_api_info_by_index(0)
numdevices = info.get('deviceCount')
for i in range(0, numdevices):
if (self.p.get_device_info_by_host_api_device_index(0, i).get('maxInputChannels')) > 0:
device_name = self.p.get_device_info_by_host_api_device_index(0, i).get('name')
if target_str in device_name:
print(f"Got matching mic: {device_name}",
file=sys.stderr)
device_index = i
got_match = True
break
if not got_match:
raise KeyError(f"Mic {which_mic} not found")
info = self.p.get_device_info_by_host_api_device_index(0, device_index)
print(f"Found mic {which_mic}: {info['name']}", file=sys.stderr)
self.sample_rate = int(info['defaultSampleRate'])
print(f"Mic sample rate: {self.sample_rate}", file=sys.stderr)
self.stream = self.p.open(
rate=self.sample_rate,
channels=AudioStream.CHANNELS,
format=AudioStream.FORMAT,
input=True,
frames_per_buffer=MicStream.CHUNK_SZ,
input_device_index=device_index,
stream_callback=self.onAudioFramesAvailable)
self.stream.start_stream()
AudioStream.__init__(self)
def dumpMicDevices(self):
info = self.p.get_host_api_info_by_index(0)
numdevices = info.get('deviceCount')
for i in range(0, numdevices):
if (self.p.get_device_info_by_host_api_device_index(0, i).get('maxInputChannels')) > 0:
device_name = self.p.get_device_info_by_host_api_device_index(0, i).get('name')
print("Input Device id ", i, " - ", device_name,
file=sys.stderr)
def onAudioFramesAvailable(self,
frames,
frame_count,
time_info,
status_flags):
decimated = b''
# In pyaudio, a `frame` is a single sample of audio data.
frame_len = AudioStream.FRAME_SZ
next_frame = 0.0
# The mic probably has a higher sample rate than Whisper wants, so
# decrease the sample rate by dropping samples. Note that this
# algorithm only works if the mic's rate is higher than whisper's
# expected rate.
keep_every = float(self.sample_rate) / AudioStream.FPS
for i in range(frame_count):
if i >= next_frame:
decimated += frames[i*frame_len:(i+1)*frame_len]
next_frame += keep_every
self.chunks.append(decimated)
return (frames, pyaudio.paContinue)
# Get audio data and the corresponding timestamp.
def getSamples(self) -> bytes:
chunks = self.chunks
self.chunks = []
result = b''.join(chunks)
return result
class AudioCollector:
def __init__(self, stream: AudioStream):
self.stream = stream
self.frames = b''
# Note: by design, this is the only spot where we anchor our timestamps
# against the real world. This is done to make it possible to profile
# test cases which read from disk (at much faster than real speed) in
# the same way that we profile real-time data.
self.wall_ts = time.time()
def getAudio(self) -> bytes:
frames = self.stream.getSamples()
if frames:
self.frames += frames
return self.frames
def dropAudioPrefix(self, dur_s: float):
n_bytes = int(dur_s * self.stream.FPS) * self.stream.FRAME_SZ
n_bytes = min(n_bytes, len(self.frames))
self.frames = self.frames[n_bytes:]
self.wall_ts = self.wall_ts + self.duration()
def keepLast(self, dur_s: float):
drop_len = max(0, self.duration() - dur_s)
self.dropAudioPrefix(drop_len)
def dropAudio(self):
self.wall_ts += self.duration()
self.frames = b''
def duration(self):
return len(self.frames) / (self.stream.FPS * self.stream.FRAME_SZ)
def begin(self):
return self.wall_ts
def now(self):
return self.begin() + self.duration()
class AudioCollectorFilter:
def __init__(self, parent: AudioCollector):
self.parent = parent
def getAudio(self) -> bytes:
return self.parent.getAudio()
def dropAudioPrefix(self, dur_s: float):
return self.parent.dropAudioPrefix(dur_s)
def keepLast(self, dur_s):
return self.parent.keepLast(dur_s)
def dropAudio(self):
return self.parent.dropAudio()
def duration(self):
return self.parent.duration()
def begin(self):
return self.parent.begin()
def now(self):
return self.parent.now()
# Audio collector that enforces a minimum length on its audio data.
class LengthEnforcingAudioCollector(AudioCollectorFilter):
def __init__(self, parent: AudioCollector, min_duration_s: float):
AudioCollectorFilter.__init__(self, parent)
self.min_duration_s = min_duration_s
def getAudio(self) -> bytes:
audio = self.parent.getAudio()
min_duration_frames = int(self.min_duration_s * AudioStream.FPS)
pad_len_frames = max(0, min_duration_frames - int(len(audio) /
AudioStream.FRAME_SZ))
pad = np.zeros(pad_len_frames, dtype=np.int16).tobytes()
return pad + audio
class NormalizingAudioCollector(AudioCollectorFilter):
def __init__(self, parent: AudioCollector):
AudioCollectorFilter.__init__(self, parent)
def getAudio(self) -> bytes:
audio = self.parent.getAudio()
audio = AudioSegment(audio, sample_width=AudioStream.FRAME_SZ,
frame_rate=AudioStream.FPS, channels=AudioStream.CHANNELS)
audio = audio.normalize()
frames = np.array(audio.get_array_of_samples())
frames = np.int16(frames).tobytes()
return frames
class CompressingAudioCollector(AudioCollectorFilter):
def __init__(self, parent: AudioCollector):
AudioCollectorFilter.__init__(self, parent)
def getAudio(self) -> bytes:
audio = self.parent.getAudio()
audio = AudioSegment(audio, sample_width=AudioStream.FRAME_SZ,
frame_rate=AudioStream.FPS, channels=AudioStream.CHANNELS)
# subtle compression has a slight positive effect on my benchmark
audio = audio.compress_dynamic_range(threshold=-10, ratio=2.0)
frames = np.array(audio.get_array_of_samples())
frames = np.int16(frames).tobytes()
return frames
# A segment of transcribed audio. `start_ts` and `end_ts` are floating point
# number of seconds since the beginning of audio data.
class Segment:
def __init__(self,
transcript: str,
start_ts: float,
end_ts: float,
wall_ts: float,
avg_logprob: float,
no_speech_prob: float):
self.transcript = transcript
# start_ts, end_ts are timestamps in seconds relative to `wall_ts`.
self.start_ts = start_ts
self.end_ts = end_ts
# wall_ts is the time.time() at which the oldest audio sample leading
# to this transcript was collected.
self.wall_ts = wall_ts
self.avg_logprob = avg_logprob
self.no_speech_prob = no_speech_prob
def __str__(self):
ts = f"(ts: {self.start_ts}-{self.end_ts}) "
wall_ts_start = datetime.utcfromtimestamp(self.start_ts + self.wall_ts).strftime('%H:%M:%S')
wall_ts_end = datetime.utcfromtimestamp(self.end_ts + self.wall_ts).strftime('%H:%M:%S')
wall_ts = f"(wall ts: {wall_ts_start}-{wall_ts_end}) "
no_speech = f"(no_speech: {self.no_speech_prob}) "
avg_logprob = f"(avg_logprob: {self.avg_logprob}) "
return f"{self.transcript} " + ts + wall_ts + no_speech + avg_logprob
class Whisper:
def __init__(self,
collector: AudioCollector,
cfg: typing.Dict):
self.collector = collector
self.model = None
self.cfg = cfg
abspath = os.path.abspath(__file__)
dname = os.path.dirname(abspath)
model_root = os.path.join(dname, "Models", cfg["model"])
print(f"Model {cfg['model']} will be saved to {model_root}",
file=sys.stderr)
model_device = "cuda"
if cfg["use_cpu"]:
model_device = "cpu"
download_it = os.path.exists(model_root)
model_str = cfg["model"]
if download_it:
model_str = model_root
self.model = WhisperModel(model_str,
device = model_device,
device_index = cfg["gpu_idx"],
compute_type = "int8",
download_root = model_root,
local_files_only = download_it)
def transcribe(self) -> typing.List[Segment]:
frames = self.collector.getAudio()
# Convert from signed 16-bit int [-32768, 32767] to signed 32-bit float on
# [-1, 1].
audio = np.frombuffer(frames,
dtype=np.int16).flatten().astype(np.float32) / 32768.0
segments, info = self.model.transcribe(
audio,
beam_size = 5,
language = langcodes.find(self.cfg["language"]).language,
temperature = 0.0,
log_prob_threshold = -1.0,
vad_filter = True,
without_timestamps = False)
res = []
for s in segments:
res.append(Segment(s.text, s.start, s.end,
self.collector.begin(),
s.avg_logprob, s.no_speech_prob))
return res
class TranscriptCommit:
def __init__(self,
delta: str,
preview: str,
latency_s: int = None,
thresh_at_commit: int = None):
self.delta = delta
self.preview = preview
self.latency_s = latency_s
self.thresh_at_commit = thresh_at_commit
# Commits audio when the transcription layer repeats the same transcript,
# within some fuzzy match distance.
class FuzzyRepeatCommitter:
def __init__(self,
collector: AudioCollector,
whisper: Whisper,
last_n_must_match: int = 2,
edit_thresh_min: float = 1,
edit_thresh_grow_begin_s: float = 1.5,
edit_thresh_grow_halflife_s: float = 0.5,
min_segment_age_s: float = 0.5):
self.collector = collector
self.whisper = whisper
# List of candidate segments. Once these all match, we commit the
# corresponding audio data.
self.candidates = []
self.last_n_must_match = last_n_must_match
self.edit_thresh_min = edit_thresh_min
self.edit_thresh_grow_begin_s = edit_thresh_grow_begin_s
self.edit_thresh_grow_halflife_s = edit_thresh_grow_halflife_s
self.min_segment_age_s = min_segment_age_s
def getDelta(self) -> TranscriptCommit:
segments = self.whisper.transcribe()
preview = ''.join(s.transcript for s in segments)
if len(segments) == 0:
self.collector.keepLast(1.0)
return TranscriptCommit("", preview, None)
s = segments[0]
if len(self.candidates) < self.last_n_must_match:
if len(self.candidates) == 0:
self.candidates.append(s)
return TranscriptCommit("", preview, None)
s0 = self.candidates[0]
if s.wall_ts != s0.wall_ts:
print("Frames dropped, committer resetting candidates")
self.candidates = []
return TranscriptCommit("", preview, None)
self.candidates.append(s)
return TranscriptCommit("", preview, None)
# Rule 1: last n segments must be within a certain edit distance of
# each other. This edit distance starts low and increases exponentially
# as the buffer size grows, thus allowing the check to get weaker under
# compute pressure.
edit_thresh = self.edit_thresh_min
dt = self.collector.now() - (self.collector.begin() + s.start_ts)
if dt > self.edit_thresh_grow_begin_s:
dt -= self.edit_thresh_grow_begin_s
edit_thresh = math.ceil(2**(dt /
self.edit_thresh_grow_halflife_s))
drop_candidates = 0
for i in range(1, len(self.candidates)):
prev = self.candidates[i-1]
cur = self.candidates[i]
dist = editdistance.eval(prev.transcript, cur.transcript)
if dist > edit_thresh:
drop_candidates = i
if drop_candidates != 0:
self.candidates = self.candidates[drop_candidates:]
return TranscriptCommit("", preview, None)
candidate = self.candidates[-1]
# Rule 2: no committing segments that are fewer than the configured
# number of seconds old.
if self.collector.now() - (candidate.end_ts + candidate.wall_ts) < self.min_segment_age_s:
self.candidates = []
return TranscriptCommit("", preview, None)
# Got a candidate! Commit it and return.
self.candidates = []
latency_s = self.collector.now() - (candidate.wall_ts + candidate.start_ts)
self.collector.dropAudioPrefix(candidate.end_ts)
return TranscriptCommit(candidate.transcript, preview, latency_s,
thresh_at_commit = edit_thresh)
def evaluate(cfg,
audio_path: str,
control_path: str,
last_n_must_match: int = 2,
edit_thresh_min: float = 1,
edit_thresh_grow_begin_s: float = 1.5,
edit_thresh_grow_halflife_s: float = 0.5,
min_segment_age_s: float = 0.5
):
stream = DiskStream(audio_path)
collector = AudioCollector(stream)
#collector = LengthEnforcingAudioCollector(collector, 5.0)
#collector = NormalizingAudioCollector(collector)
collector = CompressingAudioCollector(collector)
whisper = Whisper(collector, cfg)
com = FuzzyRepeatCommitter(collector, whisper,
last_n_must_match=last_n_must_match,
edit_thresh_min=edit_thresh_min,
edit_thresh_grow_begin_s=edit_thresh_grow_begin_s,
edit_thresh_grow_halflife_s=edit_thresh_grow_halflife_s,
min_segment_age_s=min_segment_age_s)
transcript = ""
commits = []
print(f"PARAMS")
print(f"last_n_must_match: {last_n_must_match}")
print(f"edit_thresh_min: {edit_thresh_min}")
print(f"edit_thresh_grow_begin_s: {edit_thresh_grow_begin_s}")
print(f"edit_thresh_grow_halflife_s: {edit_thresh_grow_halflife_s}")
print(f"min_segment_age_s: {min_segment_age_s}")
while len(stream.frames) > 0:
commit = com.getDelta()
if len(stream.frames) == 0:
commit.delta = commit.preview
commit.latency_s = 0
if len(commit.delta) > 0:
commits.append(commit)
transcript += commit.delta
if False and len(commit.delta):
print(f"transcript: {transcript}")
print(f"commit latency: {commit.latency_s}")
print(f"commit thresh: {commit.thresh_at_commit}")
with open(control_path, "r") as f:
control = f.read()
normalizer = EnglishTextNormalizer()
control = normalizer(control)
experiment = normalizer(transcript)
sum_latency = 0
for commit in commits:
sum_latency += commit.latency_s
avg_latency = sum_latency / len(commits)
dist = editdistance.eval(control, experiment)
print(f"RESULTS")
print(f"edit distance: {dist}")
print(f"avg latency: {avg_latency}")
print(f"num commits: {len(commits)}")
print(f"final transcript: {transcript}")
score = (3 + (dist/len(control)) * 100) * avg_latency
print(f"score: {score}")
return score
def optimize(cfg,
experiments: typing.List[typing.Tuple[str, str]]):
def wrapper_to_optimize(x):
s = 0
for audio_path, control_path in experiments:
s += evaluate(
cfg,
audio_path,
control_path,
int(x[0]), # last_n_must_match
2**x[1], # edit_thresh_min
(2**x[2])-1,# edit_thresh_grow_begin_s
x[3], # edit_thresh_grow_halflife_s
x[4] # min_segment_age_s
)
return s
initial_guess = [2.3, 1, 1.75, 1.5, 0.5]
bounds = [
(2, 3), # last_n_must_match
(1, 4), # edit_thresh_min
(0, 2.5), # edit_thresh_grow_begin_s
(0.1, 2), # edit_thresh_grow_halflife_s
(0, 3) # min_segment_age_s
]
result = minimize(
wrapper_to_optimize,
initial_guess,
bounds=bounds,
method='L-BFGS-B',
options={"maxfun": int((60/.5)*12),
"eps": 0.2},
)
optimized_params = result.x
print("Optimized Parameters:")
print(f"last_n_must_match: {int(optimized_params[0])}")
print(f"edit_thresh_min: {optimized_params[1]}")
print(f"edit_thresh_grow_begin_s: {optimized_params[2]}")
print(f"edit_thresh_grow_halflife_s: {optimized_params[3]}")
print(f"min_segment_age_s: {optimized_params[4]}")
return optimized_params
def run(cfg):
stream = MicStream(cfg["microphone"])
collector = AudioCollector(stream)
#collector = LengthEnforcingAudioCollector(collector, 5.0)
#collector = NormalizingAudioCollector(collector)
collector = CompressingAudioCollector(collector)
whisper = Whisper(collector, cfg)
com = FuzzyRepeatCommitter(collector, whisper)
transcript = ""
commits = []
while True:
commit = com.getDelta()
if len(commit.delta) > 0:
commits.append(commit)
transcript += commit.delta
if True and len(commit.delta):
print(f"{transcript}")
print(f"commit latency: {commit.latency_s}", file=sys.stderr)
print(f"commit thresh: {commit.thresh_at_commit}", file=sys.stderr)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--config", type=str, help="Path to app config YAML file.")
args = parser.parse_args()
cfg = app_config.getConfig(args.config)
experiments = [
("Evaluate/declaration_short/audio.mp3",
"Evaluate/declaration_short/control.txt"),
("Evaluate/moist/audio.mp3",
"Evaluate/moist/control.txt"),
("Evaluate/vei/audio.mp3",
"Evaluate/vei/control.txt"),
]
if False:
sum = 0
for audio, control in experiments:
sum += evaluate(cfg, audio, control)
print(f"Total score: {sum}")
else:
#optimize(cfg, experiments)
run(cfg)
|