Snackers_tracker / vision_api_call.py
Edgar Garcia
transferring files from spendtracker
b36e1d2 unverified
from openai import OpenAI
import os
import base64
from io import BytesIO
from PIL import Image
from utils import pil_to_base64, result_cleaner
my_key = os.environ.get('MY_OPENAI_KEY')
client = OpenAI(
api_key= my_key
)
prompt="""
You'll be analyzing purchase receipts you will extract the following information:
-date: date of the purchase in the format YYYY-MM-DD
-Store: Name of the store where items or services were purchased
-Store_type: Type of store (supermarket, restaurant, bookstore, etc)
-Purchase_summary: in maximum 5 words summarize the purchase (cleaning products, breakfast, books, food items for home, online services, clothes ,etc)
-items: create a list of all the items in the receipt
-total: total amount spent
Provide your answer in a dictionary like the following
{{date: "xxxx-xx-xx"
store: "example store",
store_type: "supermarket",
purchase_summary: "food items for home",
items: "xxxx, xxxx, xxx"
total: xxxx }}
"""
def analyse_image(processed_image):
#
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": prompt,
},
{
"type": "image_url",
"image_url": {"url": f"data:image/jpeg;base64,{processed_image}"},
},
],
}
],
)
return response.choices[0].message.content
def process_image(img):
image_base64=pil_to_base64(img)
analysis_result=analyse_image(image_base64)
clean_analysis_result=result_cleaner(analysis_result)
return clean_analysis_result