Class: Mindee::Input::InferenceParameters

Inherits:
Object
  • Object
show all
Defined in:
lib/mindee/input/inference_parameters.rb

Overview

Parameters to set when sending a file for inference.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_id, rag: nil, raw_text: nil, polygon: nil, confidence: nil, file_alias: nil, webhook_ids: nil, text_context: nil, polling_options: nil, close_file: true, data_schema: nil) ⇒ InferenceParameters

rubocop:disable Metrics/ParameterLists



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/mindee/input/inference_parameters.rb', line 56

def initialize(
  model_id,
  rag: nil,
  raw_text: nil,
  polygon: nil,
  confidence: nil,
  file_alias: nil,
  webhook_ids: nil,
  text_context: nil,
  polling_options: nil,
  close_file: true,
  data_schema: nil
)
  raise Errors::MindeeInputError, 'Model ID is required.' if model_id.empty? || model_id.nil?

  @model_id = model_id
  @rag = rag
  @raw_text = raw_text
  @polygon = polygon
  @confidence = confidence
  @file_alias = file_alias
  @webhook_ids = webhook_ids || []
  @text_context = text_context
  @polling_options = get_clean_polling_options(polling_options)
  @close_file = close_file.nil? || close_file
  @data_schema = DataSchema.new(data_schema) unless data_schema.nil?
  # rubocop:enable Metrics/ParameterLists
end

Instance Attribute Details

#close_fileBoolean? (readonly)



44
45
46
# File 'lib/mindee/input/inference_parameters.rb', line 44

def close_file
  @close_file
end

#confidenceBoolean? (readonly)



25
26
27
# File 'lib/mindee/input/inference_parameters.rb', line 25

def confidence
  @confidence
end

#data_schemaDataSchemaField (readonly)



41
42
43
# File 'lib/mindee/input/inference_parameters.rb', line 41

def data_schema
  @data_schema
end

#file_aliasString? (readonly)



28
29
30
# File 'lib/mindee/input/inference_parameters.rb', line 28

def file_alias
  @file_alias
end

#model_idString (readonly)



10
11
12
# File 'lib/mindee/input/inference_parameters.rb', line 10

def model_id
  @model_id
end

#polling_optionsPollingOptions (readonly)



38
39
40
# File 'lib/mindee/input/inference_parameters.rb', line 38

def polling_options
  @polling_options
end

#polygonBoolean? (readonly)



21
22
23
# File 'lib/mindee/input/inference_parameters.rb', line 21

def polygon
  @polygon
end

#ragBoolean? (readonly)



13
14
15
# File 'lib/mindee/input/inference_parameters.rb', line 13

def rag
  @rag
end

#raw_textBoolean? (readonly)



17
18
19
# File 'lib/mindee/input/inference_parameters.rb', line 17

def raw_text
  @raw_text
end

#text_contextString? (readonly)



32
33
34
# File 'lib/mindee/input/inference_parameters.rb', line 32

def text_context
  @text_context
end

#webhook_idsArray<String>? (readonly)



35
36
37
# File 'lib/mindee/input/inference_parameters.rb', line 35

def webhook_ids
  @webhook_ids
end

Class Method Details

.from_hash(params: {}) ⇒ InferenceParameters

Loads a prediction from a Hash.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/mindee/input/inference_parameters.rb', line 108

def self.from_hash(params: {})
  params.transform_keys!(&:to_sym)

  if params.empty? || params[:model_id].nil? || params[:model_id].empty?
    raise Errors::MindeeInputError, 'Model ID is required.'
  end

  model_id = params.fetch(:model_id)
  rag = params.fetch(:rag, nil)
  raw_text = params.fetch(:raw_text, nil)
  polygon = params.fetch(:polygon, nil)
  confidence = params.fetch(:confidence, nil)
  file_alias = params.fetch(:file_alias, nil)
  webhook_ids = params.fetch(:webhook_ids, [])
  polling_options_input = params.fetch(:page_options, PollingOptions.new)
  if polling_options_input.is_a?(Hash)
    polling_options_input = polling_options_input.transform_keys(&:to_sym)
    PollingOptions.new(
      initial_delay_sec: polling_options_input.fetch(:initial_delay_sec, 2.0),
      delay_sec: polling_options_input.fetch(:delay_sec, 1.5),
      max_retries: polling_options_input.fetch(:max_retries, 80)
    )
  end
  close_file = params.fetch(:close_file, true)
  InferenceParameters.new(model_id, rag: rag, raw_text: raw_text, polygon: polygon, confidence: confidence,
                                    file_alias: file_alias, webhook_ids: webhook_ids, close_file: close_file)
end

Instance Method Details

#validate_async_paramsObject

Validates the parameters for async auto-polling

Raises:

  • (ArgumentError)


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/mindee/input/inference_parameters.rb', line 86

def validate_async_params
  min_delay_sec = 1
  min_initial_delay_sec = 1
  min_retries = 2

  if @polling_options.delay_sec < min_delay_sec
    raise ArgumentError,
          "Cannot set auto-poll delay to less than #{min_delay_sec} second(s)"
  end
  if @polling_options.initial_delay_sec < min_initial_delay_sec
    raise ArgumentError,
          "Cannot set initial parsing delay to less than #{min_initial_delay_sec} second(s)"
  end
  return unless @polling_options.max_retries < min_retries

  raise ArgumentError,
        "Cannot set auto-poll retries to less than #{min_retries}"
end