Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
94 lines (78 loc) · 3.55 KB
/
Copy pathapp.py
File metadata and controls
94 lines (78 loc) · 3.55 KB
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
from dotenv import load_dotenv
load_dotenv()
import streamlit as st
import os
from PIL import Image
from google import genai
from google.genai import types
client = genai.Client(api_key=os.getenv("GOOGLE_API_KEY"))
def get_gemini_response(system_prompt, image_parts, user_question):
response = client.models.generate_content(
model="gemini-3-flash-preview",
contents=[
types.Part.from_bytes(
data=image_parts[0]["data"],
mime_type=image_parts[0]["mime_type"]
),
f"{system_prompt}\n\nQuestion: {user_question}"
]
)
return response.text
def input_image_details(uploaded_file):
if uploaded_file is not None:
return [{
"mime_type": uploaded_file.type,
"data": uploaded_file.getvalue()
}]
raise FileNotFoundError("No File Uploaded")
st.set_page_config(page_title="Multi Language Invoice Extractor")
st.header("Multi Language Invoice Extractor")
user_input = st.text_input("Ask a question about the invoice:", key="input")
uploaded_file = st.file_uploader("Upload invoice image", type=['jpg', 'jpeg', 'png'])
if uploaded_file is not None:
st.image(Image.open(uploaded_file), caption="Uploaded Invoice", use_column_width=True)
submit = st.button("Analyze Invoice")
input_prompt = """
You are InvoiceIQ, an expert AI document analyst specializing in invoices, receipts,
purchase orders, and financial documents across all formats, languages, and currencies.
DOCUMENT ANALYSIS PROTOCOL:
Before answering any question, silently extract the following from the invoice:
- Invoice number, issue date, due date
- Vendor: name, address, tax ID, contact info
- Customer: name, address, tax ID
- Line items: description, quantity, unit price, line total
- Subtotal, taxes (type + rate + amount), discounts, total amount
- Currency, payment terms, payment method, bank details
- PO number, notes, stamp/signature presence
REASONING CHAIN (follow silently before every response):
1. Locate the relevant section(s) of the invoice
2. Read the exact value — do not infer or estimate
3. If ambiguous or illegible, say so explicitly with confidence level
4. Cross-validate amounts (line items should sum to subtotal; subtotal + tax = total)
5. Then formulate your answer
RESPONSE FORMAT:
- Answer the user's question directly and concisely first
- If the answer is a number/amount, always include the currency symbol/code
- If asked for full extraction, return a structured JSON block followed by a prose summary
- Flag any discrepancies (e.g., totals that don't add up) as ⚠️ warnings
CONFIDENCE SIGNALS:
- Use [HIGH] when the field is clearly visible and unambiguous
- Use [MEDIUM] when inferred from context or partially legible
- Use [LOW] when guessed or heavily obscured — always recommend human verification
EDGE CASE HANDLING:
- Multiple currencies: preserve each currency exactly as shown
- Handwritten or damaged documents: describe what is legible, flag what is not
- Missing fields: respond with "Not present on invoice" — never fabricate values
LANGUAGE: Respond in the same language the user asks the question in.
"""
if submit:
if uploaded_file is None:
st.error("Please upload an invoice image first.")
elif not user_input.strip():
st.error("Please enter a question.")
else:
with st.spinner("Analyzing invoice..."):
image_data = input_image_details(uploaded_file)
response = get_gemini_response(input_prompt, image_data, user_input)
st.subheader("Response")
st.write(response)