Class: ModelFox::Model
- Inherits:
-
Object
- Object
- ModelFox::Model
- Defined in:
- lib/modelfox/modelfox.rb
Overview
Use this class to load a model, make predictions, and log events to the app.
Class Method Summary collapse
-
.from_bytes(bytes, options: nil) ⇒ Model
Load a model from bytes instead of a file.
-
.from_path(path, options: nil) ⇒ Model
Load a model from the ‘.modelfox` file at `path`.
Instance Method Summary collapse
-
#enqueue_log_prediction(identifier:, input:, output:, options: nil) ⇒ Object
Add a prediction event to the queue.
-
#enqueue_log_true_value(identifier:, true_value:) ⇒ Object
Add a true value event to the queue.
-
#flush_log_queue ⇒ Object
Send all events in the queue to the app.
-
#id ⇒ Object
Retrieve the model’s id.
-
#initialize(c_model, options: nil) ⇒ Model
constructor
A new instance of Model.
-
#log_prediction(identifier:, input:, output:, options: nil) ⇒ Object
Send a prediction event to the app.
-
#log_true_value(identifier:, true_value:) ⇒ Object
Send a true value event to the app.
-
#predict(input, options: nil) ⇒ Array<RegressionPredictOutput, BinaryClassificationPredictOutput, MulticlassClassificationPredictOutput>, ...
Make a prediction!.
Constructor Details
#initialize(c_model, options: nil) ⇒ Model
Returns a new instance of Model.
257 258 259 260 261 |
# File 'lib/modelfox/modelfox.rb', line 257 def initialize(c_model, options: nil) @modelfox_url = &.modelfox_url.nil? ? 'https://app.modelfox.dev' : &.modelfox_url @log_queue = [] @model = FFI::AutoPointer.new(c_model.read_pointer, LibModelFox.method(:modelfox_model_delete)) end |
Class Method Details
.from_bytes(bytes, options: nil) ⇒ Model
Load a model from bytes instead of a file. You should use this only if you already have a ‘.modelfox` loaded into memory. Otherwise, use `Model.from_path`, which is faster because it memory maps the file.
245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/modelfox/modelfox.rb', line 245 def self.from_bytes(bytes, options: nil) c_model = FFI::MemoryPointer.new(:pointer) c_err = LibModelFox.modelfox_model_from_bytes(bytes, bytes.size, c_model) unless err.null? c_err = FFI::AutoPointer.new(c_err, LibModelFox.method(:modelfox_error_delete)) c_error_s = LibModelFox::ModelFoxStringView.new LibModelFox.(c_err, c_error_s) raise errors.into_string end new(c_model, options: ) end |
.from_path(path, options: nil) ⇒ Model
Load a model from the ‘.modelfox` file at `path`.
229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/modelfox/modelfox.rb', line 229 def self.from_path(path, options: nil) c_model = FFI::MemoryPointer.new(:pointer) c_err = LibModelFox.modelfox_model_from_path(path, c_model) unless c_err.null? c_err = FFI::AutoPointer.new(c_err, LibModelFox.method(:modelfox_error_delete)) c_error_s = LibModelFox::ModelFoxStringView.new LibModelFox.(c_err, c_error_s) raise c_error_s.into_string end new(c_model, options: ) end |
Instance Method Details
#enqueue_log_prediction(identifier:, input:, output:, options: nil) ⇒ Object
Add a prediction event to the queue. Remember to call ‘flush_log_queue` at a later point to send the event to the app.
307 308 309 310 311 312 313 314 315 |
# File 'lib/modelfox/modelfox.rb', line 307 def enqueue_log_prediction(identifier:, input:, output:, options: nil) event = prediction_event( identifier: identifier, input: input, output: output, options: ) log_queue.push(event) end |
#enqueue_log_true_value(identifier:, true_value:) ⇒ Object
Add a true value event to the queue. Remember to call ‘flush_log_queue` at a later point to send the event to the app.
331 332 333 334 335 336 337 |
# File 'lib/modelfox/modelfox.rb', line 331 def enqueue_log_true_value(identifier:, true_value:) event = true_value_event( identifier: identifier, true_value: true_value ) log_queue.push(event) end |
#flush_log_queue ⇒ Object
Send all events in the queue to the app.
340 341 342 343 |
# File 'lib/modelfox/modelfox.rb', line 340 def flush_log_queue log_events(@log_queue) @log_queue = [] end |
#id ⇒ Object
Retrieve the model’s id.
264 265 266 267 268 |
# File 'lib/modelfox/modelfox.rb', line 264 def id c_id = LibModelFox::ModelFoxStringView.new LibModelFox.modelfox_model_get_id(@model, c_id) c_id.into_string end |
#log_prediction(identifier:, input:, output:, options: nil) ⇒ Object
Send a prediction event to the app. If you want to batch events, you can use ‘enqueue_log_prediction` instead.
292 293 294 295 296 297 298 299 300 |
# File 'lib/modelfox/modelfox.rb', line 292 def log_prediction(identifier:, input:, output:, options: nil) event = prediction_event( identifier: identifier, input: input, output: output, options: ) log_event(event) end |
#log_true_value(identifier:, true_value:) ⇒ Object
Send a true value event to the app. If you want to batch events, you can use ‘enqueue_log_true_value` instead.
320 321 322 323 324 325 326 |
# File 'lib/modelfox/modelfox.rb', line 320 def log_true_value(identifier:, true_value:) event = true_value_event( identifier: identifier, true_value: true_value ) log_event(event) end |
#predict(input, options: nil) ⇒ Array<RegressionPredictOutput, BinaryClassificationPredictOutput, MulticlassClassificationPredictOutput>, ...
Make a prediction!
274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/modelfox/modelfox.rb', line 274 def predict(input, options: nil) is_array = input.is_a?(Array) input = is_array ? input : [input] c_input_vec = new_predict_input_vec(input) = () c_output_vec = FFI::MemoryPointer.new(:pointer) c_error = LibModelFox.modelfox_model_predict(@model, c_input_vec, , c_output_vec) raise 'modelfox error' unless c_error.null? c_output_vec = FFI::AutoPointer.new(c_output_vec.read_pointer, LibModelFox.method(:modelfox_predict_output_vec_delete)) output = predict_output_vec_from_modelfox_predict_output_vec(c_output_vec) is_array ? output : output[0] end |