AmitJ82 commited on
Commit
09ab01d
1 Parent(s): 12d583e

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +99 -0
  2. requirements.txt +7 -0
app.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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_community.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 environment variables
14
+ load_dotenv()
15
+ google_api_key = os.getenv("GOOGLE_API_KEY")
16
+ if not google_api_key:
17
+ st.error("Google API Key is not set. Please set the GOOGLE_API_KEY in the .env file.")
18
+ else:
19
+ genai.configure(api_key=google_api_key)
20
+
21
+
22
+ def get_pdf_text(pdf_docs):
23
+ text = ""
24
+ for pdf in pdf_docs:
25
+ pdf_reader = PdfReader(pdf)
26
+ for page in pdf_reader.pages:
27
+ text += page.extract_text()
28
+ return text
29
+
30
+
31
+ def get_text_chunks(text):
32
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=1000)
33
+ chunks = text_splitter.split_text(text)
34
+ return chunks
35
+
36
+
37
+ def get_vector_store(text_chunks):
38
+ embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
39
+ vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
40
+ os.makedirs("faiss_index", exist_ok=True)
41
+ vector_store.save_local("faiss_index")
42
+ st.write("FAISS index saved successfully!")
43
+
44
+
45
+ def get_conversational_chain():
46
+ prompt_template = """
47
+ Answer the question as detailed as possible from the provided context. If the answer is not in the provided context, just say,
48
+ 'Answer is not available in the context.' Do not provide a wrong answer.
49
+
50
+ Context: {context}
51
+ Question: {question}
52
+ Answer:
53
+ """
54
+ model = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.3)
55
+ prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
56
+ chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
57
+
58
+ return chain
59
+
60
+
61
+ def user_input(user_question):
62
+ embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
63
+ try:
64
+ new_db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
65
+ except Exception as e:
66
+ st.error(f"Error loading FAISS index: {e}")
67
+ return
68
+ docs = new_db.similarity_search(user_question)
69
+ chain = get_conversational_chain()
70
+ response = chain({"input_documents": docs, "question": user_question}, return_only_outputs=True)
71
+ st.write("Reply:", response["output_text"])
72
+
73
+
74
+ def main():
75
+ st.set_page_config(page_title="Chat with PDF using Gemini💁", layout="wide")
76
+ st.header("Chat with your PDF using Gemini 💁")
77
+
78
+ with st.sidebar:
79
+ st.title("Menu:")
80
+ pdf_docs = st.file_uploader("Upload your PDF files", accept_multiple_files=True)
81
+ if st.button("Submit & Process"):
82
+ if pdf_docs:
83
+ with st.spinner("Processing..."):
84
+ raw_text = get_pdf_text(pdf_docs)
85
+ text_chunks = get_text_chunks(raw_text)
86
+ get_vector_store(text_chunks)
87
+ st.success("Processing Done")
88
+ else:
89
+ st.error("Please upload PDF files.")
90
+
91
+ st.subheader("Ask a question about your PDFs")
92
+ user_question = st.text_input("Type your question here:")
93
+ if user_question:
94
+ with st.spinner("Getting your answer..."):
95
+ user_input(user_question)
96
+
97
+
98
+ if __name__ == "__main__":
99
+ main()
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ streamlit
2
+ PyPDF2
3
+ langchain-community
4
+ langchain-google-genai
5
+ google-generativeai
6
+ faiss-cpu
7
+ python-dotenv