forked from Vaibhavs10/insanely-fast-whisper
-
Notifications
You must be signed in to change notification settings - Fork 104
/
predict.py
290 lines (250 loc) · 10.8 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
import os
from typing import Any
import time
import subprocess
import torch
import numpy as np
from transformers import (
WhisperFeatureExtractor,
WhisperTokenizerFast,
WhisperForConditionalGeneration,
pipeline,
)
from pyannote.audio import Pipeline
from transformers.pipelines.audio_utils import ffmpeg_read
from transformers.models.whisper.tokenization_whisper import LANGUAGES
from cog import BasePredictor, Input, Path
PIPELINE_URL = (
"https://weights.replicate.delivery/default/incredibly-fast-whisper-pipe.tar"
)
PIPELINE_CACHE = "whisper-cache"
def prepare_weights():
"""Shows how to get the weights from HuggingFace Hub and then upload to Replicate google cloud bucket for faster boot time."""
model_id = "openai/whisper-large-v3"
torch_dtype = torch.float16
model_cache = "model_cache"
model = WhisperForConditionalGeneration.from_pretrained(
model_id,
torch_dtype=torch_dtype,
cache_dir=model_cache,
)
tokenizer = WhisperTokenizerFast.from_pretrained(model_id, cache_dir=model_cache)
feature_extractor = WhisperFeatureExtractor.from_pretrained(
model_id, cache_dir=model_cache
)
pipe = pipeline(
"automatic-speech-recognition",
model=model,
tokenizer=tokenizer,
feature_extractor=feature_extractor,
model_kwargs={"use_flash_attention_2": True},
torch_dtype=torch_dtype,
)
pipe.save_pretrained(
PIPELINE_CACHE, safe_serialization=True
) # Then copy this dir to google cloud bucket that can later be downloaded from PIPELINE_URL
def download_weights(url, dest):
start = time.time()
print("downloading url: ", url)
print("downloading to: ", dest)
subprocess.check_call(["pget", "-x", url, dest], close_fds=False)
print("downloading took: ", time.time() - start)
class Predictor(BasePredictor):
def setup(self):
"""Loads whisper models into memory to make running multiple predictions efficient"""
model_id = "openai/whisper-large-v3"
torch_dtype = torch.float16
self.device = "cuda:0"
if not os.path.exists(PIPELINE_CACHE):
download_weights(PIPELINE_URL, PIPELINE_CACHE)
self.pipe = pipeline(
task="automatic-speech-recognition",
model=PIPELINE_CACHE,
device=self.device,
)
self.diarization_pipeline = None
def predict(
self,
audio: Path = Input(description="Audio file"),
task: str = Input(
choices=["transcribe", "translate"],
default="transcribe",
description="Task to perform: transcribe or translate to another language.",
),
language: str = Input(
default="None",
choices=["None"] + sorted(list(LANGUAGES.values())),
description="Language spoken in the audio, specify 'None' to perform language detection.",
),
batch_size: int = Input(
default=24,
description="Number of parallel batches you want to compute. Reduce if you face OOMs.",
),
timestamp: str = Input(
default="chunk",
choices=["chunk", "word"],
description="Whisper supports both chunked as well as word level timestamps.",
),
diarise_audio: bool = Input(
default=False,
description="Use Pyannote.audio to diarise the audio clips. You will need to provide hf_token below too.",
),
hf_token: str = Input(
default=None,
description="Provide a hf.co/settings/token for Pyannote.audio to diarise the audio clips. You need to agree to the terms in 'https://huggingface.co/pyannote/speaker-diarization-3.1' and 'https://huggingface.co/pyannote/segmentation-3.0' first.",
),
) -> Any:
"""Transcribes and optionally translates a single audio file"""
if diarise_audio:
assert (
hf_token is not None
), "Please provide hf_token to diarise the audio clips"
outputs = self.pipe(
str(audio),
chunk_length_s=30,
batch_size=batch_size,
generate_kwargs={
"task": task,
"language": None if language == "None" else language,
},
return_timestamps="word" if timestamp == "word" else True,
)
if diarise_audio:
if self.diarization_pipeline is None:
try:
self.diarization_pipeline = Pipeline.from_pretrained(
"pyannote/speaker-diarization-3.1",
use_auth_token=hf_token,
cache_dir=PIPELINE_CACHE,
)
self.diarization_pipeline.to(torch.device(self.device))
print("diarization_pipeline loaded!")
except Exception as e:
print(
f"https://huggingface.co/pyannote/speaker-diarization-3.1 cannot be loaded, please check the hf_token provided.: {e}"
)
if self.diarization_pipeline is not None:
print("Segmenting the audio clips.")
inputs, diarizer_inputs = preprocess_inputs(inputs=str(audio))
segments = diarize_audio(diarizer_inputs, self.diarization_pipeline)
segmented_transcript = post_process_segments_and_transcripts(
segments, outputs["chunks"], group_by_speaker=False
)
segmented_transcript.append(outputs)
print("Voila!✨ Your file has been transcribed & speaker segmented!")
return segmented_transcript
print("Voila!✨ Your file has been transcribed!")
return outputs
def preprocess_inputs(inputs):
if isinstance(inputs, str):
if inputs.startswith("http://") or inputs.startswith("https://"):
# We need to actually check for a real protocol, otherwise it's impossible to use a local file
# like http_huggingface_co.png
inputs = requests.get(inputs).content
else:
with open(inputs, "rb") as f:
inputs = f.read()
if isinstance(inputs, bytes):
inputs = ffmpeg_read(inputs, 16000)
if isinstance(inputs, dict):
# Accepting `"array"` which is the key defined in `datasets` for better integration
if not ("sampling_rate" in inputs and ("raw" in inputs or "array" in inputs)):
raise ValueError(
"When passing a dictionary to ASRDiarizePipeline, the dict needs to contain a "
'"raw" key containing the numpy array representing the audio and a "sampling_rate" key, '
"containing the sampling_rate associated with that array"
)
_inputs = inputs.pop("raw", None)
if _inputs is None:
# Remove path which will not be used from `datasets`.
inputs.pop("path", None)
_inputs = inputs.pop("array", None)
in_sampling_rate = inputs.pop("sampling_rate")
inputs = _inputs
if in_sampling_rate != 16000:
inputs = F.resample(
torch.from_numpy(inputs), in_sampling_rate, 16000
).numpy()
if not isinstance(inputs, np.ndarray):
raise ValueError(f"We expect a numpy ndarray as input, got `{type(inputs)}`")
if len(inputs.shape) != 1:
raise ValueError(
"We expect a single channel audio input for ASRDiarizePipeline"
)
# diarization model expects float32 torch tensor of shape `(channels, seq_len)`
diarizer_inputs = torch.from_numpy(inputs).float()
diarizer_inputs = diarizer_inputs.unsqueeze(0)
return inputs, diarizer_inputs
def diarize_audio(diarizer_inputs, diarization_pipeline):
diarization = diarization_pipeline(
{"waveform": diarizer_inputs, "sample_rate": 16000},
)
segments = []
for segment, track, label in diarization.itertracks(yield_label=True):
segments.append(
{
"segment": {"start": segment.start, "end": segment.end},
"track": track,
"label": label,
}
)
# diarizer output may contain consecutive segments from the same speaker (e.g. {(0 -> 1, speaker_1), (1 -> 1.5, speaker_1), ...})
# we combine these segments to give overall timestamps for each speaker's turn (e.g. {(0 -> 1.5, speaker_1), ...})
new_segments = []
prev_segment = cur_segment = segments[0]
for i in range(1, len(segments)):
cur_segment = segments[i]
# check if we have changed speaker ("label")
if cur_segment["label"] != prev_segment["label"] and i < len(segments):
# add the start/end times for the super-segment to the new list
new_segments.append(
{
"segment": {
"start": prev_segment["segment"]["start"],
"end": cur_segment["segment"]["start"],
},
"speaker": prev_segment["label"],
}
)
prev_segment = segments[i]
# add the last segment(s) if there was no speaker change
new_segments.append(
{
"segment": {
"start": prev_segment["segment"]["start"],
"end": cur_segment["segment"]["end"],
},
"speaker": prev_segment["label"],
}
)
return new_segments
def post_process_segments_and_transcripts(new_segments, transcript, group_by_speaker):
# get the end timestamps for each chunk from the ASR output
end_timestamps = np.array([chunk["timestamp"][-1] for chunk in transcript])
segmented_preds = []
# align the diarizer timestamps and the ASR timestamps
for segment in new_segments:
# get the diarizer end timestamp
end_time = segment["segment"]["end"]
# find the ASR end timestamp that is closest to the diarizer's end timestamp and cut the transcript to here
upto_idx = np.argmin(np.abs(end_timestamps - end_time))
if group_by_speaker:
segmented_preds.append(
{
"speaker": segment["speaker"],
"text": "".join(
[chunk["text"] for chunk in transcript[: upto_idx + 1]]
),
"timestamp": (
transcript[0]["timestamp"][0],
transcript[upto_idx]["timestamp"][1],
),
}
)
else:
for i in range(upto_idx + 1):
segmented_preds.append({"speaker": segment["speaker"], **transcript[i]})
# crop the transcripts and timestamp lists according to the latest timestamp (for faster argmin)
transcript = transcript[upto_idx + 1 :]
end_timestamps = end_timestamps[upto_idx + 1 :]
return segmented_preds