-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
219 lines (188 loc) · 10.1 KB
/
app.py
File metadata and controls
219 lines (188 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import streamlit as st
import os
from langchain_qdrant import QdrantVectorStore
from langchain_community.document_loaders import PyPDFLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_google_genai import ChatGoogleGenerativeAI, GoogleGenerativeAIEmbeddings
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough
from qdrant_client import QdrantClient
st.set_page_config(layout="wide", page_title="ChatPDF 🗣️", initial_sidebar_state="expanded")
st.markdown(
"""
<style>
.st-emotion-cache-1mph9ef {
flex-direction: row-reverse;
text-align: right;
}
[data-testid='stHeaderActionElements'] {
display: none;
}
</style>
""",
unsafe_allow_html=True,
)
if "connect" not in st.session_state:
st.session_state["connect"] = False
if "uploaded_file" not in st.session_state:
st.session_state["uploaded_file"] = None
if "file_mkr" not in st.session_state:
st.session_state["file_mkr"] = False
if "file_path" not in st.session_state:
st.session_state["file_path"] = None
if "file_processed" not in st.session_state:
st.session_state["file_processed"] = False
if "vector_store" not in st.session_state:
st.session_state["vector_store"] = None
if "rag_chain" not in st.session_state:
st.session_state["rag_chain"] = None
if "messages" not in st.session_state:
st.session_state["messages"] = []
def process_pdf(pdf_path: str) -> QdrantVectorStore:
loader = PyPDFLoader(pdf_path)
documents = loader.load()
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
splits = text_splitter.split_documents(documents)
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
vector_store = QdrantVectorStore.from_documents(
documents=splits,
embedding=embeddings,
url=os.environ["QDRANT_URL"],
api_key=os.environ["QDRANT_API_KEY"],
collection_name="chatpdf_vectors",
force_recreate=True,
timeout=10
)
return vector_store
def setup_rag_chain(vector_store: QdrantVectorStore):
llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash", temperature=0.2)
retriever = vector_store.as_retriever(search_kwargs={"k": 5,"lambda_mult":0.7},search_type="mmr")
rag_template = """
You are an AI assistant for answering questions about a PDF document.
Use the following context to answer the question.
If you don't know the answer, just say that you don't know, don't try to make up an answer.
Context: {context}
Question: {question}
"""
rag_prompt = ChatPromptTemplate.from_template(rag_template)
rag_chain = (
{"context": retriever, "question": RunnablePassthrough()}
| rag_prompt
| llm
| StrOutputParser()
)
return rag_chain
st.markdown("<h1 style='text-align: center;'>ChatPDF 🗣️ (Talk with your pdf)</h1>", unsafe_allow_html=True)
st.markdown("<h2 style='background: -webkit-linear-gradient(#2f85d6, #e80ecb);-webkit-text-fill-color: transparent;-webkit-background-clip: text'>" \
"Chat with your PDF files using Gemini AI. This app allows you to upload a PDF file and interact with it using natural language queries. The app " \
"uses Gemini AI for processing and Qdrant for vector storage and retrieval.</h2>", unsafe_allow_html=True)
with st.sidebar:
st.header("🔐 API Configurations",divider="gray")
st.subheader("Gemini API:",divider="blue",width="content")
st.write(f"If you don't have create one from [here](https://aistudio.google.com/app/apikey).")
gemini_api = st.text_input("Enter Gemini API Key", type="password",disabled=st.session_state["connect"])
os.environ["GOOGLE_API_KEY"] = gemini_api
st.subheader("Qdrant API:",divider="green",width="content")
st.write(f"If you don't have create one from [here](https://qdrant.tech/documentation/cloud-quickstart).")
qdrant_url = st.text_input("Enter Qdrant URL",placeholder="Ex: https://###.###.gcp.cloud.qdrant.io",disabled=st.session_state["connect"])
qdrant_api = st.text_input("Enter Qdrant API Key", type="password",disabled=st.session_state["connect"])
os.environ["QDRANT_URL"] = qdrant_url
os.environ["QDRANT_API_KEY"] = qdrant_api
btn = st.button("Connect",use_container_width=True, disabled=st.session_state["connect"])
if not gemini_api or not qdrant_url or not qdrant_api:
btn = False
if btn:
st.session_state["connect"] = True
st.rerun()
if st.session_state["connect"]:
if st.button("Disconnect", use_container_width=True):
if st.session_state["file_path"]:
os.remove(st.session_state["file_path"])
st.session_state["connect"] = False
st.session_state["uploaded_file"] = None
st.session_state["file_mkr"] = False
st.session_state["file_path"] = None
st.session_state["file_processed"] = False
st.session_state["vector_store"] = None
st.session_state["rag_chain"] = None
st.session_state["messages"] = []
try:
client = QdrantClient(url=os.environ["QDRANT_URL"], api_key=os.environ["QDRANT_API_KEY"])
client.delete_collection(collection_name="chatpdf_vectors")
except:
pass
st.rerun()
if not st.session_state["connect"]:
st.markdown("<h3>Please Give the API Configurations on the sidebar to get started.</h3>", unsafe_allow_html=True)
else:
col1, col2 = st.columns(2)
with col1:
st.session_state["uploaded_file"] = st.file_uploader("Upload a PDF file", type='pdf',disabled=st.session_state["file_mkr"])
if not st.session_state["uploaded_file"] and st.session_state["file_path"]:
os.remove(st.session_state["file_path"])
st.session_state["file_mkr"] = False
st.session_state["file_path"] = None
st.session_state["file_processed"] = False
st.session_state["vector_store"] = None
st.session_state["rag_chain"] = None
st.session_state["messages"] = []
try:
client = QdrantClient(url=os.environ["QDRANT_URL"], api_key=os.environ["QDRANT_API_KEY"])
client.delete_collection(collection_name="chatpdf_vectors")
except:
pass
st.rerun()
if st.session_state["uploaded_file"]:
target_folder = "uploaded_pdfs"
os.makedirs(target_folder, exist_ok=True)
file_path = os.path.join(target_folder, st.session_state["uploaded_file"].name)
st.session_state["file_path"] = file_path
st.success(f"File saved to {st.session_state['file_path']}")
if not st.session_state["file_mkr"]:
with open(st.session_state["file_path"], "wb") as f:
f.write(st.session_state["uploaded_file"].getbuffer())
st.session_state["file_mkr"] = True
st.rerun()
else:
st.session_state["file_path"] = None
with col2:
if not st.session_state["file_path"]:
st.markdown("<h3 style='margin-left: 20px;'>Now Upload a PDF file.</h3>", unsafe_allow_html=True)
if st.session_state["file_path"] and not st.session_state["file_processed"]:
st.markdown("<h5 style='margin-left: 20px;'>If you want to change the PDF file, please delete the current file and upload a new one.</h5>", unsafe_allow_html=True)
st.markdown("<h3 style='margin-left: 20px;'>Let's process your PDF file before chatting.</h3>", unsafe_allow_html=True)
if st.button("Process PDF", use_container_width=True):
st.write("Please wait for sometime...")
try:
st.session_state["vector_store"] = process_pdf(st.session_state["file_path"])
st.session_state["rag_chain"] = setup_rag_chain(st.session_state["vector_store"])
st.session_state["file_processed"] = True
st.rerun()
except Exception as e:
st.write("Error processing the PDF file. Please check the file format or API Configurations and try again.")
if st.session_state["file_processed"]:
st.write("\n")
st.markdown("<h5 style='margin-left: 20px;'>If you want to change the PDF file, please delete the current file and upload a new one.</h5>", unsafe_allow_html=True)
st.markdown("<h3 style='margin-left: 20px;'>Now you are ready to chat with your PDF file.</h3>", unsafe_allow_html=True)
if st.session_state["file_processed"]:
st.markdown("<hr style='border: 1px solid; margin:5px'/><h3 style='margin-left: 20px;'>Chat with your PDF file:</h3>", unsafe_allow_html=True)
st.chat_message("ai").markdown("What do you want to ask about your PDF file?")
for message in st.session_state["messages"]:
if message["role"] == "user":
st.chat_message(message["role"]).markdown(f"<p style='text-align: right;'>{message["content"]}</p>", unsafe_allow_html=True)
else:
st.chat_message(message["role"]).write(message["content"], unsafe_allow_html=True)
query = st.chat_input("Ask a question about your PDF file:")
try:
if query:
response = st.session_state["rag_chain"].invoke(query)
except Exception as e:
st.session_state["messages"].append({"role": "user", "content": query})
response = "Sorry something went wrong. We cannot process your query at the moment. Please try again"
st.session_state["messages"].append({"role": "ai", "content": response})
st.rerun()
if query:
st.session_state["messages"].append({"role": "user", "content": query})
st.session_state["messages"].append({"role": "ai", "content": response})
st.rerun()