Class: Cohere::Transcribe::Alignment::ModelProvider

Inherits:
Object
  • Object
show all
Defined in:
lib/cohere/transcribe/alignment/aligner.rb

Overview

Integrity-pinned access to an ONNX export of the exact reference MMS checkpoint. Model weights remain in the standard Hugging Face cache and are never included in the gem or passed through another language.

Constant Summary collapse

REPOSITORY =
"onnx-community/mms-300m-1130-forced-aligner-ONNX"
REVISION =
"2100fb247d8e43962eef24491597fbeb8b469531"
SOURCE_REPOSITORY =
"MahmoudAshraf/mms-300m-1130-forced-aligner"
SOURCE_REVISION =
"49402e9577b1158620820667c218cd494cc44486"
LICENSE =
"CC-BY-NC-4.0"
UTILITY_REPOSITORY =
"https://github.com/MahmoudAshraf97/ctc-forced-aligner.git"
UTILITY_REVISION =
"11855d1de76af2b490dd2e8e2db2661805ae90a0"
UROMAN_COMPATIBILITY_VERSION =
"1.3.1.1-compatible-ruby-port"
ARTIFACTS =
{
  "fp32" => ModelArtifact.new(
    filename: "onnx/model.onnx",
    size: 1_262_529_881,
    sha256: "429e5d05c62acc8a9264db874a1b131e359fc626e40c253ac7b1fe52b11149b4"
  ),
  "fp16" => ModelArtifact.new(
    filename: "onnx/model_fp16.onnx",
    size: 631_591_191,
    sha256: "e98082b382375f3528ec7514e175b5cd0eb77fcc4d4531a7142b9e45a1ce6deb"
  )
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hub: Hub.new, artifacts: ARTIFACTS) ⇒ ModelProvider

Returns a new instance of ModelProvider.



50
51
52
53
# File 'lib/cohere/transcribe/alignment/aligner.rb', line 50

def initialize(hub: Hub.new, artifacts: ARTIFACTS)
  @hub = hub
  @artifacts = artifacts
end

Instance Attribute Details

#hubObject (readonly)

Returns the value of attribute hub.



48
49
50
# File 'lib/cohere/transcribe/alignment/aligner.rb', line 48

def hub
  @hub
end

Instance Method Details

#fetch(dtype) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/cohere/transcribe/alignment/aligner.rb', line 55

def fetch(dtype)
  artifact = @artifacts.fetch(dtype) do
    raise ArgumentError, "Unsupported aligner dtype: #{dtype.inspect}"
  end
  path = hub.download(REPOSITORY, artifact.filename, revision: REVISION)
  with_integrity_lock(Pathname("#{path}.integrity.lock")) do |lock|
    lock.flock(File::LOCK_EX)
    return path if valid?(path, artifact)

    FileUtils.rm_f(path)
    path = hub.download(REPOSITORY, artifact.filename, revision: REVISION)
    unless valid?(path, artifact)
      FileUtils.rm_f(path)
      raise BackendUnavailable,
            "Downloaded aligner #{artifact.filename} failed its pinned size/SHA-256 check"
    end
  end
  path
rescue Hub::Error, SystemCallError => e
  raise BackendUnavailable, "Cannot prepare MMS aligner: #{e.message}"
end