-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_vision.py
More file actions
78 lines (64 loc) · 3.52 KB
/
Copy pathtest_vision.py
File metadata and controls
78 lines (64 loc) · 3.52 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import base64
import json
from anthropic import Anthropic
from dotenv import load_dotenv
# Load the API key from .env
load_dotenv()
# Point at the test image
IMAGE_PATH = "test_images/sisterly_theelevator.jpg"
# Read the image file and encode it as base64 (how the API expects images)
with open(IMAGE_PATH, "rb") as f:
image_data = base64.standard_b64encode(f.read()).decode("utf-8")
# Create the client (it picks up ANTHROPIC_API_KEY from the environment)
client = Anthropic()
# Ask Claude to read the label
message = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=4096,
messages=[
{
"role": "user",
"content": [
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/jpeg",
"data": image_data,
},
},
{
"type": "text",
"text": """Read this supplement label and return ONLY a JSON object, with no other text, no markdown, and no code fences. Use exactly this structure: {"product_name": string, "form": string, "directions": string, "ingredients": [{"name": string, "canonical_name": string, "amount": string, "unit": string}], "needs_review": [string]}. Record only what is visible on the label. If a field is not present or cannot be read with confidence, use an empty string for it and add a short plain-English note to needs_review saying which field is unclear and why (for example, "dose for Vitamin D3 is blurred" or "no 'Av. per' header visible, so it is unclear whether amounts are per tablet or per serving"). Never guess a value to fill a gap. For canonical_name, give the single most standard common name for the ingredient (for example, "Methylcobalamin" or "Cobalamin" both become "Vitamin B12"; "Folic Acid" becomes "Folate"; "Thiamine" becomes "Vitamin B1"). Keep the original printed name in the name field unchanged. Take ingredients ONLY from the nutrition table (the panel with amounts per tablet or per serving). Do not add rows from the ingredients declaration paragraph, and do not include excipients, bulking agents, flavourings, sweeteners or carriers. If the ingredients paragraph names a specific chemical form for a nutrient already in the table (for example "zinc citrate" for Zinc), put that form in that ingredient's canonical_name rather than creating a new row, and never merge two nutrients into one row. Do not add advice, warnings, or commentary.""",
},
],
}
],
)
# Get the raw text Claude returned
raw_text = message.content[0].text
# Parse the JSON text into a real Python dictionary
data = json.loads(raw_text)
# Now Python understands it as data — let's prove it by reaching into the pieces
print("PRODUCT:", data["product_name"])
print("FORM:", data["form"])
print("DIRECTIONS:", data["directions"])
print()
print("INGREDIENTS:")
for item in data["ingredients"]:
print(" -", item["name"], "|", item["amount"], item["unit"])
# --- Save this entry to a log file so it persists ---
LOG_FILE = "log.json"
# Try to load any existing log; if there's none yet, start an empty list
try:
with open(LOG_FILE, "r") as f:
log = json.load(f)
except FileNotFoundError:
log = []
# Add this supplement to the log
log.append(data)
# Write the whole log back to the file
with open(LOG_FILE, "w") as f:
json.dump(log, f, indent=2)
print()
print(f"Saved! The log now contains {len(log)} entry/entries.")