clarifai-rails 0.1.4 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +1 -1
- data/clarifai-rails.gemspec +1 -1
- data/lib/clarifai/rails.rb +4 -5
- data/lib/clarifai/rails/detector.rb +20 -36
- data/lib/clarifai/rails/image.rb +15 -13
- data/lib/clarifai/rails/version.rb +1 -1
- metadata +4 -6
- data/lib/clarifai/rails/token.rb +0 -32
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2c39c147a05f29329235122744b6b83c8d4975cb
|
4
|
+
data.tar.gz: cda67986574fc8a87b27345e9afeb74a0c7e0b24
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 57624358d02d08c999f6bff12bc5a69890d141eb8fd3b4d375b80ded2610d25f5f6e000a8e56ac90a03cc93e6fa04107516add9a2deb813d33b5aa541bf98d83
|
7
|
+
data.tar.gz: 3941691e80d0adad8c2147a23c615116b024faaed1de462b2047c0b795fc406483046c65b7b9eba0f6c52fab339ad9ed1760e5a45f489b3e5c7dd43fe375d433
|
data/Gemfile
CHANGED
data/clarifai-rails.gemspec
CHANGED
@@ -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 = ["
|
9
|
+
spec.authors = ["Khoa Nguyen"]
|
10
10
|
spec.email = ["thanhkhoait@gmail.com"]
|
11
11
|
|
12
12
|
spec.summary = %q{Clarifai API Rails Client}
|
data/lib/clarifai/rails.rb
CHANGED
@@ -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 :
|
12
|
+
mattr_accessor :api_key, :tag_url, :model_code
|
14
13
|
|
15
|
-
@@
|
16
|
-
@@
|
17
|
-
@@
|
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,
|
9
|
-
raise
|
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
|
-
@
|
13
|
-
|
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
|
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[:
|
21
|
+
to_hash[:outputs]
|
28
22
|
end
|
29
23
|
|
30
24
|
def images
|
31
|
-
results.map do |
|
32
|
-
Clarifai::Rails::Image.new(
|
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[:
|
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
|
57
|
-
|
58
|
-
@download ? urls_protected : urls_unprotected
|
51
|
+
def endpoint
|
52
|
+
"#{Clarifai::Rails.tag_url}/#{model_code}/outputs"
|
59
53
|
end
|
60
54
|
|
61
|
-
def
|
62
|
-
|
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
|
68
|
-
|
69
|
-
|
70
|
-
|
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
|
81
|
-
|
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
|
data/lib/clarifai/rails/image.rb
CHANGED
@@ -14,29 +14,27 @@ module Clarifai
|
|
14
14
|
json[:url]
|
15
15
|
end
|
16
16
|
|
17
|
-
def
|
17
|
+
def concepts
|
18
18
|
raise error if error?
|
19
|
-
|
19
|
+
data[:concepts].map{ |item| item['name'] }
|
20
20
|
end
|
21
21
|
|
22
|
-
def
|
23
|
-
|
24
|
-
|
25
|
-
|
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
|
-
|
29
|
+
status[:code]
|
32
30
|
end
|
33
31
|
|
34
32
|
def status_messages
|
35
|
-
|
33
|
+
status[:description]
|
36
34
|
end
|
37
35
|
|
38
36
|
def docid_str
|
39
|
-
|
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
|
59
|
-
json[:
|
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
|
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.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
-
|
7
|
+
- Khoa Nguyen
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
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.
|
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:
|
data/lib/clarifai/rails/token.rb
DELETED
@@ -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
|