ibm-ml 0.2.2 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/.gitignore +3 -0
- data/.rubocop.yml +3 -1
- data/.travis.yml +3 -3
- data/README.md +8 -7
- data/ibm-ml.gemspec +3 -3
- data/lib/ibm/ml.rb +17 -10
- data/lib/ibm/ml/cloud.rb +30 -15
- data/lib/ibm/ml/local.rb +4 -7
- data/lib/ibm/ml/version.rb +1 -1
- data/lib/ibm/ml/zos.rb +1 -1
- metadata +6 -6
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
|
-
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 28e28748b9764e40344b434a24248af233a64c0e8cc8a72b3584f5956af9538a
|
|
4
|
+
data.tar.gz: e3e0f009477cc585f0adf0a1429c286973f80da9a20e61bb4d933956a64d5492
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 5a7e67d7af443cfa10feca24d2ff5c4b36f8efd104e25c3e353786d8b11d6453fdd6b41f56ae6bbd0ae99609d3d8d063a15e3b40538856d7d014e521c7f69db4
|
|
7
|
+
data.tar.gz: 0044f91526e70317f14dc09910110765793483b36dbbac28b9b1c477557d489a4d559b93a6ec740745cd5011ae6eb69ef934df7076819736b38aed88bf0c9a66
|
data/.gitignore
CHANGED
data/.rubocop.yml
CHANGED
data/.travis.yml
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
sudo: false
|
|
2
2
|
language: ruby
|
|
3
3
|
rvm:
|
|
4
|
-
- 2.4.
|
|
5
|
-
- 2.
|
|
4
|
+
- 2.4.3
|
|
5
|
+
- 2.3.6
|
|
6
6
|
cache: bundler
|
|
7
7
|
script:
|
|
8
8
|
- bundle exec rake
|
|
@@ -13,4 +13,4 @@ deploy:
|
|
|
13
13
|
api_key:
|
|
14
14
|
secure: KrW9QsWC2deO9ubwZxQSRcbL8Azj4xkE0KpY30B564/o3qJOFZN98cevVJ7EujyxA+HVXWsdvEsr+PHk17fCSbSlbnmcRS0C7BSy7RCNWgHhDd2hiUG32QXszpr5ZFvXkzlZ9N5gNRzlIpOVckQS2swYCt20GNTn6XQ69a9j7wMER68bDT4gWCkq+xssioS5PaE1hFqOvPDXRltigIz3cZz8+8UDnPzprTyyJ235KNktEFs4OUWGrDwfbgoJs3K744+PvYyTxw+xenlZ6huZqEYmYAws/okIPKenQ/qiTBOx4RRRkVOXWzZ4kUexJiAdIoxKunZ1bPjTtip3AKa1nu3FdgJqq3DZxxX5wftKyeeZYyOoZs+e24Vi79JGXSRcHpMDi578nd5Fqu1UU6weTjS11DHhxX2rPNYRBcvCwXxKCnrw3IY8bzhQ2cF3K1PTV6g6RCXqEY/IKqex6RUw+dcPxqFNaH5VIgyDxs3TUUkEUmsGfHZmyz1MXNjnrLJRQki+H25LpHhOP/lcwJZuLjwNca5uBp01M0R89gsumLVvI5fxpteWjFMUTMZ397sC6Ve0/PmggYxuwLre1nT1N8MMVX79oHXwxeOHltw9lcR4wOc7oIPX8BzgtLvnCvsha9AHXP8F0n0u2F8UrJr25sBQ0CXLpTeTG+zG2t63L5s=
|
|
15
15
|
on:
|
|
16
|
-
rvm: 2.4.
|
|
16
|
+
rvm: 2.4.3
|
data/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# IBM::ML
|
|
2
2
|
[](https://badge.fury.io/rb/ibm-ml)
|
|
3
|
-
[](https://travis-ci.org/IBM-DSE/ibm-ml-ruby)
|
|
4
4
|
|
|
5
5
|
A Ruby gem to invoke the IBM Machine Learning service REST API.
|
|
6
6
|
|
|
@@ -11,7 +11,7 @@ Currently supports:
|
|
|
11
11
|
## Installation
|
|
12
12
|
|
|
13
13
|
### With Gem
|
|
14
|
-
After [installing Ruby](https://www.ruby-lang.org/en/documentation/installation/) >= 2.
|
|
14
|
+
After [installing Ruby](https://www.ruby-lang.org/en/documentation/installation/) >= 2.3:
|
|
15
15
|
|
|
16
16
|
```bash
|
|
17
17
|
$ gem install ibm-ml
|
|
@@ -52,12 +52,13 @@ record = {
|
|
|
52
52
|
|
|
53
53
|
### Cloud
|
|
54
54
|
```ruby
|
|
55
|
-
CLOUD_USERNAME
|
|
56
|
-
CLOUD_PASSWORD
|
|
57
|
-
|
|
55
|
+
CLOUD_USERNAME = # WML service username
|
|
56
|
+
CLOUD_PASSWORD = # WML service password
|
|
57
|
+
CLOUD_INSTANCE_ID = # WML instance ID
|
|
58
|
+
DEPLOYMENT_ID = # deployment ID
|
|
58
59
|
|
|
59
60
|
# Create the service object
|
|
60
|
-
ml_service = IBM::ML::Cloud.new(CLOUD_USERNAME, CLOUD_PASSWORD)
|
|
61
|
+
ml_service = IBM::ML::Cloud.new(CLOUD_USERNAME, CLOUD_PASSWORD, CLOUD_INSTANCE_ID)
|
|
61
62
|
|
|
62
63
|
# Fetch an authentication token
|
|
63
64
|
pp ml_service.fetch_token
|
|
@@ -114,5 +115,5 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
|
114
115
|
|
|
115
116
|
## Contributing
|
|
116
117
|
|
|
117
|
-
Bug reports and pull requests are welcome on GitHub at [https://github.com/IBM-
|
|
118
|
+
Bug reports and pull requests are welcome on GitHub at [https://github.com/IBM-DSE/ibm-ml-ruby](https://github.com/IBM-DSE/ibm-ml-ruby).
|
|
118
119
|
|
data/ibm-ml.gemspec
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
|
|
2
|
-
lib = File.expand_path('
|
|
2
|
+
lib = File.expand_path('lib', __dir__)
|
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
require 'ibm/ml/version'
|
|
5
5
|
|
|
@@ -13,7 +13,7 @@ Gem::Specification.new do |spec|
|
|
|
13
13
|
spec.description = 'Simplifies development of applications using an IBM Machine Learning service '\
|
|
14
14
|
'by providing methods for getting deployments and calling them. '\
|
|
15
15
|
'Operates with both IBM Watson Machine Learning as well as Machine Learning on DSX Local.'
|
|
16
|
-
spec.homepage = 'https://github.com/IBM-
|
|
16
|
+
spec.homepage = 'https://github.com/IBM-DSE/ibm-ml-ruby'
|
|
17
17
|
|
|
18
18
|
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
|
19
19
|
f.match(%r{^(test|spec|features)/})
|
|
@@ -25,5 +25,5 @@ Gem::Specification.new do |spec|
|
|
|
25
25
|
spec.add_development_dependency 'bundler', '~> 1.14'
|
|
26
26
|
spec.add_development_dependency 'rake', '~> 10.0'
|
|
27
27
|
spec.add_development_dependency 'rspec', '~> 3.0'
|
|
28
|
-
spec.add_development_dependency 'rubocop', '~> 0.
|
|
28
|
+
spec.add_development_dependency 'rubocop', '~> 0.54'
|
|
29
29
|
end
|
data/lib/ibm/ml.rb
CHANGED
|
@@ -9,7 +9,8 @@ module IBM
|
|
|
9
9
|
require_relative 'ml/local'
|
|
10
10
|
require_relative 'ml/zos'
|
|
11
11
|
|
|
12
|
-
def initialize(username, password)
|
|
12
|
+
def initialize(host, username, password)
|
|
13
|
+
@host = host
|
|
13
14
|
@username = username
|
|
14
15
|
@password = password
|
|
15
16
|
uri = URI("https://#{@host}")
|
|
@@ -18,19 +19,25 @@ module IBM
|
|
|
18
19
|
end
|
|
19
20
|
|
|
20
21
|
def fetch_token
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
url = URI(ldap_url)
|
|
23
|
+
request = ldap_request(url)
|
|
24
|
+
response = @http.request request
|
|
24
25
|
|
|
25
|
-
response
|
|
26
|
+
if response.is_a? Net::HTTPClientError
|
|
27
|
+
begin
|
|
28
|
+
body = JSON.parse(response.read_body)
|
|
29
|
+
raise(AuthError, body['errors'][0]['message']) if body.key?('errors')
|
|
30
|
+
rescue JSON::ParserError
|
|
31
|
+
raise response.class.to_s
|
|
32
|
+
end
|
|
33
|
+
end
|
|
26
34
|
|
|
27
|
-
raise response.class.to_s if response.is_a? Net::HTTPClientError
|
|
28
35
|
process_ldap_response(response)
|
|
29
36
|
end
|
|
30
37
|
|
|
31
38
|
def query_ml_score(score, field, values_key)
|
|
32
39
|
fields = score['fields'].map(&:upcase)
|
|
33
|
-
index
|
|
40
|
+
index = fields.index(field.upcase)
|
|
34
41
|
score[values_key].map { |record| record[index] }[0]
|
|
35
42
|
end
|
|
36
43
|
|
|
@@ -52,14 +59,14 @@ module IBM
|
|
|
52
59
|
end
|
|
53
60
|
|
|
54
61
|
def post_request(url, body)
|
|
55
|
-
request
|
|
62
|
+
request = Net::HTTP::Post.new(url, post_header)
|
|
56
63
|
request.body = body
|
|
57
|
-
response
|
|
64
|
+
response = @http.request(request)
|
|
58
65
|
JSON.parse(response.read_body)
|
|
59
66
|
end
|
|
60
67
|
|
|
61
68
|
def post_header
|
|
62
|
-
header
|
|
69
|
+
header = auth_header
|
|
63
70
|
header['content-type'] = 'application/json'
|
|
64
71
|
header
|
|
65
72
|
end
|
data/lib/ibm/ml/cloud.rb
CHANGED
|
@@ -4,18 +4,18 @@ module IBM
|
|
|
4
4
|
class Cloud
|
|
5
5
|
include ML
|
|
6
6
|
|
|
7
|
-
def initialize(username, password)
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
def initialize(username, password, instance_id)
|
|
8
|
+
super('ibm-watson-ml.mybluemix.net', username, password)
|
|
9
|
+
@instance_id = instance_id
|
|
10
10
|
@http.use_ssl = true
|
|
11
11
|
end
|
|
12
12
|
|
|
13
13
|
def models
|
|
14
|
-
get_request "
|
|
14
|
+
get_request "#{instance_url}/published_models", 'resources'
|
|
15
15
|
end
|
|
16
16
|
|
|
17
17
|
def deployments
|
|
18
|
-
get_request "
|
|
18
|
+
get_request "#{instance_url}/deployments", 'resources'
|
|
19
19
|
end
|
|
20
20
|
|
|
21
21
|
def deployment(deployment_id)
|
|
@@ -27,7 +27,7 @@ module IBM
|
|
|
27
27
|
end
|
|
28
28
|
|
|
29
29
|
def model(model_id)
|
|
30
|
-
get_request "
|
|
30
|
+
get_request "#{instance_url}/published_models/#{model_id}", 'entity'
|
|
31
31
|
end
|
|
32
32
|
|
|
33
33
|
def model_by_name(name)
|
|
@@ -40,12 +40,17 @@ module IBM
|
|
|
40
40
|
end
|
|
41
41
|
|
|
42
42
|
def score(deployment_id, record)
|
|
43
|
-
deployment
|
|
44
|
-
|
|
43
|
+
deployment = deployment(deployment_id)['entity']
|
|
44
|
+
field_names = model_of_deployment(deployment)['entity']['input_data_schema']['fields'].map do |f|
|
|
45
|
+
f['name']
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
cleaned_rec = record.map { |k, v| [k.downcase, v] }.to_h
|
|
49
|
+
record_values = field_names.map { |name| cleaned_rec[name.downcase] }
|
|
45
50
|
|
|
46
|
-
response = post_request deployment['
|
|
47
|
-
fields:
|
|
48
|
-
values: [
|
|
51
|
+
response = post_request deployment['scoring_url'], {
|
|
52
|
+
fields: field_names,
|
|
53
|
+
values: [record_values]
|
|
49
54
|
}.to_json
|
|
50
55
|
|
|
51
56
|
raise(response['message'] + ' : ' + response['description']) if response.key?('message')
|
|
@@ -59,12 +64,15 @@ module IBM
|
|
|
59
64
|
private
|
|
60
65
|
|
|
61
66
|
def ldap_url
|
|
62
|
-
"https://#{@host}/
|
|
67
|
+
"https://#{@host}/v3/identity/token"
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def instance_url
|
|
71
|
+
"https://#{@host}/v3/wml_instances/#{@instance_id}"
|
|
63
72
|
end
|
|
64
73
|
|
|
65
|
-
def ldap_request(
|
|
66
|
-
|
|
67
|
-
request = Net::HTTP::Get.new url
|
|
74
|
+
def ldap_request(url)
|
|
75
|
+
request = Net::HTTP::Get.new(url)
|
|
68
76
|
request.basic_auth @username, @password
|
|
69
77
|
request
|
|
70
78
|
end
|
|
@@ -77,6 +85,7 @@ module IBM
|
|
|
77
85
|
response['resources'].each do |resource|
|
|
78
86
|
return resource if resource['metadata']['guid'] == guid
|
|
79
87
|
end
|
|
88
|
+
raise(QueryError, "Could not find resource with id \"#{guid}\"")
|
|
80
89
|
end
|
|
81
90
|
|
|
82
91
|
def find_by_name(response, name)
|
|
@@ -85,8 +94,14 @@ module IBM
|
|
|
85
94
|
end
|
|
86
95
|
raise(QueryError, "Could not find resource with name \"#{name}\"")
|
|
87
96
|
end
|
|
97
|
+
|
|
98
|
+
def model_of_deployment(deployment)
|
|
99
|
+
model(deployment['published_model']['guid'])
|
|
100
|
+
end
|
|
88
101
|
end
|
|
89
102
|
|
|
103
|
+
class AuthError < StandardError
|
|
104
|
+
end
|
|
90
105
|
class QueryError < StandardError
|
|
91
106
|
end
|
|
92
107
|
end
|
data/lib/ibm/ml/local.rb
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
|
|
2
1
|
module IBM
|
|
3
2
|
module ML
|
|
4
3
|
# Class for calling Local Machine Learning scoring service
|
|
@@ -6,13 +5,12 @@ module IBM
|
|
|
6
5
|
include ML
|
|
7
6
|
|
|
8
7
|
def initialize(host, username, password)
|
|
9
|
-
|
|
10
|
-
super(username, password)
|
|
8
|
+
super
|
|
11
9
|
@http.use_ssl = true
|
|
12
10
|
@http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
|
13
11
|
end
|
|
14
12
|
|
|
15
|
-
def score(deployment_id,
|
|
13
|
+
def score(deployment_id, record)
|
|
16
14
|
url = URI("https://#{@host}/v2/scoring/online/#{deployment_id}")
|
|
17
15
|
|
|
18
16
|
# noinspection RubyStringKeysInHashInspection
|
|
@@ -22,7 +20,7 @@ module IBM
|
|
|
22
20
|
}
|
|
23
21
|
|
|
24
22
|
request = Net::HTTP::Post.new(url, header)
|
|
25
|
-
request.body = { fields:
|
|
23
|
+
request.body = { fields: record.keys, records: [record.values] }.to_json
|
|
26
24
|
|
|
27
25
|
response = @http.request(request)
|
|
28
26
|
|
|
@@ -44,8 +42,7 @@ module IBM
|
|
|
44
42
|
"https://#{@host}/v2/identity/token"
|
|
45
43
|
end
|
|
46
44
|
|
|
47
|
-
def ldap_request(
|
|
48
|
-
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
|
45
|
+
def ldap_request(uri)
|
|
49
46
|
request = Net::HTTP::Get.new uri
|
|
50
47
|
request['Username'] = @username
|
|
51
48
|
request['Password'] = @password
|
data/lib/ibm/ml/version.rb
CHANGED
data/lib/ibm/ml/zos.rb
CHANGED
|
@@ -18,7 +18,7 @@ module IBM
|
|
|
18
18
|
"http://#{@host}:#{@ldap_port}/v2/identity/ldap"
|
|
19
19
|
end
|
|
20
20
|
|
|
21
|
-
def ldap_request(
|
|
21
|
+
def ldap_request(url)
|
|
22
22
|
request = Net::HTTP::Post.new(url)
|
|
23
23
|
request.set_content_type 'application/json'
|
|
24
24
|
request.body = { username: @username, password: @password }.to_json
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: ibm-ml
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- David Thomason
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2018-03-28 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: bundler
|
|
@@ -58,14 +58,14 @@ dependencies:
|
|
|
58
58
|
requirements:
|
|
59
59
|
- - "~>"
|
|
60
60
|
- !ruby/object:Gem::Version
|
|
61
|
-
version: '0.
|
|
61
|
+
version: '0.54'
|
|
62
62
|
type: :development
|
|
63
63
|
prerelease: false
|
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
|
65
65
|
requirements:
|
|
66
66
|
- - "~>"
|
|
67
67
|
- !ruby/object:Gem::Version
|
|
68
|
-
version: '0.
|
|
68
|
+
version: '0.54'
|
|
69
69
|
description: Simplifies development of applications using an IBM Machine Learning
|
|
70
70
|
service by providing methods for getting deployments and calling them. Operates
|
|
71
71
|
with both IBM Watson Machine Learning as well as Machine Learning on DSX Local.
|
|
@@ -90,7 +90,7 @@ files:
|
|
|
90
90
|
- lib/ibm/ml/local.rb
|
|
91
91
|
- lib/ibm/ml/version.rb
|
|
92
92
|
- lib/ibm/ml/zos.rb
|
|
93
|
-
homepage: https://github.com/IBM-
|
|
93
|
+
homepage: https://github.com/IBM-DSE/ibm-ml-ruby
|
|
94
94
|
licenses:
|
|
95
95
|
- Apache-2.0
|
|
96
96
|
metadata: {}
|
|
@@ -110,7 +110,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
110
110
|
version: '0'
|
|
111
111
|
requirements: []
|
|
112
112
|
rubyforge_project:
|
|
113
|
-
rubygems_version: 2.6
|
|
113
|
+
rubygems_version: 2.7.6
|
|
114
114
|
signing_key:
|
|
115
115
|
specification_version: 4
|
|
116
116
|
summary: Client library for calling IBM Machine Learning API
|