-
Notifications
You must be signed in to change notification settings - Fork 0
/
finalProduct.py
192 lines (168 loc) · 7.19 KB
/
finalProduct.py
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import streamlit as st
import os
import io
from PIL import Image
import fitz # PyMuPDF
import numpy as np
from dotenv import load_dotenv
import google.generativeai as genai
from paddleocr import PaddleOCR
import logging
import json
# Load environment variables and configure generative AI
load_dotenv()
genai.configure(api_key=os.getenv('GEMINI_API_KEY'))
logging.getLogger('ppocr').setLevel(logging.WARNING)
# Function definitions
def extract_invoice_details(content, from_image=False):
try:
model = genai.GenerativeModel(model_name="gemini-1.5-pro")
if from_image:
content = genai.upload_file(path=content, display_name="Invoice Image")
prompt = (
"Extract the following details from this invoice text and provide the output in JSON format, just give braces and its data, nothing else:\n"
"1. Customer details:\n"
" - Name\n"
" - Address\n"
" - Contact (multiple entries allowed, use an array of strings)\n"
"2. Products:\n"
" - Name\n"
" - Quantity\n"
" - Price\n"
"3. Total Amount\n"
"The JSON response should be structured as follows:\n"
"{\n"
" \"Customer Details\": {\n"
" \"Name\": \"\",\n"
" \"Address\": \"\",\n"
" \"Contact\": [\n"
" \"\"\n"
" ]\n"
" },\n"
" \"Products\": [\n"
" {\n"
" \"Name\": \"\",\n"
" \"Quantity\": \"\",\n"
" \"Price\": \"\"\n"
" }\n"
" ],\n"
" \"Total Amount\": \"\"\n"
"}\n"
"Provide the output in this exact JSON structure. Do not include any additional text or formatting."
)
response = model.generate_content([content, prompt])
return response.text
except Exception as e:
st.error(f"An error occurred while extracting details: {e}")
return None
def extract_text_from_image(image):
ocr = PaddleOCR(use_angle_cls=True, lang='en')
result = ocr.ocr(np.array(image), cls=True)
return "\n".join([line[1][0] for line in result[0]])
def extract_text_from_pdf(pdf_path):
all_text = ""
try:
pdf_document = fitz.open(pdf_path)
for page in pdf_document:
text = page.get_text() or extract_text_from_image(Image.open(io.BytesIO(page.get_pixmap().tobytes())))
all_text += text + "\n"
pdf_document.close()
except Exception as e:
st.error(f"An error occurred while extracting text from PDF: {e}")
return all_text.strip()
def check_file_type(file_path):
if not os.path.isfile(file_path):
raise ValueError(f"File not found: {file_path}")
ext = os.path.splitext(file_path)[1].lower()
if ext == '.pdf':
return "PDF"
elif ext in ['.png', '.jpg', '.jpeg', '.gif', '.bmp', '.tiff']:
return "Image"
return "Other"
def process_file(file_path, flag):
file_type = check_file_type(file_path)
extracted_text, response = None, None
if file_type == "PDF":
extracted_text = extract_text_from_pdf(file_path)
response = extract_invoice_details(extracted_text) if extracted_text else None
elif file_type == "Image":
if flag == 1:
extracted_text = extract_text_from_image(Image.open(file_path))
response = extract_invoice_details(extracted_text) if extracted_text else None
else:
response = extract_invoice_details(file_path, from_image=True)
else:
st.error("Unsupported file type detected.")
return None
return response
# Streamlit app
st.set_page_config(page_title="Invoice Extraction App", page_icon=":memo:", layout="wide")
# Centered title and styled welcome message
st.markdown("""
<style>
.title {
font-size: 36px;
color: #007BFF;
text-align: center;
margin-bottom: 10px;
}
.description {
font-size: 20px;
text-align: center;
color: #555555;
}
</style>
<div class="title">Invoice Extraction Application</div>
<div class="description">
Welcome to the Invoice Extraction App! Upload your invoice file and choose the method of extraction.
The app supports both PDF and image files.
</div>
""", unsafe_allow_html=True)
uploaded_file = st.file_uploader("Choose an invoice file", type=['pdf', 'png', 'jpg', 'jpeg', 'gif', 'bmp', 'tiff'])
if uploaded_file:
st.success("File uploaded successfully.")
option = st.selectbox(
"Choose extraction method",
["Direct Extraction (Image-based)", "Text Extraction (Text-based)"]
)
flag = 0 if option == "Direct Extraction (Image-based)" else 1
if st.button("Process"):
with st.spinner("Processing..."):
try:
if uploaded_file.type == 'application/pdf':
pdf_path = os.path.join("/tmp", uploaded_file.name)
with open(pdf_path, "wb") as f:
f.write(uploaded_file.read())
response = process_file(pdf_path, flag)
else:
image = Image.open(uploaded_file)
image_path = os.path.join("/tmp", uploaded_file.name)
image.save(image_path)
response = process_file(image_path, flag)
if response:
try:
# Attempt to parse the response as JSON
json_response = json.loads(response)
st.subheader("Extracted Details:")
# Display Customer Details
if "Customer Details" in json_response:
st.markdown("**Customer Details:**", unsafe_allow_html=True)
for key, value in json_response["Customer Details"].items():
st.markdown(f"**{key}:** {value}", unsafe_allow_html=True)
# Display Products
if "Products" in json_response:
st.markdown("**Products:**", unsafe_allow_html=True)
for product in json_response["Products"]:
st.markdown(f"**Name:** {product['Name']}", unsafe_allow_html=True)
st.markdown(f"**Quantity:** {product['Quantity']}", unsafe_allow_html=True)
st.markdown(f"**Price:** {product['Price']}", unsafe_allow_html=True)
st.markdown("---", unsafe_allow_html=True)
# Display Total Amount
if "Total Amount" in json_response:
st.markdown(f"**Total Amount:** {json_response['Total Amount']}", unsafe_allow_html=True)
except json.JSONDecodeError as e:
st.error(f"Error parsing JSON response: {e}")
else:
st.warning("No response generated.")
except Exception as e:
st.error(f"An error occurred during processing: {e}")