Spaces:
Sleeping
Sleeping
darthPanda
commited on
Commit
•
7110704
1
Parent(s):
8c3ae58
added simple api call
Browse files- .gitignore +0 -0
- app.py +25 -0
- requirements.txt +6 -0
- utils.py +67 -0
.gitignore
ADDED
File without changes
|
app.py
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import streamlit as st
|
3 |
+
import utils
|
4 |
+
|
5 |
+
# Set up the sidebar
|
6 |
+
# Ask the user for an API key in the sidebar
|
7 |
+
os.environ["OPENAI_API_KEY"] = st.sidebar.text_input("OpenAI API Key", type="password")
|
8 |
+
|
9 |
+
# Main body of the app
|
10 |
+
# Display a title
|
11 |
+
st.title("Invoice Data Extractor")
|
12 |
+
|
13 |
+
if os.environ["OPENAI_API_KEY"] == "":
|
14 |
+
disable_file_uploader = True
|
15 |
+
st.error('Kindly enter OpenAI API key')
|
16 |
+
else:
|
17 |
+
disable_file_uploader = False
|
18 |
+
|
19 |
+
# Add a file uploader widget
|
20 |
+
uploaded_file = st.file_uploader("Upload invoice image", type=['png', 'jpg'], disabled=disable_file_uploader)
|
21 |
+
|
22 |
+
if uploaded_file is not None:
|
23 |
+
response = utils.pass_to_openai_vision_api(uploaded_file)
|
24 |
+
st.write('Data extracted from invoice is ')
|
25 |
+
st.write(response)
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit==1.25.0
|
2 |
+
llama-index-callbacks-wandb
|
3 |
+
llama-index-llms-openai
|
4 |
+
llama-index-multi-modal-llms-openai
|
5 |
+
openai
|
6 |
+
matplotlib
|
utils.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import base64
|
2 |
+
import requests
|
3 |
+
import os
|
4 |
+
|
5 |
+
def pass_to_openai_vision_api(image):
|
6 |
+
# OpenAI API Key
|
7 |
+
api_key = os.environ["OPENAI_API_KEY"]
|
8 |
+
|
9 |
+
# Getting the base64 string
|
10 |
+
base64_image = base64.b64encode(image.read()).decode('utf-8')
|
11 |
+
|
12 |
+
headers = {
|
13 |
+
"Content-Type": "application/json",
|
14 |
+
"Authorization": f"Bearer {api_key}"
|
15 |
+
}
|
16 |
+
|
17 |
+
gpt_prompt='''Above is the text extracted from an invoice.
|
18 |
+
You are an assistant tasked with extracting information from the invoice. Do this step by step.
|
19 |
+
1. First extract the date and due date.
|
20 |
+
2. Then assign it a category (e.g Food).
|
21 |
+
3. Extract the invoice number and vendor account number.
|
22 |
+
4. Extract the total amount.
|
23 |
+
5. Extract the items along with their name, quantity and individual price.
|
24 |
+
Output should only contain a dictionary in the following format
|
25 |
+
{
|
26 |
+
"Date": None,
|
27 |
+
"Due Date": None,
|
28 |
+
"Category": None,
|
29 |
+
"Invoice Number": None,
|
30 |
+
"Vendor Account Number": None,
|
31 |
+
"Total Amount": None,
|
32 |
+
"Items": [
|
33 |
+
{
|
34 |
+
"Item": None,
|
35 |
+
"Quantity": None,
|
36 |
+
"Individual Price": None
|
37 |
+
}
|
38 |
+
]
|
39 |
+
}
|
40 |
+
If a key is not mentioned in invoice or you dont understand, then make its value None
|
41 |
+
'''
|
42 |
+
|
43 |
+
payload = {
|
44 |
+
"model": "gpt-4-vision-preview",
|
45 |
+
"messages": [
|
46 |
+
{
|
47 |
+
"role": "user",
|
48 |
+
"content": [
|
49 |
+
{
|
50 |
+
"type": "text",
|
51 |
+
"text": gpt_prompt
|
52 |
+
},
|
53 |
+
{
|
54 |
+
"type": "image_url",
|
55 |
+
"image_url": {
|
56 |
+
"url": f"data:image/jpeg;base64,{base64_image}"
|
57 |
+
}
|
58 |
+
}
|
59 |
+
]
|
60 |
+
}
|
61 |
+
],
|
62 |
+
"max_tokens": 300 # To be investigated
|
63 |
+
}
|
64 |
+
|
65 |
+
response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
|
66 |
+
|
67 |
+
return response.json()['choices'][0]['message']['content']
|