Spaces:
Running
Running
ruby : bug fix on callbacks and no_speech_prob (#2656)
Browse files* Don't generate documentation on test
* Move .startup to TestBase class
* Extract new_segment_callback as a function
* Extract progress_callback as a function
* Extract abort_callback as a function
* Extract register_callbacks as a function
* Call callbacks in Whiser::Context#full and #full_parallel
* Fix README
* Care about the cases content-size is nil and TTY is not available
* Add tests for no_speech_prob
* Add Whisper::Context#full_get_segment_no_speech_prob and Whisper::Segment#no_speech_prob
bindings/ruby/README.md
CHANGED
|
@@ -63,7 +63,7 @@ whisper = Whisper::Context.new("base.en")
|
|
| 63 |
You can see the list of prepared model names by `Whisper::Model.preconverted_models.keys`:
|
| 64 |
|
| 65 |
```ruby
|
| 66 |
-
puts Whisper::Model.
|
| 67 |
# tiny
|
| 68 |
# tiny.en
|
| 69 |
# tiny-q5_1
|
|
@@ -220,7 +220,7 @@ whisper.each_segment do |segment|
|
|
| 220 |
end
|
| 221 |
```
|
| 222 |
|
| 223 |
-
The second argument `samples` may be an array, an object with `length` method, or a MemoryView. If you can prepare audio data as C array and export it as a MemoryView, whispercpp accepts and works with it with zero copy.
|
| 224 |
|
| 225 |
License
|
| 226 |
-------
|
|
|
|
| 63 |
You can see the list of prepared model names by `Whisper::Model.preconverted_models.keys`:
|
| 64 |
|
| 65 |
```ruby
|
| 66 |
+
puts Whisper::Model.preconverted_models.keys
|
| 67 |
# tiny
|
| 68 |
# tiny.en
|
| 69 |
# tiny-q5_1
|
|
|
|
| 220 |
end
|
| 221 |
```
|
| 222 |
|
| 223 |
+
The second argument `samples` may be an array, an object with `length` and `each` method, or a MemoryView. If you can prepare audio data as C array and export it as a MemoryView, whispercpp accepts and works with it with zero copy.
|
| 224 |
|
| 225 |
License
|
| 226 |
-------
|
bindings/ruby/ext/ruby_whisper.cpp
CHANGED
|
@@ -53,6 +53,9 @@ static ID id_pre_converted_models;
|
|
| 53 |
|
| 54 |
static bool is_log_callback_finalized = false;
|
| 55 |
|
|
|
|
|
|
|
|
|
|
| 56 |
/*
|
| 57 |
* call-seq:
|
| 58 |
* lang_max_id -> Integer
|
|
@@ -187,6 +190,69 @@ static ruby_whisper_callback_container * rb_whisper_callback_container_allocate(
|
|
| 187 |
return container;
|
| 188 |
}
|
| 189 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
static VALUE ruby_whisper_params_allocate(VALUE klass) {
|
| 191 |
ruby_whisper_params *rwp;
|
| 192 |
rwp = ALLOC(ruby_whisper_params);
|
|
@@ -230,8 +296,25 @@ static VALUE ruby_whisper_initialize(int argc, VALUE *argv, VALUE self) {
|
|
| 230 |
return self;
|
| 231 |
}
|
| 232 |
|
| 233 |
-
|
| 234 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
|
| 236 |
/*
|
| 237 |
* transcribe a single file
|
|
@@ -353,80 +436,7 @@ static VALUE ruby_whisper_transcribe(int argc, VALUE *argv, VALUE self) {
|
|
| 353 |
rwp->params.encoder_begin_callback_user_data = &is_aborted;
|
| 354 |
}
|
| 355 |
|
| 356 |
-
|
| 357 |
-
rwp->params.new_segment_callback = [](struct whisper_context * ctx, struct whisper_state * state, int n_new, void * user_data) {
|
| 358 |
-
const ruby_whisper_callback_container *container = (ruby_whisper_callback_container *)user_data;
|
| 359 |
-
|
| 360 |
-
// Currently, doesn't support state because
|
| 361 |
-
// those require to resolve GC-related problems.
|
| 362 |
-
if (!NIL_P(container->callback)) {
|
| 363 |
-
rb_funcall(container->callback, id_call, 4, *container->context, Qnil, INT2NUM(n_new), container->user_data);
|
| 364 |
-
}
|
| 365 |
-
const long callbacks_len = RARRAY_LEN(container->callbacks);
|
| 366 |
-
if (0 == callbacks_len) {
|
| 367 |
-
return;
|
| 368 |
-
}
|
| 369 |
-
const int n_segments = whisper_full_n_segments_from_state(state);
|
| 370 |
-
for (int i = n_new; i > 0; i--) {
|
| 371 |
-
int i_segment = n_segments - i;
|
| 372 |
-
VALUE segment = rb_whisper_segment_initialize(*container->context, i_segment);
|
| 373 |
-
for (int j = 0; j < callbacks_len; j++) {
|
| 374 |
-
VALUE cb = rb_ary_entry(container->callbacks, j);
|
| 375 |
-
rb_funcall(cb, id_call, 1, segment);
|
| 376 |
-
}
|
| 377 |
-
}
|
| 378 |
-
};
|
| 379 |
-
rwp->new_segment_callback_container->context = &self;
|
| 380 |
-
rwp->params.new_segment_callback_user_data = rwp->new_segment_callback_container;
|
| 381 |
-
}
|
| 382 |
-
|
| 383 |
-
if (!NIL_P(rwp->progress_callback_container->callback) || 0 != RARRAY_LEN(rwp->progress_callback_container->callbacks)) {
|
| 384 |
-
rwp->params.progress_callback = [](struct whisper_context *ctx, struct whisper_state * /*state*/, int progress_cur, void *user_data) {
|
| 385 |
-
const ruby_whisper_callback_container *container = (ruby_whisper_callback_container *)user_data;
|
| 386 |
-
const VALUE progress = INT2NUM(progress_cur);
|
| 387 |
-
// Currently, doesn't support state because
|
| 388 |
-
// those require to resolve GC-related problems.
|
| 389 |
-
if (!NIL_P(container->callback)) {
|
| 390 |
-
rb_funcall(container->callback, id_call, 4, *container->context, Qnil, progress, container->user_data);
|
| 391 |
-
}
|
| 392 |
-
const long callbacks_len = RARRAY_LEN(container->callbacks);
|
| 393 |
-
if (0 == callbacks_len) {
|
| 394 |
-
return;
|
| 395 |
-
}
|
| 396 |
-
for (int j = 0; j < callbacks_len; j++) {
|
| 397 |
-
VALUE cb = rb_ary_entry(container->callbacks, j);
|
| 398 |
-
rb_funcall(cb, id_call, 1, progress);
|
| 399 |
-
}
|
| 400 |
-
};
|
| 401 |
-
rwp->progress_callback_container->context = &self;
|
| 402 |
-
rwp->params.progress_callback_user_data = rwp->progress_callback_container;
|
| 403 |
-
}
|
| 404 |
-
|
| 405 |
-
if (!NIL_P(rwp->abort_callback_container->callback) || 0 != RARRAY_LEN(rwp->abort_callback_container->callbacks)) {
|
| 406 |
-
rwp->params.abort_callback = [](void * user_data) {
|
| 407 |
-
const ruby_whisper_callback_container *container = (ruby_whisper_callback_container *)user_data;
|
| 408 |
-
if (!NIL_P(container->callback)) {
|
| 409 |
-
VALUE result = rb_funcall(container->callback, id_call, 1, container->user_data);
|
| 410 |
-
if (!NIL_P(result) && Qfalse != result) {
|
| 411 |
-
return true;
|
| 412 |
-
}
|
| 413 |
-
}
|
| 414 |
-
const long callbacks_len = RARRAY_LEN(container->callbacks);
|
| 415 |
-
if (0 == callbacks_len) {
|
| 416 |
-
return false;
|
| 417 |
-
}
|
| 418 |
-
for (int j = 0; j < callbacks_len; j++) {
|
| 419 |
-
VALUE cb = rb_ary_entry(container->callbacks, j);
|
| 420 |
-
VALUE result = rb_funcall(cb, id_call, 1, container->user_data);
|
| 421 |
-
if (!NIL_P(result) && Qfalse != result) {
|
| 422 |
-
return true;
|
| 423 |
-
}
|
| 424 |
-
}
|
| 425 |
-
return false;
|
| 426 |
-
};
|
| 427 |
-
rwp->abort_callback_container->context = &self;
|
| 428 |
-
rwp->params.abort_callback_user_data = rwp->abort_callback_container;
|
| 429 |
-
}
|
| 430 |
|
| 431 |
if (whisper_full_parallel(rw->context, rwp->params, pcmf32.data(), pcmf32.size(), 1) != 0) {
|
| 432 |
fprintf(stderr, "failed to process audio\n");
|
|
@@ -631,6 +641,7 @@ VALUE ruby_whisper_full(int argc, VALUE *argv, VALUE self) {
|
|
| 631 |
}
|
| 632 |
}
|
| 633 |
}
|
|
|
|
| 634 |
const int result = whisper_full(rw->context, rwp->params, c_samples, n_samples);
|
| 635 |
if (0 == result) {
|
| 636 |
return Qnil;
|
|
@@ -719,6 +730,7 @@ static VALUE ruby_whisper_full_parallel(int argc, VALUE *argv,VALUE self) {
|
|
| 719 |
}
|
| 720 |
}
|
| 721 |
}
|
|
|
|
| 722 |
const int result = whisper_full_parallel(rw->context, rwp->params, c_samples, n_samples, n_processors);
|
| 723 |
if (0 == result) {
|
| 724 |
return Qnil;
|
|
@@ -823,6 +835,18 @@ static VALUE ruby_whisper_full_get_segment_text(VALUE self, VALUE i_segment) {
|
|
| 823 |
return rb_str_new2(text);
|
| 824 |
}
|
| 825 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 826 |
/*
|
| 827 |
* params.language = "auto" | "en", etc...
|
| 828 |
*
|
|
@@ -1547,6 +1571,18 @@ static VALUE ruby_whisper_segment_get_text(VALUE self) {
|
|
| 1547 |
return rb_str_new2(text);
|
| 1548 |
}
|
| 1549 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1550 |
static void rb_whisper_model_mark(ruby_whisper_model *rwm) {
|
| 1551 |
rb_gc_mark(rwm->context);
|
| 1552 |
}
|
|
@@ -1809,6 +1845,7 @@ void Init_whisper() {
|
|
| 1809 |
rb_define_method(cContext, "full_get_segment_t1", ruby_whisper_full_get_segment_t1, 1);
|
| 1810 |
rb_define_method(cContext, "full_get_segment_speaker_turn_next", ruby_whisper_full_get_segment_speaker_turn_next, 1);
|
| 1811 |
rb_define_method(cContext, "full_get_segment_text", ruby_whisper_full_get_segment_text, 1);
|
|
|
|
| 1812 |
rb_define_method(cContext, "full", ruby_whisper_full, -1);
|
| 1813 |
rb_define_method(cContext, "full_parallel", ruby_whisper_full_parallel, -1);
|
| 1814 |
|
|
@@ -1887,6 +1924,7 @@ void Init_whisper() {
|
|
| 1887 |
rb_define_method(cSegment, "end_time", ruby_whisper_segment_get_end_time, 0);
|
| 1888 |
rb_define_method(cSegment, "speaker_next_turn?", ruby_whisper_segment_get_speaker_turn_next, 0);
|
| 1889 |
rb_define_method(cSegment, "text", ruby_whisper_segment_get_text, 0);
|
|
|
|
| 1890 |
|
| 1891 |
cModel = rb_define_class_under(mWhisper, "Model", rb_cObject);
|
| 1892 |
rb_define_alloc_func(cModel, ruby_whisper_model_allocate);
|
|
|
|
| 53 |
|
| 54 |
static bool is_log_callback_finalized = false;
|
| 55 |
|
| 56 |
+
// High level API
|
| 57 |
+
static VALUE rb_whisper_segment_initialize(VALUE context, int index);
|
| 58 |
+
|
| 59 |
/*
|
| 60 |
* call-seq:
|
| 61 |
* lang_max_id -> Integer
|
|
|
|
| 190 |
return container;
|
| 191 |
}
|
| 192 |
|
| 193 |
+
static void new_segment_callback(struct whisper_context *ctx, struct whisper_state *state, int n_new, void *user_data) {
|
| 194 |
+
const ruby_whisper_callback_container *container = (ruby_whisper_callback_container *)user_data;
|
| 195 |
+
|
| 196 |
+
// Currently, doesn't support state because
|
| 197 |
+
// those require to resolve GC-related problems.
|
| 198 |
+
if (!NIL_P(container->callback)) {
|
| 199 |
+
rb_funcall(container->callback, id_call, 4, *container->context, Qnil, INT2NUM(n_new), container->user_data);
|
| 200 |
+
}
|
| 201 |
+
const long callbacks_len = RARRAY_LEN(container->callbacks);
|
| 202 |
+
if (0 == callbacks_len) {
|
| 203 |
+
return;
|
| 204 |
+
}
|
| 205 |
+
const int n_segments = whisper_full_n_segments_from_state(state);
|
| 206 |
+
for (int i = n_new; i > 0; i--) {
|
| 207 |
+
int i_segment = n_segments - i;
|
| 208 |
+
VALUE segment = rb_whisper_segment_initialize(*container->context, i_segment);
|
| 209 |
+
for (int j = 0; j < callbacks_len; j++) {
|
| 210 |
+
VALUE cb = rb_ary_entry(container->callbacks, j);
|
| 211 |
+
rb_funcall(cb, id_call, 1, segment);
|
| 212 |
+
}
|
| 213 |
+
}
|
| 214 |
+
}
|
| 215 |
+
|
| 216 |
+
static void progress_callback(struct whisper_context *ctx, struct whisper_state *state, int progress_cur, void *user_data) {
|
| 217 |
+
const ruby_whisper_callback_container *container = (ruby_whisper_callback_container *)user_data;
|
| 218 |
+
const VALUE progress = INT2NUM(progress_cur);
|
| 219 |
+
// Currently, doesn't support state because
|
| 220 |
+
// those require to resolve GC-related problems.
|
| 221 |
+
if (!NIL_P(container->callback)) {
|
| 222 |
+
rb_funcall(container->callback, id_call, 4, *container->context, Qnil, progress, container->user_data);
|
| 223 |
+
}
|
| 224 |
+
const long callbacks_len = RARRAY_LEN(container->callbacks);
|
| 225 |
+
if (0 == callbacks_len) {
|
| 226 |
+
return;
|
| 227 |
+
}
|
| 228 |
+
for (int j = 0; j < callbacks_len; j++) {
|
| 229 |
+
VALUE cb = rb_ary_entry(container->callbacks, j);
|
| 230 |
+
rb_funcall(cb, id_call, 1, progress);
|
| 231 |
+
}
|
| 232 |
+
}
|
| 233 |
+
|
| 234 |
+
static bool abort_callback(void * user_data) {
|
| 235 |
+
const ruby_whisper_callback_container *container = (ruby_whisper_callback_container *)user_data;
|
| 236 |
+
if (!NIL_P(container->callback)) {
|
| 237 |
+
VALUE result = rb_funcall(container->callback, id_call, 1, container->user_data);
|
| 238 |
+
if (!NIL_P(result) && Qfalse != result) {
|
| 239 |
+
return true;
|
| 240 |
+
}
|
| 241 |
+
}
|
| 242 |
+
const long callbacks_len = RARRAY_LEN(container->callbacks);
|
| 243 |
+
if (0 == callbacks_len) {
|
| 244 |
+
return false;
|
| 245 |
+
}
|
| 246 |
+
for (int j = 0; j < callbacks_len; j++) {
|
| 247 |
+
VALUE cb = rb_ary_entry(container->callbacks, j);
|
| 248 |
+
VALUE result = rb_funcall(cb, id_call, 1, container->user_data);
|
| 249 |
+
if (!NIL_P(result) && Qfalse != result) {
|
| 250 |
+
return true;
|
| 251 |
+
}
|
| 252 |
+
}
|
| 253 |
+
return false;
|
| 254 |
+
}
|
| 255 |
+
|
| 256 |
static VALUE ruby_whisper_params_allocate(VALUE klass) {
|
| 257 |
ruby_whisper_params *rwp;
|
| 258 |
rwp = ALLOC(ruby_whisper_params);
|
|
|
|
| 296 |
return self;
|
| 297 |
}
|
| 298 |
|
| 299 |
+
static void register_callbacks(ruby_whisper_params * rwp, VALUE * self) {
|
| 300 |
+
if (!NIL_P(rwp->new_segment_callback_container->callback) || 0 != RARRAY_LEN(rwp->new_segment_callback_container->callbacks)) {
|
| 301 |
+
rwp->new_segment_callback_container->context = self;
|
| 302 |
+
rwp->params.new_segment_callback = new_segment_callback;
|
| 303 |
+
rwp->params.new_segment_callback_user_data = rwp->new_segment_callback_container;
|
| 304 |
+
}
|
| 305 |
+
|
| 306 |
+
if (!NIL_P(rwp->progress_callback_container->callback) || 0 != RARRAY_LEN(rwp->progress_callback_container->callbacks)) {
|
| 307 |
+
rwp->progress_callback_container->context = self;
|
| 308 |
+
rwp->params.progress_callback = progress_callback;
|
| 309 |
+
rwp->params.progress_callback_user_data = rwp->progress_callback_container;
|
| 310 |
+
}
|
| 311 |
+
|
| 312 |
+
if (!NIL_P(rwp->abort_callback_container->callback) || 0 != RARRAY_LEN(rwp->abort_callback_container->callbacks)) {
|
| 313 |
+
rwp->abort_callback_container->context = self;
|
| 314 |
+
rwp->params.abort_callback = abort_callback;
|
| 315 |
+
rwp->params.abort_callback_user_data = rwp->abort_callback_container;
|
| 316 |
+
}
|
| 317 |
+
}
|
| 318 |
|
| 319 |
/*
|
| 320 |
* transcribe a single file
|
|
|
|
| 436 |
rwp->params.encoder_begin_callback_user_data = &is_aborted;
|
| 437 |
}
|
| 438 |
|
| 439 |
+
register_callbacks(rwp, &self);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 440 |
|
| 441 |
if (whisper_full_parallel(rw->context, rwp->params, pcmf32.data(), pcmf32.size(), 1) != 0) {
|
| 442 |
fprintf(stderr, "failed to process audio\n");
|
|
|
|
| 641 |
}
|
| 642 |
}
|
| 643 |
}
|
| 644 |
+
register_callbacks(rwp, &self);
|
| 645 |
const int result = whisper_full(rw->context, rwp->params, c_samples, n_samples);
|
| 646 |
if (0 == result) {
|
| 647 |
return Qnil;
|
|
|
|
| 730 |
}
|
| 731 |
}
|
| 732 |
}
|
| 733 |
+
register_callbacks(rwp, &self);
|
| 734 |
const int result = whisper_full_parallel(rw->context, rwp->params, c_samples, n_samples, n_processors);
|
| 735 |
if (0 == result) {
|
| 736 |
return Qnil;
|
|
|
|
| 835 |
return rb_str_new2(text);
|
| 836 |
}
|
| 837 |
|
| 838 |
+
/*
|
| 839 |
+
* call-seq:
|
| 840 |
+
* full_get_segment_no_speech_prob -> Float
|
| 841 |
+
*/
|
| 842 |
+
static VALUE ruby_whisper_full_get_segment_no_speech_prob(VALUE self, VALUE i_segment) {
|
| 843 |
+
ruby_whisper *rw;
|
| 844 |
+
Data_Get_Struct(self, ruby_whisper, rw);
|
| 845 |
+
const int c_i_segment = ruby_whisper_full_check_segment_index(rw, i_segment);
|
| 846 |
+
const float no_speech_prob = whisper_full_get_segment_no_speech_prob(rw->context, c_i_segment);
|
| 847 |
+
return DBL2NUM(no_speech_prob);
|
| 848 |
+
}
|
| 849 |
+
|
| 850 |
/*
|
| 851 |
* params.language = "auto" | "en", etc...
|
| 852 |
*
|
|
|
|
| 1571 |
return rb_str_new2(text);
|
| 1572 |
}
|
| 1573 |
|
| 1574 |
+
/*
|
| 1575 |
+
* call-seq:
|
| 1576 |
+
* no_speech_prob -> Float
|
| 1577 |
+
*/
|
| 1578 |
+
static VALUE ruby_whisper_segment_get_no_speech_prob(VALUE self) {
|
| 1579 |
+
ruby_whisper_segment *rws;
|
| 1580 |
+
Data_Get_Struct(self, ruby_whisper_segment, rws);
|
| 1581 |
+
ruby_whisper *rw;
|
| 1582 |
+
Data_Get_Struct(rws->context, ruby_whisper, rw);
|
| 1583 |
+
return DBL2NUM(whisper_full_get_segment_no_speech_prob(rw->context, rws->index));
|
| 1584 |
+
}
|
| 1585 |
+
|
| 1586 |
static void rb_whisper_model_mark(ruby_whisper_model *rwm) {
|
| 1587 |
rb_gc_mark(rwm->context);
|
| 1588 |
}
|
|
|
|
| 1845 |
rb_define_method(cContext, "full_get_segment_t1", ruby_whisper_full_get_segment_t1, 1);
|
| 1846 |
rb_define_method(cContext, "full_get_segment_speaker_turn_next", ruby_whisper_full_get_segment_speaker_turn_next, 1);
|
| 1847 |
rb_define_method(cContext, "full_get_segment_text", ruby_whisper_full_get_segment_text, 1);
|
| 1848 |
+
rb_define_method(cContext, "full_get_segment_no_speech_prob", ruby_whisper_full_get_segment_no_speech_prob, 1);
|
| 1849 |
rb_define_method(cContext, "full", ruby_whisper_full, -1);
|
| 1850 |
rb_define_method(cContext, "full_parallel", ruby_whisper_full_parallel, -1);
|
| 1851 |
|
|
|
|
| 1924 |
rb_define_method(cSegment, "end_time", ruby_whisper_segment_get_end_time, 0);
|
| 1925 |
rb_define_method(cSegment, "speaker_next_turn?", ruby_whisper_segment_get_speaker_turn_next, 0);
|
| 1926 |
rb_define_method(cSegment, "text", ruby_whisper_segment_get_text, 0);
|
| 1927 |
+
rb_define_method(cSegment, "no_speech_prob", ruby_whisper_segment_get_no_speech_prob, 0);
|
| 1928 |
|
| 1929 |
cModel = rb_define_class_under(mWhisper, "Model", rb_cObject);
|
| 1930 |
rb_define_alloc_func(cModel, ruby_whisper_model_allocate);
|
bindings/ruby/lib/whisper/model/uri.rb
CHANGED
|
@@ -79,30 +79,36 @@ class Whisper::Model
|
|
| 79 |
downloaded += chunk.bytesize
|
| 80 |
show_progress downloaded, size
|
| 81 |
end
|
|
|
|
| 82 |
end
|
| 83 |
downloading_path.rename path
|
| 84 |
end
|
| 85 |
|
| 86 |
def show_progress(current, size)
|
| 87 |
-
|
| 88 |
-
return unless size
|
| 89 |
|
| 90 |
unless @prev
|
| 91 |
@prev = Time.now
|
| 92 |
-
$stderr.puts "Downloading #{@uri}"
|
| 93 |
end
|
| 94 |
|
| 95 |
now = Time.now
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
@prev = now
|
| 107 |
end
|
| 108 |
|
|
|
|
| 79 |
downloaded += chunk.bytesize
|
| 80 |
show_progress downloaded, size
|
| 81 |
end
|
| 82 |
+
$stderr.puts
|
| 83 |
end
|
| 84 |
downloading_path.rename path
|
| 85 |
end
|
| 86 |
|
| 87 |
def show_progress(current, size)
|
| 88 |
+
progress_rate_available = size && $stderr.tty?
|
|
|
|
| 89 |
|
| 90 |
unless @prev
|
| 91 |
@prev = Time.now
|
| 92 |
+
$stderr.puts "Downloading #{@uri} to #{cache_path}"
|
| 93 |
end
|
| 94 |
|
| 95 |
now = Time.now
|
| 96 |
+
|
| 97 |
+
if progress_rate_available
|
| 98 |
+
return if now - @prev < 1 && current < size
|
| 99 |
+
|
| 100 |
+
progress_width = 20
|
| 101 |
+
progress = current.to_f / size
|
| 102 |
+
arrow_length = progress * progress_width
|
| 103 |
+
arrow = "=" * (arrow_length - 1) + ">" + " " * (progress_width - arrow_length)
|
| 104 |
+
line = "[#{arrow}] (#{format_bytesize(current)} / #{format_bytesize(size)})"
|
| 105 |
+
padding = ' ' * ($stderr.winsize[1] - line.size)
|
| 106 |
+
$stderr.print "\r#{line}#{padding}"
|
| 107 |
+
else
|
| 108 |
+
return if now - @prev < 1
|
| 109 |
+
|
| 110 |
+
$stderr.print "."
|
| 111 |
+
end
|
| 112 |
@prev = now
|
| 113 |
end
|
| 114 |
|
bindings/ruby/tests/helper.rb
CHANGED
|
@@ -4,4 +4,21 @@ require_relative "jfk_reader/jfk_reader"
|
|
| 4 |
|
| 5 |
class TestBase < Test::Unit::TestCase
|
| 6 |
AUDIO = File.join(__dir__, "..", "..", "..", "samples", "jfk.wav")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
end
|
|
|
|
| 4 |
|
| 5 |
class TestBase < Test::Unit::TestCase
|
| 6 |
AUDIO = File.join(__dir__, "..", "..", "..", "samples", "jfk.wav")
|
| 7 |
+
|
| 8 |
+
class << self
|
| 9 |
+
attr_reader :whisper
|
| 10 |
+
|
| 11 |
+
def startup
|
| 12 |
+
@whisper = Whisper::Context.new("base.en")
|
| 13 |
+
params = Whisper::Params.new
|
| 14 |
+
params.print_timestamps = false
|
| 15 |
+
@whisper.transcribe(TestBase::AUDIO, params)
|
| 16 |
+
end
|
| 17 |
+
end
|
| 18 |
+
|
| 19 |
+
private
|
| 20 |
+
|
| 21 |
+
def whisper
|
| 22 |
+
self.class.whisper
|
| 23 |
+
end
|
| 24 |
end
|
bindings/ruby/tests/test_package.rb
CHANGED
|
@@ -23,7 +23,7 @@ class TestPackage < TestBase
|
|
| 23 |
version = match_data[2]
|
| 24 |
basename = "whisper.#{RbConfig::CONFIG["DLEXT"]}"
|
| 25 |
Dir.mktmpdir do |dir|
|
| 26 |
-
system "gem", "install", "--install-dir", dir.shellescape, "pkg/#{filename.shellescape}", exception: true
|
| 27 |
assert_path_exist File.join(dir, "gems/whispercpp-#{version}/lib", basename)
|
| 28 |
end
|
| 29 |
end
|
|
|
|
| 23 |
version = match_data[2]
|
| 24 |
basename = "whisper.#{RbConfig::CONFIG["DLEXT"]}"
|
| 25 |
Dir.mktmpdir do |dir|
|
| 26 |
+
system "gem", "install", "--install-dir", dir.shellescape, "--no-document", "pkg/#{filename.shellescape}", exception: true
|
| 27 |
assert_path_exist File.join(dir, "gems/whispercpp-#{version}/lib", basename)
|
| 28 |
end
|
| 29 |
end
|
bindings/ruby/tests/test_segment.rb
CHANGED
|
@@ -1,17 +1,6 @@
|
|
| 1 |
require_relative "helper"
|
| 2 |
|
| 3 |
class TestSegment < TestBase
|
| 4 |
-
class << self
|
| 5 |
-
attr_reader :whisper
|
| 6 |
-
|
| 7 |
-
def startup
|
| 8 |
-
@whisper = Whisper::Context.new("base.en")
|
| 9 |
-
params = Whisper::Params.new
|
| 10 |
-
params.print_timestamps = false
|
| 11 |
-
@whisper.transcribe(TestBase::AUDIO, params)
|
| 12 |
-
end
|
| 13 |
-
end
|
| 14 |
-
|
| 15 |
def test_iteration
|
| 16 |
whisper.each_segment do |segment|
|
| 17 |
assert_instance_of Whisper::Segment, segment
|
|
@@ -43,6 +32,14 @@ class TestSegment < TestBase
|
|
| 43 |
end
|
| 44 |
end
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
def test_on_new_segment
|
| 47 |
params = Whisper::Params.new
|
| 48 |
seg = nil
|
|
@@ -74,10 +71,4 @@ class TestSegment < TestBase
|
|
| 74 |
end
|
| 75 |
whisper.transcribe(AUDIO, params)
|
| 76 |
end
|
| 77 |
-
|
| 78 |
-
private
|
| 79 |
-
|
| 80 |
-
def whisper
|
| 81 |
-
self.class.whisper
|
| 82 |
-
end
|
| 83 |
end
|
|
|
|
| 1 |
require_relative "helper"
|
| 2 |
|
| 3 |
class TestSegment < TestBase
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
def test_iteration
|
| 5 |
whisper.each_segment do |segment|
|
| 6 |
assert_instance_of Whisper::Segment, segment
|
|
|
|
| 32 |
end
|
| 33 |
end
|
| 34 |
|
| 35 |
+
def test_no_speech_prob
|
| 36 |
+
no_speech_prob = nil
|
| 37 |
+
whisper.each_segment do |segment|
|
| 38 |
+
no_speech_prob = segment.no_speech_prob
|
| 39 |
+
end
|
| 40 |
+
assert no_speech_prob > 0.0
|
| 41 |
+
end
|
| 42 |
+
|
| 43 |
def test_on_new_segment
|
| 44 |
params = Whisper::Params.new
|
| 45 |
seg = nil
|
|
|
|
| 71 |
end
|
| 72 |
whisper.transcribe(AUDIO, params)
|
| 73 |
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
end
|
bindings/ruby/tests/test_whisper.rb
CHANGED
|
@@ -21,21 +21,6 @@ class TestWhisper < TestBase
|
|
| 21 |
end
|
| 22 |
|
| 23 |
sub_test_case "After transcription" do
|
| 24 |
-
class << self
|
| 25 |
-
attr_reader :whisper
|
| 26 |
-
|
| 27 |
-
def startup
|
| 28 |
-
@whisper = Whisper::Context.new("base.en")
|
| 29 |
-
params = Whisper::Params.new
|
| 30 |
-
params.print_timestamps = false
|
| 31 |
-
@whisper.transcribe(TestBase::AUDIO, params)
|
| 32 |
-
end
|
| 33 |
-
end
|
| 34 |
-
|
| 35 |
-
def whisper
|
| 36 |
-
self.class.whisper
|
| 37 |
-
end
|
| 38 |
-
|
| 39 |
def test_full_n_segments
|
| 40 |
assert_equal 1, whisper.full_n_segments
|
| 41 |
end
|
|
@@ -70,6 +55,12 @@ class TestWhisper < TestBase
|
|
| 70 |
def test_full_get_segment_text
|
| 71 |
assert_match /ask not what your country can do for you, ask what you can do for your country/, whisper.full_get_segment_text(0)
|
| 72 |
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
end
|
| 74 |
|
| 75 |
def test_lang_max_id
|
|
|
|
| 21 |
end
|
| 22 |
|
| 23 |
sub_test_case "After transcription" do
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
def test_full_n_segments
|
| 25 |
assert_equal 1, whisper.full_n_segments
|
| 26 |
end
|
|
|
|
| 55 |
def test_full_get_segment_text
|
| 56 |
assert_match /ask not what your country can do for you, ask what you can do for your country/, whisper.full_get_segment_text(0)
|
| 57 |
end
|
| 58 |
+
|
| 59 |
+
def test_full_get_segment_no_speech_prob
|
| 60 |
+
prob = whisper.full_get_segment_no_speech_prob(0)
|
| 61 |
+
assert prob > 0.0
|
| 62 |
+
assert prob < 1.0
|
| 63 |
+
end
|
| 64 |
end
|
| 65 |
|
| 66 |
def test_lang_max_id
|