-
Notifications
You must be signed in to change notification settings - Fork 9
/
calorie_counter.py
59 lines (52 loc) · 1.63 KB
/
calorie_counter.py
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
from openai import OpenAI
from dotenv import load_dotenv
import base64
import json
import sys
load_dotenv()
client = OpenAI()
def get_calories_from_image(image_path):
with open(image_path, "rb") as image:
base64_image = base64.b64encode(image.read()).decode("utf-8")
response = client.chat.completions.create(
model="gpt-4o",
response_format={"type": "json_object"},
messages=[
{
"role": "system",
"content": """You are a dietitian. A user sends you an image of a meal and you tell them how many calories are in it. Use the following JSON format:
{
"reasoning": "reasoning for the total calories",
"food_items": [
{
"name": "food item name",
"calories": "calories in the food item"
}
],
"total": "total calories in the meal"
}"""
},
{
"role": "user",
"content": [
{
"type": "text",
"text": "How many calories is in this meal?"
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}"
}
}
]
},
],
)
response_message = response.choices[0].message
content = response_message.content
return json.loads(content)
if __name__ == "__main__":
image_path = sys.argv[1]
calories = get_calories_from_image(image_path)
print(json.dumps(calories, indent=4))