-
Notifications
You must be signed in to change notification settings - Fork 50
/
predict.py
377 lines (329 loc) · 13.6 KB
/
predict.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
# Prediction interface for Cog ⚙️
from typing import Any, List
import base64
import datetime
import subprocess
import os
import requests
import time
import torch
import re
from cog import BasePredictor, BaseModel, Input, File, Path
from faster_whisper import WhisperModel
from pyannote.audio import Pipeline
import torchaudio
class Output(BaseModel):
segments: list
language: str = None
num_speakers: int = None
class Predictor(BasePredictor):
def setup(self):
"""Load the model into memory to make running multiple predictions efficient"""
model_name = "large-v3"
self.model = WhisperModel(
model_name,
device="cuda" if torch.cuda.is_available() else "cpu",
compute_type="float16",
)
self.diarization_model = Pipeline.from_pretrained(
"pyannote/speaker-diarization-3.1",
use_auth_token="YOUR HF TOKEN",
).to(torch.device("cuda"))
def predict(
self,
file_string: str = Input(
description="Either provide: Base64 encoded audio file,", default=None
),
file_url: str = Input(
description="Or provide: A direct audio file URL", default=None
),
file: Path = Input(description="Or an audio file", default=None),
group_segments: bool = Input(
description="Group segments of same speaker shorter apart than 2 seconds",
default=True,
),
transcript_output_format: str = Input(
description="Specify the format of the transcript output: individual words with timestamps, full text of segments, or a combination of both.",
default="both",
choices=["words_only", "segments_only", "both"],
),
num_speakers: int = Input(
description="Number of speakers, leave empty to autodetect.",
ge=1,
le=50,
default=None,
),
translate: bool = Input(
description="Translate the speech into English.",
default=False,
),
language: str = Input(
description="Language of the spoken words as a language code like 'en'. Leave empty to auto detect language.",
default=None,
),
prompt: str = Input(
description="Vocabulary: provide names, acronyms and loanwords in a list. Use punctuation for best accuracy.",
default=None,
),
# word_timestamps: bool = Input(description="Return word timestamps", default=True), needs to be implemented
offset_seconds: int = Input(
description="Offset in seconds, used for chunked inputs", default=0, ge=0
),
) -> Output:
"""Run a single prediction on the model"""
# Check if either filestring, filepath or file is provided, but only 1 of them
""" if sum([file_string is not None, file_url is not None, file is not None]) != 1:
raise RuntimeError("Provide either file_string, file or file_url") """
try:
# Generate a temporary filename
temp_wav_filename = f"temp-{time.time_ns()}.wav"
if file is not None:
subprocess.run(
[
"ffmpeg",
"-i",
file,
"-ar",
"16000",
"-ac",
"1",
"-c:a",
"pcm_s16le",
temp_wav_filename,
]
)
elif file_url is not None:
response = requests.get(file_url)
temp_audio_filename = f"temp-{time.time_ns()}.audio"
with open(temp_audio_filename, "wb") as file:
file.write(response.content)
subprocess.run(
[
"ffmpeg",
"-i",
temp_audio_filename,
"-ar",
"16000",
"-ac",
"1",
"-c:a",
"pcm_s16le",
temp_wav_filename,
]
)
if os.path.exists(temp_audio_filename):
os.remove(temp_audio_filename)
elif file_string is not None:
audio_data = base64.b64decode(
file_string.split(",")[1] if "," in file_string else file_string
)
temp_audio_filename = f"temp-{time.time_ns()}.audio"
with open(temp_audio_filename, "wb") as f:
f.write(audio_data)
subprocess.run(
[
"ffmpeg",
"-i",
temp_audio_filename,
"-ar",
"16000",
"-ac",
"1",
"-c:a",
"pcm_s16le",
temp_wav_filename,
]
)
if os.path.exists(temp_audio_filename):
os.remove(temp_audio_filename)
segments, detected_num_speakers, detected_language = self.speech_to_text(
temp_wav_filename,
num_speakers,
prompt,
offset_seconds,
group_segments,
language,
word_timestamps=True,
transcript_output_format=transcript_output_format,
translate=translate,
)
print(f"done with inference")
# Return the results as a JSON object
return Output(
segments=segments,
language=detected_language,
num_speakers=detected_num_speakers,
)
except Exception as e:
raise RuntimeError("Error Running inference with local model", e)
finally:
# Clean up
if os.path.exists(temp_wav_filename):
os.remove(temp_wav_filename)
def convert_time(self, secs, offset_seconds=0):
return datetime.timedelta(seconds=(round(secs) + offset_seconds))
def speech_to_text(
self,
audio_file_wav,
num_speakers=None,
prompt="",
offset_seconds=0,
group_segments=True,
language=None,
word_timestamps=True,
transcript_output_format="both",
translate=False,
):
time_start = time.time()
# Transcribe audio
print("Starting transcribing")
options = dict(
vad_filter=True,
vad_parameters=dict(min_silence_duration_ms=1000),
initial_prompt=prompt,
word_timestamps=word_timestamps,
language=language,
task="translate" if translate else "transcribe",
hotwords=prompt
)
segments, transcript_info = self.model.transcribe(audio_file_wav, **options)
segments = list(segments)
segments = [
{
"avg_logprob": s.avg_logprob,
"start": float(s.start + offset_seconds),
"end": float(s.end + offset_seconds),
"text": s.text,
"words": [
{
"start": float(w.start + offset_seconds),
"end": float(w.end + offset_seconds),
"word": w.word,
"probability": w.probability,
}
for w in s.words
],
}
for s in segments
]
time_transcribing_end = time.time()
print(
f"Finished with transcribing, took {time_transcribing_end - time_start:.5} seconds, {len(segments)} segments"
)
print("Starting diarization")
waveform, sample_rate = torchaudio.load(audio_file_wav)
diarization = self.diarization_model(
{"waveform": waveform, "sample_rate": sample_rate},
num_speakers=num_speakers,
)
time_diraization_end = time.time()
print(
f"Finished with diarization, took {time_diraization_end - time_transcribing_end:.5} seconds"
)
print("Starting merging")
# Initialize variables to keep track of the current position in both lists
margin = 0.1 # 0.1 seconds margin
# Initialize an empty list to hold the final segments with speaker info
final_segments = []
diarization_list = list(diarization.itertracks(yield_label=True))
unique_speakers = {
speaker for _, _, speaker in diarization.itertracks(yield_label=True)
}
detected_num_speakers = len(unique_speakers)
speaker_idx = 0
n_speakers = len(diarization_list)
# Iterate over each segment
for segment in segments:
segment_start = segment["start"] + offset_seconds
segment_end = segment["end"] + offset_seconds
segment_text = []
segment_words = []
# Iterate over each word in the segment
for word in segment["words"]:
word_start = word["start"] + offset_seconds - margin
word_end = word["end"] + offset_seconds + margin
while speaker_idx < n_speakers:
turn, _, speaker = diarization_list[speaker_idx]
if turn.start <= word_end and turn.end >= word_start:
# Add word without modifications
segment_text.append(word["word"])
# Strip here for individual word storage
word["word"] = word["word"].strip()
segment_words.append(word)
if turn.end <= word_end:
speaker_idx += 1
break
elif turn.end < word_start:
speaker_idx += 1
else:
break
if segment_text:
combined_text = "".join(segment_text)
cleaned_text = re.sub(" ", " ", combined_text).strip()
new_segment = {
"avg_logprob": segment["avg_logprob"],
"start": segment_start - offset_seconds,
"end": segment_end - offset_seconds,
"speaker": speaker,
"text": cleaned_text,
"words": segment_words,
}
final_segments.append(new_segment)
time_merging_end = time.time()
print(
f"Finished with merging, took {time_merging_end - time_diraization_end:.5} seconds, {len(final_segments)} segments, {detected_num_speakers} speakers"
)
# Check if final_segments is empty
if not final_segments:
return [], detected_num_speakers, transcript_info.language
print("Starting cleaning")
segments = final_segments
# Make output
output = [] # Initialize an empty list for the output
# Initialize the first group with the first segment
current_group = {
"start": segments[0]["start"],
"end": segments[0]["end"],
"speaker": segments[0]["speaker"],
"avg_logprob": segments[0]["avg_logprob"],
}
if transcript_output_format in ("segments_only", "both"):
current_group["text"] = segments[0]["text"]
if transcript_output_format in ("words_only", "both"):
current_group["words"] = segments[0]["words"]
for i in range(1, len(segments)):
# Calculate time gap between consecutive segments
time_gap = segments[i]["start"] - segments[i - 1]["end"]
# If the current segment's speaker is the same as the previous segment's speaker,
# and the time gap is less than or equal to 2 seconds, group them
if segments[i]["speaker"] == segments[i - 1]["speaker"] and time_gap <= 2 and group_segments:
current_group["end"] = segments[i]["end"]
if transcript_output_format in ("segments_only", "both"):
current_group["text"] += " " + segments[i]["text"]
if transcript_output_format in ("words_only", "both"):
current_group.setdefault("words", []).extend(segments[i]["words"])
else:
# Add the current_group to the output list
output.append(current_group)
# Start a new group with the current segment
current_group = {
"start": segments[i]["start"],
"end": segments[i]["end"],
"speaker": segments[i]["speaker"],
"avg_logprob": segments[i]["avg_logprob"],
}
if transcript_output_format in ("segments_only", "both"):
current_group["text"] = segments[i]["text"]
if transcript_output_format in ("words_only", "both"):
current_group["words"] = segments[i]["words"]
# Add the last group to the output list
output.append(current_group)
time_cleaning_end = time.time()
print(
f"Finished with cleaning, took {time_cleaning_end - time_merging_end:.5} seconds"
)
time_end = time.time()
time_diff = time_end - time_start
system_info = f"""Processing time: {time_diff:.5} seconds"""
print(system_info)
return output, detected_num_speakers, transcript_info.language