Spaces:
Sleeping
Sleeping
File size: 1,770 Bytes
b36e1d2 |
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 |
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 |