souvikmaji22 commited on
Commit
1fdd79a
1 Parent(s): 05174b9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -0
app.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from dotenv import load_dotenv
2
+ load_dotenv()
3
+
4
+ import streamlit as st
5
+ import os
6
+ import google.generativeai as genai
7
+ from PyPDF2 import PdfReader
8
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
9
+ from langchain_google_genai import GoogleGenerativeAIEmbeddings
10
+ from langchain.vectorstores import FAISS
11
+ from langchain.prompts import PromptTemplate
12
+ from langchain.chains.question_answering import load_qa_chain
13
+ from langchain_google_genai import ChatGoogleGenerativeAI
14
+
15
+ genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
16
+
17
+ def get_text(pdfs):
18
+ text = ""
19
+ for pdf in pdfs:
20
+ pdf_reader = PdfReader(pdf)
21
+ for page in pdf_reader.pages:
22
+ text += page.extract_text()
23
+ return text
24
+
25
+ def get_chunks(text):
26
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=1000)
27
+ chunks = text_splitter.split_text(text)
28
+ return chunks
29
+
30
+ def get_vectors(text_chunks):
31
+ embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
32
+ vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
33
+ vector_store.save_local("faiss_index")
34
+
35
+ def get_conv_chain():
36
+ prompt_template = """
37
+ Answer the question as detailed as possible based on the provided context. If the answer is not in the provided context, simply state, "The answer is not in the context." Do not provide incorrect or misleading information.\n\n
38
+ Context:\n {context}?\n
39
+ Question: \n{question}\n
40
+
41
+ Answer:
42
+ """
43
+ model = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.5)
44
+ prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
45
+ chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
46
+
47
+ return chain
48
+
49
+ def user_input(qs):
50
+ embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
51
+ new_db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
52
+ docs = new_db.similarity_search(qs)
53
+ chain = get_conv_chain()
54
+
55
+ res = chain(
56
+ {"input_documents": docs, "question": qs}, return_only_outputs=True
57
+ )
58
+
59
+ print(res)
60
+ st.write("Response: ", res["output_text"])
61
+
62
+ # Streamlit
63
+
64
+ def main():
65
+ st.set_page_config(page_title="Chat with Multiple PDF")
66
+ st.header("Chat with Multiple PDF using Gemini-Pro")
67
+
68
+ user_qs = st.text_input("Ask a question from your PDF Files")
69
+
70
+ if user_qs:
71
+ user_input(user_qs)
72
+ with st.sidebar:
73
+ st.title("Menu: ")
74
+ docs = st.file_uploader("Upload your PDF files", type=["pdf"], accept_multiple_files=True)
75
+
76
+ if st.button("Submit & Proceed"):
77
+ if docs:
78
+ with st.spinner("Please wait a moment..."):
79
+ t = get_text(docs)
80
+ text_chunks = get_chunks(t)
81
+ get_vectors(text_chunks)
82
+ st.success("Done!")
83
+ else:
84
+ st.warning("Please upload at least one PDF file.")
85
+
86
+ if __name__ == "__main__":
87
+ main()