added buffer support

This commit is contained in:
aidan perkins 2023-04-07 20:09:10 -05:00
parent e2581c8aad
commit a0337b36a1
6 changed files with 10016 additions and 15 deletions

0
python3.11 Normal file
View File

View File

@ -8,20 +8,20 @@ if sys.platform == 'darwin':
os.environ['CXXFLAGS'] = '-DGGML_USE_ACCELERATE -O3 -std=c++11'
os.environ['LDFLAGS'] = '-framework Accelerate'
else:
os.environ['CFLAGS'] = '-mavx -mavx2 -mfma -mf16c -O3 -std=gnu11'
os.environ['CXXFLAGS'] = '-mavx -mavx2 -mfma -mf16c -O3 -std=c++11'
os.environ['CFLAGS'] = '-mavx -mavx2 -mfma -mf16c -O3 /std:c++14'
os.environ['CXXFLAGS'] = '-mavx -mavx2 -mfma -mf16c -O3 /std:c++14'
ext_modules = [
Extension(
name="whispercpp",
sources=["whispercpp.pyx", "whisper.cpp/whisper.cpp"],
sources=["C:\\Users\\aidan\\Desktop\\Code\\projectMesa\\whispercpp.py\\whispercpp.pyx", "C:\\Users\\aidan\\Desktop\\Code\\projectMesa\\whispercpp.py\\whisper.cpp/whisper.cpp"],
language="c++",
extra_compile_args=["-std=c++11"],
)
extra_compile_args=["/std:c++14"],
)
]
ext_modules = cythonize(ext_modules)
whisper_clib = ('whisper_clib', {'sources': ['whisper.cpp/ggml.c']})
whisper_clib = ('whisper_clib', {'sources': ['C:\\Users\\aidan\\Desktop\\Code\\projectMesa\\whispercpp.py\\whisper.cpp/ggml.c']})
setup(
name='whispercpp',
@ -30,8 +30,8 @@ setup(
author='Luke Southam',
author_email='luke@devthe.com',
libraries=[whisper_clib],
ext_modules = cythonize("whispercpp.pyx"),
include_dirs = ['./whisper.cpp/', numpy.get_include()],
ext_modules=cythonize(ext_modules),
include_dirs=['C:\\Users\\aidan\\Desktop\\Code\\projectMesa\\whispercpp.py\\whisper.cpp', numpy.get_include()],
install_requires=[
'numpy',
'ffmpeg-python',

@ -1 +1 @@
Subproject commit 1d716d6e34f3f4ba57bd9706a9258a0bdb008153
Subproject commit 0a2d1210bcb98978214bbf4e100922a413afd39d

9984
whispercpp.cpp Normal file

File diff suppressed because it is too large Load Diff

View File

@ -71,7 +71,8 @@ cdef extern from "whisper.h" nogil:
whisper_encoder_begin_callback encoder_begin_callback
void* encoder_begin_callback_user_data
whisper_full_params whisper_full_default_params(whisper_sampling_strategy)
cdef whisper_context* whisper_init(char*)
cdef whisper_context* whisper_init_from_file(char*)
cdef whisper_context* whisper_init_from_buffer(voidptr, int)
cdef void whisper_free(whisper_context*)
cdef int whisper_pcm_to_mel(whisper_context*, float*, int, int)
cdef int whisper_set_mel(whisper_context*, float*, int, int)

View File

@ -16,7 +16,7 @@ cimport numpy as cnp
cdef int SAMPLE_RATE = 16000
cdef char* TEST_FILE = 'test.wav'
cdef char* DEFAULT_MODEL = 'tiny'
cdef char* LANGUAGE = b'fr'
cdef char* LANGUAGE = b'en'
cdef int N_THREADS = os.cpu_count()
MODELS = {
@ -84,21 +84,37 @@ 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'.encode('utf8')
def __init__(self, model=DEFAULT_MODEL, pb=None, buf=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)
if buf is not None:
self.ctx = whisper_init_from_buffer(buf, buf.size)
else:
self.ctx = whisper_init_from_file(model_b)
self.params = default_params()
whisper_print_system_info()
def __dealloc__(self):
whisper_free(self.ctx)
def transcribe(self, filename=TEST_FILE):
print("Loading data..")
cdef cnp.ndarray[cnp.float32_t, ndim=1, mode="c"] frames = load_audio(<bytes>filename)
if (type(filename) == np.ndarray) :
temp = filename
elif (type(filename) == str) :
temp = load_audio(<bytes>filename)
else :
temp = load_audio(<bytes>TEST_FILE)
cdef cnp.ndarray[cnp.float32_t, ndim=1, mode="c"] frames = temp
print("Transcribing..")
return whisper_full(self.ctx, self.params, &frames[0], len(frames))