sengartech commited on
Commit
aeffa0a
·
1 Parent(s): ad4668e

pdf chat RAG app

Browse files
Files changed (3) hide show
  1. .gitignore +1 -0
  2. app.py +96 -0
  3. requirements.txt +8 -0
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ /faiss_index
app.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PyPDF2 import PdfReader
3
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
4
+ from langchain_google_genai import GoogleGenerativeAIEmbeddings
5
+ import google.generativeai as genai
6
+ from langchain.vectorstores import FAISS
7
+ from langchain_google_genai import ChatGoogleGenerativeAI
8
+ from langchain.chains.question_answering import load_qa_chain
9
+ from langchain.prompts import PromptTemplate
10
+ from dotenv import load_dotenv
11
+ import os
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, allow_dangerous_deserialization=True,)
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
+
77
+ def main():
78
+ st.set_page_config("Chat PDF")
79
+ st.header("Chat with PDF")
80
+
81
+ user_question = st.text_input("Ask a Question from the PDF Files")
82
+
83
+ if user_question:
84
+ user_input(user_question)
85
+
86
+ with st.sidebar:
87
+ st.title("Menu:")
88
+ pdf_docs = st.file_uploader("Upload your PDF Files and Click on the Submit & Process Button", accept_multiple_files=True)
89
+ if st.button("Submit & Process"):
90
+ with st.spinner("Processing..."):
91
+ raw_text = get_pdf_text(pdf_docs)
92
+ text_chunks = get_text_chunks(raw_text)
93
+ get_vector_store(text_chunks)
94
+ st.success("Done")
95
+
96
+ main()
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ streamlit
2
+ google-generativeai
3
+ python-dotenv
4
+ langchain
5
+ PyPDF2
6
+ faiss-cpu
7
+ langchain_google_genai
8
+ langchain-community