Upload 2 files
Browse files- app.py +74 -0
- requirements.txt +7 -0
app.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
from langchain_google_genai import GoogleGenerativeAIEmbeddings
|
4 |
+
import google.generativeai as genai
|
5 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
6 |
+
from langchain.chains.question_answering import load_qa_chain
|
7 |
+
from langchain.prompts import PromptTemplate
|
8 |
+
from dotenv import load_dotenv
|
9 |
+
from PIL import Image
|
10 |
+
import google.generativeai as genai
|
11 |
+
|
12 |
+
load_dotenv()
|
13 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
14 |
+
|
15 |
+
|
16 |
+
# Load Gemini pro vision
|
17 |
+
|
18 |
+
model = genai.GenerativeModel("gemini-pro-vision")
|
19 |
+
|
20 |
+
def get_final_response(system_prompt,input_image,user_prompt):
|
21 |
+
response = model.generate_content([system_prompt,input_image[0],user_prompt])
|
22 |
+
for candidate in response.candidates:
|
23 |
+
return [part.text for part in candidate.content.parts][0]
|
24 |
+
#return response.text
|
25 |
+
|
26 |
+
def image_processing(upload_file):
|
27 |
+
"""
|
28 |
+
This function converts the image in bytes
|
29 |
+
"""
|
30 |
+
|
31 |
+
if upload_file is not None:
|
32 |
+
data_bytes = upload_file.getvalue()
|
33 |
+
image_parts = [
|
34 |
+
{
|
35 |
+
"mime_type" : upload_file.type,
|
36 |
+
"data" : data_bytes
|
37 |
+
}
|
38 |
+
]
|
39 |
+
return image_parts
|
40 |
+
else:
|
41 |
+
raise FileNotFoundError("No file is uploaded.")
|
42 |
+
|
43 |
+
system_prompt = """
|
44 |
+
|
45 |
+
You are a professional Invoice reader with high expertise in multiple languages.
|
46 |
+
Once the image of invoice or any other document image is uploaded, you will correctly
|
47 |
+
answer the questions asked based on the uploaded file. If you do not find any answer just say
|
48 |
+
that "Answer cannot be found in the given file."
|
49 |
+
|
50 |
+
"""
|
51 |
+
|
52 |
+
## Stremlit code:
|
53 |
+
|
54 |
+
st.set_page_config(page_title="Gemini Image Analysis")
|
55 |
+
|
56 |
+
st.header("Multilanguage Invoice/Image reader")
|
57 |
+
input = st.text_input("Input Prompt: ", key= "user_prompt")
|
58 |
+
upload_file = st.file_uploader("Upload your invoice image", type = ["jpg", "jpeg", "png"])
|
59 |
+
submit = st.button("Click here to Get Answer...")
|
60 |
+
|
61 |
+
image = ""
|
62 |
+
|
63 |
+
if submit:
|
64 |
+
image_data = image_processing(upload_file)
|
65 |
+
response = get_final_response(system_prompt,image_data,input)
|
66 |
+
st.success(f"Answer : {response}")
|
67 |
+
|
68 |
+
if upload_file is not None:
|
69 |
+
image = Image.open(upload_file)
|
70 |
+
st.image(image, caption = "Uploaded image")
|
71 |
+
|
72 |
+
|
73 |
+
|
74 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
google-generativeai
|
3 |
+
python-dotenv
|
4 |
+
langchain
|
5 |
+
PyPDF2
|
6 |
+
chromadb
|
7 |
+
langchain_google_genai
|