From a3a911171ce7836108e598082ccfbcb1653d31ae Mon Sep 17 00:00:00 2001 From: Eren Akbiyik Date: Mon, 21 Nov 2022 17:22:14 +0000 Subject: [PATCH] convert vector to use with C api --- examples/stream/stream.cpp | 3 ++- whisper.cpp | 11 ++++++++--- whisper.h | 5 ++++- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/examples/stream/stream.cpp b/examples/stream/stream.cpp index 2256e69..a5bb692 100644 --- a/examples/stream/stream.cpp +++ b/examples/stream/stream.cpp @@ -345,7 +345,8 @@ int main(int argc, char ** argv) { wparams.audio_ctx = params.audio_ctx; wparams.speed_up = params.speed_up; - wparams.prompt_tokens = &prompt_tokens; + wparams.prompt_tokens = &prompt_tokens[0]; + wparams.prompt_n_tokens = prompt_tokens.size(); if (whisper_full(ctx, wparams, pcmf32.data(), pcmf32.size()) != 0) { fprintf(stderr, "%s: failed to process audio\n", argv[0]); diff --git a/whisper.cpp b/whisper.cpp index 48bdb31..f8c1ada 100644 --- a/whisper.cpp +++ b/whisper.cpp @@ -2413,6 +2413,8 @@ struct whisper_full_params whisper_full_default_params(enum whisper_sampling_str /*.audio_ctx =*/ 0, /*.prompt_tokens =*/ nullptr, + /*.prompt_n_tokens =*/ 0, + /*.language =*/ "en", /*.greedy =*/ { @@ -2457,6 +2459,8 @@ struct whisper_full_params whisper_full_default_params(enum whisper_sampling_str /*.audio_ctx =*/ 0, /*.prompt_tokens =*/ nullptr, + /*.prompt_n_tokens =*/ 0, + /*.language =*/ "en", /*.greedy =*/ { @@ -2588,10 +2592,11 @@ int whisper_full( // Prepend the prompt tokens to the prompt_past if (params.prompt_tokens) { - for (int i = 0; i < (int) params.prompt_tokens->size(); i++) { - prompt_past.push_back((*params.prompt_tokens)[i]); + // Parse tokens from the pointer (it points to an std::vector) + for (int i = 0; i < params.prompt_n_tokens; i++) { + prompt_past.push_back(params.prompt_tokens[i]); } - std::rotate(prompt_past.begin(), prompt_past.end() - params.prompt_tokens->size(), prompt_past.end()); + std::rotate(prompt_past.begin(), prompt_past.end() - params.prompt_n_tokens, prompt_past.end()); } // overwrite audio_ctx diff --git a/whisper.h b/whisper.h index 917b459..ad989e9 100644 --- a/whisper.h +++ b/whisper.h @@ -209,7 +209,10 @@ extern "C" { bool speed_up; // speed-up the audio by 2x using Phase Vocoder int audio_ctx; // overwrite the audio context size (0 = use default) - const std::vector * prompt_tokens; + // std::vector: tokents to provide the whisper model as initial prompt + const whisper_token * prompt_tokens; + int prompt_n_tokens; + const char * language; struct {