Rajut commited on
Commit
81ae4b7
·
verified ·
1 Parent(s): 7eefb19

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +100 -0
app.py ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ 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", google_api_key="AIzaSyAudl0KD5TC7XgQFEbp3jpGUuqHoPYUk5U")
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
+
77
+
78
+
79
+ def main():
80
+ st.set_page_config("Chat PDF")
81
+ st.header("PDF_Query_Chatbot")
82
+
83
+ user_question = st.text_input("Ask any Question from the PDF Files")
84
+
85
+ if user_question:
86
+ user_input(user_question)
87
+
88
+ with st.sidebar:
89
+ st.title("Menu:")
90
+ pdf_docs = st.file_uploader("Upload your PDF Files and Click on the Submit & Process Button", accept_multiple_files=True)
91
+ if st.button("Submit & Process"):
92
+ with st.spinner("Processing..."):
93
+ raw_text = get_pdf_text(pdf_docs)
94
+ text_chunks = get_text_chunks(raw_text)
95
+ get_vector_store(text_chunks)
96
+ st.success("Done")
97
+
98
+
99
+ if __name__ == "__main__":
100
+ main()