RubaKhan242 commited on
Commit
3b0750c
·
verified ·
1 Parent(s): eccce4e

Upload 4 files

Browse files
Files changed (4) hide show
  1. app.py +96 -0
  2. index.faiss +0 -0
  3. index.pkl +3 -0
  4. requirements.txt +9 -0
app.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ 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")
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
+ prompt_template = """
41
+ Answer the question as detailed as possible from the provided context, make sure to provide all the details, if the answer is not in
42
+ provided context just say, "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
+
51
+ prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
52
+ chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
53
+
54
+ return chain
55
+
56
+
57
+ def user_input(user_question):
58
+ embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
59
+
60
+ # Allow dangerous deserialization for your own data
61
+ new_db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
62
+ docs = new_db.similarity_search(user_question)
63
+
64
+ chain = get_conversational_chain()
65
+
66
+ response = chain(
67
+ {"input_documents": docs, "question": user_question},
68
+ return_only_outputs=True
69
+ )
70
+
71
+ print(response)
72
+ st.write("Reply: ", response["output_text"])
73
+
74
+
75
+ def main():
76
+ st.set_page_config("Chat PDF")
77
+ st.header("Chat with PDF using Gemini💁")
78
+
79
+ user_question = st.text_input("Ask a Question from the PDF Files")
80
+
81
+ if user_question:
82
+ user_input(user_question)
83
+
84
+ with st.sidebar:
85
+ st.title("Menu:")
86
+ pdf_docs = st.file_uploader("Upload your PDF Files and Click on the Submit & Process Button", accept_multiple_files=True)
87
+ if st.button("Submit & Process"):
88
+ with st.spinner("Processing..."):
89
+ raw_text = get_pdf_text(pdf_docs)
90
+ text_chunks = get_text_chunks(raw_text)
91
+ get_vector_store(text_chunks)
92
+ st.success("Done")
93
+
94
+
95
+ if __name__ == "__main__":
96
+ main()
index.faiss ADDED
Binary file (3.12 kB). View file
 
index.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:65169d5dde73cd789d7c1e8b7c59a65a0331568b3b81389d42a799cb8e7a15eb
3
+ size 545
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ streamlit
2
+ google-generativeai
3
+ python-dotenv
4
+ langchain
5
+ PyPDF2
6
+ chromadb
7
+ faiss-cpu
8
+ langchain_google_genai
9
+ langchain-community