Skip to content

Commit 673e33a

Browse files
committed
Add Whisper::Model class
1 parent 5e17987 commit 673e33a

File tree

1 file changed

+165
-0
lines changed

1 file changed

+165
-0
lines changed

bindings/ruby/ext/ruby_whisper.cpp

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,7 +1135,12 @@ typedef struct {
11351135
int index;
11361136
} ruby_whisper_segment;
11371137

1138+
typedef struct {
1139+
whisper_context *context;
1140+
} ruby_whisper_model;
1141+
11381142
VALUE cSegment;
1143+
VALUE cModel;
11391144

11401145
static void rb_whisper_segment_mark(ruby_whisper_segment *rws) {
11411146
rb_gc_mark(rws->context);
@@ -1308,6 +1313,150 @@ static VALUE ruby_whisper_segment_get_text(VALUE self) {
13081313
return rb_str_new2(text);
13091314
}
13101315

1316+
static VALUE ruby_whisper_model_allocate(VALUE klass) {
1317+
ruby_whisper_model *rwm;
1318+
rwm = ALLOC(ruby_whisper_model);
1319+
return Data_Wrap_Struct(klass, NULL, RUBY_DEFAULT_FREE, rwm);
1320+
}
1321+
1322+
static VALUE rb_whisper_model_initialize(whisper_context *context) {
1323+
ruby_whisper_model *rwm;
1324+
const VALUE model = ruby_whisper_model_allocate(cModel);
1325+
Data_Get_Struct(model, ruby_whisper_model, rwm);
1326+
rwm->context = context;
1327+
return model;
1328+
};
1329+
1330+
/*
1331+
* call-seq:
1332+
* model -> Whisper::Model
1333+
*/
1334+
static VALUE ruby_whisper_get_model(VALUE self) {
1335+
ruby_whisper *rw;
1336+
Data_Get_Struct(self, ruby_whisper, rw);
1337+
return rb_whisper_model_initialize(rw->context);
1338+
}
1339+
1340+
/*
1341+
* call-seq:
1342+
* n_vocab -> Integer
1343+
*/
1344+
static VALUE ruby_whisper_c_model_n_vocab(VALUE self) {
1345+
ruby_whisper_model *rwm;
1346+
Data_Get_Struct(self, ruby_whisper_model, rwm);
1347+
return INT2NUM(whisper_model_n_vocab(rwm->context));
1348+
}
1349+
1350+
/*
1351+
* call-seq:
1352+
* n_audio_ctx -> Integer
1353+
*/
1354+
static VALUE ruby_whisper_c_model_n_audio_ctx(VALUE self) {
1355+
ruby_whisper_model *rwm;
1356+
Data_Get_Struct(self, ruby_whisper_model, rwm);
1357+
return INT2NUM(whisper_model_n_audio_ctx(rwm->context));
1358+
}
1359+
1360+
/*
1361+
* call-seq:
1362+
* n_audio_state -> Integer
1363+
*/
1364+
static VALUE ruby_whisper_c_model_n_audio_state(VALUE self) {
1365+
ruby_whisper_model *rwm;
1366+
Data_Get_Struct(self, ruby_whisper_model, rwm);
1367+
return INT2NUM(whisper_model_n_audio_state(rwm->context));
1368+
}
1369+
1370+
/*
1371+
* call-seq:
1372+
* n_audio_head -> Integer
1373+
*/
1374+
static VALUE ruby_whisper_c_model_n_audio_head(VALUE self) {
1375+
ruby_whisper_model *rwm;
1376+
Data_Get_Struct(self, ruby_whisper_model, rwm);
1377+
return INT2NUM(whisper_model_n_audio_head(rwm->context));
1378+
}
1379+
1380+
/*
1381+
* call-seq:
1382+
* n_audio_layer -> Integer
1383+
*/
1384+
static VALUE ruby_whisper_c_model_n_audio_layer(VALUE self) {
1385+
ruby_whisper_model *rwm;
1386+
Data_Get_Struct(self, ruby_whisper_model, rwm);
1387+
return INT2NUM(whisper_model_n_audio_layer(rwm->context));
1388+
}
1389+
1390+
/*
1391+
* call-seq:
1392+
* n_text_ctx -> Integer
1393+
*/
1394+
static VALUE ruby_whisper_c_model_n_text_ctx(VALUE self) {
1395+
ruby_whisper_model *rwm;
1396+
Data_Get_Struct(self, ruby_whisper_model, rwm);
1397+
return INT2NUM(whisper_model_n_text_ctx(rwm->context));
1398+
}
1399+
1400+
/*
1401+
* call-seq:
1402+
* n_text_state -> Integer
1403+
*/
1404+
static VALUE ruby_whisper_c_model_n_text_state(VALUE self) {
1405+
ruby_whisper_model *rwm;
1406+
Data_Get_Struct(self, ruby_whisper_model, rwm);
1407+
return INT2NUM(whisper_model_n_text_state(rwm->context));
1408+
}
1409+
1410+
/*
1411+
* call-seq:
1412+
* n_text_head -> Integer
1413+
*/
1414+
static VALUE ruby_whisper_c_model_n_text_head(VALUE self) {
1415+
ruby_whisper_model *rwm;
1416+
Data_Get_Struct(self, ruby_whisper_model, rwm);
1417+
return INT2NUM(whisper_model_n_text_head(rwm->context));
1418+
}
1419+
1420+
/*
1421+
* call-seq:
1422+
* n_text_layer -> Integer
1423+
*/
1424+
static VALUE ruby_whisper_c_model_n_text_layer(VALUE self) {
1425+
ruby_whisper_model *rwm;
1426+
Data_Get_Struct(self, ruby_whisper_model, rwm);
1427+
return INT2NUM(whisper_model_n_text_layer(rwm->context));
1428+
}
1429+
1430+
/*
1431+
* call-seq:
1432+
* n_mels -> Integer
1433+
*/
1434+
static VALUE ruby_whisper_c_model_n_mels(VALUE self) {
1435+
ruby_whisper_model *rwm;
1436+
Data_Get_Struct(self, ruby_whisper_model, rwm);
1437+
return INT2NUM(whisper_model_n_mels(rwm->context));
1438+
}
1439+
1440+
/*
1441+
* call-seq:
1442+
* ftype -> Integer
1443+
*/
1444+
static VALUE ruby_whisper_c_model_ftype(VALUE self) {
1445+
ruby_whisper_model *rwm;
1446+
Data_Get_Struct(self, ruby_whisper_model, rwm);
1447+
return INT2NUM(whisper_model_ftype(rwm->context));
1448+
}
1449+
1450+
/*
1451+
* call-seq:
1452+
* type -> String
1453+
*/
1454+
static VALUE ruby_whisper_c_model_type(VALUE self) {
1455+
ruby_whisper_model *rwm;
1456+
Data_Get_Struct(self, ruby_whisper_model, rwm);
1457+
return rb_str_new2(whisper_model_type_readable(rwm->context));
1458+
}
1459+
13111460
void Init_whisper() {
13121461
id_to_s = rb_intern("to_s");
13131462
id_call = rb_intern("call");
@@ -1416,6 +1565,22 @@ void Init_whisper() {
14161565
rb_define_method(cSegment, "end_time", ruby_whisper_segment_get_end_time, 0);
14171566
rb_define_method(cSegment, "speaker_next_turn?", ruby_whisper_segment_get_speaker_turn_next, 0);
14181567
rb_define_method(cSegment, "text", ruby_whisper_segment_get_text, 0);
1568+
1569+
cModel = rb_define_class_under(mWhisper, "Model", rb_cObject);
1570+
rb_define_alloc_func(cModel, ruby_whisper_model_allocate);
1571+
rb_define_method(cContext, "model", ruby_whisper_get_model, 0);
1572+
rb_define_method(cModel, "n_vocab", ruby_whisper_c_model_n_vocab, 0);
1573+
rb_define_method(cModel, "n_audio_ctx", ruby_whisper_c_model_n_audio_ctx, 0);
1574+
rb_define_method(cModel, "n_audio_state", ruby_whisper_c_model_n_audio_state, 0);
1575+
rb_define_method(cModel, "n_audio_head", ruby_whisper_c_model_n_audio_head, 0);
1576+
rb_define_method(cModel, "n_audio_layer", ruby_whisper_c_model_n_audio_layer, 0);
1577+
rb_define_method(cModel, "n_text_ctx", ruby_whisper_c_model_n_text_ctx, 0);
1578+
rb_define_method(cModel, "n_text_state", ruby_whisper_c_model_n_text_state, 0);
1579+
rb_define_method(cModel, "n_text_head", ruby_whisper_c_model_n_text_head, 0);
1580+
rb_define_method(cModel, "n_text_layer", ruby_whisper_c_model_n_text_layer, 0);
1581+
rb_define_method(cModel, "n_mels", ruby_whisper_c_model_n_mels, 0);
1582+
rb_define_method(cModel, "ftype", ruby_whisper_c_model_ftype, 0);
1583+
rb_define_method(cModel, "type", ruby_whisper_c_model_type, 0);
14191584
}
14201585
#ifdef __cplusplus
14211586
}

0 commit comments

Comments
 (0)