Skip to content

Commit c84b83c

Browse files
ruby : Fix of C++ header guard name, model URI support, type signature and more (#2683)
* Add test to make Whisper::Context.new accept URI string * Add test to make Whisper::Context.new accept URI * Make Whisper::Context.new accept URI string and URI * Update README Revert "Fix argument of rb_undefine_finalizer" * Fix typos * Add type signature file * Assign literarl to const variable * Load Whisper::Model::URI from Init_whisper * Simplify .gitignore * Don't load whisper.so from whisper/model/uri.rb * Use each_with_object instead of each * Add Development section to README * Rename header guard to conform to C++ naming convention
1 parent 5136fd9 commit c84b83c

File tree

8 files changed

+357
-143
lines changed

8 files changed

+357
-143
lines changed

bindings/ruby/.gitignore

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
LICENSE
22
pkg/
3-
lib/whisper.so
4-
lib/whisper.bundle
5-
lib/whisper.dll
3+
lib/whisper.*

bindings/ruby/README.md

+16-4
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ You also can use shorthand for pre-converted models:
6060
whisper = Whisper::Context.new("base.en")
6161
```
6262

63-
You can see the list of prepared model names by `Whisper::Model.preconverted_models.keys`:
63+
You can see the list of prepared model names by `Whisper::Model.pre_converted_models.keys`:
6464

6565
```ruby
66-
puts Whisper::Model.preconverted_models.keys
66+
puts Whisper::Model.pre_converted_models.keys
6767
# tiny
6868
# tiny.en
6969
# tiny-q5_1
@@ -87,8 +87,9 @@ whisper = Whisper::Context.new("path/to/your/model.bin")
8787
Or, you can download model files:
8888

8989
```ruby
90-
model_uri = Whisper::Model::URI.new("http://example.net/uri/of/your/model.bin")
91-
whisper = Whisper::Context.new(model_uri)
90+
whisper = Whisper::Context.new("https://example.net/uri/of/your/model.bin")
91+
# Or
92+
whisper = Whisper::Context.new(URI("https://example.net/uri/of/your/model.bin"))
9293
```
9394

9495
See [models][] page for details.
@@ -222,6 +223,17 @@ end
222223

223224
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.
224225

226+
Development
227+
-----------
228+
229+
% git clone https://github.com/ggerganov/whisper.cpp.git
230+
% cd whisper.cpp/bindings/ruby
231+
% rake test
232+
233+
First call of `rake test` builds an extension and downloads a model for testing. After that, you add tests in `tests` directory and modify `ext/ruby_whisper.cpp`.
234+
235+
If something seems wrong on build, running `rake clean` solves some cases.
236+
225237
License
226238
-------
227239

bindings/ruby/ext/ruby_whisper.cpp

+17-2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ static ID id_length;
4949
static ID id_next;
5050
static ID id_new;
5151
static ID id_to_path;
52+
static ID id_URI;
5253
static ID id_pre_converted_models;
5354

5455
static bool is_log_callback_finalized = false;
@@ -283,6 +284,17 @@ static VALUE ruby_whisper_initialize(int argc, VALUE *argv, VALUE self) {
283284
if (!NIL_P(pre_converted_model)) {
284285
whisper_model_file_path = pre_converted_model;
285286
}
287+
if (TYPE(whisper_model_file_path) == T_STRING) {
288+
const char * whisper_model_file_path_str = StringValueCStr(whisper_model_file_path);
289+
if (strncmp("http://", whisper_model_file_path_str, 7) == 0 || strncmp("https://", whisper_model_file_path_str, 8) == 0) {
290+
VALUE uri_class = rb_const_get(cModel, id_URI);
291+
whisper_model_file_path = rb_class_new_instance(1, &whisper_model_file_path, uri_class);
292+
}
293+
}
294+
if (rb_obj_is_kind_of(whisper_model_file_path, rb_path2class("URI::HTTP"))) {
295+
VALUE uri_class = rb_const_get(cModel, id_URI);
296+
whisper_model_file_path = rb_class_new_instance(1, &whisper_model_file_path, uri_class);
297+
}
286298
if (rb_respond_to(whisper_model_file_path, id_to_path)) {
287299
whisper_model_file_path = rb_funcall(whisper_model_file_path, id_to_path, 0);
288300
}
@@ -837,7 +849,7 @@ static VALUE ruby_whisper_full_get_segment_text(VALUE self, VALUE i_segment) {
837849

838850
/*
839851
* call-seq:
840-
* full_get_segment_no_speech_prob -> Float
852+
* full_get_segment_no_speech_prob(segment_index) -> Float
841853
*/
842854
static VALUE ruby_whisper_full_get_segment_no_speech_prob(VALUE self, VALUE i_segment) {
843855
ruby_whisper *rw;
@@ -1755,7 +1767,7 @@ static VALUE ruby_whisper_c_model_type(VALUE self) {
17551767

17561768
static VALUE ruby_whisper_error_initialize(VALUE self, VALUE code) {
17571769
const int c_code = NUM2INT(code);
1758-
char *raw_message;
1770+
const char *raw_message;
17591771
switch (c_code) {
17601772
case -2:
17611773
raw_message = "failed to compute log mel spectrogram";
@@ -1802,6 +1814,7 @@ void Init_whisper() {
18021814
id_next = rb_intern("next");
18031815
id_new = rb_intern("new");
18041816
id_to_path = rb_intern("to_path");
1817+
id_URI = rb_intern("URI");
18051818
id_pre_converted_models = rb_intern("pre_converted_models");
18061819

18071820
mWhisper = rb_define_module("Whisper");
@@ -1941,6 +1954,8 @@ void Init_whisper() {
19411954
rb_define_method(cModel, "n_mels", ruby_whisper_c_model_n_mels, 0);
19421955
rb_define_method(cModel, "ftype", ruby_whisper_c_model_ftype, 0);
19431956
rb_define_method(cModel, "type", ruby_whisper_c_model_type, 0);
1957+
1958+
rb_require("whisper/model/uri");
19441959
}
19451960
#ifdef __cplusplus
19461961
}

bindings/ruby/ext/ruby_whisper.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef __RUBY_WHISPER_H
2-
#define __RUBY_WHISPER_H
1+
#ifndef RUBY_WHISPER_H
2+
#define RUBY_WHISPER_H
33

44
#include "whisper.h"
55

bindings/ruby/lib/whisper.rb

-2
This file was deleted.

0 commit comments

Comments
 (0)