Added list_languages function

This commit is contained in:
lightmare 2023-02-23 21:39:05 +00:00
parent b0795baa43
commit 5a4f948785
2 changed files with 18 additions and 0 deletions

View File

@ -80,6 +80,8 @@ cdef extern from "whisper.h" nogil:
cdef whisper_token_data whisper_sample_best(whisper_context*)
cdef whisper_token whisper_sample_timestamp(whisper_context*)
cdef int whisper_lang_id(char*)
cdef int whisper_lang_max_id()
const char* whisper_lang_str(int)
cdef int whisper_n_len(whisper_context*)
cdef int whisper_n_vocab(whisper_context*)
cdef int whisper_n_text_ctx(whisper_context*)

View File

@ -61,6 +61,22 @@ def download_model(model, models_dir=MODELS_DIR):
f.write(r.read())
def list_languages():
"""Returns a list of tuples of language codes understood by whisper.cpp.
Returns:
e.g. [(0, "en"), (1, "zh"), ...]
"""
cdef int max_id = whisper_lang_max_id() + 1
cdef list results = []
for i in range(max_id):
results.append((
i,
whisper_lang_str(i).decode()
))
return results
cdef cnp.ndarray[cnp.float32_t, ndim=1, mode="c"] load_audio(bytes file, int sr = SAMPLE_RATE):
try:
out = (