-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathask.rb
More file actions
26 lines (19 loc) · 967 Bytes
/
Copy pathask.rb
File metadata and controls
26 lines (19 loc) · 967 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# frozen_string_literal: true
# Visual Q&A — up to 5 questions about one file, priced like a single extraction.
#
# ruby examples/ask.rb photo.jpg "Is there a dog in the image?" "How many people?"
require "vision_api"
abort("usage: ruby examples/ask.rb <file> [question…]") if ARGV.empty?
path, *questions = ARGV
vision = VisionAPI.new
res = vision.ask(file: path, questions: questions.empty? ? ["What is in this image?"] : questions)
res["answers"].each do |a|
# `verdict` is the machine-readable half: yes | no | uncertain | n/a.
# "uncertain" is a yes/no question the image does not settle — a real answer, not a
# failure. "n/a" means it wasn't a yes/no question in the first place.
puts "Q: #{a['question']}"
puts "A: #{a['answer']}"
puts " verdict=#{a['verdict']} confidence=#{a['confidence']}\n\n"
end
puts "#{res['credits_used']} credit(s) — the questions themselves are free, " \
"you pay per image or per selected page."