Himanshu2003 commited on
Commit
458fcad
1 Parent(s): 373012e

Upload 2 files

Browse files
Files changed (2) hide show
  1. .env +1 -0
  2. app.py +90 -0
.env ADDED
@@ -0,0 +1 @@
 
 
1
+ GOOGLE_API_KEY = "AIzaSyCXtDpJFJVvI_FDO_X4oOXuQnFnL5xEYoM"
app.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
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
+
21
+ pdf_reader = PdfReader(pdf)
22
+ for page in pdf_reader.pages:
23
+ text += page.extract_text()
24
+
25
+ return text
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
+ 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
+ def get_conversational_chain():
39
+ prompt_template = """
40
+ Answer the question as detailed as possible from the provided context,
41
+ make sure to provide all the details, if the answer is not in the provided context just say,
42
+ "answer is not available in the context", don't provide the wrong answer.\n\n
43
+ Context: \n {context}?\n
44
+ Question: \n {question}\n
45
+
46
+ Answer:
47
+ """
48
+
49
+ model = ChatGoogleGenerativeAI(model = "gemini-pro", temperature=0.3)
50
+ prompt = PromptTemplate(template = prompt_template, input_variables = ["context", "question"])
51
+ chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
52
+ return chain
53
+
54
+
55
+ def user_input(user_question):
56
+ embeddings = GoogleGenerativeAIEmbeddings(model = "models/embedding-001")
57
+
58
+ new_db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
59
+ docs = new_db.similarity_search(user_question)
60
+
61
+ chain = get_conversational_chain()
62
+
63
+ response = chain(
64
+ {"input_documents": docs, "question": user_question},
65
+ return_only_outputs = True)
66
+
67
+ st.write("Reply: ", response["output_text"])
68
+
69
+ def main():
70
+ st.set_page_config("Chat With Multiple PDF")
71
+ st.header("Chat with Mulitple PDF using Gemini 👨‍💻")
72
+
73
+ user_question = st.text_input("Ask a Question from the PDF Files")
74
+
75
+ if user_question:
76
+ user_input(user_question)
77
+
78
+ with st.sidebar:
79
+ st.title("Menu: ")
80
+ pdf_docs = st.file_uploader("Upload your PDF Files and click on the Submit & Process Button", accept_multiple_files=True)
81
+ if st.button("Submit & Process"):
82
+ with st.spinner("Processing...."):
83
+ raw_text = get_pdf_text(pdf_docs)
84
+ text_chunks = get_text_chunks(raw_text)
85
+ get_vector_store(text_chunks)
86
+ st.success("Done")
87
+
88
+
89
+ if __name__ == "__main__":
90
+ main()