Upload 3 files
Browse files- .env +1 -0
- app.py +101 -0
- requirements.txt +9 -0
.env
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
GOOGLE_API_KEY="AIzaSyDsQ2F-6XfxuRFUXYFrcG6LSB95Z6xLre8"
|
app.py
ADDED
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from PyPDF2 import PdfReader
|
3 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
4 |
+
import os
|
5 |
+
from langchain_google_genai import GoogleGenerativeAIEmbeddings
|
6 |
+
import google.generativeai as genai
|
7 |
+
from langchain.vectorstores import FAISS
|
8 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
9 |
+
from langchain.chains.question_answering import load_qa_chain
|
10 |
+
from langchain.prompts import PromptTemplate
|
11 |
+
from dotenv import load_dotenv
|
12 |
+
from PIL import Image
|
13 |
+
import io
|
14 |
+
|
15 |
+
# Load environment variables
|
16 |
+
load_dotenv()
|
17 |
+
os.getenv("GOOGLE_API_KEY")
|
18 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
19 |
+
|
20 |
+
# Global variable for embeddings
|
21 |
+
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
|
22 |
+
|
23 |
+
def get_pdf_text(pdf_docs):
|
24 |
+
text = ""
|
25 |
+
for pdf in pdf_docs:
|
26 |
+
pdf_reader = PdfReader(pdf)
|
27 |
+
for page in pdf_reader.pages:
|
28 |
+
text += page.extract_text()
|
29 |
+
return text
|
30 |
+
|
31 |
+
def get_image_text(image_files):
|
32 |
+
# Placeholder function for extracting text from images
|
33 |
+
# Implement OCR or other text extraction methods as needed
|
34 |
+
text = ""
|
35 |
+
for image in image_files:
|
36 |
+
# Simulate text extraction
|
37 |
+
text += "Extracted text from image.\n"
|
38 |
+
return text
|
39 |
+
|
40 |
+
def get_text_chunks(text):
|
41 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=1000)
|
42 |
+
chunks = text_splitter.split_text(text)
|
43 |
+
return chunks
|
44 |
+
|
45 |
+
def get_vector_store(text_chunks):
|
46 |
+
vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
|
47 |
+
vector_store.save_local("faiss_index")
|
48 |
+
|
49 |
+
def load_faiss_index():
|
50 |
+
return FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
|
51 |
+
|
52 |
+
def get_conversational_chain():
|
53 |
+
prompt_template = """
|
54 |
+
Answer the question as detailed as possible from the provided context, make sure to provide all the details, if the answer is not in
|
55 |
+
provided context just say, "answer is not available in the context", don't provide the wrong answer\n\n
|
56 |
+
Context:\n {context}?\n
|
57 |
+
Question: \n{question}\n
|
58 |
+
|
59 |
+
Answer:
|
60 |
+
"""
|
61 |
+
|
62 |
+
model = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.3)
|
63 |
+
prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
|
64 |
+
chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
|
65 |
+
return chain
|
66 |
+
|
67 |
+
def user_input(user_question):
|
68 |
+
new_db = load_faiss_index()
|
69 |
+
docs = new_db.similarity_search(user_question)
|
70 |
+
chain = get_conversational_chain()
|
71 |
+
response = chain({"input_documents": docs, "question": user_question}, return_only_outputs=True)
|
72 |
+
print(response)
|
73 |
+
st.write("Reply: ", response["output_text"])
|
74 |
+
st.snow() # Trigger snowflakes animation after receiving reply
|
75 |
+
|
76 |
+
def main():
|
77 |
+
st.set_page_config("Chat with Documents and Images", page_icon="π")
|
78 |
+
st.header("Chat with Multi Docs and Images π")
|
79 |
+
|
80 |
+
user_question = st.text_input("Ask a Question from the PDF Files or Uploaded Images")
|
81 |
+
|
82 |
+
if user_question:
|
83 |
+
user_input(user_question)
|
84 |
+
|
85 |
+
with st.sidebar:
|
86 |
+
st.title("Menu:")
|
87 |
+
|
88 |
+
pdf_docs = st.file_uploader("Upload your PDF Files", accept_multiple_files=True, type=["pdf"])
|
89 |
+
image_files = st.file_uploader("Upload your Image Files", accept_multiple_files=True, type=["jpg", "jpeg", "png"])
|
90 |
+
|
91 |
+
if st.button("Submit & Process"):
|
92 |
+
with st.spinner("Processing..."):
|
93 |
+
raw_text = get_pdf_text(pdf_docs) + get_image_text(image_files)
|
94 |
+
text_chunks = get_text_chunks(raw_text)
|
95 |
+
get_vector_store(text_chunks)
|
96 |
+
st.success("Done")
|
97 |
+
st.balloons() # Trigger balloons animation
|
98 |
+
|
99 |
+
if __name__ == "__main__":
|
100 |
+
main()
|
101 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
google-generativeai
|
3 |
+
python-dotenv
|
4 |
+
langchain
|
5 |
+
PyPDF2
|
6 |
+
chroma
|
7 |
+
faiss-cpu
|
8 |
+
langchain_google_genai
|
9 |
+
langchain_community
|