karthiksagarn
commited on
Commit
β’
19049a4
1
Parent(s):
35f5456
Upload 3 files
Browse files- app.py +83 -0
- background_image.png +0 -0
- requirements.txt +7 -0
app.py
ADDED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
### MULTI-LANGUAGE INVOICE EXTRACTOR USING GEMINI-PRO
|
2 |
+
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
load_dotenv() # load the environment variables
|
5 |
+
|
6 |
+
import streamlit as st
|
7 |
+
import os
|
8 |
+
import base64
|
9 |
+
from io import BytesIO
|
10 |
+
from PIL import Image
|
11 |
+
import google.generativeai as genai
|
12 |
+
|
13 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
14 |
+
|
15 |
+
## Function to load gemini-pro-vision
|
16 |
+
model= genai.GenerativeModel("gemini-pro-vision")
|
17 |
+
|
18 |
+
def get_response(input, image, prompt):
|
19 |
+
response = model.generate_content([input, image[0], prompt]) # gemini-pro always takes input in form of a list.
|
20 |
+
# first input always describes or instructs how the model should behave.
|
21 |
+
return response
|
22 |
+
|
23 |
+
def input_image_details(uploaded_file):
|
24 |
+
if uploaded_file is not None:
|
25 |
+
# Read the file into bytes
|
26 |
+
bytes_data = uploaded_file.getvalue()
|
27 |
+
|
28 |
+
image_parts = [
|
29 |
+
{
|
30 |
+
"mime_type": uploaded_file.type,
|
31 |
+
"data": bytes_data
|
32 |
+
}
|
33 |
+
]
|
34 |
+
return image_parts
|
35 |
+
else:
|
36 |
+
raise FileNotFoundError("File Not Uploaded")
|
37 |
+
|
38 |
+
## initialize the streamlit app
|
39 |
+
|
40 |
+
st.set_page_config(page_title="MultiLanguage-InvoiceExtractor")
|
41 |
+
|
42 |
+
# Function to set a background image
|
43 |
+
def set_background(image_file):
|
44 |
+
with open(image_file, "rb") as image:
|
45 |
+
b64_image = base64.b64encode(image.read()).decode("utf-8")
|
46 |
+
css = f"""
|
47 |
+
<style>
|
48 |
+
.stApp {{
|
49 |
+
background: url(data:image/png;base64,{b64_image});
|
50 |
+
background-size: cover;
|
51 |
+
background-position: centre;
|
52 |
+
backgroun-repeat: no-repeat;
|
53 |
+
}}
|
54 |
+
</style>
|
55 |
+
"""
|
56 |
+
st.markdown(css, unsafe_allow_html=True)
|
57 |
+
|
58 |
+
# Set the background image
|
59 |
+
set_background("background_image.png")
|
60 |
+
|
61 |
+
st.header("MultiLanguage - Invoice Extractor ππ€")
|
62 |
+
|
63 |
+
input = st.text_input("Input Prompt: ", key="input")
|
64 |
+
uploaded_file = st.file_uploader("Choose an image...", type=['png', 'jpg', 'jpeg'])
|
65 |
+
image= ""
|
66 |
+
if uploaded_file is not None:
|
67 |
+
image = Image.open(uploaded_file)
|
68 |
+
st.image(image, caption="Image Uploaded.", use_column_width = True)
|
69 |
+
|
70 |
+
submit = st.button("Tell me about the invoice")
|
71 |
+
|
72 |
+
input_prompt = """
|
73 |
+
You are an expert in understanding invoices. We will upload an image as invoice.
|
74 |
+
You will have to analyze the invoice image provided to you carefully and answer
|
75 |
+
any questions asked related to the invoice image.
|
76 |
+
"""
|
77 |
+
|
78 |
+
# if submit button is clicked
|
79 |
+
if submit:
|
80 |
+
image_data = input_image_details(uploaded_file)
|
81 |
+
response = get_response(input_prompt, image_data, input)
|
82 |
+
st.subheader("Response : ")
|
83 |
+
st.write(response.text)
|
background_image.png
ADDED
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
google-generativeai
|
3 |
+
python-dotenv
|
4 |
+
langchain-community
|
5 |
+
langchain
|
6 |
+
PyPDF2
|
7 |
+
chromadb
|