Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Language Configuration #23

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ from whispercpp import Whisper

w = Whisper('tiny')

result = w.transcribe("myfile.mp3")
result = w.transcribe("myfile.mp3", lang="en")
text = w.extract_text(result)
```

Expand Down
15 changes: 9 additions & 6 deletions whispercpp.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ 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 = {
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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...")
Expand Down