Spaces:
Running
Running
Commit
•
1d758a2
1
Parent(s):
aa45e63
Update app.py
Browse files
app.py
CHANGED
@@ -14,87 +14,67 @@ 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
|
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
|
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
|
43 |
Context:\n {context}?\n
|
44 |
Question: \n{question}\n
|
45 |
|
46 |
Answer:
|
47 |
"""
|
48 |
-
|
49 |
-
|
50 |
-
temperature=0.1)
|
51 |
-
|
52 |
-
prompt = PromptTemplate(template = prompt_template, input_variables = ["context", "question"])
|
53 |
chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
|
54 |
-
|
55 |
return chain
|
56 |
|
57 |
-
|
58 |
-
|
59 |
def user_input(user_question):
|
60 |
-
embeddings = GoogleGenerativeAIEmbeddings(model
|
61 |
-
|
62 |
-
new_db = FAISS.load_local("faiss_index", embeddings,allow_dangerous_deserialization= True)
|
63 |
docs = new_db.similarity_search(user_question)
|
64 |
-
|
65 |
chain = get_conversational_chain()
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
st.
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
get_vector_store(text_chunks)
|
95 |
-
st.success("Done")
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
if __name__ == "__main__":
|
100 |
-
main()
|
|
|
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 |
def get_text_chunks(text):
|
25 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=1000)
|
26 |
chunks = text_splitter.split_text(text)
|
27 |
return chunks
|
28 |
|
|
|
29 |
def get_vector_store(text_chunks):
|
30 |
+
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
|
31 |
vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
|
32 |
vector_store.save_local("faiss_index")
|
33 |
|
|
|
34 |
def get_conversational_chain():
|
|
|
35 |
prompt_template = """
|
36 |
Answer the question as detailed as possible from the provided context, make sure to provide all the details, if the answer is not in
|
37 |
+
provided context just say, "answer is not available in the Provided PDF", don't provide the wrong answer\n\n
|
38 |
Context:\n {context}?\n
|
39 |
Question: \n{question}\n
|
40 |
|
41 |
Answer:
|
42 |
"""
|
43 |
+
model = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.1)
|
44 |
+
prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
|
|
|
|
|
|
|
45 |
chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
|
|
|
46 |
return chain
|
47 |
|
|
|
|
|
48 |
def user_input(user_question):
|
49 |
+
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
|
50 |
+
new_db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
|
|
|
51 |
docs = new_db.similarity_search(user_question)
|
|
|
52 |
chain = get_conversational_chain()
|
53 |
+
response = chain({"input_documents": docs, "question": user_question}, return_only_outputs=True)
|
54 |
+
return response["output_text"]
|
55 |
+
|
56 |
+
# Streamlit app initialization
|
57 |
+
st.title("Chat With PDF 📄")
|
58 |
+
|
59 |
+
if 'messages' not in st.session_state:
|
60 |
+
st.session_state.messages = [{'role': 'assistant', "content": 'Hello! Upload a PDF and ask me anything about its content.'}]
|
61 |
+
|
62 |
+
with st.sidebar:
|
63 |
+
st.title("Menu:")
|
64 |
+
uploaded_file = st.file_uploader("Upload your PDF Files and Click on the Submit & Process Button", accept_multiple_files=True)
|
65 |
+
if st.button("Submit & Process"):
|
66 |
+
with st.spinner("Processing..."):
|
67 |
+
raw_text = get_pdf_text(uploaded_file)
|
68 |
+
text_chunks = get_text_chunks(raw_text)
|
69 |
+
get_vector_store(text_chunks)
|
70 |
+
st.success("Done")
|
71 |
+
|
72 |
+
user_prompt = st.chat_input("Ask me anything about the content of the PDF:")
|
73 |
+
if user_prompt:
|
74 |
+
st.session_state.messages.append({'role': 'user', "content": user_prompt})
|
75 |
+
response = user_input(user_prompt)
|
76 |
+
st.session_state.messages.append({'role': 'assistant', "content": response})
|
77 |
+
|
78 |
+
for message in st.session_state.messages:
|
79 |
+
with st.chat_message(message['role']):
|
80 |
+
st.write(message['content'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|