Class: IBM::ML::Cloud

Inherits:
Object
  • Object
show all
Includes:
IBM::ML
Defined in:
lib/ibm/ml/cloud.rb

Overview

Class for calling cloud-based Watson Machine Learning scoring service

Constant Summary

Constants included from IBM::ML

VERSION

Instance Method Summary collapse

Methods included from IBM::ML

#fetch_token

Constructor Details

#initialize(username, password) ⇒ Cloud

Returns a new instance of Cloud.



7
8
9
10
11
# File 'lib/ibm/ml/cloud.rb', line 7

def initialize(username, password)
  @host = 'ibm-watson-ml.mybluemix.net'
  super
  @http.use_ssl = true
end

Instance Method Details

#deployment(deployment_id) ⇒ Object



21
22
23
# File 'lib/ibm/ml/cloud.rb', line 21

def deployment(deployment_id)
  find_by_id(deployments, deployment_id)
end

#deployment_by_name(name) ⇒ Object



25
26
27
# File 'lib/ibm/ml/cloud.rb', line 25

def deployment_by_name(name)
  find_by_name(deployments, name)
end

#deploymentsObject



17
18
19
# File 'lib/ibm/ml/cloud.rb', line 17

def deployments
  get_request "https://#{@host}/v2/deployments", 'resources'
end

#model(model_id) ⇒ Object



29
30
31
# File 'lib/ibm/ml/cloud.rb', line 29

def model(model_id)
  get_request "https://#{@host}/v2/published_models/#{model_id}", 'entity'
end

#model_by_name(name) ⇒ Object



33
34
35
# File 'lib/ibm/ml/cloud.rb', line 33

def model_by_name(name)
  find_by_name(models, name)
end

#modelsObject



13
14
15
# File 'lib/ibm/ml/cloud.rb', line 13

def models
  get_request "https://#{@host}/v2/published_models", 'resources'
end

#query_score(score, field) ⇒ Object



55
56
57
58
59
# File 'lib/ibm/ml/cloud.rb', line 55

def query_score(score, field)
  fields = score['fields'].map(&:upcase)
  index = fields.index(field.upcase)
  score['values'].map { |record| record[index] }[0]
end

#score(deployment_id, record) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ibm/ml/cloud.rb', line 42

def score(deployment_id, record)
  deployment = deployment(deployment_id)['entity']
  model_fields = model(deployment['published_model']['guid'])['entity']['input_data_schema']['fields']

  response = post_request deployment['scoring_href'], {
    fields: model_fields.map { |field| field['name'] },
    values: [record.values]
  }.to_json

  raise(response['message'] + ' : ' + response['description']) if response.key?('message')
  response
end

#score_by_name(name, record) ⇒ Object



37
38
39
40
# File 'lib/ibm/ml/cloud.rb', line 37

def score_by_name(name, record)
  deployment = find_by_name(deployments, name)
  score(deployment['metadata']['guid'], record)
end