clarifai-rb 0.0.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 +7 -0
- data/.gitignore +10 -0
- data/Gemfile +3 -0
- data/clarifai-rb.gemspec +23 -0
- data/lib/clarifai/configuration.rb +12 -0
- data/lib/clarifai/predict.rb +76 -0
- data/lib/clarifai.rb +19 -0
- metadata +63 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5bb4d11b3d84be9ebdbc6df9c8c9965f8d9dce137225df681ce0470ecf80a020
|
4
|
+
data.tar.gz: 9b6a1f09300506e303b60a66e56e0bca2048280886cf5b987cb9694b433a39eb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a1fe924db1d6dbdc8ef18e119c0071386ee603445edb346e5dfe846880f40c7402cc2babdd0c0766e97a3779949f7e5c8e0b74748726070dd935c315706d20fb
|
7
|
+
data.tar.gz: e12274b7b64cd85ab3f33025c790066fc4ee91165bea31b3cfdfb669ed31f7a941f266bf08f8178d913dbcb69f60a4182d875a1d1a62069822089126710d6127
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/clarifai-rb.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "clarifai-rb"
|
8
|
+
s.version = "0.0.0"
|
9
|
+
s.summary = "An unofficial Ruby wrapper for Clarifai API"
|
10
|
+
s.description = "Alpha Version - Predict using Clarifai"
|
11
|
+
s.authors = ["Muhamad Rizky Sya'ban"]
|
12
|
+
s.email = "rizkyshaban@gmail.com"
|
13
|
+
s.files = `git ls-files`.split("\n")
|
14
|
+
s.executables = s.files.grep(%r{^bin/}).map { |f| File.basename(f) }
|
15
|
+
s.require_paths = ["lib"]
|
16
|
+
s.homepage =
|
17
|
+
"https://rubygems.org/gems/clarifai-rb"
|
18
|
+
s.license = "MIT"
|
19
|
+
s.required_ruby_version = '>=2.2'
|
20
|
+
s.metadata['rubygems_mfa_required'] = 'true'
|
21
|
+
|
22
|
+
s.add_dependency 'json', '>= 1.8'
|
23
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "open-uri"
|
4
|
+
|
5
|
+
module Clarifai
|
6
|
+
class Predict
|
7
|
+
SUPPORTED_TYPES = %w[image video text].freeze
|
8
|
+
|
9
|
+
def initialize(model_id:)
|
10
|
+
@model_id = model_id
|
11
|
+
end
|
12
|
+
|
13
|
+
def call(type:, urls:)
|
14
|
+
raise "Type is not supported! (#{SUPPORTED_TYPES.join(', ')} only)" unless SUPPORTED_TYPES.include?(type)
|
15
|
+
raise 'Input data not supported! (Array of Strings or String only)' unless valid_params?(urls)
|
16
|
+
|
17
|
+
http = setup_http_client
|
18
|
+
request = setup_predict_request(type, urls)
|
19
|
+
response = http.request(request)
|
20
|
+
JSON.parse(response.body)
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
attr_reader :urls, :model_id
|
26
|
+
|
27
|
+
def config
|
28
|
+
@config ||= Clarifai.configuration
|
29
|
+
end
|
30
|
+
|
31
|
+
def valid_params?(urls)
|
32
|
+
urls.is_a?(Array) || urls.is_a?(String)
|
33
|
+
end
|
34
|
+
|
35
|
+
def endpoint
|
36
|
+
"#{config.base_url}/users/#{config.user_id}/apps/#{config.app_id}/models/#{model_id}/outputs"
|
37
|
+
end
|
38
|
+
|
39
|
+
def headers
|
40
|
+
@headers ||= {
|
41
|
+
Authorization: "Key #{config.pat}",
|
42
|
+
'Content-Type': 'application/json'
|
43
|
+
}
|
44
|
+
end
|
45
|
+
|
46
|
+
def setup_http_client
|
47
|
+
http = Net::HTTP.new(predict_uri.host, predict_uri.port)
|
48
|
+
http.use_ssl = true
|
49
|
+
http
|
50
|
+
end
|
51
|
+
|
52
|
+
def predict_uri
|
53
|
+
@predict_uri ||= URI(endpoint)
|
54
|
+
end
|
55
|
+
|
56
|
+
def setup_predict_request(type, urls)
|
57
|
+
request = Net::HTTP::Post.new(predict_uri.path, headers)
|
58
|
+
request.body = predict_body(type, urls)
|
59
|
+
request
|
60
|
+
end
|
61
|
+
|
62
|
+
def url_data(url)
|
63
|
+
{ data: { image: { url: url } } }
|
64
|
+
end
|
65
|
+
|
66
|
+
def predict_body(type, image_url)
|
67
|
+
{ inputs: [{
|
68
|
+
data: {
|
69
|
+
"#{type}": {
|
70
|
+
url: image_url
|
71
|
+
}
|
72
|
+
}
|
73
|
+
}] }.to_json
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
data/lib/clarifai.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'clarifai/configuration'
|
4
|
+
require 'clarifai/predict'
|
5
|
+
|
6
|
+
require 'net/http'
|
7
|
+
require 'uri'
|
8
|
+
require 'json'
|
9
|
+
|
10
|
+
module Clarifai
|
11
|
+
class << self
|
12
|
+
attr_accessor :configuration
|
13
|
+
|
14
|
+
def configure
|
15
|
+
self.configuration ||= Configuration.new
|
16
|
+
yield(self.configuration)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: clarifai-rb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Muhamad Rizky Sya'ban
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-09-14 00:00:00.000000000 Z
|
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: '1.8'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.8'
|
27
|
+
description: Alpha Version - Predict using Clarifai
|
28
|
+
email: rizkyshaban@gmail.com
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- ".gitignore"
|
34
|
+
- Gemfile
|
35
|
+
- clarifai-rb.gemspec
|
36
|
+
- lib/clarifai.rb
|
37
|
+
- lib/clarifai/configuration.rb
|
38
|
+
- lib/clarifai/predict.rb
|
39
|
+
homepage: https://rubygems.org/gems/clarifai-rb
|
40
|
+
licenses:
|
41
|
+
- MIT
|
42
|
+
metadata:
|
43
|
+
rubygems_mfa_required: 'true'
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options: []
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '2.2'
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
requirements: []
|
59
|
+
rubygems_version: 3.1.6
|
60
|
+
signing_key:
|
61
|
+
specification_version: 4
|
62
|
+
summary: An unofficial Ruby wrapper for Clarifai API
|
63
|
+
test_files: []
|