-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyze.py
More file actions
38 lines (29 loc) · 1.53 KB
/
Copy pathanalyze.py
File metadata and controls
38 lines (29 loc) · 1.53 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
"""The smallest useful call, and how to read what comes back.
export VISION_API_KEY=sk_live_…
python examples/analyze.py invoice.pdf [preset]
"""
import json
import sys
from visionapi import VisionAPI, below_confidence, missing, unwrap
path = sys.argv[1] if len(sys.argv) > 1 else "invoice.pdf"
preset = sys.argv[2] if len(sys.argv) > 2 else "auto"
vision = VisionAPI()
res = vision.analyze(file=path, preset=preset)
print(
f"request {res['id']} — {res['pages']} page(s), "
f"{res['credits_used']} credit(s), {res['credits_remaining']} left"
)
# Under preset="auto" the API tells you what it decided the file was, for free.
detection = res.get("detection")
if detection:
note = " — fallback, so treat the shape as unknown" if detection["fallback"] else ""
print(f"detected {detection['preset']} ({detection['confidence']}){note}")
for alt in detection["alternatives"]:
print(f" runner-up: {alt['preset']} ({alt['confidence']}) — {alt['reason']}")
# unwrap() strips the {value, confidence} wrappers; drop_null leaves out the fields the
# document did not carry, which for a 37-field preset is usually most of them.
print(json.dumps(unwrap(res.get("result"), drop_null=True), indent=2, ensure_ascii=False))
# The two readings worth having in production: what was absent, and what was found but
# weakly. The second is your review queue.
print("absent:", ", ".join(missing(res.get("result"))) or "(nothing)")
print("below high confidence:", ", ".join(below_confidence(res.get("result"), "high")) or "(nothing)")