Bandipreethamreddy commited on
Commit
996167c
1 Parent(s): 578319b

Upload 2 files

Browse files
Files changed (2) hide show
  1. chatwithpdf.py +114 -0
  2. requirements.txt +9 -0
chatwithpdf.py ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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_community.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
+
13
+ load_dotenv()
14
+ os.getenv("GOOGLE_API_KEY")
15
+ genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
16
+
17
+ def 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
+
25
+
26
+
27
+ def get_text_chunks(text):
28
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=1000)
29
+ chunks = text_splitter.split_text(text)
30
+ return chunks
31
+
32
+
33
+ def get_vector_store(text_chunks):
34
+ embeddings = GoogleGenerativeAIEmbeddings(model = "models/embedding-001")
35
+ vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
36
+ vector_store.save_local("faiss_index")
37
+
38
+
39
+ def get_conversational_chain():
40
+
41
+ prompt_template = """
42
+ Answer the question as detailed as possible from the provided context, make sure to provide all the details, if the answer is not in
43
+ provided context just say, "answer is not available in the context", don't provide the wrong answer\n\n
44
+ Context:\n {context}?\n
45
+ Question: \n{question}\n
46
+
47
+ Answer:
48
+ """
49
+
50
+ model = ChatGoogleGenerativeAI(model="gemini-pro",
51
+ temperature=0.3)
52
+
53
+ prompt = PromptTemplate(template = prompt_template, input_variables = ["context", "question"])
54
+ chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
55
+
56
+ return chain
57
+
58
+
59
+
60
+ # def user_input(user_question):
61
+ # embeddings = GoogleGenerativeAIEmbeddings(model = "models/embedding-001")
62
+
63
+ # new_db = FAISS.load_local("faiss_index", embeddings)
64
+ # docs = new_db.similarity_search(user_question)
65
+
66
+ # chain = get_conversational_chain()
67
+
68
+
69
+ # response = chain(
70
+ # {"input_documents":docs, "question": user_question}
71
+ # , return_only_outputs=True)
72
+
73
+ # print(response)
74
+ # st.write("Reply: ", response["output_text"])
75
+
76
+ def user_input(user_question):
77
+ embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
78
+
79
+ # Here, set allow_dangerous_deserialization to True since you trust the source of your data
80
+ new_db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
81
+ docs = new_db.similarity_search(user_question)
82
+
83
+ chain = get_conversational_chain()
84
+
85
+ response = chain(
86
+ {"input_documents":docs, "question": user_question}
87
+ , return_only_outputs=True)
88
+ st.write("Reply: ", response["output_text"])
89
+
90
+
91
+
92
+ def main():
93
+ st.set_page_config("Chat With PDF")
94
+ st.header("Chat with PDF using Gemini💁")
95
+
96
+ user_question = st.text_input("Ask a Question from the PDF Files uploaded")
97
+
98
+ if user_question:
99
+ user_input(user_question)
100
+
101
+ with st.sidebar:
102
+ st.title("Menu:")
103
+ pdf_docs = st.file_uploader("Upload your PDF Files and Click on the Submit & Process Button", accept_multiple_files=True)
104
+ if st.button("Submit & Process"):
105
+ with st.spinner("Processing..."):
106
+ raw_text = get_pdf_text(pdf_docs)
107
+ text_chunks = get_text_chunks(raw_text)
108
+ get_vector_store(text_chunks)
109
+ st.success("Done")
110
+
111
+
112
+
113
+ if __name__ == "__main__":
114
+ main()
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ streamlit
2
+ google-generativeai
3
+ python-dotenv
4
+ langchain
5
+ PyPDF2
6
+ chromadb
7
+ faiss-cpu
8
+ langchain_google_genai
9
+ langchain-community