ml_client 0.1.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 561daa295dedc527ddcf186be65baa736b9b1c99d6dc6dc11048065e9ad33c20
4
+ data.tar.gz: f179f09adabf2fbb81033dbf5042fb5aaa8101c222eef53a0648925839caae87
5
+ SHA512:
6
+ metadata.gz: bc4f696fcd968d1019d8c8fd3f7b8f8213c5cf345457dea454a7239e528baac115c4597cc433cedd6c3b3070a14a26de822cfc2083e989097006a06f36037188
7
+ data.tar.gz: 62c07b6e58d94490ac52ff669ecb115419a72e4967b9dbe8e9347ba9c51534fbd83a3717856807d20ad6c79ceb98abce04ea858b681832f8f0abb73c570010f5
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ *.gem
data/.rubocop.yml ADDED
@@ -0,0 +1,220 @@
1
+ AllCops:
2
+ Exclude:
3
+ - 'bin/*'
4
+ - 'config/**/*'
5
+ - 'coverage/**/*'
6
+ - 'db/**/*'
7
+ - 'log/**/*'
8
+ - 'public/**/*'
9
+ - 'tmp/**/*'
10
+ NewCops: enable
11
+
12
+ Layout/BeginEndAlignment:
13
+ Enabled: true
14
+
15
+ Layout/DotPosition:
16
+ EnforcedStyle: trailing
17
+
18
+ Layout/EmptyLinesAroundAttributeAccessor:
19
+ Enabled: false
20
+
21
+ Layout/LineLength:
22
+ Max: 120
23
+
24
+ Layout/SpaceAroundMethodCallOperator:
25
+ Enabled: true
26
+
27
+ Lint/BinaryOperatorWithIdenticalOperands:
28
+ Enabled: false
29
+
30
+ Lint/ConstantDefinitionInBlock:
31
+ Enabled: true
32
+
33
+ Lint/DeprecatedOpenSSLConstant:
34
+ Enabled: false
35
+
36
+ Lint/DuplicateElsifCondition:
37
+ Enabled: true
38
+
39
+ Lint/DuplicateRequire:
40
+ Enabled: true
41
+
42
+ Lint/DuplicateRescueException:
43
+ Enabled: true
44
+
45
+ Lint/EmptyConditionalBody:
46
+ Enabled: true
47
+
48
+ Lint/EmptyFile:
49
+ Enabled: false
50
+
51
+ Lint/FloatComparison:
52
+ Enabled: false
53
+
54
+ Lint/HashCompareByIdentity:
55
+ Enabled: false
56
+
57
+ Lint/IdentityComparison:
58
+ Enabled: true
59
+
60
+ Lint/MissingSuper:
61
+ Enabled: false
62
+
63
+ Lint/MixedRegexpCaptureTypes:
64
+ Enabled: true
65
+
66
+ Lint/OutOfRangeRegexpRef:
67
+ Enabled: true
68
+
69
+ Lint/RaiseException:
70
+ Enabled: true
71
+
72
+ Lint/RedundantSafeNavigation:
73
+ Enabled: false
74
+
75
+ Lint/SelfAssignment:
76
+ Enabled: true
77
+
78
+ Lint/StructNewOverride:
79
+ Enabled: true
80
+
81
+ Lint/TopLevelReturnWithArgument:
82
+ Enabled: true
83
+
84
+ Lint/TrailingCommaInAttributeDeclaration:
85
+ Enabled: true
86
+
87
+ Lint/UnreachableLoop:
88
+ Enabled: true
89
+
90
+ Lint/UselessMethodDefinition:
91
+ Enabled: true
92
+
93
+ Lint/UselessTimes:
94
+ Enabled: false
95
+
96
+ Metrics/AbcSize:
97
+ Enabled: false
98
+
99
+ Metrics/BlockLength:
100
+ Enabled: false
101
+
102
+ Metrics/ClassLength:
103
+ Enabled: false
104
+
105
+ Metrics/CyclomaticComplexity:
106
+ Enabled: false
107
+
108
+ Metrics/MethodLength:
109
+ Include:
110
+ - 'app/controllers/**/*.rb'
111
+ Enabled: true
112
+ Max: 20
113
+
114
+ Metrics/ModuleLength:
115
+ Enabled: false
116
+
117
+ Metrics/PerceivedComplexity:
118
+ Enabled: false
119
+
120
+ Naming/PredicateName:
121
+ Enabled: false
122
+
123
+ Style/AccessorGrouping:
124
+ Enabled: true
125
+ EnforcedStyle: separated
126
+
127
+ Style/ArrayCoercion:
128
+ Enabled: true
129
+
130
+ Style/AsciiComments:
131
+ Enabled: false
132
+
133
+ Style/BisectedAttrAccessor:
134
+ Enabled: true
135
+
136
+ Style/CaseLikeIf:
137
+ Enabled: true
138
+
139
+ Style/ClassAndModuleChildren:
140
+ EnforcedStyle: compact
141
+
142
+ Style/ClassEqualityComparison:
143
+ Enabled: true
144
+
145
+ Style/CombinableLoops:
146
+ Enabled: false
147
+
148
+ Style/Documentation:
149
+ Exclude:
150
+ - 'test/**/*'
151
+ - 'app/presenters/**/*'
152
+
153
+ Style/DocumentationMethod:
154
+ Enabled: false
155
+
156
+ Style/EmptyMethod:
157
+ EnforcedStyle: expanded
158
+
159
+ Style/ExplicitBlockArgument:
160
+ Enabled: true
161
+
162
+ Style/ExponentialNotation:
163
+ Enabled: false
164
+
165
+ Style/GlobalStdStream:
166
+ Enabled: true
167
+
168
+ Style/HashAsLastArrayItem:
169
+ Enabled: true
170
+
171
+ Style/HashEachMethods:
172
+ Enabled: true
173
+
174
+ Style/HashLikeCase:
175
+ Enabled: true
176
+
177
+ Style/HashTransformKeys:
178
+ Enabled: true
179
+
180
+ Style/HashTransformValues:
181
+ Enabled: true
182
+
183
+ Style/KeywordParametersOrder:
184
+ Enabled: true
185
+
186
+ Style/OptionalBooleanParameter:
187
+ Enabled: true
188
+
189
+ Style/RedundantAssignment:
190
+ Enabled: true
191
+
192
+ Style/RedundantFetchBlock:
193
+ Enabled: true
194
+
195
+ Style/RedundantFileExtensionInRequire:
196
+ Enabled: true
197
+
198
+ Style/RedundantRegexpCharacterClass:
199
+ Enabled: true
200
+
201
+ Style/RedundantRegexpEscape:
202
+ Enabled: true
203
+
204
+ Style/RedundantSelfAssignment:
205
+ Enabled: false
206
+
207
+ Style/SingleArgumentDig:
208
+ Enabled: false
209
+
210
+ Style/SlicingWithRange:
211
+ Enabled: false
212
+
213
+ Style/SoleNestedConditional:
214
+ Enabled: true
215
+
216
+ Style/StringConcatenation:
217
+ Enabled: true
218
+
219
+ Style/RegexpLiteral:
220
+ Enabled: false
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in ml_client.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 12.0'
data/Gemfile.lock ADDED
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ml_client (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ addressable (2.7.0)
10
+ public_suffix (>= 2.0.2, < 5.0)
11
+ crack (0.4.5)
12
+ rexml
13
+ hashdiff (1.0.1)
14
+ minitest (5.14.4)
15
+ public_suffix (4.0.6)
16
+ rake (12.3.3)
17
+ rexml (3.2.5)
18
+ vcr (6.0.0)
19
+ webmock (3.12.2)
20
+ addressable (>= 2.3.6)
21
+ crack (>= 0.3.2)
22
+ hashdiff (>= 0.4.0, < 2.0.0)
23
+
24
+ PLATFORMS
25
+ ruby
26
+
27
+ DEPENDENCIES
28
+ minitest (>= 5.14)
29
+ ml_client!
30
+ rake (~> 12.0)
31
+ vcr (~> 6.0)
32
+ webmock (~> 3.12)
33
+
34
+ BUNDLED WITH
35
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Flatlooker
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # MLClient : simple predictions with google cloud functions
2
+
3
+ This gem allows interaction within ruby code with various APIs hosted in google cloud functions. It is primarly designed to predict results of machine learning models and works synchronously (for light predictions) or asynchronously (for heavier predictions).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'ml_client'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install ml_client
20
+
21
+ ## Usage
22
+
23
+ After installing the gem, complete the file in `config/initializers/ml_client.rb` with required `api_url` and `api_bearer` for authentication.
24
+
25
+ Here is what the file should look like :
26
+
27
+ ```ruby
28
+ MLClient.configure do |config|
29
+ config.api_url = 'https://api.com/'
30
+ config.api_bearer = 'fake_bearer'
31
+ end
32
+ ```
33
+
34
+ Two methods are available (as of 01/11/2021) :
35
+
36
+ - `MLClient.predict(model_name, params)` which returns synchronously the result of the model with the corresponding model_name.
37
+ - `MLClient.predict_async(model_name, params, webhook_url)` which returns asynchronously the result of the model. It requires a webhook url on which the response will be sent.
38
+
39
+ ## Development
40
+
41
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
42
+
43
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
44
+
45
+ ## Contributing
46
+
47
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Flatlooker/ml_client.
48
+
49
+ ## License
50
+
51
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << 'test'
8
+ t.libs << 'lib'
9
+ t.test_files = FileList['test/**/*_test.rb']
10
+ end
11
+
12
+ task default: :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'ml_client'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Install Generator inherits from Rails::Generator::Base
4
+ class MLClient::Generators::InstallGenerator < Rails::Generators::Base
5
+ source_root File.expand_path('../../templates', __dir__)
6
+ desc 'Creates MLClient initializer for your application'
7
+
8
+ def copy_initializer
9
+ template 'ml_client_initializer.rb', 'config/initializers/ml_client.rb'
10
+
11
+ puts 'Install complete!'
12
+ end
13
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ MLClient.configure do |config|
4
+ config.api_url = 'your_url'
5
+ config.api_bearer = 'your_bearer'
6
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MLClient
4
+ # MLClient is the base error from which all other more specific errors
5
+ class MLClientError < StandardError
6
+ attr_accessor :message
7
+
8
+ def initialize(message = nil)
9
+ @message = message
10
+ super
11
+ end
12
+ end
13
+
14
+ # in case of 400
15
+ class FailedValidationError < MLClientError
16
+ end
17
+
18
+ # in case of 403
19
+ class AuthentificationError < MLClientError
20
+ end
21
+
22
+ # in case of 404
23
+ class WrongModelName < MLClientError
24
+ end
25
+
26
+ # in case of 405
27
+ class AsyncError < MLClientError
28
+ end
29
+
30
+ # in case of 500
31
+ class InternalServerError < MLClientError
32
+ end
33
+
34
+ # in case of configuration errors
35
+ class ConfigurationError < MLClientError
36
+ end
37
+
38
+ # in case of other errors
39
+ class UnhandledError < MLClientError
40
+ end
41
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MLClient
4
+ VERSION = '0.1.0.1'
5
+ end
data/lib/ml_client.rb ADDED
@@ -0,0 +1,94 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Ruby bindings
4
+ require 'json'
5
+ require 'net/http'
6
+
7
+ # Resource and support classes
8
+ require 'ml_client/version'
9
+ require 'ml_client/errors'
10
+
11
+ # Main class for prediction
12
+ module MLClient
13
+ class << self
14
+ attr_accessor :configuration
15
+
16
+ def predict(model_name, instances, url: api_url, endpoint: nil)
17
+ endpoint ||= 'predict'
18
+ query_strings = { model: model_name }
19
+ post(url, endpoint, query_strings, formatted_instances(instances))
20
+ end
21
+
22
+ def predict_async(model_name, instances, url: api_url, endpoint: nil, webhook_url: nil)
23
+ endpoint ||= 'predict_async'
24
+ query_strings = { model: model_name, webhook_url: webhook_url }
25
+ post(url, endpoint, query_strings, formatted_instances(instances))
26
+ end
27
+
28
+ def configure
29
+ self.configuration ||= Configuration.new
30
+ yield configuration
31
+ end
32
+
33
+ def api_url
34
+ configuration&.api_url
35
+ end
36
+
37
+ private
38
+
39
+ def post(url, endpoint, query_strings, payload)
40
+ raise ConfigurationError, 'Missing url' if url.nil?
41
+
42
+ uri = URI.join(url, endpoint || '')
43
+ uri.query = URI.encode_www_form(query_strings)
44
+
45
+ req = Net::HTTP::Post.new(uri)
46
+ req['Authorization'] = "Bearer #{configuration.api_bearer}" if configuration&.api_bearer
47
+ req['Content-Type'] = 'application/json'
48
+
49
+ req.body = payload.to_json
50
+ generate_https = Net::HTTP.new(uri.host, uri.port)
51
+ generate_https.use_ssl = true if uri.scheme == 'https'
52
+ generate_https.set_debug_output($stdout) if configuration&.debug
53
+
54
+ res = generate_https.request(req)
55
+ response = JSON.parse(res.body)
56
+
57
+ case res.code
58
+ when '400'
59
+ raise FailedValidationError, "Wrong inputs #{params} in model #{model_name}"
60
+ when '403'
61
+ raise AuthentificationError, 'Authorization error'
62
+ when '404'
63
+ raise WrongModelName, "Wrong model name #{model_name}"
64
+ when '405'
65
+ raise AsyncError, "#{model_name} should be called in an asynchronous manner"
66
+ when '500'
67
+ raise InternalServerError, 'Internal server error in api'
68
+ when '200'
69
+ # it's sync
70
+ response
71
+ when '202'
72
+ # it's async
73
+ true
74
+ else
75
+ raise UnhandledError, 'Cannot process error code'
76
+ end
77
+ end
78
+
79
+ def formatted_instances(params)
80
+ { instances: params }
81
+ end
82
+ end
83
+
84
+ # Configuration for client
85
+ class Configuration
86
+ attr_accessor :api_url
87
+ attr_accessor :api_bearer
88
+ attr_accessor :debug
89
+
90
+ def initialize
91
+ @debug = false
92
+ end
93
+ end
94
+ end
data/ml_client.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/ml_client/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'ml_client'
7
+ spec.version = MLClient::VERSION
8
+ spec.authors = ['Nicolas Goyet']
9
+ spec.email = ['nicolas@flatlooker.com']
10
+
11
+ spec.summary = 'This gem is a thin client for MLServer'
12
+ spec.homepage = 'https://github.com/Flatlooker/ml_client'
13
+ spec.license = 'MIT'
14
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.5.0')
15
+ spec.add_development_dependency 'minitest', '>= 5.14'
16
+ spec.add_development_dependency 'vcr', '~> 6.0'
17
+ spec.add_development_dependency 'webmock', '~> 3.12'
18
+ spec.metadata['homepage_uri'] = spec.homepage
19
+ spec.metadata['source_code_uri'] = 'https://github.com/Flatlooker/ml_client'
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.bindir = 'exe'
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ['lib']
29
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ml_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Nicolas Goyet
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-11-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '5.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '5.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: vcr
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '6.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '6.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: webmock
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.12'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.12'
55
+ description:
56
+ email:
57
+ - nicolas@flatlooker.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rubocop.yml"
64
+ - Gemfile
65
+ - Gemfile.lock
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - lib/generators/ml_client/install_generator.rb
72
+ - lib/generators/templates/ml_client_initializer.rb
73
+ - lib/ml_client.rb
74
+ - lib/ml_client/errors.rb
75
+ - lib/ml_client/version.rb
76
+ - ml_client.gemspec
77
+ homepage: https://github.com/Flatlooker/ml_client
78
+ licenses:
79
+ - MIT
80
+ metadata:
81
+ homepage_uri: https://github.com/Flatlooker/ml_client
82
+ source_code_uri: https://github.com/Flatlooker/ml_client
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: 2.5.0
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubygems_version: 3.1.2
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: This gem is a thin client for MLServer
102
+ test_files: []