clarification 0.0.0 → 0.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8daa3c9b734cebcb402ecf17267c816986b9fb63
4
- data.tar.gz: 5a97cf519409b9d2d93369a959b806935dc35604
3
+ metadata.gz: e64a471ed5071485e467ef49e82658ab0b349c63
4
+ data.tar.gz: 92f9619926fa087af02ac0b1ab7cbe88606d9b29
5
5
  SHA512:
6
- metadata.gz: 68e625faabe30c5d21402b4d042bcda745db52ea47655663c3d7feb1f5629eef979b4166c961f031e17e461eb37234190679730db868fb284b24ee64c0f05517
7
- data.tar.gz: a4a44a02633d75efbfe5bd3f4bbffb4cb4e87a4278db92715dd3251ae540166f812ce23a9cb7b4dcef11a77067c6d95ace9bec7870192dad1ee74be89da65362
6
+ metadata.gz: 9de55bd0c81de343006762c2cdee4b6420f77d7a9746a97ca2d251a1d0184c3165c63c9beceab886bd96f6e1df2de02077370c9e0db26de11df122f380fac246
7
+ data.tar.gz: 576bc98bf184e7f9ca6b1d23352707dd6a807f19c5b2b15aece2b125d945f4e1af8aa788b872f8ac4a8f68a76b37bd3b884ab621dc8bea5691804314f2b3e7f7
data/.travis.yml CHANGED
@@ -2,4 +2,6 @@ sudo: false
2
2
  language: ruby
3
3
  rvm:
4
4
  - 2.4.1
5
+ - 2.3
6
+ - 2.2
5
7
  before_install: gem install bundler -v 1.15.4
data/README.md CHANGED
@@ -1,4 +1,9 @@
1
- # Clarification - DO NOT INSTALL - IN DEVELOPMENT
1
+ [![Gem Version](https://badge.fury.io/rb/clarification.svg)](https://badge.fury.io/rb/clarification)
2
+ [![Build Status](https://travis-ci.org/nicholalexander/clarification.svg?branch=master)](https://travis-ci.org/nicholalexander/clarification)
3
+ [![Codacy Badge](https://api.codacy.com/project/badge/Grade/cb0dd6cce7ec48a191696780951c5efe)](https://www.codacy.com/app/nicholalexander/clarification?utm_source=github.com&utm_medium=referral&utm_content=nicholalexander/clarification&utm_campaign=Badge_Grade)
4
+
5
+
6
+ # Clarification
2
7
 
3
8
  Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/clarification`. To experiment with that code, run `bin/console` for an interactive prompt.
4
9
 
@@ -22,32 +27,69 @@ Or install it yourself as:
22
27
 
23
28
  Configure the gem as you would normally.
24
29
 
25
- ```
30
+ ```ruby
26
31
  Clarification.configure do |config|
27
32
  config.api_key = 'a_big_secret_you_got_from_clarifai'
28
- config.end_points = [:food]
33
+ config.end_points = [:food, :general]
29
34
  end
30
35
  ```
31
36
 
32
37
  This should also work nicely inside a rails initializer.
33
38
 
39
+ Each endpoint initialized in your configuration will be called. _So any predictions using a client configured with :food and :general will make two API requests._
40
+
34
41
  ## Usage
35
42
 
36
- You
43
+ ```ruby
44
+ client = Clarification::Client.new
45
+ response = client.predict(some_public_url_of_an_image)
46
+ ```
37
47
 
38
- client = Clarification::Client.new(:faces, :food)
39
- response = client.analyze(some_public_url)
48
+ ### Prediction
40
49
 
41
- Each endpoint initialized will be called. So this analyze action will call two separate API routes and will count as two uses of your key.
50
+ The response object returned from the prediction is a hash containing a nicely parsed result for each of the models called. In our example, we initialized with two models so we will have response[:food] and response[:general]. Each of those model results have an object which has three pieces of relevant information: status, concepts, and response_json.
42
51
 
52
+ Thusly you can do things like this:
53
+
54
+ ```ruby
55
+ response[:food].concepts.each do |concept|
56
+ if concept.value > 0.90
57
+ puts "#{concept.name}"
58
+ end
59
+ end
43
60
  ```
44
- response[:faces].face_count
45
- # => 3
46
- response[:food].concepts.first
47
- # => { concept_name: "cake",
48
- confidence: .89 }
61
+
62
+ ```ruby
63
+ response[:general].response_json
49
64
  ```
50
65
 
66
+ ```ruby
67
+ if response[:food].status.code == 10000
68
+ puts response[:food].status.description
69
+ end
70
+ ```
71
+
72
+ All the objects are OpenStructs currently, but I suspect this will change shortly.
73
+
74
+ As a convenience, the client also maintains the parsed response in the last_response variable.
75
+
76
+ ```ruby
77
+ client.predict(some_public_url_of_an_image)
78
+ client.last_response #=> {:food => Objectifier...}
79
+ ```
80
+
81
+ ## TODO's
82
+
83
+ Lots and lots of things. Amongst them:
84
+
85
+ * validate public model selections
86
+ * error handling
87
+ * predict multiple images per call
88
+ * predict video
89
+ * use workflows
90
+ * searching
91
+ * training custom models
92
+
51
93
  ## Development
52
94
 
53
95
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -56,7 +98,11 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
56
98
 
57
99
  ## Contributing
58
100
 
59
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/clarification. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
101
+ Bug reports and pull requests are welcome on GitHub at https://github.com/nicholalexander/clarification. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
102
+
103
+ https://codeclimate.com/github/nicholalexander/clarification
104
+ https://www.codacy.com/app/nicholalexander/clarification
105
+
60
106
 
61
107
  ## License
62
108
 
@@ -64,4 +110,4 @@ The gem is available as open source under the terms of the [MIT License](http://
64
110
 
65
111
  ## Code of Conduct
66
112
 
67
- Everyone interacting in the Clarification project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/clarification/blob/master/CODE_OF_CONDUCT.md).
113
+ Everyone interacting in the Clarification project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/nicholalexander/clarification/blob/master/CODE_OF_CONDUCT.md).
data/bin/console CHANGED
@@ -6,9 +6,17 @@ require "clarification"
6
6
  # You can add fixtures and/or initialization code here to make experimenting
7
7
  # with your gem easier. You can also use a different console, if you like.
8
8
 
9
+ Clarification.configure do |config|
10
+ config.api_key = 'f7cc628178994e16b2470ae739ef927a'
11
+ config.default_models = [:food]
12
+ end
13
+
14
+ @target_url = 'https://www.cicis.com/media/1138/pizza_trad_pepperoni.png'
15
+
16
+ @client = Clarification::Client.new
17
+ @response = @client.predict(@target_url)
18
+
9
19
  # (If you use this, don't forget to add pry to your Gemfile!)
10
20
  require "pry"
11
21
  Pry.start
12
22
 
13
- # require "irb"
14
- # IRB.start(__FILE__)
@@ -21,11 +21,17 @@ Gem::Specification.new do |spec|
21
21
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
22
  spec.require_paths = ["lib"]
23
23
 
24
+ spec.required_ruby_version = '>=2.2'
25
+
26
+ spec.add_runtime_dependency "json"
27
+
24
28
  spec.add_development_dependency "bundler", "~> 1.15"
25
29
  spec.add_development_dependency "rake", "~> 10.0"
26
30
  spec.add_development_dependency "rspec", "~> 3.0"
27
31
  spec.add_development_dependency "pry"
28
32
  spec.add_development_dependency "guard"
29
33
  spec.add_development_dependency "guard-rspec"
30
-
34
+ spec.add_development_dependency "simplecov"
35
+ spec.add_development_dependency "codacy-coverage"
36
+
31
37
  end
@@ -1,45 +1,25 @@
1
1
  module Clarification
2
2
  class Client
3
3
 
4
+ attr_reader :active_models, :last_response
5
+
4
6
  def initialize
5
-
7
+ raise "No Configuration Found." if Clarification.configuration.nil?
8
+ @active_models = Clarification.configuration.default_models
9
+ @last_response = nil
6
10
  end
7
11
 
8
- def request(url)
9
-
10
- r = Requester.new(:food)
11
- response = r.get(url)
12
-
12
+ def predict(url)
13
+ requester = Requester.new(@active_models)
14
+ response = requester.get(url)
15
+ @last_response = Enrich.new(response).run
16
+ return @last_response
13
17
  end
14
18
 
15
-
16
- def make_request
17
- uri = URI.parse("https://api.clarifai.com/v2/models/bd367be194cf45149e75f01d59f77ba7/outputs")
18
- request = Net::HTTP::Post.new(uri)
19
- request.content_type = "application/json"
20
- request["Authorization"] = "Key f7cc628178994e16b2470ae739ef927a"
21
- request.body = JSON.dump({
22
- "inputs" => [
23
- {
24
- "data" => {
25
- "image" => {
26
- "url" => "https://samples.clarifai.com/food.jpg"
27
- }
28
- }
29
- }
30
- ]
31
- })
32
-
33
- req_options = {
34
- use_ssl: uri.scheme == "https",
35
- }
36
-
37
- response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
38
- http.request(request)
39
- end
40
-
41
- puts response
19
+ def set_models(model_array)
20
+ @active_models = model_array
42
21
  end
43
22
 
44
23
  end
24
+
45
25
  end
@@ -1,9 +1,10 @@
1
1
  module Clarification
2
2
  class Configuration
3
- attr_accessor :api_key
3
+ attr_accessor :api_key, :default_models
4
4
 
5
5
  def initialize
6
6
  @api_key = 'YOUR_API_KEY'
7
+ @default_models = []
7
8
  end
8
9
 
9
10
  end
@@ -1,8 +1,22 @@
1
1
  module Clarification
2
2
  BASE_URL = 'https://api.clarifai.com/v2/models/'.freeze
3
- APPAREL = 'e0be3b9d6a454f0493ac3a30784001ff'.freeze
4
- CELEBRITY = 'e466caa0619f444ab97497640cefc4dc'.freeze
5
- COLOR = 'eeed0b6733a644cea07cf4c60f87ebb7'.freeze
6
- DEMOGRAPHICS = 'c0c0ac362b03416da06ab3fa36fb58e3'.freeze
7
- FOOD = 'bd367be194cf45149e75f01d59f77ba7'.freeze
3
+
4
+ PUBLIC_MODELS = {
5
+ apparel: 'e0be3b9d6a454f0493ac3a30784001ff',
6
+ celebrity: 'e466caa0619f444ab97497640cefc4dc',
7
+ color: 'eeed0b6733a644cea07cf4c60f87ebb7',
8
+ demographics: 'c0c0ac362b03416da06ab3fa36fb58e3',
9
+ face_detection: 'a403429f2ddf4b49b307e318f00e528b',
10
+ face_embedding: 'd02b4508df58432fbb84e800597b8959',
11
+ focus: 'c2cf7cecd8a6427da375b9f35fcd2381',
12
+ food: 'bd367be194cf45149e75f01d59f77ba7',
13
+ general: 'aaa03c23b3724a16a56b629203edc62c',
14
+ general_embedding: 'bbb5f41425b8468d9b7a554ff10f8581',
15
+ logo: 'c443119bf2ed4da98487520d01a0b1e3',
16
+ moderation: 'd16f390eb32cad478c7ae150069bd2c6',
17
+ nsfw: 'e9576d86d2004ed1a38ba0cf39ecb4b1',
18
+ travel: 'eee28c313d69466f836ab83287a54ed9',
19
+ wedding: 'c386b7a870114f4a87477c0824499348'
20
+ }
21
+
8
22
  end
@@ -0,0 +1,21 @@
1
+ module Clarification
2
+ class Enrich
3
+
4
+ attr_reader :results
5
+
6
+ def initialize(response_array)
7
+ @response_array = response_array
8
+ @results = {}
9
+ end
10
+
11
+ def run
12
+ @response_array.each do |model_name, raw_response |
13
+ @results[model_name] = Objectifier.new(raw_response.body)
14
+ end
15
+
16
+ return @results
17
+ end
18
+
19
+ end
20
+
21
+ end
@@ -0,0 +1,21 @@
1
+ module Clarification
2
+ class Objectifier
3
+
4
+ attr_reader :response_json, :status, :concepts
5
+
6
+ def initialize(response)
7
+ @response_json = JSON.parse(response)
8
+ @status = OpenStruct.new(@response_json["status"])
9
+ @concepts = []
10
+
11
+ build_concept_objects
12
+ end
13
+
14
+ def build_concept_objects
15
+ @response_json["outputs"][0]["data"]["concepts"].each do |concept|
16
+ @concepts << OpenStruct.new(concept)
17
+ end
18
+ end
19
+
20
+ end
21
+ end
@@ -1,12 +1,20 @@
1
1
  module Clarification
2
2
  class Requester
3
3
 
4
- def initialize(model)
5
- @model = model
4
+ def initialize(model_array)
5
+ @model_array = model_array
6
6
  end
7
7
 
8
8
  def get(target_url)
9
- uri = uri_builder
9
+ response = {}
10
+ @model_array.each do |model|
11
+ response[model] = get_with_model(target_url, model)
12
+ end
13
+ return response
14
+ end
15
+
16
+ def get_with_model(target_url, model)
17
+ uri = uri_builder(model)
10
18
  body = body_builder(target_url)
11
19
  request = request_builder(uri, body)
12
20
  options = options_builder(uri)
@@ -18,7 +26,6 @@ module Clarification
18
26
  return response
19
27
  end
20
28
 
21
-
22
29
  private
23
30
 
24
31
  def body_builder(url)
@@ -35,8 +42,10 @@ module Clarification
35
42
  }
36
43
  end
37
44
 
38
- def uri_builder
39
- uri = URI.parse("https://api.clarifai.com/v2/models/bd367be194cf45149e75f01d59f77ba7/outputs")
45
+ def uri_builder(model)
46
+ model_key = Clarification::PUBLIC_MODELS[model]
47
+ url = "#{Clarification::BASE_URL}#{model_key}/outputs"
48
+ uri = URI.parse(url)
40
49
  end
41
50
 
42
51
  def request_builder(uri, body)
@@ -1,3 +1,3 @@
1
1
  module Clarification
2
- VERSION = "0.0.0"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/clarification.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  require "clarification/client"
2
2
  require "clarification/configuration"
3
3
  require "clarification/end_points"
4
+ require "clarification/enrich"
5
+ require "clarification/objectifier"
4
6
  require "clarification/requester"
5
7
  require "clarification/version"
6
8
 
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clarification
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nichol Alexander
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-09-19 00:00:00.000000000 Z
11
+ date: 2017-09-20 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -94,6 +108,34 @@ dependencies:
94
108
  - - ">="
95
109
  - !ruby/object:Gem::Version
96
110
  version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: simplecov
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: codacy-coverage
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
97
139
  description: Alpha Version - configure, access Clarifai set end points, interrogate
98
140
  responses.
99
141
  email:
@@ -118,6 +160,8 @@ files:
118
160
  - lib/clarification/client.rb
119
161
  - lib/clarification/configuration.rb
120
162
  - lib/clarification/end_points.rb
163
+ - lib/clarification/enrich.rb
164
+ - lib/clarification/objectifier.rb
121
165
  - lib/clarification/requester.rb
122
166
  - lib/clarification/version.rb
123
167
  homepage: https://github.com/nicholalexander/clarification
@@ -132,7 +176,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
132
176
  requirements:
133
177
  - - ">="
134
178
  - !ruby/object:Gem::Version
135
- version: '0'
179
+ version: '2.2'
136
180
  required_rubygems_version: !ruby/object:Gem::Requirement
137
181
  requirements:
138
182
  - - ">="