Skip to content

Instantly share code, notes, and snippets.

@Manzanit0
Created June 26, 2024 14:03
Show Gist options
  • Save Manzanit0/e6dabee4848c5313fc0e47b434f18ab4 to your computer and use it in GitHub Desktop.
Save Manzanit0/e6dabee4848c5313fc0e47b434f18ab4 to your computer and use it in GitHub Desktop.
Python script to parse receipts with Chat GPT
#!/usr/bin/python3
import base64
import requests
# OpenAI API Key
api_key = "<INSERT KEY HERE>"
# Function to encode the image
def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
initial_prompt = """
You are an assistant that can read all kind of receipts and extract its
contents.
You will provide the total price paid, the currency, a summary of the items
purchased and the vendor name.
The description should not be an enumeration of the receipt contents. It should
be a summary of twenty words top of what was purchased.
The total price paid should not include the currency.
You will provide all this in JSON format where they property names are
"amount", "currency", "description" and "vendor".
When available, you shall also provide the purchase date in the dd/MM/yyyy
format. That should come in the JSON response as "purchase_date"
"""
# Path to your image
image_path = "<INSERT PATH HERE>"
# Getting the base64 string
base64_image = encode_image(image_path)
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
payload = {
"model": "gpt-4o",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": initial_prompt
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}"
}
}
]
}
],
"max_tokens": 300
}
response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
print(response.json()["choices"][0]["message"]["content"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment