Skip to content

Commit f8abbeb

Browse files
ruby : add Whisper::VERSION (ggml-org#3292)
* Add a test for segment * Check option existence * Use more proper variable to define build option * Assert Core ML enabled * Define Whisper::VERSION * Add test for Whisper::VERSION * Add signature of Whisper::VERSION
1 parent 32cf4e2 commit f8abbeb

File tree

6 files changed

+24
-4
lines changed

6 files changed

+24
-4
lines changed

bindings/ruby/ext/options.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def configure_metal
6464
def configure_coreml
6565
if enabled?("WHISPER_COREML")
6666
$LDFLAGS << " -framework Foundation -framework CoreML"
67-
$CPPFLAGS << " -DRUBY_WHISPER_USE_COREML"
67+
$defs << "-DRUBY_WHISPER_USE_COREML"
6868
end
6969
end
7070

@@ -73,10 +73,13 @@ def option_name(name)
7373
end
7474

7575
def enabled?(option)
76-
if @options[option][1].nil?
76+
op = @options[option]
77+
raise "Option not exist: #{option}" unless op
78+
raise "Option not boolean: #{option}(#{op[0]})" unless op[0] == "BOOL"
79+
if op[1].nil?
7780
cmake_options[option][1]
7881
else
79-
@options[option][1]
82+
op[1]
8083
end
8184
end
8285
end

bindings/ruby/ext/ruby_whisper.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ void Init_whisper() {
148148
mWhisper = rb_define_module("Whisper");
149149
mVAD = rb_define_module_under(mWhisper, "VAD");
150150

151+
rb_define_const(mWhisper, "VERSION", rb_str_new2(whisper_version()));
151152
rb_define_const(mWhisper, "LOG_LEVEL_NONE", INT2NUM(GGML_LOG_LEVEL_NONE));
152153
rb_define_const(mWhisper, "LOG_LEVEL_INFO", INT2NUM(GGML_LOG_LEVEL_INFO));
153154
rb_define_const(mWhisper, "LOG_LEVEL_WARN", INT2NUM(GGML_LOG_LEVEL_WARN));

bindings/ruby/sig/whisper.rbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module Whisper
1010
type encoder_begin_callback = ^(Whisper::Context, void, Object user_data) -> void
1111
type abort_callback = ^(Whisper::Context, void, Object user_data) -> boolish
1212

13+
VERSION: String
1314
LOG_LEVEL_NONE: Integer
1415
LOG_LEVEL_INFO: Integer
1516
LOG_LEVEL_WARN: Integer

bindings/ruby/test/test_package.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ def test_install_with_coreml
3131
Dir.mktmpdir do |dir|
3232
system "gem", "install", "--install-dir", dir.shellescape, "--no-document", "pkg/#{gemspec.file_name.shellescape}", "--", "--enable-whisper-coreml", exception: true
3333
assert_installed dir, gemspec.version
34+
libdir = File.join(dir, "gems", "#{gemspec.name}-#{gemspec.version}", "lib")
3435
assert_nothing_raised do
35-
libdir = File.join(dir, "gems", "#{gemspec.name}-#{gemspec.version}", "lib")
3636
system "ruby", "-I", libdir, "-r", "whisper", "-e", "Whisper::Context.new('tiny')", exception: true
3737
end
38+
assert_match(/COREML = 1/, `ruby -I #{libdir.shellescape} -r whisper -e 'puts Whisper.system_info_str'`)
3839
end
3940
end
4041
end

bindings/ruby/test/test_segment.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ def test_on_new_segment_twice
7272
whisper.transcribe(AUDIO, params)
7373
end
7474

75+
def test_transcription_after_segment_retrieved
76+
params = Whisper::Params.new
77+
segment = whisper.each_segment.first
78+
assert_match(/ask not what your country can do for you, ask what you can do for your country/, segment.text)
79+
80+
whisper.transcribe(AUDIO, Whisper::Params.new(offset: 5000))
81+
assert_not_match(/ask not what your country can do for you, ask what you can do for your country/, segment.text)
82+
assert_match(/what you can do for your country/i, segment.text)
83+
end
84+
7585
def test_pattern_matching
7686
segment = whisper.each_segment.first
7787
segment => {start_time:, end_time:, text:, no_speech_prob:, speaker_turn_next:}

bindings/ruby/test/test_whisper.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ def test_system_info_str
116116
assert_match(/\AWHISPER : COREML = \d | OPENVINO = \d |/, Whisper.system_info_str)
117117
end
118118

119+
def test_version
120+
assert_kind_of String, Whisper::VERSION
121+
end
122+
119123
def test_log_set
120124
user_data = Object.new
121125
logs = []

0 commit comments

Comments
 (0)