Mr-Vicky-01 commited on
Commit
b39298d
1 Parent(s): a93f476

Upload 2 files

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