Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +80 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from pdf2image import convert_from_path
|
3 |
+
import google.generativeai as genai
|
4 |
+
import os
|
5 |
+
import imghdr
|
6 |
+
import PIL.Image
|
7 |
+
import json
|
8 |
+
import tempfile
|
9 |
+
from dotenv import load_dotenv
|
10 |
+
load_dotenv()
|
11 |
+
|
12 |
+
# Configure Gemini API
|
13 |
+
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
|
14 |
+
genai.configure(api_key=GOOGLE_API_KEY)
|
15 |
+
model = genai.GenerativeModel('gemini-1.5-flash')
|
16 |
+
|
17 |
+
def system_prompt() -> str:
|
18 |
+
return """You are a Invoice/Receipt Analysing tool. Analyse the image of the invoice provided and extract information from the following receipt image and return a JSON object with these exact keys:Customer_Details,Products,Total Amount.
|
19 |
+
|
20 |
+
Rules:
|
21 |
+
1. For total_cost, use the highest monetary value in the text.
|
22 |
+
2. Customer_Details will consist of another json object consisting the keys Customer_Name,Customer_Address,Email Address,Phone Number,Customer ID,Billing Address,Shipping Address,Account Number,Tax ID/VAT Number,Company Name,Payment Method.
|
23 |
+
3.Products will consist of another json object consisting of keys Product_name_1,quantity,unit_price.
|
24 |
+
6. If any value is not found, return null.
|
25 |
+
7. If all values are null, return null.
|
26 |
+
Ensure the strictly that output is a valid JSON object containing strictly the above keys, without any explanations.
|
27 |
+
Generate a JSON response in the following format without using the ```json block. Ensure the output is properly formatted as plain text JSON.
|
28 |
+
|
29 |
+
"""
|
30 |
+
|
31 |
+
def get_invoice_details(image):
|
32 |
+
response = model.generate_content([system_prompt(), image], stream=True)
|
33 |
+
response.resolve()
|
34 |
+
return response.text
|
35 |
+
|
36 |
+
def process_file(file):
|
37 |
+
images = []
|
38 |
+
if file.type == "application/pdf":
|
39 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file:
|
40 |
+
tmp_file.write(file.getvalue())
|
41 |
+
tmp_file_path = tmp_file.name
|
42 |
+
|
43 |
+
pdf_images = convert_from_path(tmp_file_path)
|
44 |
+
for img in pdf_images:
|
45 |
+
images.append(img)
|
46 |
+
|
47 |
+
os.unlink(tmp_file_path)
|
48 |
+
elif file.type.startswith('image'):
|
49 |
+
image = PIL.Image.open(file)
|
50 |
+
images.append(image)
|
51 |
+
else:
|
52 |
+
st.error(f"Unsupported file type: {file.type}")
|
53 |
+
return None
|
54 |
+
|
55 |
+
return images
|
56 |
+
|
57 |
+
def main():
|
58 |
+
st.title("Invoice Analyzer")
|
59 |
+
|
60 |
+
uploaded_file = st.file_uploader("Choose an image or PDF file", type=["jpg", "jpeg", "png", "pdf"])
|
61 |
+
|
62 |
+
if uploaded_file is not None:
|
63 |
+
images = process_file(uploaded_file)
|
64 |
+
|
65 |
+
if images:
|
66 |
+
for i, img in enumerate(images):
|
67 |
+
st.image(img, caption=f"Page {i+1}", use_column_width=True)
|
68 |
+
|
69 |
+
with st.spinner(f"Analyzing page {i+1}..."):
|
70 |
+
json_output = get_invoice_details(img)
|
71 |
+
|
72 |
+
try:
|
73 |
+
parsed_json = json.loads(json_output)
|
74 |
+
st.json(parsed_json)
|
75 |
+
except json.JSONDecodeError:
|
76 |
+
st.error(f"Failed to parse JSON for page {i+1}. Raw output:")
|
77 |
+
st.text(json_output)
|
78 |
+
|
79 |
+
if __name__ == "__main__":
|
80 |
+
main()
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
pdf2image
|
3 |
+
google-generativeai
|
4 |
+
Pillow
|