Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,64 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
from dataclasses import dataclass
|
3 |
-
import pytesseract
|
4 |
-
from PIL import Image
|
5 |
-
import io
|
6 |
-
import re
|
7 |
-
import cv2
|
8 |
-
import numpy as np
|
9 |
-
import OCR
|
10 |
-
|
11 |
-
from OCR import *
|
12 |
-
|
13 |
-
# Initialize chat history
|
14 |
-
if "messages" not in st.session_state:
|
15 |
-
st.session_state.messages = [{"role": "Invoice Reader", "content": "Submit an invoice and I will read it."}]
|
16 |
-
|
17 |
-
# Display chat messages from history on app rerun
|
18 |
-
for message in st.session_state.messages:
|
19 |
-
with st.chat_message(message["role"]):
|
20 |
-
st.markdown(message["content"])
|
21 |
-
|
22 |
-
USER = "user"
|
23 |
-
ASSISTANT = "Invoice Reader"
|
24 |
-
|
25 |
-
# Accept file uploads
|
26 |
-
uploaded_file = st.file_uploader("Upload an invoice", type=["pdf", "png", "jpg", "jpeg"])
|
27 |
-
if uploaded_file is not None:
|
28 |
-
# Display uploaded file content
|
29 |
-
file_content = uploaded_file.getvalue()
|
30 |
-
st.session_state.messages.append({"role": USER, "content": f"Uploaded file: {uploaded_file.name}"})
|
31 |
-
with st.chat_message(USER):
|
32 |
-
st.markdown(f"Uploaded file: {uploaded_file.name}")
|
33 |
-
|
34 |
-
# Preprocess and extract text from image or PDF
|
35 |
-
try:
|
36 |
-
if uploaded_file.type == "application/pdf":
|
37 |
-
text = extract_text_from_pdf(file_content)
|
38 |
-
else:
|
39 |
-
text = extract_text_from_image(file_content)
|
40 |
-
|
41 |
-
# Extract specific details
|
42 |
-
details = extract_invoice_details(text)
|
43 |
-
|
44 |
-
# Create and display assistant's response to extracted text
|
45 |
-
assistant_response = (
|
46 |
-
f"Extracted text from the uploaded file:\n\n{text}\n\n"
|
47 |
-
f"**Extracted Details:**\n"
|
48 |
-
f"**Invoice Number:** {details['Invoice Number']}\n"
|
49 |
-
|
50 |
-
f"**Amount:** {details['Amount']}\n"
|
51 |
-
f"**Invoice Date:** {details['Invoice Date']}\n"
|
52 |
-
f"**Due Date:** {details['Due Date']}"
|
53 |
-
)
|
54 |
-
st.session_state.messages.append({"role": ASSISTANT, "content": assistant_response})
|
55 |
-
with st.chat_message(ASSISTANT):
|
56 |
-
st.markdown(assistant_response)
|
57 |
-
except Exception as e:
|
58 |
-
error_message = f"An error occurred while processing the file: {e}"
|
59 |
-
st.session_state.messages.append({"role": ASSISTANT, "content": error_message})
|
60 |
-
with st.chat_message(ASSISTANT):
|
61 |
-
st.markdown(error_message)
|
62 |
-
|
63 |
-
|
64 |
#streamlit run C:/Users/leahw/PycharmProjects/Int-to-Artificial-Intelligence-Final-Project/app.py
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from dataclasses import dataclass
|
3 |
+
import pytesseract
|
4 |
+
from PIL import Image
|
5 |
+
import io
|
6 |
+
import re
|
7 |
+
import cv2
|
8 |
+
import numpy as np
|
9 |
+
import OCR
|
10 |
+
import os
|
11 |
+
from OCR import *
|
12 |
+
|
13 |
+
# Initialize chat history
|
14 |
+
if "messages" not in st.session_state:
|
15 |
+
st.session_state.messages = [{"role": "Invoice Reader", "content": "Submit an invoice and I will read it."}]
|
16 |
+
|
17 |
+
# Display chat messages from history on app rerun
|
18 |
+
for message in st.session_state.messages:
|
19 |
+
with st.chat_message(message["role"]):
|
20 |
+
st.markdown(message["content"])
|
21 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
22 |
+
USER = "user"
|
23 |
+
ASSISTANT = "Invoice Reader"
|
24 |
+
|
25 |
+
# Accept file uploads
|
26 |
+
uploaded_file = st.file_uploader("Upload an invoice", type=["pdf", "png", "jpg", "jpeg"])
|
27 |
+
if uploaded_file is not None:
|
28 |
+
# Display uploaded file content
|
29 |
+
file_content = uploaded_file.getvalue()
|
30 |
+
st.session_state.messages.append({"role": USER, "content": f"Uploaded file: {uploaded_file.name}"})
|
31 |
+
with st.chat_message(USER):
|
32 |
+
st.markdown(f"Uploaded file: {uploaded_file.name}")
|
33 |
+
|
34 |
+
# Preprocess and extract text from image or PDF
|
35 |
+
try:
|
36 |
+
if uploaded_file.type == "application/pdf":
|
37 |
+
text = extract_text_from_pdf(file_content)
|
38 |
+
else:
|
39 |
+
text = extract_text_from_image(file_content)
|
40 |
+
|
41 |
+
# Extract specific details
|
42 |
+
details = extract_invoice_details(text)
|
43 |
+
|
44 |
+
# Create and display assistant's response to extracted text
|
45 |
+
assistant_response = (
|
46 |
+
f"Extracted text from the uploaded file:\n\n{text}\n\n"
|
47 |
+
f"**Extracted Details:**\n"
|
48 |
+
f"**Invoice Number:** {details['Invoice Number']}\n"
|
49 |
+
|
50 |
+
f"**Amount:** {details['Amount']}\n"
|
51 |
+
f"**Invoice Date:** {details['Invoice Date']}\n"
|
52 |
+
f"**Due Date:** {details['Due Date']}"
|
53 |
+
)
|
54 |
+
st.session_state.messages.append({"role": ASSISTANT, "content": assistant_response})
|
55 |
+
with st.chat_message(ASSISTANT):
|
56 |
+
st.markdown(assistant_response)
|
57 |
+
except Exception as e:
|
58 |
+
error_message = f"An error occurred while processing the file: {e}"
|
59 |
+
st.session_state.messages.append({"role": ASSISTANT, "content": error_message})
|
60 |
+
with st.chat_message(ASSISTANT):
|
61 |
+
st.markdown(error_message)
|
62 |
+
|
63 |
+
|
64 |
#streamlit run C:/Users/leahw/PycharmProjects/Int-to-Artificial-Intelligence-Final-Project/app.py
|