Class: Secryst::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/secryst/model.rb

Defined Under Namespace

Classes: Onnx

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#input_vocabObject

Returns the value of attribute input_vocab.



3
4
5
# File 'lib/secryst/model.rb', line 3

def input_vocab
  @input_vocab
end

#modelObject

Returns the value of attribute model.



3
4
5
# File 'lib/secryst/model.rb', line 3

def model
  @model
end

#target_vocabObject

Returns the value of attribute target_vocab.



3
4
5
# File 'lib/secryst/model.rb', line 3

def target_vocab
  @target_vocab
end

Class Method Details

.from_file(model_file) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/secryst/model.rb', line 5

def self.from_file(model_file)
  # A models.yaml model id resolves (download -> verify -> cache)
  # through the same contract as the Python/TS runtimes.
  model_file = IMF.resolve(model_file) unless model_file.to_s.end_with?('.zip') || File.file?(model_file.to_s)
  model_file = Provisioning.locate(model_file)

  Zip::File.open(model_file) do |zip_file|
     = zip_file.glob('metadata.yaml').first
     = YAML.safe_load(.get_input_stream.read) if 

    # IMF v1: the Interscript Model Format zip.
    return Byt5Onnx.new(model_file) if  && ['format'] == 'imf-v1'

    name =  && ['name']

    # Modern byte-level seq2seq (ByT5 family): encoder.onnx + decoder.onnx.
    return Byt5Onnx.new(model_file) if name == 'byt5'

    # Legacy single-file ONNX transformer zips (vocabs.yaml based).
    vocabs = zip_file.glob('vocabs.yaml').first
    raise 'vocabs.yaml is missing in model zip!' unless vocabs
    vocabs = YAML.safe_load(vocabs.get_input_stream.read)
    input_vocab = Vocab.new(vocabs['input'], specials: [])
    target_vocab = Vocab.new(vocabs['target'], specials: [])

    onnx = zip_file.glob('*.onnx').first
    raise 'onnx model file is missing in model zip!' unless onnx
    Onnx.new(onnx.get_input_stream.read, input_vocab, target_vocab)
  end
end