Skip to content

Commit

Permalink
ruby : Fix of C++ header guard name, model URI support, type signatur…
Browse files Browse the repository at this point in the history
…e 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
  • Loading branch information
KitaitiMakoto authored Dec 30, 2024
1 parent 5136fd9 commit c84b83c
Show file tree
Hide file tree
Showing 8 changed files with 357 additions and 143 deletions.
4 changes: 1 addition & 3 deletions bindings/ruby/.gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
LICENSE
pkg/
lib/whisper.so
lib/whisper.bundle
lib/whisper.dll
lib/whisper.*
20 changes: 16 additions & 4 deletions bindings/ruby/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ You also can use shorthand for pre-converted models:
whisper = Whisper::Context.new("base.en")
```

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

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

```ruby
model_uri = Whisper::Model::URI.new("http://example.net/uri/of/your/model.bin")
whisper = Whisper::Context.new(model_uri)
whisper = Whisper::Context.new("https://example.net/uri/of/your/model.bin")
# Or
whisper = Whisper::Context.new(URI("https://example.net/uri/of/your/model.bin"))
```

See [models][] page for details.
Expand Down Expand Up @@ -222,6 +223,17 @@ end

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.

Development
-----------

% git clone https://github.com/ggerganov/whisper.cpp.git
% cd whisper.cpp/bindings/ruby
% rake test

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

If something seems wrong on build, running `rake clean` solves some cases.

License
-------

Expand Down
19 changes: 17 additions & 2 deletions bindings/ruby/ext/ruby_whisper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ static ID id_length;
static ID id_next;
static ID id_new;
static ID id_to_path;
static ID id_URI;
static ID id_pre_converted_models;

static bool is_log_callback_finalized = false;
Expand Down Expand Up @@ -283,6 +284,17 @@ static VALUE ruby_whisper_initialize(int argc, VALUE *argv, VALUE self) {
if (!NIL_P(pre_converted_model)) {
whisper_model_file_path = pre_converted_model;
}
if (TYPE(whisper_model_file_path) == T_STRING) {
const char * whisper_model_file_path_str = StringValueCStr(whisper_model_file_path);
if (strncmp("http://", whisper_model_file_path_str, 7) == 0 || strncmp("https://", whisper_model_file_path_str, 8) == 0) {
VALUE uri_class = rb_const_get(cModel, id_URI);
whisper_model_file_path = rb_class_new_instance(1, &whisper_model_file_path, uri_class);
}
}
if (rb_obj_is_kind_of(whisper_model_file_path, rb_path2class("URI::HTTP"))) {
VALUE uri_class = rb_const_get(cModel, id_URI);
whisper_model_file_path = rb_class_new_instance(1, &whisper_model_file_path, uri_class);
}
if (rb_respond_to(whisper_model_file_path, id_to_path)) {
whisper_model_file_path = rb_funcall(whisper_model_file_path, id_to_path, 0);
}
Expand Down Expand Up @@ -837,7 +849,7 @@ static VALUE ruby_whisper_full_get_segment_text(VALUE self, VALUE i_segment) {

/*
* call-seq:
* full_get_segment_no_speech_prob -> Float
* full_get_segment_no_speech_prob(segment_index) -> Float
*/
static VALUE ruby_whisper_full_get_segment_no_speech_prob(VALUE self, VALUE i_segment) {
ruby_whisper *rw;
Expand Down Expand Up @@ -1755,7 +1767,7 @@ static VALUE ruby_whisper_c_model_type(VALUE self) {

static VALUE ruby_whisper_error_initialize(VALUE self, VALUE code) {
const int c_code = NUM2INT(code);
char *raw_message;
const char *raw_message;
switch (c_code) {
case -2:
raw_message = "failed to compute log mel spectrogram";
Expand Down Expand Up @@ -1802,6 +1814,7 @@ void Init_whisper() {
id_next = rb_intern("next");
id_new = rb_intern("new");
id_to_path = rb_intern("to_path");
id_URI = rb_intern("URI");
id_pre_converted_models = rb_intern("pre_converted_models");

mWhisper = rb_define_module("Whisper");
Expand Down Expand Up @@ -1941,6 +1954,8 @@ void Init_whisper() {
rb_define_method(cModel, "n_mels", ruby_whisper_c_model_n_mels, 0);
rb_define_method(cModel, "ftype", ruby_whisper_c_model_ftype, 0);
rb_define_method(cModel, "type", ruby_whisper_c_model_type, 0);

rb_require("whisper/model/uri");
}
#ifdef __cplusplus
}
Expand Down
4 changes: 2 additions & 2 deletions bindings/ruby/ext/ruby_whisper.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef __RUBY_WHISPER_H
#define __RUBY_WHISPER_H
#ifndef RUBY_WHISPER_H
#define RUBY_WHISPER_H

#include "whisper.h"

Expand Down
2 changes: 0 additions & 2 deletions bindings/ruby/lib/whisper.rb

This file was deleted.

Loading

0 comments on commit c84b83c

Please sign in to comment.