This repository has been archived by the owner on Jun 29, 2024. It is now read-only.
forked from moevm/zoom-transcriber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
385 lines (311 loc) · 11.2 KB
/
main.py
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
import json
import os
import subprocess
import textwrap
import time
from argparse import ArgumentParser, RawTextHelpFormatter
from concurrent.futures import Future, ThreadPoolExecutor, wait
from functools import lru_cache, partial
from pathlib import Path
from typing import Callable, Optional, Union
from vosk import KaldiRecognizer, Model, SetLogLevel, SpkModel
from question_detection.ru import is_phrase_a_question
from utils import merge_iterable
from vosk_utils import (
Denoiser,
SpkResult,
VoskResult,
extract_spk_from_result,
process_vosk_result,
)
SetLogLevel(-1)
NUM_ALTERNATIVES = 4
MERGE_DIFF_SEC = 2
@lru_cache(maxsize=5)
def _get_model(model_path: str) -> Model:
print(f"Init {model_path} model loading")
model = Model(model_path)
print("Model loading completed")
return model
@lru_cache(maxsize=5)
def _get_spk_model(spk_model_path: Optional[str] = None) -> SpkModel:
if spk_model_path is not None:
print(f"Init spk {spk_model_path} model loading")
model = SpkModel(spk_model_path)
print(f"Spk model loading completed")
return model
@lru_cache(maxsize=5)
def _get_spk_vectors(path):
with open(path, "r", encoding="utf-8") as f:
data = json.load(f)
return data
class Loader:
def __init__(self, loader, path: Optional[str] = None):
self.path = path
self.loader = loader
self._data = None
def set_path(self, path: str) -> None:
self.path = path
def load(self):
if not self.path:
raise AttributeError("Model path is not set")
self._data = self.loader(self.path)
def get(self):
return self._data
model_loader = Loader(_get_model)
spk_model_loader = Loader(_get_spk_model)
spk_vectors_loader = Loader(_get_spk_vectors)
def process_audiofile(
file_path_raw: Union[str, Path],
model: Model,
process_fn: Callable[[dict, str], Union[VoskResult, SpkResult]],
spk_model: Optional[SpkModel] = None,
reduce_noise: bool = False,
) -> tuple[list[VoskResult], str]:
result: list[VoskResult] = []
file_path = Path(file_path_raw).absolute()
file_name = file_path.stem
print(f"Started processing file {file_name}")
start_time = time.time()
sample_rate = 16000
process = subprocess.Popen(
[
"ffmpeg",
"-loglevel",
"quiet",
"-i",
file_path,
"-ar",
str(sample_rate),
"-ac",
"1",
"-f",
"s16le",
"-",
],
bufsize=0,
stdout=subprocess.PIPE,
)
rec = KaldiRecognizer(model, sample_rate)
if spk_model:
rec.SetSpkModel(spk_model)
rec.SetWords(True)
rec.Reset()
i = 0
read_n = 4000
if reduce_noise:
denoiser = Denoiser(samplerate=sample_rate, channels=1)
else:
denoiser = lambda x: x
while bytes_data := process.stdout.read(read_n):
denoised_bytes_data = denoiser(bytes_data)
if rec.AcceptWaveform(denoised_bytes_data):
recognized_data = json.loads(rec.Result())
processed_result = process_fn(recognized_data)
if processed_result is not None:
result.append(processed_result)
if i % 250 == 0:
print(f"{file_name=}, processed data chunk #{i}")
i += 1
process.wait()
recognized_data = json.loads(rec.FinalResult())
processed_result = process_fn(recognized_data)
if processed_result is not None:
result.append(processed_result)
elapsed_time = time.time() - start_time
print(f"Finished processig file {file_name}, elapsed time {elapsed_time}s")
return result, file_name
def apply(segment: VoskResult) -> VoskResult:
return VoskResult(
start=segment.start,
end=segment.end,
words=segment.words,
conf=segment.conf,
text=segment.text,
speaker=segment.speaker,
is_question=is_phrase_a_question(segment.text.strip()),
)
def records_close(a: VoskResult, b: VoskResult):
first_scenario = a.speaker == b.speaker and a.is_question == b.is_question
second_scenario = (
a.speaker == b.speaker and a.is_question and (b.start - a.end) <= MERGE_DIFF_SEC
)
return first_scenario or second_scenario
def records_merge(a: VoskResult, b: VoskResult):
combined_words = (a.words or []) + (b.words or [])
return VoskResult(
start=a.start,
end=b.end,
words=combined_words,
text=" ".join((a.text, b.text)).strip(),
conf=(a.conf + b.conf) / 2,
speaker=a.speaker,
is_question=a.is_question or b.is_question,
)
def worker(audio_file: str, reduce_noise: bool):
fn = partial(process_vosk_result, spk_vectors=spk_vectors_loader.get())
result, _ = process_audiofile(
file_path_raw=audio_file,
model=model_loader.get(),
process_fn=fn,
spk_model=spk_model_loader.get(),
reduce_noise=reduce_noise,
)
return result
def extract_spk_worker(audio_file: str, reduce_noise: bool):
speaker_result, speaker_name = process_audiofile(
file_path_raw=audio_file,
model=model_loader.get(),
process_fn=extract_spk_from_result,
spk_model=spk_model_loader.get(),
reduce_noise=reduce_noise,
)
return speaker_result, speaker_name
def process_dir(
dir_path_raw: str,
model_path: str,
spk_model_path: str = None,
spk_vectors_path: str = None,
reduce_noise: bool = False,
patterns: list[str] = ["*.m4a", "*.mp3", "*.wav"],
) -> None:
dir_path = Path(dir_path_raw).absolute()
dir_name = dir_path.stem
audio_file_paths = (f for pattern in patterns for f in dir_path.glob(pattern))
os.makedirs(dir_name, exist_ok=True)
print(f"Started processing directory {dir_name}")
start_time = time.time()
meeting_result: list[VoskResult] = []
model_loader.set_path(model_path)
model_loader.load()
if spk_model_path:
spk_model_loader.set_path(spk_model_path)
spk_model_loader.load()
if spk_vectors_path:
spk_vectors_loader.set_path(spk_vectors_path)
spk_vectors_loader.load()
futures: list[Future] = []
with ThreadPoolExecutor(4) as pool:
for path in audio_file_paths:
futures.append(pool.submit(worker, path, reduce_noise))
wait(futures)
res = [f.result() for f in futures]
meeting_result = [x for y in res for x in y] # flatten
meeting_result.sort(key=lambda r: r.start)
meeting_result = merge_iterable(
meeting_result, records_close, records_merge, apply=apply
)
with open(dir_name + "/result.json", "w", encoding="utf-8") as f:
json.dump(
[r._asdict() for r in meeting_result], f, ensure_ascii=False, indent=2
)
elapsed_time = time.time() - start_time
print(f"Finished processing directory {dir_path}, elapsed time: {elapsed_time}s")
def process_extract_spk_dir(
dir_path_raw: str,
model_path: str,
spk_model_path: str = None,
reduce_noise: bool = False,
patterns: list[str] = ["*.m4a", "*.mp3", "*.wav"],
) -> None:
dir_path = Path(dir_path_raw).absolute()
dir_name = dir_path.stem
audio_file_paths = (f for pattern in patterns for f in dir_path.glob(pattern))
os.makedirs(dir_name, exist_ok=True)
print(f"Started processing directory {dir_name} in spk mode")
start_time = time.time()
model_loader.set_path(model_path)
model_loader.load()
if spk_model_path:
spk_model_loader.set_path(spk_model_path)
spk_model_loader.load()
futures: list[Future] = []
with ThreadPoolExecutor(4) as pool:
for path in audio_file_paths:
futures.append(pool.submit(extract_spk_worker, path, reduce_noise))
wait(futures)
res: list[tuple[list[SpkResult], str]] = [f.result() for f in futures]
spk_data = {}
for spk_results, speaker_name in res:
data = list(filter(lambda x: x is not None and x.spk_frames > 400, spk_results))
data = [
el.spk
for el in sorted(data, key=lambda el: el.spk_frames, reverse=True)[:25]
]
spk_data[speaker_name] = data
with open(dir_name + "/spk_vectors.json", "w", encoding="utf-8") as f:
json.dump(spk_data, f, ensure_ascii=False, indent=2)
elapsed_time = time.time() - start_time
print(f"Finished processing directory {dir_path}, elapsed time: {elapsed_time}s")
if __name__ == "__main__":
parser = ArgumentParser(
description=textwrap.dedent(
"""
CLI app for transcribing audio files for speaker and question detection.
Can run in 2 modes:
1) default mode - processes all mp4 files in provided dir and creates a single result.json file with all detected phrases in timestamp order from the start. To process multispeaker recording provided dir should contain single audio file with combined sound.
2) spk extraction mode - prepares speaker features data for default mode, provided dir should contain speaker recording, with one file for each speaker about 2-5 minutes long (with long sentences and pauses about 2 secons between each sentence).
"""
),
formatter_class=RawTextHelpFormatter,
)
parser.add_argument(
"--dir",
type=str,
required=True,
help="Path to directory with m4a audio files, for speaker features extraction filenames would be used as speaker names. App would create the folder with the same name as provided dir in current folder to write results to: `spk_vectors.json` in case of spk extraction, `result.json` in case of default processing mode",
)
parser.add_argument(
"--model",
type=str,
required=False,
default="models/vosk-model-small-ru-0.22",
help="path to Vosk language model",
)
parser.add_argument(
"--spk-model",
type=str,
required=False,
default=None,
help="path to Vosk spk model",
)
parser.add_argument(
"--spk-vectors",
type=str,
required=False,
default=None,
help="Path to spk_vectors.json file, containing info about speaker names and feauteres. Can be prepared in --extract-spk mode, and required for default processing mode",
)
parser.add_argument(
"--extract-spk",
action="store_true",
help="if set, runs in spk extraction mode",
)
parser.add_argument(
"--reduce_noise",
action="store_true",
help="if set, perform noise reduction",
)
args = parser.parse_args()
dir_path = args.dir
if args.spk_model is not None:
args.spk_model = os.path.abspath(args.spk_model)
if args.extract_spk:
process_extract_spk_dir(
dir_path,
os.path.abspath(args.model),
args.spk_model,
args.reduce_noise,
)
else:
if not args.spk_vectors:
print("No spk vectors path provided")
exit(1)
args.spk_vectors = os.path.abspath(args.spk_vectors)
process_dir(
dir_path,
os.path.abspath(args.model),
args.spk_model,
args.spk_vectors,
args.reduce_noise,
)