shivam11 commited on
Commit
61ec34a
·
verified ·
1 Parent(s): ead7bdd

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +144 -0
app.py ADDED
@@ -0,0 +1,144 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from PyPDF2 import PdfReader
3
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
4
+ from langchain_google_genai import GoogleGenerativeAIEmbeddings
5
+ import streamlit as st
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
+ # read all pdf files and return text
18
+
19
+
20
+ def get_pdf_text(pdf_docs):
21
+ text = ""
22
+ for pdf in pdf_docs:
23
+ pdf_reader = PdfReader(pdf)
24
+ for page in pdf_reader.pages:
25
+ text += page.extract_text()
26
+ return text
27
+
28
+ # split text into chunks
29
+
30
+
31
+ def get_text_chunks(text):
32
+ splitter = RecursiveCharacterTextSplitter(
33
+ chunk_size=10000, chunk_overlap=1000)
34
+ chunks = splitter.split_text(text)
35
+ return chunks # list of strings
36
+
37
+ # get embeddings for each chunk
38
+
39
+
40
+ def get_vector_store(chunks):
41
+ embeddings = GoogleGenerativeAIEmbeddings(
42
+ model="models/embedding-001") # type: ignore
43
+ vector_store = FAISS.from_texts(chunks, embedding=embeddings)
44
+ vector_store.save_local("faiss_index")
45
+
46
+
47
+ def get_conversational_chain():
48
+ prompt_template = """
49
+ Answer the question as detailed as possible from the provided context, make sure to provide all the details, if the answer is not in
50
+ provided context just say, "answer is not available in the context", don't provide the wrong answer\n\n
51
+ Context:\n {context}?\n
52
+ Question: \n{question}\n
53
+
54
+ Answer:
55
+ """
56
+
57
+ model = ChatGoogleGenerativeAI(model="gemini-pro",
58
+ client=genai,
59
+ temperature=0.3,
60
+ )
61
+ prompt = PromptTemplate(template=prompt_template,
62
+ input_variables=["context", "question"])
63
+ chain = load_qa_chain(llm=model, chain_type="stuff", prompt=prompt)
64
+ return chain
65
+
66
+
67
+ def clear_chat_history():
68
+ st.session_state.messages = [
69
+ {"role": "assistant", "content": "upload some pdfs and ask me a question"}]
70
+
71
+
72
+ def user_input(user_question):
73
+ embeddings = GoogleGenerativeAIEmbeddings(
74
+ model="models/embedding-001") # type: ignore
75
+
76
+ new_db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
77
+ docs = new_db.similarity_search(user_question)
78
+
79
+ chain = get_conversational_chain()
80
+
81
+ response = chain(
82
+ {"input_documents": docs, "question": user_question}, return_only_outputs=True, )
83
+
84
+ print(response)
85
+ return response
86
+
87
+
88
+ def main():
89
+ st.set_page_config(
90
+ page_title="Gemini PDF Chatbot",
91
+ page_icon="🤖"
92
+ )
93
+
94
+ # Sidebar for uploading PDF files
95
+ with st.sidebar:
96
+ st.title("Menu:")
97
+ pdf_docs = st.file_uploader(
98
+ "Upload your PDF Files and Click on the Submit & Process Button", accept_multiple_files=True)
99
+ if st.button("Submit & Process"):
100
+ with st.spinner("Processing..."):
101
+ raw_text = get_pdf_text(pdf_docs)
102
+ text_chunks = get_text_chunks(raw_text)
103
+ get_vector_store(text_chunks)
104
+ st.success("Done")
105
+
106
+ # Main content area for displaying chat messages
107
+ st.title("Chat with PDF files using Gemini🤖")
108
+ st.write("Welcome to the chat!")
109
+ st.sidebar.button('Clear Chat History', on_click=clear_chat_history)
110
+
111
+ # Chat input
112
+ # Placeholder for chat messages
113
+
114
+ if "messages" not in st.session_state.keys():
115
+ st.session_state.messages = [
116
+ {"role": "assistant", "content": "upload some pdfs and ask me a question"}]
117
+
118
+ for message in st.session_state.messages:
119
+ with st.chat_message(message["role"]):
120
+ st.write(message["content"])
121
+
122
+ if prompt := st.chat_input():
123
+ st.session_state.messages.append({"role": "user", "content": prompt})
124
+ with st.chat_message("user"):
125
+ st.write(prompt)
126
+
127
+ # Display chat messages and bot response
128
+ if st.session_state.messages[-1]["role"] != "assistant":
129
+ with st.chat_message("assistant"):
130
+ with st.spinner("Thinking..."):
131
+ response = user_input(prompt)
132
+ placeholder = st.empty()
133
+ full_response = ''
134
+ for item in response['output_text']:
135
+ full_response += item
136
+ placeholder.markdown(full_response)
137
+ placeholder.markdown(full_response)
138
+ if response is not None:
139
+ message = {"role": "assistant", "content": full_response}
140
+ st.session_state.messages.append(message)
141
+
142
+
143
+ if __name__ == "__main__":
144
+ main()