Skip to content

Commit

Permalink
whisper : add whisper_full_lang_id() for getting the context lang (gg…
Browse files Browse the repository at this point in the history
  • Loading branch information
kamranjon authored Feb 5, 2023
1 parent 29dd47e commit 0ed4a24
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
9 changes: 8 additions & 1 deletion whisper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,8 @@ struct whisper_context {

mutable std::mt19937 rng; // used for sampling at t > 0.0

int lang_id;

// [EXPERIMENTAL] token-level timestamps data
int64_t t_beg;
int64_t t_last;
Expand Down Expand Up @@ -3478,7 +3480,7 @@ int whisper_full(
fprintf(stderr, "%s: failed to auto-detect language\n", __func__);
return -3;
}

ctx->lang_id = lang_id;
params.language = whisper_lang_str(lang_id);

fprintf(stderr, "%s: auto-detected language: %s (p = %f)\n", __func__, params.language, probs[whisper_lang_id(params.language)]);
Expand Down Expand Up @@ -3575,6 +3577,7 @@ int whisper_full(
std::vector<whisper_token> prompt_init = { whisper_token_sot(ctx) };
if (whisper_is_multilingual(ctx)) {
const int lang_id = whisper_lang_id(params.language);
ctx->lang_id = lang_id;
prompt_init.push_back(whisper_token_lang(ctx, lang_id));
if (params.translate) {
prompt_init.push_back(whisper_token_translate());
Expand Down Expand Up @@ -4295,6 +4298,10 @@ int whisper_full_n_segments(struct whisper_context * ctx) {
return ctx->result_all.size();
}

int whisper_full_lang_id(struct whisper_context * ctx) {
return ctx->lang_id;
}

int64_t whisper_full_get_segment_t0(struct whisper_context * ctx, int i_segment) {
return ctx->result_all[i_segment].t0;
}
Expand Down
3 changes: 3 additions & 0 deletions whisper.h
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,9 @@ extern "C" {
// A segment can be a few words, a sentence, or even a paragraph.
WHISPER_API int whisper_full_n_segments(struct whisper_context * ctx);

// Language id associated with the current context
WHISPER_API int whisper_full_lang_id(struct whisper_context * ctx);

// Get the start and end time of the specified segment.
WHISPER_API int64_t whisper_full_get_segment_t0(struct whisper_context * ctx, int i_segment);
WHISPER_API int64_t whisper_full_get_segment_t1(struct whisper_context * ctx, int i_segment);
Expand Down

0 comments on commit 0ed4a24

Please sign in to comment.