Compare commits

...

5 Commits

Author SHA1 Message Date
ed barz 77e743482a README pip link edit 2023-08-22 19:29:47 +02:00
John Ryan 439d49d16d Updated readme 2023-08-14 14:21:52 +01:00
John Ryan 4338423833 Byte convert inside 2023-08-14 14:01:15 +01:00
John Ryan d448db6dc3 Configure language from python 2023-08-14 13:56:36 +01:00
Luke Southam 7af678159c
Update whispercpp.pyx 2023-07-08 15:22:52 +01:00
2 changed files with 12 additions and 11 deletions

View File

@ -1,17 +1,15 @@
Python bindings for whisper.cpp
===============================
<a href="https://www.buymeacoffee.com/lukeFxC" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee" height="41" width="174"></a>
---
`pip install git+https://github.com/stlukey/whispercpp.py`
`pip install git+https://git.brz9.dev/ed/whispercpp.py`
```python
from whispercpp import Whisper
w = Whisper('tiny')
result = w.transcribe("myfile.mp3")
result = w.transcribe("myfile.mp3", lang="en")
text = w.extract_text(result)
```

View File

@ -16,13 +16,12 @@ 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/ggerganov/whisper.cpp/resolve/main/ggml-tiny.bin',
'ggml-base.bin': 'https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-base.bin',
'ggml-small.bin': 'hhttps://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-small.bin',
'ggml-small.bin': 'https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-small.bin',
'ggml-medium.bin': 'https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-medium.bin',
'ggml-large.bin': 'https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-large.bin',
}
@ -68,14 +67,14 @@ cdef cnp.ndarray[cnp.float32_t, ndim=1, mode="c"] load_audio(bytes file, int sr
return frames
cdef whisper_full_params default_params() nogil:
cdef whisper_full_params default_params(char* language) nogil:
cdef whisper_full_params params = whisper_full_default_params(
whisper_sampling_strategy.WHISPER_SAMPLING_GREEDY
)
params.print_realtime = True
params.print_progress = True
params.translate = False
params.language = <const char *> LANGUAGE
params.language = <const char *>language
n_threads = N_THREADS
return params
@ -96,14 +95,13 @@ cdef class Whisper:
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):
def transcribe(self, filename=TEST_FILE, lang="auto"):
print("Loading data..")
if (type(filename) == np.ndarray) :
temp = filename
@ -115,9 +113,14 @@ cdef class Whisper:
cdef cnp.ndarray[cnp.float32_t, ndim=1, mode="c"] frames = temp
language_bytes = str(lang).encode("utf-8")
cdef char* c_string = language_bytes # Convert bytes to char*
params = default_params(c_string)
print("Transcribing..")
return whisper_full(self.ctx, self.params, &frames[0], len(frames))
return whisper_full(self.ctx, params, &frames[0], len(frames))
def extract_text(self, int res):
print("Extracting text...")