whisper : add whisper_init_from_buffer

pull/353/head
Syahmi Azhar 3 years ago committed by Georgi Gerganov
parent 878a84d78f
commit 0f46aa84b1
No known key found for this signature in database
GPG Key ID: 449E073F9DC10735

@ -2233,7 +2233,7 @@ static std::vector<whisper_vocab::id> 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<uint8_t*>(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<buf_context *>(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<buf_context *>(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();

@ -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.

Loading…
Cancel
Save