Upload 2 files
Browse files- app.py +65 -0
- requirements.txt +7 -0
app.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# importing libraries
|
2 |
+
import streamlit as st
|
3 |
+
import google.generativeai as genai
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
from PIL import Image
|
6 |
+
import os
|
7 |
+
|
8 |
+
load_dotenv() # load all the environment variables from .env
|
9 |
+
|
10 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
11 |
+
|
12 |
+
# Function to load Gemini Pro Vision
|
13 |
+
# In Gemini Pro, model takes it in a list
|
14 |
+
model = genai.GenerativeModel('gemini-pro-vision')
|
15 |
+
|
16 |
+
def get_gemini_response(input, image, prompt):
|
17 |
+
response = model.generate_content([input, image[0], prompt])
|
18 |
+
return response.text
|
19 |
+
|
20 |
+
def input_image_details(uploaded_file):
|
21 |
+
# Check if a file has been uploaded
|
22 |
+
if uploaded_file is not None:
|
23 |
+
# Read the file into bytes
|
24 |
+
bytes_data = uploaded_file.getvalue()
|
25 |
+
|
26 |
+
image_parts = [
|
27 |
+
{
|
28 |
+
"mime_type": uploaded_file.type, # Get the mime type of the uploaded file
|
29 |
+
"data": bytes_data
|
30 |
+
}
|
31 |
+
]
|
32 |
+
return image_parts
|
33 |
+
else:
|
34 |
+
raise FileNotFoundError("No file uploaded")
|
35 |
+
|
36 |
+
|
37 |
+
# streamlit setup
|
38 |
+
st.set_page_config(layout="wide", page_title="Multilanguage Invoice Extractor")
|
39 |
+
|
40 |
+
st.header("Gemini Application")
|
41 |
+
input=st.text_input("Input Prompt: ",key="input")
|
42 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
43 |
+
image=""
|
44 |
+
if uploaded_file is not None:
|
45 |
+
image = Image.open(uploaded_file)
|
46 |
+
st.image(image, caption="Uploaded Image.", use_column_width=True)
|
47 |
+
|
48 |
+
submit=st.button("Tell me about the image")
|
49 |
+
|
50 |
+
input_prompt = """
|
51 |
+
You are an expert in understanding invoices.
|
52 |
+
You will receive input images as invoices &
|
53 |
+
you will have to answer questions based on the input image
|
54 |
+
|
55 |
+
"""
|
56 |
+
|
57 |
+
# if submit button is clicked
|
58 |
+
if submit:
|
59 |
+
image_data = input_image_details(uploaded_file)
|
60 |
+
response = get_gemini_response(input_prompt, image_data, input)
|
61 |
+
st.subheader("The Response is")
|
62 |
+
st.write(response)
|
63 |
+
|
64 |
+
|
65 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
langchain
|
3 |
+
google.generativeai
|
4 |
+
python-dotenv
|
5 |
+
PyPDF2
|
6 |
+
chromadb # vector embeddings/vector stores
|
7 |
+
|