Class: Clarinet::Models

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/clarinet/models.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#raw_dataHash (readonly)

Returns Raw API data used to construct this instance.

Returns:

  • (Hash)

    Raw API data used to construct this instance



27
28
29
# File 'lib/clarinet/models.rb', line 27

def raw_data
  @raw_data
end

Instance Method Details

#[]Clarinet::Model

Returns:

See Also:

  • Array#[]


24
# File 'lib/clarinet/models.rb', line 24

delegate [:[], :each, :map, :find, :first, :last, :select, :reject, :size] => :@models

#eachObject

See Also:

  • Array#each


24
# File 'lib/clarinet/models.rb', line 24

delegate [:[], :each, :map, :find, :first, :last, :select, :reject, :size] => :@models

#findObject

See Also:

  • Array#find


24
# File 'lib/clarinet/models.rb', line 24

delegate [:[], :each, :map, :find, :first, :last, :select, :reject, :size] => :@models

#firstObject

See Also:

  • Array#first


24
# File 'lib/clarinet/models.rb', line 24

delegate [:[], :each, :map, :find, :first, :last, :select, :reject, :size] => :@models

#get(id) ⇒ Clarinet::Model

Returns a model specified by ID

Parameters:

  • id (String)

    The model’s id

Returns:



86
87
88
89
# File 'lib/clarinet/models.rb', line 86

def get(id)
  data = @app.client.model id
  Clarinet::Model.new @app, data[:model]
end

#init_model(model) ⇒ Clarinet::Model

Returns a Model instance given model id or name. It will call search if name is given.

Parameters:

  • model (String, Hash, Clarinet::Model)

    If String, it is assumed to be model id. Otherwise, if Hash is given, it can have any of the following keys:

Options Hash (model):

  • :id (String)

    Model id

  • :name (String)

    Model name

  • :version (String)

    Model version

  • :type (String) — default: nil

    This can be “concept”, “color”, “embed”, “facedetect”, “cluster” or “blur”

Returns:

  • (Clarinet::Model)

    Model instance corresponding to the given id or the first search result



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/clarinet/models.rb', line 48

def init_model(model)
  model_data = {}

  model_data[:id] = model if model.is_a? String
  model_data = model if model.is_a? Hash
  model_data = model.raw_data if model.is_a? Clarinet::Model

  return Clarinet::Model.new @app, model_data if model_data[:id]

  search_results = search model_data[:name], model_data[:type]

  return search_results.find { |result| result.model_version.id == model_data[:version] }.first if model_data[:version]

  search_results.first
end

#lastObject

See Also:

  • Array#last


24
# File 'lib/clarinet/models.rb', line 24

delegate [:[], :each, :map, :find, :first, :last, :select, :reject, :size] => :@models

#list(options = { page: 1, per_page: 20 }) ⇒ Clarinet::Models

Return all the models

Parameters:

  • options (Hash) (defaults to: { page: 1, per_page: 20 })

    Listing options

Options Hash (options):

  • :page (Int) — default: 1

    The page number

  • :per_page (Int) — default: 20

    Number of models to return per page

Returns:



78
79
80
81
# File 'lib/clarinet/models.rb', line 78

def list(options = { page: 1, per_page: 20 })
  data = @app.client.models options
  Clarinet::Models.new @app, data[:models]
end

#mapObject

See Also:

  • Array#map


24
# File 'lib/clarinet/models.rb', line 24

delegate [:[], :each, :map, :find, :first, :last, :select, :reject, :size] => :@models

#predict(model, inputs) ⇒ Hash

Predict using a specific model

Parameters:

  • model (String, Hash, Clarinet::Model)

    If String, it is assumed to be model id. Otherwise, if Hash is given, it can have any of the following keys:

  • inputs (String, Hash, Array<String>, Array<Hash>)

    An array of objects/object/string pointing to an image resource. A string can either be a url or base64 image bytes. Object keys explained below:

Options Hash (inputs):

  • :image (Hash)

    Object with at least :url or :base64 key as explained below:

    • :url (String) A publicly accessibly

    • :base64 (String) Base64 string representing image bytes

    • :crop (Array<Float>) An array containing the percent to be cropped from top, left, bottom and right

Returns:

  • (Hash)

    Data returned by the API with symbolized keys



69
70
71
# File 'lib/clarinet/models.rb', line 69

def predict(model, inputs)
  init_model(model).predict(inputs)
end

#search(name, type = nil) ⇒ Clarinet::Models

Search for models by name or type

Parameters:

  • name (String)

    The model name

  • type (String) (defaults to: nil)

    This can be “concept”, “color”, “embed”, “facedetect”, “cluster” or “blur”

Returns:



95
96
97
98
99
100
101
102
103
# File 'lib/clarinet/models.rb', line 95

def search(name, type = nil)
  query = {
    name: name,
    type: type
  }

  data = @app.client.models_search query
  Clarinet::Models.new @app, data[:models]
end

#selectObject

See Also:

  • Array#select


24
# File 'lib/clarinet/models.rb', line 24

delegate [:[], :each, :map, :find, :first, :last, :select, :reject, :size] => :@models