Spaces:
Sleeping
Sleeping
barghavani
commited on
Commit
•
2321c66
1
Parent(s):
44f17b9
Create app.py
Browse files
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.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 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
14 |
+
|
15 |
+
|
16 |
+
def get_pdf_text(pdf_docs):
|
17 |
+
text=""
|
18 |
+
for pdf in pdf_docs:
|
19 |
+
pdf_reader= PdfReader(pdf)
|
20 |
+
for page in pdf_reader.pages:
|
21 |
+
text+= page.extract_text()
|
22 |
+
return text
|
23 |
+
|
24 |
+
|
25 |
+
|
26 |
+
def get_text_chunks(text):
|
27 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=1000)
|
28 |
+
chunks = text_splitter.split_text(text)
|
29 |
+
return chunks
|
30 |
+
|
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 |
+
|
38 |
+
def get_conversational_chain():
|
39 |
+
|
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 |
+
Answer:
|
46 |
+
"""
|
47 |
+
|
48 |
+
model = ChatGoogleGenerativeAI(model="gemini-pro",
|
49 |
+
temperature=0.1)
|
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 |
+
|
58 |
+
def user_input(user_question):
|
59 |
+
embeddings = GoogleGenerativeAIEmbeddings(model = "models/embedding-001")
|
60 |
+
|
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 |
+
|
67 |
+
response = chain(
|
68 |
+
{"input_documents":docs, "question": user_question}
|
69 |
+
, return_only_outputs=True)
|
70 |
+
|
71 |
+
print(response)
|
72 |
+
st.write("Reply: ", response["output_text"])
|
73 |
+
|
74 |
+
|
75 |
+
|
76 |
+
|
77 |
+
def main():
|
78 |
+
st.set_page_config("Chat PDF")
|
79 |
+
st.header("QnA with Multiple PDF files💁")
|
80 |
+
|
81 |
+
user_question = st.text_input("Ask a Question from the PDF Files")
|
82 |
+
|
83 |
+
if user_question:
|
84 |
+
user_input(user_question)
|
85 |
+
|
86 |
+
with st.sidebar:
|
87 |
+
st.title("Menu:")
|
88 |
+
pdf_docs = st.file_uploader("Upload your PDF Files and Click on the Submit & Process Button", accept_multiple_files=True)
|
89 |
+
if st.button("Submit & Process"):
|
90 |
+
with st.spinner("Processing..."):
|
91 |
+
raw_text = get_pdf_text(pdf_docs)
|
92 |
+
text_chunks = get_text_chunks(raw_text)
|
93 |
+
get_vector_store(text_chunks)
|
94 |
+
st.success("Done")
|
95 |
+
|
96 |
+
|
97 |
+
|
98 |
+
if __name__ == "__main__":
|
99 |
+
main()
|