This repository was archived by the owner on Oct 8, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain_whisper.py
More file actions
286 lines (224 loc) · 8.36 KB
/
Copy pathmain_whisper.py
File metadata and controls
286 lines (224 loc) · 8.36 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
import datetime
import functools
import json
import logging
import os
import sys
import time
from argparse import ArgumentParser
from functools import lru_cache
from typing import Dict, List, Tuple, Union
import numpy
import torch
from pyannote.audio import Pipeline
from whisper import Whisper, load_model
from whisper.audio import SAMPLE_RATE, load_audio
from question_detection.ru import is_phrase_a_question
from utils import merge_iterable
logging.basicConfig(
stream=sys.stdout,
level=logging.INFO,
format="[%(levelname)s]:%(pathname)s:%(lineno)d - %(message)s",
)
logger = logging.getLogger(__file__)
MERGE_DIFF_SEC = 2
@lru_cache(maxsize=None)
def get_pyannote_pipeline(config_path: str) -> Pipeline:
return Pipeline.from_pretrained(config_path)
def get_whisper_model(model_type: str) -> Whisper:
return load_model(model_type)
def _is_overlapping(a, b, th=0.35):
s1, e1 = a
s2, e2 = b
if (s2 >= s1 and e2 <= s1) or (s1 >= s2 and e1 <= e2):
return True
return (s2 <= e1) and (e2 >= s1)
def measure_duration(f):
@functools.wraps(f)
def wrapper(*args, **kwargs):
start = time.monotonic()
result = f(*args, **kwargs)
end = time.monotonic()
return datetime.timedelta(seconds=end - start), result
return wrapper
def prepare_audio_file(file_path: str):
full_path = os.path.abspath(file_path)
return full_path, torch.from_numpy(load_audio(file_path))
@measure_duration
def identify_speakers(pipeline: Pipeline, audio: torch.Tensor):
if not torch.is_tensor(audio):
raise ValueError(
f"Provided audio is not of type `torch.Tensor`, prepare the audio using the `prepare_audio_file` function"
)
# pyannote.audio accepts Tensor of shape [<channels_num>, <time>], in our case we read audio as mono waveform always
audio_in_memory = {
"waveform": torch.reshape(audio, (1, -1)),
"sample_rate": SAMPLE_RATE,
}
speaker_diarization = pipeline(audio_in_memory)
speakers_data = []
for speech_turn, _, speaker in speaker_diarization.itertracks(yield_label=True):
speakers_data.append(
(
float(f"{speech_turn.start:.1f}"),
float(f"{speech_turn.end:.1f}"),
speaker,
)
)
return speakers_data
@measure_duration
def extract_questions_and_merge(annotated_result: List[Dict]) -> Dict:
if not annotated_result:
return {"questions": [], "segments": annotated_result}
def apply(segment: Dict) -> Dict:
segment["is_question"] = is_phrase_a_question(segment["text"].strip())
return segment
def is_close(a: Dict, b: Dict):
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 merge(a: Dict, b: Dict) -> Dict:
return {
"text": a["text"] + b["text"],
"start": a["start"],
"end": b["end"],
"speaker": a["speaker"],
"is_question": a["is_question"] or b["is_question"],
}
annotated_with_questions = merge_iterable(
annotated_result, is_close=is_close, merge=merge, apply=apply
)
return annotated_with_questions
@measure_duration
def transcribe_recording(
model: Whisper,
audio: Union[str, torch.Tensor, numpy.ndarray],
lang: str = "russian",
no_speech_th=0.8,
) -> Dict:
result = model.transcribe(
audio=audio,
verbose=False,
word_timestamps=True,
no_speech_threshold=no_speech_th,
condition_on_previous_text=False,
language="russian",
)
segments = result["segments"]
lang = result["language"]
segments = sorted(
[s for s in segments if s["no_speech_prob"] <= no_speech_th],
key=lambda s: s["start"],
)
text = " ".join(s["text"] for s in segments)
return {"text": text, "segments": segments, "language": lang}
@measure_duration
def annotate_transcribed_result(
transcribed_result: Dict, speaker_intervals: List[Tuple[float, float, str]]
) -> Dict:
transcribed_result_annotated = []
for transribed_segment in transcribed_result["segments"]:
for speaker_segment in speaker_intervals:
speaker_interval, speaker_label = speaker_segment[:2], speaker_segment[2]
if _is_overlapping(
[transribed_segment["start"], transribed_segment["end"]],
speaker_interval,
):
speaker = speaker_label
break
else:
speaker = "UNKNOWN"
transcribed_result_annotated.append(
{
"text": transribed_segment["text"],
"start": transribed_segment["start"],
"end": transribed_segment["end"],
"speaker": speaker,
}
)
return transcribed_result_annotated
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument(
"-f",
"--file",
type=str,
required=True,
help="path to audio recording to process",
)
parser.add_argument(
"-m",
"--whisper-model",
type=str,
default="small",
help="type of Whisper model to run (base, small, medium, large). See https://github.com/openai/whisper#available-models-and-languages",
)
parser.add_argument(
"-r",
"--raw",
action="store_true",
help="flag to indicate whether to save raw annotated result (no question detection, no merge phrases) alonside the processed one or not",
)
parser.add_argument(
"-n",
"--no-speakers",
action="store_true",
help="flag to indicate whether to do speaker diarization or not",
)
args = vars(parser.parse_args())
logging.info("CUDA availability: %s", torch.cuda.is_available())
logger.info("Loading whisper model `%s`", args["whisper_model"])
model = get_whisper_model(args["whisper_model"])
steps_reports = []
if not args["no_speakers"]:
logger.info("Loading pyannote.audio pipeline from './models/config.yaml'")
pyannote_pipline = get_pyannote_pipeline("./models/config.yaml")
logger.info("Preparing input recording %s", args["file"])
full_path, audio = prepare_audio_file(args["file"])
logger.info("Transcribing input audio...")
tr_duration, transribed_data = transcribe_recording(
model, audio, no_speech_th=0.765
)
steps_reports.append(f"Transcribing - {tr_duration}")
if not args["no_speakers"]:
logger.info("Identifying speakers...")
sp_duration, speaker_invervals = identify_speakers(pyannote_pipline, audio)
steps_reports.append(f"Speaker diarization - {sp_duration}")
else:
speaker_invervals = []
logger.info("Preparing the final result...")
an_duration, annotated_result = annotate_transcribed_result(
transribed_data, speaker_invervals
)
q_duration, final_result = extract_questions_and_merge(
annotated_result=annotated_result
)
steps_reports.append(
f"Annotating and question extraction - {(an_duration + q_duration)}"
)
dir_name = os.path.dirname(full_path)
file_name, _ = os.path.splitext(os.path.basename(full_path))
result_json = os.path.join(dir_name, f"{file_name}_whisper_result.json")
result_raw_json = os.path.join(dir_name, f"{file_name}_whisper_result_raw.json")
result_speaker_raw_json = os.path.join(
dir_name, f"{file_name}_pyannote_speaker_result_raw.json"
)
with open(result_json, "w") as f:
json.dump(final_result, f, indent=2, ensure_ascii=False)
logger.info("All done! Saved result to %s", result_json)
if args["raw"]:
with open(result_raw_json, "w") as f:
json.dump(annotated_result, f, indent=2, ensure_ascii=False)
logger.info("Saved raw whisper result to %s", result_raw_json)
with open(result_speaker_raw_json, "w") as f:
json.dump(speaker_invervals, f, indent=2, ensure_ascii=False)
logger.info("Saved raw pyannote speaker result to %s", result_speaker_raw_json)
print("Time taken:")
for step_report in steps_reports:
print(step_report)