Implement Parakeet::Context#initialize
This commit is contained in:
parent
8615ac87ec
commit
fdaf031858
|
|
@ -94,6 +94,10 @@ typedef struct {
|
|||
ruby_whisper_callback_container *abort_callback_container;
|
||||
} ruby_whisper_parakeet_params;
|
||||
|
||||
typedef struct {
|
||||
struct parakeet_context *context;
|
||||
} ruby_whisper_parakeet_context;
|
||||
|
||||
#define GetContext(obj, rw) do { \
|
||||
TypedData_Get_Struct((obj), ruby_whisper, &ruby_whisper_type, (rw)); \
|
||||
if ((rw)->context == NULL) { \
|
||||
|
|
|
|||
|
|
@ -1,9 +1,75 @@
|
|||
#include "ruby_whisper.h"
|
||||
|
||||
extern ID id_to_s;
|
||||
|
||||
extern VALUE ruby_whisper_normalize_model_path(VALUE model_path);
|
||||
|
||||
static void
|
||||
ruby_whisper_parakeet_context_free(void *p)
|
||||
{
|
||||
ruby_whisper_parakeet_context *rwpc = (ruby_whisper_parakeet_context *)p;
|
||||
if (rwpc->context) {
|
||||
parakeet_free(rwpc->context);
|
||||
rwpc->context = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static size_t
|
||||
ruby_whisper_parakeet_context_memsize(const void *p)
|
||||
{
|
||||
ruby_whisper_parakeet_context *rwpc = (ruby_whisper_parakeet_context *)p;
|
||||
if (!rwpc) {
|
||||
return 0;
|
||||
}
|
||||
size_t size = sizeof(*rwpc);
|
||||
return size;
|
||||
}
|
||||
|
||||
static const rb_data_type_t ruby_whisper_parakeet_context_type = {
|
||||
"ruby_whisper_parakeet_context",
|
||||
{0, ruby_whisper_parakeet_context_free, ruby_whisper_parakeet_context_memsize,},
|
||||
0, 0,
|
||||
0
|
||||
};
|
||||
|
||||
static VALUE
|
||||
ruby_whisper_parakeet_context_allocate(VALUE klass)
|
||||
{
|
||||
ruby_whisper_parakeet_context *rwpc;
|
||||
|
||||
VALUE obj = TypedData_Make_Struct(klass, ruby_whisper_parakeet_context, &ruby_whisper_parakeet_context_type, rwpc);
|
||||
rwpc->context = NULL;
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
static VALUE
|
||||
ruby_whisper_parakeet_context_initialize(int argc, VALUE *argv, VALUE self)
|
||||
{
|
||||
ruby_whisper_parakeet_context *rwpc;
|
||||
VALUE model_path;
|
||||
|
||||
rb_scan_args(argc, argv, "1", &model_path);
|
||||
TypedData_Get_Struct(self, ruby_whisper_parakeet_context, &ruby_whisper_parakeet_context_type, rwpc);
|
||||
|
||||
model_path = ruby_whisper_normalize_model_path(model_path);
|
||||
if (!rb_respond_to(model_path, id_to_s)) {
|
||||
rb_raise(rb_eRuntimeError, "Expected file path to model to initialize Parakeet::Context");
|
||||
}
|
||||
rwpc->context = parakeet_init_from_file_with_params(StringValueCStr(model_path), parakeet_context_default_params());
|
||||
if (rwpc->context == NULL) {
|
||||
rb_raise(rb_eRuntimeError, "Failed to load model");
|
||||
}
|
||||
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
void
|
||||
init_ruby_whisper_parakeet_context(VALUE *mParakeet)
|
||||
{
|
||||
VALUE cParakeetContext = rb_define_class_under(*mParakeet, "Context", rb_cObject);
|
||||
|
||||
rb_define_alloc_func(cParakeetContext, ruby_whisper_parakeet_context_allocate);
|
||||
|
||||
rb_define_method(cParakeetContext, "initialize", ruby_whisper_parakeet_context_initialize, -1);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue