forked from stlukey/whispercpp.py
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwhispercpp.pyx
More file actions
179 lines (148 loc) · 6.3 KB
/
whispercpp.pyx
File metadata and controls
179 lines (148 loc) · 6.3 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
#!python
# cython: language_level=3
import ffmpeg
import numpy as np
import requests
import os
from pathlib import Path
MODELS_DIR = os.environ.get("WHISPERCPP_CACHE", str(Path('~/.ggml-models').expanduser()))
print(f"MODELS_DIR: {MODELS_DIR}")
cimport numpy as cnp
cdef int SAMPLE_RATE = 16000
cdef char* TEST_FILE = 'test.wav'
cdef char* DEFAULT_MODEL = 'tiny'
cdef char* LANGUAGE = b'en'
cdef int N_THREADS = os.cpu_count()
MODELS = {
'ggml-tiny.bin': 'https://huggingface.co/datasets/ggerganov/whisper.cpp/resolve/main/ggml-tiny.bin',
'ggml-tiny.en.bin': 'https://huggingface.co/datasets/ggerganov/whisper.cpp/resolve/main/ggml-tiny.en.bin',
'ggml-base.bin': 'https://huggingface.co/datasets/ggerganov/whisper.cpp/resolve/main/ggml-base.bin',
'ggml-base.en.bin': 'https://huggingface.co/datasets/ggerganov/whisper.cpp/resolve/main/ggml-base.en.bin',
'ggml-small.bin': 'https://huggingface.co/datasets/ggerganov/whisper.cpp/resolve/main/ggml-small.bin',
'ggml-small.en.bin': 'https://huggingface.co/datasets/ggerganov/whisper.cpp/resolve/main/ggml-small.en.bin',
'ggml-medium.bin': 'https://huggingface.co/datasets/ggerganov/whisper.cpp/resolve/main/ggml-medium.bin',
'ggml-medium.en.bin': 'https://huggingface.co/datasets/ggerganov/whisper.cpp/resolve/main/ggml-medium.en.bin',
'ggml-large.bin': 'https://huggingface.co/datasets/ggerganov/whisper.cpp/resolve/main/ggml-large.bin',
}
def model_exists(model):
return os.path.exists(Path(MODELS_DIR).joinpath(model))
def download_model(model):
if model_exists(model):
return
print(f'Downloading {model}...')
url = MODELS[model]
r = requests.get(url, allow_redirects=True)
os.makedirs(MODELS_DIR, exist_ok=True)
with open(Path(MODELS_DIR).joinpath(model), 'wb') as f:
f.write(r.content)
cdef cnp.ndarray[cnp.float32_t, ndim=1, mode="c"] load_audio(bytes file, int sr = SAMPLE_RATE):
try:
out, _ = (
ffmpeg.input(file, threads=0)
.output(
"-", format="s16le",
acodec="pcm_s16le",
ac=1, ar=sr
)
.run(
cmd=["ffmpeg", "-nostdin"],
capture_stdout=True,
capture_stderr=True
)
)
except ffmpeg.Error as e:
raise RuntimeError(f"failed to load audio: {e.stderr.decode('utf8')}") from e
cdef cnp.ndarray[cnp.float32_t, ndim=1, mode="c"] frames = (
np.frombuffer(out, np.int16)
.flatten()
.astype(np.float32)
) / pow(2, 15)
return frames
cdef cnp.ndarray[cnp.float32_t, ndim=1, mode="c"] load_audio_segment(bytes file, float start = 0, float end = 0, int sr = SAMPLE_RATE):
if start >= end:
raise ValueError("start must be less than end")
try:
out, _ = (
ffmpeg.input(file, threads=0)
.filter('atrim', start=start, end=end)
.filter('asetpts', 'PTS-STARTPTS')
.output(
"-", format="s16le",
acodec="pcm_s16le",
ac=1, ar=sr
)
.run(
cmd=["ffmpeg", "-nostdin"],
capture_stdout=True,
capture_stderr=True
)
)
except ffmpeg.Error as e:
raise RuntimeError(f"failed to load audio: {e.stderr.decode('utf8')}") from e
cdef cnp.ndarray[cnp.float32_t, ndim=1, mode="c"] frames = (
np.frombuffer(out, np.int16)
.flatten()
.astype(np.float32)
) / pow(2, 15)
return frames
cdef whisper_full_params default_params() nogil:
cdef whisper_full_params params = whisper_full_default_params(
whisper_sampling_strategy.WHISPER_SAMPLING_GREEDY
)
params.print_realtime = False
params.print_progress = False
params.translate = False
params.language = <const char *> LANGUAGE
n_threads = N_THREADS
return params
cdef class Whisper:
cdef whisper_context * ctx
cdef whisper_full_params params
def __init__(self, model=DEFAULT_MODEL, pb=None):
model_fullname = f'ggml-{model}.bin'
download_model(model_fullname)
model_path = Path(MODELS_DIR).joinpath(model_fullname)
cdef bytes model_b = str(model_path).encode('utf8')
self.ctx = whisper_init(model_b)
self.params = default_params()
def __dealloc__(self):
whisper_free(self.ctx)
def transcribe_segment(self, filename, start, end):
if start >= end:
raise ValueError("start must be less than end")
print("Loading data..")
cdef cnp.ndarray[cnp.float32_t, ndim=1, mode="c"] frames = load_audio_segment(<bytes>filename, start, end)
print("Transcribing..")
return whisper_full(self.ctx, self.params, &frames[0], len(frames))
def transcribe(self, filename=TEST_FILE):
print("Loading data..")
cdef cnp.ndarray[cnp.float32_t, ndim=1, mode="c"] frames = load_audio(<bytes>filename)
print("Transcribing..")
return whisper_full(self.ctx, self.params, &frames[0], len(frames))
def extract_segment(self, int res):
print("Extracting segments...")
if res != 0:
raise RuntimeError
cdef int n_segments = whisper_full_n_segments(self.ctx)
segments = []
for i in range(n_segments):
text = whisper_full_get_segment_text(self.ctx, i).decode()
start = float(whisper_full_get_segment_t0(self.ctx, i)) / 100
end = float(whisper_full_get_segment_t1(self.ctx, i)) / 100
segments.append({ 'text': text, 'start': start, 'end': end })
language = <bytes>self.params.language
if len(segments) == 0:
print(f"Warning: no segments found.")
return { 'text': '', 'start': 0, 'end': 0, 'segments': [], 'language': language.decode()}
text = ''.join([s['text'] for s in segments])
start = segments[0]['start']
end = segments[-1]['end']
return { 'text': text, 'start': start, 'end': end, 'segments': segments, 'language': language.decode() }
def extract_text(self, int res):
print("Extracting text...")
if res != 0:
raise RuntimeError
cdef int n_segments = whisper_full_n_segments(self.ctx)
return [
whisper_full_get_segment_text(self.ctx, i).decode() for i in range(n_segments)
]