clarifai-rails 0.1.4 → 0.2.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: 7bada0ca5c7cc4594a0a449144b13799f4f65973
4
- data.tar.gz: 62a997af00984238fc741d4179a6393c5c5470bf
3
+ metadata.gz: 2c39c147a05f29329235122744b6b83c8d4975cb
4
+ data.tar.gz: cda67986574fc8a87b27345e9afeb74a0c7e0b24
5
5
  SHA512:
6
- metadata.gz: f792436b7f7ebdcc9cbdbee7a62e5bd73085a31cd2e158e8ba49576f948fdf1cb6629133ee91d912ae8f8e38f30b3d9e395109ded7ceaa7a6e8f7375a36abfe8
7
- data.tar.gz: bce5ac6b6972c60cbe88dcc64219270d8b5a48beba6a8640f5c7dc23a30f6cc35a96e3ceb8649abde8602c4c85a49bd828a70acb8453f656e8478849a41d67c8
6
+ metadata.gz: 57624358d02d08c999f6bff12bc5a69890d141eb8fd3b4d375b80ded2610d25f5f6e000a8e56ac90a03cc93e6fa04107516add9a2deb813d33b5aa541bf98d83
7
+ data.tar.gz: 3941691e80d0adad8c2147a23c615116b024faaed1de462b2047c0b795fc406483046c65b7b9eba0f6c52fab339ad9ed1760e5a45f489b3e5c7dd43fe375d433
data/Gemfile CHANGED
@@ -1,6 +1,6 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem 'rest-client', '~> 1.8'
3
+ gem 'rest-client', '2.1.0.rc1'
4
4
 
5
5
  # Specify your gem's dependencies in clarifai-rails.gemspec
6
6
  gemspec
@@ -6,7 +6,7 @@ require 'clarifai/rails/version'
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "clarifai-rails"
8
8
  spec.version = Clarifai::Rails::VERSION
9
- spec.authors = ["Karl Nguyen"]
9
+ spec.authors = ["Khoa Nguyen"]
10
10
  spec.email = ["thanhkhoait@gmail.com"]
11
11
 
12
12
  spec.summary = %q{Clarifai API Rails Client}
@@ -1,7 +1,6 @@
1
1
  module Clarifai
2
2
  module Rails
3
3
 
4
- autoload :Token, "clarifai/rails/token"
5
4
  autoload :Detector, "clarifai/rails/detector"
6
5
  autoload :Image, "clarifai/rails/image"
7
6
  autoload :Error, "clarifai/rails/error"
@@ -10,11 +9,11 @@ module Clarifai
10
9
  yield self
11
10
  end
12
11
 
13
- mattr_accessor :client_id, :client_secret, :tag_url
12
+ mattr_accessor :api_key, :tag_url, :model_code
14
13
 
15
- @@client_id = nil
16
- @@client_secret = nil
17
- @@tag_url = "https://api.clarifai.com/v1/tag"
14
+ @@api_key = nil
15
+ @@tag_url = "https://api.clarifai.com/v2/models"
16
+ @@model_code = nil
18
17
 
19
18
  end
20
19
  end
@@ -5,31 +5,25 @@ module Clarifai
5
5
  module Rails
6
6
  class Detector
7
7
 
8
- def initialize(urls, download=false)
9
- raise "Input data not supported! (String Array or String only)" unless urls.is_a?(Array) || urls.is_a?(String)
10
-
8
+ def initialize(urls, opts={})
9
+ raise 'Input data not supported! (String Array or String only)' unless valid_params?(urls)
11
10
  @urls = urls.is_a?(Array) ? urls : [urls]
12
- @download = download
13
- $clarifai_token_expire ||= Time.current
14
- end
15
-
16
- def need_download!
17
- @download = true
18
- self
11
+ @model_code = opts[:model] || Clarifai::Rails.model_code
12
+ raise 'Model need to set' if @model_code.blank?
19
13
  end
20
14
 
21
15
  def to_hash
22
- @data_urls = fetch if @data_urls.blank?
16
+ @data_urls ||= fetch
23
17
  @data_urls.is_a?(Hash) ? @data_urls : JSON.parse(@data_urls).symbolize_keys
24
18
  end
25
19
 
26
20
  def results
27
- to_hash[:results]
21
+ to_hash[:outputs]
28
22
  end
29
23
 
30
24
  def images
31
- results.map do |json|
32
- Clarifai::Rails::Image.new(json)
25
+ results.map do |image|
26
+ Clarifai::Rails::Image.new(image)
33
27
  end
34
28
  end
35
29
 
@@ -40,7 +34,7 @@ module Clarifai
40
34
  # Status method
41
35
 
42
36
  def error
43
- Clarifai::Rails::Error.detector(to_hash[:status_code])
37
+ Clarifai::Rails::Error.detector(to_hash[:status])
44
38
  end
45
39
 
46
40
  def error?
@@ -52,34 +46,24 @@ module Clarifai
52
46
  end
53
47
 
54
48
  private
49
+ attr_reader :urls, :model_code
55
50
 
56
- def fetch
57
- new_token if $clarifai_token_expire <= Time.current
58
- @download ? urls_protected : urls_unprotected
51
+ def endpoint
52
+ "#{Clarifai::Rails.tag_url}/#{model_code}/outputs"
59
53
  end
60
54
 
61
- def urls_unprotected
62
- params_string = @urls.join("&url=")
63
- response = open("#{Clarifai::Rails.tag_url}?url=#{params_string}", "Authorization" => "Bearer #{$clarifai_token[:access_token]}")
64
- response.read
55
+ def valid_params?(urls)
56
+ urls.is_a?(Array) || urls.is_a?(String)
65
57
  end
66
58
 
67
- def urls_protected
68
- data = { results: [], status_code: nil }
69
- @urls.each do |url|
70
- result = RestClient.post( Clarifai::Rails.tag_url,
71
- { encoded_data: File.new(open(url)) },
72
- Authorization: "Bearer #{$clarifai_token[:access_token]}")
73
- json = JSON.parse(result).symbolize_keys
74
- data[:results] << json[:results].first
75
- data[:status_code] = json[:status_code]
76
- end
77
- data
59
+ def fetch
60
+ params = { inputs: urls.map{ |url| url_data(url) } }
61
+ result = RestClient.post(endpoint, params.to_json, Authorization: "Key #{Clarifai::Rails.api_key}")
62
+ JSON.parse(result).symbolize_keys
78
63
  end
79
64
 
80
- def new_token
81
- $clarifai_token = Clarifai::Rails::Token.new.create.symbolize_keys
82
- $clarifai_token_expire = Time.current + $clarifai_token[:expires_in]
65
+ def url_data(url)
66
+ { data: { image: { url: url } } }
83
67
  end
84
68
 
85
69
  end
@@ -14,29 +14,27 @@ module Clarifai
14
14
  json[:url]
15
15
  end
16
16
 
17
- def tags
17
+ def concepts
18
18
  raise error if error?
19
- result["tag"]["classes"]
19
+ data[:concepts].map{ |item| item['name'] }
20
20
  end
21
21
 
22
- def tags_with_percent
23
- tags_hash = {}
24
- tags.each_with_index do |tag, index|
25
- tags_hash["#{tag}"] = result["tag"]["probs"][index]
26
- end
27
- tags_hash.symbolize_keys
22
+ def concepts_with_percent
23
+ data[:concepts].map do |item|
24
+ [item['name'], item['value']]
25
+ end.to_h
28
26
  end
29
27
 
30
28
  def status_code
31
- json[:status_code]
29
+ status[:code]
32
30
  end
33
31
 
34
32
  def status_messages
35
- json[:status_msg]
33
+ status[:description]
36
34
  end
37
35
 
38
36
  def docid_str
39
- result["docid_str"]
37
+ data[:docid_str]
40
38
  end
41
39
 
42
40
  def error
@@ -55,8 +53,12 @@ module Clarifai
55
53
 
56
54
  attr_reader :json
57
55
 
58
- def result
59
- json[:result]
56
+ def data
57
+ json[:data].symbolize_keys
58
+ end
59
+
60
+ def status
61
+ json[:status].symbolize_keys
60
62
  end
61
63
 
62
64
  end
@@ -1,5 +1,5 @@
1
1
  module Clarifai
2
2
  module Rails
3
- VERSION = "0.1.4"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clarifai-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
- - Karl Nguyen
7
+ - Khoa Nguyen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-03-01 00:00:00.000000000 Z
11
+ date: 2017-12-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -74,7 +74,6 @@ files:
74
74
  - lib/clarifai/rails/detector.rb
75
75
  - lib/clarifai/rails/error.rb
76
76
  - lib/clarifai/rails/image.rb
77
- - lib/clarifai/rails/token.rb
78
77
  - lib/clarifai/rails/version.rb
79
78
  - lib/generators/clarifai/install_generator.rb
80
79
  - lib/generators/templates/clarifai.rb
@@ -98,9 +97,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
98
97
  version: '0'
99
98
  requirements: []
100
99
  rubyforge_project:
101
- rubygems_version: 2.4.6
100
+ rubygems_version: 2.6.11
102
101
  signing_key:
103
102
  specification_version: 4
104
103
  summary: Clarifai API Rails Client
105
104
  test_files: []
106
- has_rdoc:
@@ -1,32 +0,0 @@
1
- module Clarifai
2
- module Rails
3
-
4
- private
5
-
6
- class Token
7
-
8
- def initialize
9
- if Clarifai::Rails.client_id.blank? || Clarifai::Rails.client_secret.blank?
10
- raise "You need config client_id and client_secret for Application!"
11
- end
12
- end
13
-
14
- def create
15
- params = {
16
- grant_type: :client_credentials,
17
- client_id: Clarifai::Rails.client_id,
18
- client_secret: Clarifai::Rails.client_secret
19
- }
20
- token_uri = URI("https://api.clarifai.com/v1/token")
21
-
22
- https = Net::HTTP.new(token_uri.host, token_uri.port)
23
- https.use_ssl = true
24
- request = Net::HTTP::Post.new(token_uri.request_uri)
25
- request.set_form_data(params)
26
- body = https.request(request).body
27
- JSON.parse(body)
28
- end
29
-
30
- end
31
- end
32
- end