Spaces:
Running
Running
ruby : specify Apple frameworks explicitly on build (#3270)
Browse files* Add Apple frameworks to $LDFLAGS when needed
* Add utility method to Options
* Remove unnecessary propaty date from gemspec
* Add Apple frameworks for CoreML build
* Add Accelerate framework only for Apple platform
* Fix ZipURI#cache signature
* Download test fixtures if needed
bindings/ruby/.gitignore
CHANGED
|
@@ -6,3 +6,4 @@ ext/ggml/
|
|
| 6 |
ext/include/
|
| 7 |
ext/scripts/
|
| 8 |
ext/src/
|
|
|
|
|
|
| 6 |
ext/include/
|
| 7 |
ext/scripts/
|
| 8 |
ext/src/
|
| 9 |
+
test/fixtures/
|
bindings/ruby/Rakefile
CHANGED
|
@@ -69,6 +69,21 @@ CLEAN.include LIB_FILE
|
|
| 69 |
|
| 70 |
Rake::TestTask.new
|
| 71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
TEST_MEMORY_VIEW = "test/jfk_reader/jfk_reader.#{RbConfig::CONFIG['DLEXT']}"
|
| 73 |
file TEST_MEMORY_VIEW => "test/jfk_reader/jfk_reader.c" do |t|
|
| 74 |
chdir "test/jfk_reader" do
|
|
@@ -76,6 +91,6 @@ file TEST_MEMORY_VIEW => "test/jfk_reader/jfk_reader.c" do |t|
|
|
| 76 |
sh "make"
|
| 77 |
end
|
| 78 |
end
|
| 79 |
-
CLEAN.include
|
| 80 |
|
| 81 |
-
task test: [LIB_FILE, TEST_MEMORY_VIEW]
|
|
|
|
| 69 |
|
| 70 |
Rake::TestTask.new
|
| 71 |
|
| 72 |
+
TEST_FIXTURE_AUDIO = "test/fixtures/jfk.wav"
|
| 73 |
+
TEST_FIXTURE_AUDIO_SRC = File.expand_path(File.join(__dir__, "..", "..", "samples", "jfk.wav"))
|
| 74 |
+
TEST_FIXTURE_AUDIO_DIR = TEST_FIXTURE_AUDIO.pathmap("%d")
|
| 75 |
+
directory TEST_FIXTURE_AUDIO_DIR
|
| 76 |
+
if File.exist? TEST_FIXTURE_AUDIO_SRC
|
| 77 |
+
file TEST_FIXTURE_AUDIO => [TEST_FIXTURE_AUDIO_SRC, TEST_FIXTURE_AUDIO_DIR] do |t|
|
| 78 |
+
symlink t.source, t.name
|
| 79 |
+
end
|
| 80 |
+
else
|
| 81 |
+
require "open-uri"
|
| 82 |
+
file TEST_FIXTURE_AUDIO => TEST_FIXTURE_AUDIO_DIR do |t|
|
| 83 |
+
File.write t.name, URI("https://github.com/ggml-org/whisper.cpp/raw/refs/heads/master/samples/jfk.wav").read
|
| 84 |
+
end
|
| 85 |
+
end
|
| 86 |
+
|
| 87 |
TEST_MEMORY_VIEW = "test/jfk_reader/jfk_reader.#{RbConfig::CONFIG['DLEXT']}"
|
| 88 |
file TEST_MEMORY_VIEW => "test/jfk_reader/jfk_reader.c" do |t|
|
| 89 |
chdir "test/jfk_reader" do
|
|
|
|
| 91 |
sh "make"
|
| 92 |
end
|
| 93 |
end
|
| 94 |
+
CLEAN.include TEST_MEMORY_VIEW
|
| 95 |
|
| 96 |
+
task test: [LIB_FILE, TEST_MEMORY_VIEW, TEST_FIXTURE_AUDIO]
|
bindings/ruby/ext/options.rb
CHANGED
|
@@ -43,19 +43,40 @@ class Options
|
|
| 43 |
@options[name] = [type, value]
|
| 44 |
end
|
| 45 |
|
|
|
|
|
|
|
| 46 |
configure_coreml
|
| 47 |
end
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
def configure_coreml
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
end
|
| 55 |
-
$CPPFLAGS << " -DRUBY_WHISPER_USE_COREML" if use_coreml
|
| 56 |
end
|
| 57 |
|
| 58 |
def option_name(name)
|
| 59 |
name.downcase.gsub("_", "-")
|
| 60 |
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
end
|
|
|
|
| 43 |
@options[name] = [type, value]
|
| 44 |
end
|
| 45 |
|
| 46 |
+
configure_accelerate
|
| 47 |
+
configure_metal
|
| 48 |
configure_coreml
|
| 49 |
end
|
| 50 |
|
| 51 |
+
# See ggml/src/ggml-cpu/CMakeLists.txt
|
| 52 |
+
def configure_accelerate
|
| 53 |
+
if RUBY_PLATFORM.match?(/darwin/) && enabled?("GGML_ACCELERATE")
|
| 54 |
+
$LDFLAGS << " -framework Accelerate"
|
| 55 |
+
end
|
| 56 |
+
end
|
| 57 |
+
|
| 58 |
+
# See ggml/src/ggml-metal/CMakeLists.txt
|
| 59 |
+
def configure_metal
|
| 60 |
+
$LDFLAGS << " -framework Foundation -framework Metal -framework MetalKit" if enabled?("GGML_METAL")
|
| 61 |
+
end
|
| 62 |
+
|
| 63 |
+
# See src/CmakeLists.txt
|
| 64 |
def configure_coreml
|
| 65 |
+
if enabled?("WHISPER_COREML")
|
| 66 |
+
$LDFLAGS << " -framework Foundation -framework CoreML"
|
| 67 |
+
$CPPFLAGS << " -DRUBY_WHISPER_USE_COREML"
|
| 68 |
+
end
|
|
|
|
|
|
|
| 69 |
end
|
| 70 |
|
| 71 |
def option_name(name)
|
| 72 |
name.downcase.gsub("_", "-")
|
| 73 |
end
|
| 74 |
+
|
| 75 |
+
def enabled?(option)
|
| 76 |
+
if @options[option][1].nil?
|
| 77 |
+
cmake_options[option][1]
|
| 78 |
+
else
|
| 79 |
+
@options[option][1]
|
| 80 |
+
end
|
| 81 |
+
end
|
| 82 |
end
|
bindings/ruby/lib/whisper/model/uri.rb
CHANGED
|
@@ -132,13 +132,13 @@ module Whisper
|
|
| 132 |
|
| 133 |
class ZipURI < URI
|
| 134 |
def cache
|
| 135 |
-
zip_path =
|
| 136 |
dest = unzipped_path
|
| 137 |
return if dest.exist? && dest.mtime >= zip_path.mtime
|
| 138 |
escaping dest do
|
| 139 |
system "unzip", "-q", "-d", zip_path.dirname.to_path, zip_path.to_path, exception: true
|
| 140 |
end
|
| 141 |
-
zip_path
|
| 142 |
end
|
| 143 |
|
| 144 |
def clear_cache
|
|
|
|
| 132 |
|
| 133 |
class ZipURI < URI
|
| 134 |
def cache
|
| 135 |
+
zip_path = super
|
| 136 |
dest = unzipped_path
|
| 137 |
return if dest.exist? && dest.mtime >= zip_path.mtime
|
| 138 |
escaping dest do
|
| 139 |
system "unzip", "-q", "-d", zip_path.dirname.to_path, zip_path.to_path, exception: true
|
| 140 |
end
|
| 141 |
+
zip_path
|
| 142 |
end
|
| 143 |
|
| 144 |
def clear_cache
|
bindings/ruby/sig/whisper.rbs
CHANGED
|
@@ -412,7 +412,7 @@ module Whisper
|
|
| 412 |
end
|
| 413 |
|
| 414 |
class ZipURI < URI
|
| 415 |
-
def cache: () ->
|
| 416 |
def clear_cache: () -> void
|
| 417 |
end
|
| 418 |
end
|
|
|
|
| 412 |
end
|
| 413 |
|
| 414 |
class ZipURI < URI
|
| 415 |
+
def cache: () -> Pathname
|
| 416 |
def clear_cache: () -> void
|
| 417 |
end
|
| 418 |
end
|
bindings/ruby/test/helper.rb
CHANGED
|
@@ -3,7 +3,7 @@ require "whisper"
|
|
| 3 |
require_relative "jfk_reader/jfk_reader"
|
| 4 |
|
| 5 |
class TestBase < Test::Unit::TestCase
|
| 6 |
-
AUDIO = File.join(__dir__, "
|
| 7 |
|
| 8 |
class << self
|
| 9 |
def whisper
|
|
|
|
| 3 |
require_relative "jfk_reader/jfk_reader"
|
| 4 |
|
| 5 |
class TestBase < Test::Unit::TestCase
|
| 6 |
+
AUDIO = File.join(__dir__, "fixtures", "jfk.wav")
|
| 7 |
|
| 8 |
class << self
|
| 9 |
def whisper
|
bindings/ruby/whispercpp.gemspec
CHANGED
|
@@ -4,7 +4,6 @@ Gem::Specification.new do |s|
|
|
| 4 |
s.name = "whispercpp"
|
| 5 |
s.authors = ["Georgi Gerganov", "Todd A. Fisher"]
|
| 6 |
s.version = '1.3.3'
|
| 7 |
-
s.date = '2025-06-10'
|
| 8 |
s.description = %q{High-performance inference of OpenAI's Whisper automatic speech recognition (ASR) model via Ruby}
|
| 9 |
s.email = '[email protected]'
|
| 10 |
s.extra_rdoc_files = ['LICENSE', 'README.md']
|
|
|
|
| 4 |
s.name = "whispercpp"
|
| 5 |
s.authors = ["Georgi Gerganov", "Todd A. Fisher"]
|
| 6 |
s.version = '1.3.3'
|
|
|
|
| 7 |
s.description = %q{High-performance inference of OpenAI's Whisper automatic speech recognition (ASR) model via Ruby}
|
| 8 |
s.email = '[email protected]'
|
| 9 |
s.extra_rdoc_files = ['LICENSE', 'README.md']
|