1
0
mirror of https://github.com/danbee/my-images synced 2025-03-04 08:49:05 +00:00
This commit is contained in:
Daniel Barber 2018-08-24 22:25:17 -04:00
parent 31f1e29800
commit 5bf0727fd0
Signed by: danbarber
GPG Key ID: 931D8112E0103DD8
2 changed files with 18 additions and 19 deletions

View File

@ -1,9 +1,10 @@
require 'base64' require "base64"
require 'http' require "http"
class Clarifai class Clarifai
KEY = ENV.fetch("CLARIFAI_API_KEY").freeze KEY = ENV.fetch("CLARIFAI_API_KEY").freeze
API_URL = "https://api.clarifai.com/v2/models/aaa03c23b3724a16a56b629203edc62c/outputs" API_URL = "https://api.clarifai.com/v2/models/" \
"aaa03c23b3724a16a56b629203edc62c/outputs".freeze
attr_reader :tags attr_reader :tags
@ -14,18 +15,18 @@ class Clarifai
def predict! def predict!
headers = { headers = {
"Authorization": "Key #{KEY}", "Authorization": "Key #{KEY}",
"Content-Type": "application/json" "Content-Type": "application/json",
} }
params = { params = {
"inputs": [ "inputs": [
{ {
"data": { "data": {
"image": { "image": {
"base64": Base64.encode64(File.read(@image_path)) "base64": Base64.encode64(File.read(@image_path)),
} },
} },
} },
] ],
} }
resp = HTTP. resp = HTTP.

View File

@ -13,22 +13,22 @@ describe Clarifai do
{ {
"id": "ai_PpTcwbdQ", "id": "ai_PpTcwbdQ",
"name": "computer", "name": "computer",
"value": 0.96887743 "value": 0.96887743,
}, },
{ {
"id": "ai_62K34TR4", "id": "ai_62K34TR4",
"name": "technology", "name": "technology",
"value": 0.96544206 "value": 0.96544206,
} },
] ],
} },
} },
] ],
}.to_json }.to_json
WebMock. WebMock.
stub_request(:post, Clarifai::API_URL). stub_request(:post, Clarifai::API_URL).
to_return(:status => 200, :body => stub_body) to_return(status: 200, body: stub_body)
clarifai_image = Clarifai.new("spec/fixtures/spectrum.jpg") clarifai_image = Clarifai.new("spec/fixtures/spectrum.jpg")
clarifai_image.predict! clarifai_image.predict!
@ -37,5 +37,3 @@ describe Clarifai do
end end
end end
end end