khatri-indra commited on
Commit
d8baed7
·
verified ·
1 Parent(s): 1d21ed9

Upload 5 files

Browse files
Files changed (5) hide show
  1. .env +1 -0
  2. app.py +88 -0
  3. faiss_index/index.faiss +0 -0
  4. faiss_index/index.pkl +3 -0
  5. requirements.txt +9 -0
.env ADDED
@@ -0,0 +1 @@
 
 
1
+ GOOGLE_API_KEY = "AIzaSyDKsDooyPRIIz_HAcm5YilrW7iWI4hhpcs"
app.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
14
+
15
+ load_dotenv()
16
+
17
+ genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
18
+
19
+ def get_pdf_text(pdf_docs):
20
+ text=""
21
+ for pdf in pdf_docs:
22
+ pdf_reader = PdfReader(pdf)
23
+ for page in pdf_reader.pages:
24
+ text += page.extract_text()
25
+ return text
26
+
27
+ def get_text_chunks(text):
28
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=100)
29
+ chunks = text_splitter.split_text(text)
30
+ return chunks
31
+
32
+ def get_vector_store(text_chunks):
33
+ embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
34
+ vector_store=FAISS.from_texts(text_chunks, embedding=embeddings)
35
+ vector_store.save_local("faiss_index")
36
+
37
+ def get_conversational_chain():
38
+ prompt_template = """
39
+ Answer the question as detailed as possible from the provided context, make sure to provide all the details, if the answer is not in the provided
40
+ context just say. "answer is not available in the context", don't provide the wrong answer\n\n
41
+ Context: \n {context}?\n
42
+ Question: \n {question}\n
43
+
44
+ Answer:
45
+ """
46
+
47
+ model = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.3)
48
+
49
+ prompt = PromptTemplate(template = prompt_template, input_variables=["context", "question"])
50
+ chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
51
+ return chain
52
+
53
+ def user_input(user_question):
54
+ embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
55
+
56
+ new_db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
57
+ docs = new_db.similarity_search(user_question)
58
+
59
+ chain = get_conversational_chain()
60
+
61
+ response = chain(
62
+ {"input_documents":docs, "question": user_question},
63
+ return_only_outputs=True
64
+ )
65
+
66
+ print(response)
67
+ st.write("Reply: ", response["output_text"])
68
+
69
+ def main():
70
+ st.set_page_config("Chit Chat with PDFs")
71
+ st.header("Chit Chat with your PDFs")
72
+
73
+ user_question = st.text_input("Ask a question to chat with your pdf")
74
+
75
+ if user_question:
76
+ user_input(user_question)
77
+ with st.sidebar:
78
+ st.title("Menu:")
79
+ pdf_docs = st.file_uploader("Upload your PDF files here!!!", accept_multiple_files=True, type=['pdf'])
80
+ if st.button("Submit"):
81
+ with st.spinner("Processing..."):
82
+ raw_text = get_pdf_text(pdf_docs)
83
+ text_chunks = get_text_chunks(raw_text)
84
+ get_vector_store(text_chunks)
85
+ st.success("Done")
86
+
87
+ if __name__ == "__main__":
88
+ main()
faiss_index/index.faiss ADDED
Binary file (3.12 kB). View file
 
faiss_index/index.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:76d379f18dcc1f9502791d7278e7b4857ccee07a8e290c12c4f014688282872f
3
+ size 2772
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