Vikrant26 commited on
Commit
d1e292f
Β·
verified Β·
1 Parent(s): 58839f5

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +94 -0
  2. requirements.txt +8 -0
app.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PyPDF2 import PdfReader
3
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
4
+ from langchain_google_genai import GoogleGenerativeAIEmbeddings, ChatGoogleGenerativeAI
5
+ from langchain_community.vectorstores import FAISS # Updated import
6
+ from langchain.chains.question_answering import load_qa_chain
7
+ from langchain.prompts import PromptTemplate
8
+ from dotenv import load_dotenv
9
+ import os
10
+ import google.generativeai as genai
11
+
12
+ load_dotenv()
13
+
14
+ # Configure Generative AI API key
15
+ genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
16
+
17
+ def get_pdf_text(pdf_docs):
18
+ """Extract text from PDF documents."""
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
+ def get_text_chunks(text):
27
+ """Split text into manageable chunks."""
28
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=1000)
29
+ chunks = text_splitter.split_text(text)
30
+ return chunks
31
+
32
+ def get_vector_store(text_chunks):
33
+ """Generate embeddings and create FAISS index."""
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
+ """Load conversational chain for question answering."""
40
+ prompt_template = """
41
+ Answer the question as detailed as possible from the provided context,
42
+ 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",
44
+ 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", temperature=0.3)
52
+ prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
53
+ chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
54
+ return chain
55
+
56
+ def user_input(user_question):
57
+ """Process user input and generate response."""
58
+ embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
59
+
60
+ # Load FAISS index with the necessary flag
61
+ new_db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
62
+ docs = new_db.similarity_search(user_question)
63
+
64
+ # Load conversational chain
65
+ chain = get_conversational_chain()
66
+
67
+ # Generate response
68
+ response = chain({"input_documents": docs, "question": user_question}, return_only_outputs=True)
69
+
70
+ return response["output_text"]
71
+
72
+ def main():
73
+ """Main Streamlit application function."""
74
+ st.set_page_config("Chat PDF")
75
+ st.header("πŸ“„πŸ“„ Chat with Multi_PDFs πŸ“„πŸ“„")
76
+
77
+ user_question = st.text_input("Ask a Question from the PDF Files")
78
+
79
+ if user_question:
80
+ response = user_input(user_question)
81
+ st.write("Reply: ", response)
82
+
83
+ with st.sidebar:
84
+ st.title("Menu:")
85
+ pdf_docs = st.file_uploader("Upload your PDF Files and Click on the Submit & Process Button", accept_multiple_files=True)
86
+ if st.button("Submit & Process"):
87
+ with st.spinner("Processing..."):
88
+ raw_text = get_pdf_text(pdf_docs)
89
+ text_chunks = get_text_chunks(raw_text)
90
+ get_vector_store(text_chunks)
91
+ st.success("Done")
92
+
93
+ if __name__ == "__main__":
94
+ 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