Class: Magika

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

Defined Under Namespace

Classes: FileType

Constant Summary collapse

MODEL_NAME =
"standard_v3_3"
ASSETS_DIR =
File.join(__dir__, "../vendor/magika/assets")
MODEL_PATH =
File.join(model_dir, "model.onnx")
MODEL_CONFIG_PATH =
File.join(model_dir, "config.min.json")
MODEL_CONFIG =
JSON.load_file(MODEL_CONFIG_PATH, symbolize_names: true)
TARGET_LABELS_SPACE =
MODEL_CONFIG[:target_labels_space]
MIN_FILE_SIZE_FOR_DL =
MODEL_CONFIG[:min_file_size_for_dl]
VERSION_MAJOR =
MODEL_CONFIG[:version_major]

Instance Method Summary collapse

Constructor Details

#initializeMagika

Returns a new instance of Magika.



19
20
21
# File 'lib/magika.rb', line 19

def initialize
  @session = OnnxRuntime::InferenceSession.new(MODEL_PATH)
end

Instance Method Details

#identify(path) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/magika.rb', line 23

def identify(path)
  features, label = extract_features_or_label(path)
  if features
    target_label = @session.run([:target_label], {bytes: features.reshape(1, FEATURES_SIZE)}, output_type: :numo)[0]
    index = target_label.argmax
    score = target_label[index]
    FileType.new(TARGET_LABELS_SPACE[index], score)
  else
    FileType.new(label, 1.0)
  end
end