From 0f46aa84b1c3a74e32b4c9615b820785705b58bf Mon Sep 17 00:00:00 2001 From: Syahmi Azhar Date: Sun, 1 Jan 2023 20:53:39 +0800 Subject: [PATCH] whisper : add whisper_init_from_buffer --- whisper.cpp | 37 ++++++++++++++++++++++++++++++++++++- whisper.h | 1 + 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/whisper.cpp b/whisper.cpp index f7ddfe3..51449fb 100644 --- a/whisper.cpp +++ b/whisper.cpp @@ -2233,7 +2233,7 @@ static std::vector tokenize(const whisper_vocab & vocab, cons // struct whisper_context * whisper_init_from_file(const char * path_model) { - whisper_model_loader loader; + whisper_model_loader loader = {}; fprintf(stderr, "%s: loading model from '%s'\n", __func__, path_model); @@ -2263,6 +2263,41 @@ struct whisper_context * whisper_init_from_file(const char * path_model) { return whisper_init(&loader); } +struct whisper_context * whisper_init_from_buffer(void * buffer, size_t buffer_size) { + struct buf_context { + uint8_t* buffer; + size_t size; + size_t current_offset; + }; + + buf_context ctx = { reinterpret_cast(buffer), buffer_size, 0 }; + whisper_model_loader loader = {}; + + fprintf(stderr, "%s: loading model from buffer\n", __func__); + + loader.context = &ctx; + loader.read = [](void * ctx, void * output, size_t read_size) { + buf_context * buf = reinterpret_cast(ctx); + + size_t size_to_copy = buf->current_offset + read_size < buf->size ? read_size : buf->size - buf->current_offset; + + memcpy(output, buf->buffer + buf->current_offset, size_to_copy); + buf->current_offset += size_to_copy; + + return size_to_copy; + }; + + loader.eof = [](void * ctx) { + buf_context * buf = reinterpret_cast(ctx); + + return buf->current_offset >= buf->size; + }; + + loader.close = [](void * ctx) { }; + + return whisper_init(&loader); +} + struct whisper_context * whisper_init(struct whisper_model_loader * loader) { ggml_time_init(); diff --git a/whisper.h b/whisper.h index 0d9c1cc..f38d3aa 100644 --- a/whisper.h +++ b/whisper.h @@ -95,6 +95,7 @@ extern "C" { // Allocates all memory needed for the model and loads the model from the given file. // Returns NULL on failure. WHISPER_API struct whisper_context * whisper_init_from_file(const char * path_model); + WHISPER_API struct whisper_context * whisper_init_from_buffer(void * buffer, size_t buffer_size); WHISPER_API struct whisper_context * whisper_init(struct whisper_model_loader * loader); // Frees all memory allocated by the model.