-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsk.java
More file actions
44 lines (36 loc) · 1.56 KB
/
Copy pathAsk.java
File metadata and controls
44 lines (36 loc) · 1.56 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
* Visual Q&A — up to 5 questions about one file, priced like a single extraction.
*
* java -cp target/visionapi-java-1.0.0.jar examples/Ask.java photo.jpg "Is there a dog?"
*/
import io.visionapi.AskRequest;
import io.visionapi.AskResponse;
import io.visionapi.VisionApi;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
public class Ask {
public static void main(String[] args) {
if (args.length < 1) {
System.err.println("usage: Ask <file> [question…]");
System.exit(1);
}
List<String> questions = args.length > 1
? Arrays.asList(args).subList(1, args.length)
: List.of("What is in this image?");
VisionApi vision = VisionApi.create();
AskResponse res = vision.ask(AskRequest.builder()
.file(Path.of(args[0]))
.questions(questions)
.build());
for (AskResponse.Answer answer : res.answers()) {
// The verdict is the machine-readable half. UNCERTAIN is a yes/no question the
// image does not settle — a real answer, not a failure. NOT_APPLICABLE means it
// wasn't a yes/no question in the first place.
System.out.printf("Q: %s%nA: %s%n verdict=%s confidence=%s%n%n",
answer.question(), answer.answer(), answer.verdict(), answer.confidence());
}
System.out.printf("%d credit(s) — the questions themselves are free, "
+ "you pay per image or per selected page.%n", res.creditsUsed());
}
}