atharv20/multiPDFchat
0
1import streamlit as st
2from PyPDF2 import PdfReader
3from langchain.text_splitter import RecursiveCharacterTextSplitter
4import os
5from langchain_google_genai import GoogleGenerativeAIEmbeddings
6import google.generativeai as genai
7from langchain.vectorstores import FAISS
8from langchain_google_genai import ChatGoogleGenerativeAI
9from langchain.chains.question_answering import load_qa_chain
10from langchain.prompts import PromptTemplate
11from dotenv import load_dotenv
12
13load_dotenv()
14os.getenv("GOOGLE_API_KEY")
15genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
16
17def get_pdf_text(pdf_docs):
18 text = ""
19 for pdf in pdf_docs:
20 pdf_reader = PdfReader(pdf)
21 for page in pdf_reader.pages:
22 text += page.extract_text()
23 return text
24
25def get_text_chunks(text):
26 text_splitter = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=1000)
27 chunks = text_splitter.split_text(text)
28 return chunks
29
30def get_vector_store(text_chunks):
31 embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
32 vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
33 vector_store.save_local("faiss_index")
34
35def get_conversational_chain():
36 prompt_template = """
37 Answer the question as detailed as possible from the provided context, make sure to provide all the details. If the answer is not in
38 the provided context, just say, "The answer is not available in the context." Don't provide the wrong answer.
39
40 Context:\n{context}\n
41 Question:\n{question}\n
42 Answer:
43 """
44
45 model = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.3)
46 prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
47 chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
48
49 return chain
50
51def user_input(user_question):
52 embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
53 new_db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
54 docs = new_db.similarity_search(user_question)
55 chain = get_conversational_chain()
56 response = chain({"input_documents": docs, "question": user_question}, return_only_outputs=True)
57 st.write("Reply:", response["output_text"])
58
59def main():
60 st.set_page_config(page_title="Chat PDF", page_icon="๐", layout="wide")
61 st.markdown("<h1 style='text-align: center; color: #4CAF50;'>Chat with PDF using Gemini ๐</h1>", unsafe_allow_html=True)
62
63 user_question = st.text_input("Ask a Question from the PDF Files", "")
64
65 if user_question:
66 user_input(user_question)
67
68 with st.sidebar:
69 st.title("Menu")
70 pdf_docs = st.file_uploader("Upload your PDF Files and Click on the Submit & Process Button", accept_multiple_files=True)
71 if st.button("Submit & Process"):
72 with st.spinner("Processing..."):
73 raw_text = get_pdf_text(pdf_docs)
74 text_chunks = get_text_chunks(raw_text)
75 get_vector_store(text_chunks)
76 st.success("Processing Complete!")
77
78if __name__ == "__main__":
79 main()
80 