Upload 2 files
Browse files- app (1).py +81 -0
- requirements.txt +8 -0
app (1).py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from dotenv import load_dotenv
|
2 |
+
load_dotenv()
|
3 |
+
import streamlit as st
|
4 |
+
import os
|
5 |
+
import google.generativeai as genai
|
6 |
+
from PIL import Image
|
7 |
+
import pdfplumber
|
8 |
+
import io
|
9 |
+
|
10 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
11 |
+
# load gemini model
|
12 |
+
model = genai.GenerativeModel("gemini-1.5-flash")
|
13 |
+
|
14 |
+
def get_gemini_response(input, image, prompt):
|
15 |
+
response = model.generate_content([input, image[0], prompt])
|
16 |
+
return response.text
|
17 |
+
|
18 |
+
def input_image_setup(uploaded_img):
|
19 |
+
if uploaded_img is not None:
|
20 |
+
bytes_data = uploaded_img.getvalue()
|
21 |
+
image_parts = [
|
22 |
+
{
|
23 |
+
"mime_type": uploaded_img.type,
|
24 |
+
"data": bytes_data
|
25 |
+
}
|
26 |
+
]
|
27 |
+
return image_parts
|
28 |
+
else:
|
29 |
+
raise FileNotFoundError("Image not found")
|
30 |
+
|
31 |
+
def extract_text_from_pdf(pdf_file):
|
32 |
+
text = ""
|
33 |
+
with pdfplumber.open(pdf_file) as pdf:
|
34 |
+
for page in pdf.pages:
|
35 |
+
text += page.extract_text()
|
36 |
+
return text
|
37 |
+
|
38 |
+
def input_file_setup(uploaded_file):
|
39 |
+
if uploaded_file is not None:
|
40 |
+
if uploaded_file.type.startswith('image'):
|
41 |
+
bytes_data = uploaded_file.getvalue()
|
42 |
+
image_parts = [
|
43 |
+
{
|
44 |
+
"mime_type": uploaded_file.type,
|
45 |
+
"data": bytes_data
|
46 |
+
}
|
47 |
+
]
|
48 |
+
return image_parts
|
49 |
+
elif uploaded_file.type.startswith('application/pdf'):
|
50 |
+
text = extract_text_from_pdf(uploaded_file)
|
51 |
+
return [{"text": text}]
|
52 |
+
else:
|
53 |
+
raise ValueError("Unsupported file type")
|
54 |
+
else:
|
55 |
+
raise FileNotFoundError("File not found")
|
56 |
+
|
57 |
+
st.set_page_config(page_title="Invoice Extractor", page_icon="🔮")
|
58 |
+
st.title("Invoice Extractor using LLM")
|
59 |
+
st.write("Upload your invoice and we will give you all the information we can based on your query")
|
60 |
+
|
61 |
+
input_query = st.text_input("Ask a question", key="input")
|
62 |
+
uploaded_file = st.file_uploader("Upload an image or PDF", type=["jpg", "jpeg", "png", "pdf"])
|
63 |
+
|
64 |
+
if uploaded_file is not None:
|
65 |
+
if uploaded_file.type.startswith('image'):
|
66 |
+
image = Image.open(uploaded_file)
|
67 |
+
st.image(image, caption="Uploaded Image.", use_column_width=True)
|
68 |
+
elif uploaded_file.type.startswith('application/pdf'):
|
69 |
+
st.write("PDF uploaded. Extracting text...")
|
70 |
+
|
71 |
+
submit = st.button("Submit")
|
72 |
+
|
73 |
+
input_prompt = """
|
74 |
+
You are an expert in understanding invoices. We will show you an invoice and you have to answer the following questions based on the invoice:
|
75 |
+
"""
|
76 |
+
|
77 |
+
if submit:
|
78 |
+
file_data = input_file_setup(uploaded_file)
|
79 |
+
response = get_gemini_response(input_prompt, file_data, input_query)
|
80 |
+
st.subheader("Response:")
|
81 |
+
st.write(response)
|
requirements.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
google-generativeai
|
3 |
+
python-dotenv
|
4 |
+
langchain
|
5 |
+
PyPDF2
|
6 |
+
chromadb
|
7 |
+
pymupdf
|
8 |
+
pdfplumber
|