Commit
·
7dec787
1
Parent(s):
950d3b0
up
Browse files- run_whisper_streaming.py +68 -0
run_whisper_streaming.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python3
|
2 |
+
from transformers import (
|
3 |
+
WhisperForConditionalGeneration,
|
4 |
+
WhisperProcessor,
|
5 |
+
)
|
6 |
+
import torch
|
7 |
+
import re
|
8 |
+
import numpy as np
|
9 |
+
from datasets import load_dataset
|
10 |
+
|
11 |
+
device = "cpu"
|
12 |
+
dtype = torch.float32
|
13 |
+
|
14 |
+
processor = WhisperProcessor.from_pretrained("openai/whisper-tiny")
|
15 |
+
model = WhisperForConditionalGeneration.from_pretrained(
|
16 |
+
"openai/whisper-tiny", low_cpu_mem_usage=True, torch_dtype=dtype
|
17 |
+
)
|
18 |
+
model.to(device)
|
19 |
+
|
20 |
+
STREAMING_INTERVAL = 0.33 # in seconds
|
21 |
+
SAMPLING_RATE = 16_000
|
22 |
+
INTERVAL_LENGTH = int(STREAMING_INTERVAL * SAMPLING_RATE)
|
23 |
+
|
24 |
+
|
25 |
+
ds = load_dataset(
|
26 |
+
"hf-internal-testing/librispeech_asr_dummy", "clean", split="validation"
|
27 |
+
)
|
28 |
+
audio_array = np.concatenate([x["array"] for x in ds["audio"]])
|
29 |
+
|
30 |
+
# fake streaming by decoding every STREAMING_INTERVAL seconds
|
31 |
+
start_idx = 0
|
32 |
+
fully_decoded = ""
|
33 |
+
for end_idx in range(INTERVAL_LENGTH, audio_array.shape[-1], INTERVAL_LENGTH):
|
34 |
+
input_audio = audio_array[start_idx:end_idx]
|
35 |
+
|
36 |
+
processor_kwargs = (
|
37 |
+
{"padding": "longest", "truncation": False, "return_attention_mask": True}
|
38 |
+
if input_audio.shape[0] / SAMPLING_RATE > 30.0
|
39 |
+
else {}
|
40 |
+
)
|
41 |
+
inputs = processor(
|
42 |
+
input_audio,
|
43 |
+
sampling_rate=SAMPLING_RATE,
|
44 |
+
return_tensors="pt",
|
45 |
+
**processor_kwargs,
|
46 |
+
)
|
47 |
+
inputs = inputs.to(dtype=dtype, device=device)
|
48 |
+
tokens = model.generate(
|
49 |
+
**inputs,
|
50 |
+
return_timestamps=True,
|
51 |
+
)
|
52 |
+
|
53 |
+
sequences = processor.batch_decode(tokens, decode_with_timestamps=True)[0]
|
54 |
+
sequences_no_special = processor.batch_decode(tokens, skip_special_tokens=True)[0]
|
55 |
+
|
56 |
+
regex_search = re.findall(r"<\|[\d\.]+\|><\|[\d\.]+\|>", sequences)
|
57 |
+
regex_split = re.split(r"<\|[\d\.]+\|><\|[\d\.]+\|>", sequences)
|
58 |
+
|
59 |
+
# at least two timestamps seperations and 5 new words have to have been detected to cut input audio
|
60 |
+
if len(regex_search) > 1 and len("".join(regex_split[1:]).split()) > 5:
|
61 |
+
cut_idx = int(SAMPLING_RATE * float(regex_search[0].split("|><|")[0][2:]))
|
62 |
+
|
63 |
+
start_idx += cut_idx
|
64 |
+
fully_decoded += sequences_no_special
|
65 |
+
sequences_no_special = ""
|
66 |
+
|
67 |
+
print(fully_decoded + sequences_no_special)
|
68 |
+
print(f"Passed time: {end_idx / 16_000}")
|