absiitr commited on
Commit
73eaaac
·
verified ·
1 Parent(s): 06f5408

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -26
app.py CHANGED
@@ -33,8 +33,6 @@ st.set_page_config(
33
  # ---------------- FORCE DARK UI (THEME INDEPENDENT) ----------------
34
  st.markdown("""
35
  <style>
36
-
37
- /* ========= FORCE DARK THEME ========= */
38
  :root {
39
  --primary-color: #1e3a8a;
40
  --background-color: #0e1117;
@@ -47,7 +45,7 @@ html, body, [data-testid="stAppViewContainer"], .main {
47
  color: #f0f2f6 !important;
48
  }
49
 
50
- /* Hide Streamlit chrome */
51
  header[data-testid="stHeader"], footer {
52
  display: none;
53
  }
@@ -61,7 +59,7 @@ header[data-testid="stHeader"], footer {
61
  overflow-y: auto;
62
  }
63
 
64
- /* Fixed Header */
65
  .fixed-header {
66
  position: fixed;
67
  top: 0;
@@ -92,7 +90,7 @@ header[data-testid="stHeader"], footer {
92
  text-decoration: none;
93
  }
94
 
95
- /* Main container */
96
  .main .block-container {
97
  margin-top: 6rem;
98
  padding-bottom: 5rem;
@@ -142,7 +140,7 @@ section[data-testid="stFileUploader"] small {
142
  display: none;
143
  }
144
 
145
- /* Scrollbars */
146
  *::-webkit-scrollbar {
147
  width: 8px;
148
  }
@@ -150,7 +148,6 @@ section[data-testid="stFileUploader"] small {
150
  background: #2d3748;
151
  border-radius: 4px;
152
  }
153
-
154
  </style>
155
  """, unsafe_allow_html=True)
156
 
@@ -159,17 +156,15 @@ st.markdown("""
159
  <div class="fixed-header">
160
  <div class="title-text">📘 PDF Assistant</div>
161
  <div class="creator-text">
162
- by <a href="https://www.linkedin.com/in/abhishek-iitr/" target="_blank">Abhishek Saxena</a>
 
163
  </div>
164
  </div>
165
  """, unsafe_allow_html=True)
166
 
167
  # ---------------- SESSION STATE ----------------
168
- if "chat" not in st.session_state:
169
- st.session_state.chat = []
170
-
171
- if "vectorstore" not in st.session_state:
172
- st.session_state.vectorstore = None
173
 
174
  if "retriever" not in st.session_state:
175
  st.session_state.retriever = None
@@ -182,10 +177,9 @@ if "uploader_key" not in st.session_state:
182
 
183
  # ---------------- FUNCTIONS ----------------
184
  def clear_chat():
185
- st.session_state.chat = []
186
 
187
  def clear_memory():
188
- st.session_state.vectorstore = None
189
  st.session_state.retriever = None
190
  st.session_state.uploaded_file_name = None
191
  st.session_state.uploader_key += 1
@@ -221,7 +215,8 @@ def ask(question):
221
  prompt = f"""
222
  You are a strict RAG assistant.
223
  Use ONLY the PDF content.
224
- If not found, say: I cannot find this in the PDF.
 
225
 
226
  CONTEXT:
227
  {context}
@@ -248,7 +243,8 @@ with st.sidebar:
248
  st.markdown("---")
249
 
250
  uploaded = st.file_uploader(
251
- "Upload PDF", type=["pdf"],
 
252
  key=st.session_state.uploader_key,
253
  label_visibility="collapsed"
254
  )
@@ -257,7 +253,7 @@ with st.sidebar:
257
  with st.spinner("Processing PDF..."):
258
  process_pdf(uploaded)
259
  st.session_state.uploaded_file_name = uploaded.name
260
- st.session_state.chat = []
261
  st.success("PDF ready")
262
 
263
  if not uploaded:
@@ -266,10 +262,10 @@ with st.sidebar:
266
  # ---------------- INPUT ----------------
267
  disabled = not st.session_state.uploaded_file_name or not client
268
 
269
- with st.form("chat", clear_on_submit=True):
270
  col1, col2 = st.columns([0.85, 0.15])
271
  with col1:
272
- q = st.text_input(
273
  "",
274
  placeholder="Ask a question about the loaded PDF...",
275
  disabled=disabled
@@ -277,15 +273,15 @@ with st.form("chat", clear_on_submit=True):
277
  with col2:
278
  send = st.form_submit_button("➤", disabled=disabled)
279
 
280
- if send and q:
281
- st.session_state.chat.append(("user", q))
282
  with st.spinner("Thinking..."):
283
- ans = ask(q)
284
- st.session_state.chat.append(("bot", ans))
285
  st.rerun()
286
 
287
- # ---------------- CHAT ----------------
288
- for role, msg in reversed(st.session_state.chat):
289
  if role == "user":
290
  st.markdown(f"<div class='chat-user'>{msg}</div>", unsafe_allow_html=True)
291
  else:
 
33
  # ---------------- FORCE DARK UI (THEME INDEPENDENT) ----------------
34
  st.markdown("""
35
  <style>
 
 
36
  :root {
37
  --primary-color: #1e3a8a;
38
  --background-color: #0e1117;
 
45
  color: #f0f2f6 !important;
46
  }
47
 
48
+ /* Hide Streamlit default UI */
49
  header[data-testid="stHeader"], footer {
50
  display: none;
51
  }
 
59
  overflow-y: auto;
60
  }
61
 
62
+ /* Fixed header */
63
  .fixed-header {
64
  position: fixed;
65
  top: 0;
 
90
  text-decoration: none;
91
  }
92
 
93
+ /* Main content spacing */
94
  .main .block-container {
95
  margin-top: 6rem;
96
  padding-bottom: 5rem;
 
140
  display: none;
141
  }
142
 
143
+ /* Scrollbar */
144
  *::-webkit-scrollbar {
145
  width: 8px;
146
  }
 
148
  background: #2d3748;
149
  border-radius: 4px;
150
  }
 
151
  </style>
152
  """, unsafe_allow_html=True)
153
 
 
156
  <div class="fixed-header">
157
  <div class="title-text">📘 PDF Assistant</div>
158
  <div class="creator-text">
159
+ by <a href="https://www.linkedin.com/in/abhishek-iitr/" target="_blank">
160
+ Abhishek Saxena</a>
161
  </div>
162
  </div>
163
  """, unsafe_allow_html=True)
164
 
165
  # ---------------- SESSION STATE ----------------
166
+ if "messages" not in st.session_state:
167
+ st.session_state.messages = []
 
 
 
168
 
169
  if "retriever" not in st.session_state:
170
  st.session_state.retriever = None
 
177
 
178
  # ---------------- FUNCTIONS ----------------
179
  def clear_chat():
180
+ st.session_state.messages = []
181
 
182
  def clear_memory():
 
183
  st.session_state.retriever = None
184
  st.session_state.uploaded_file_name = None
185
  st.session_state.uploader_key += 1
 
215
  prompt = f"""
216
  You are a strict RAG assistant.
217
  Use ONLY the PDF content.
218
+ If the answer is not found, say:
219
+ "I cannot find this in the PDF."
220
 
221
  CONTEXT:
222
  {context}
 
243
  st.markdown("---")
244
 
245
  uploaded = st.file_uploader(
246
+ "Upload PDF",
247
+ type=["pdf"],
248
  key=st.session_state.uploader_key,
249
  label_visibility="collapsed"
250
  )
 
253
  with st.spinner("Processing PDF..."):
254
  process_pdf(uploaded)
255
  st.session_state.uploaded_file_name = uploaded.name
256
+ st.session_state.messages = []
257
  st.success("PDF ready")
258
 
259
  if not uploaded:
 
262
  # ---------------- INPUT ----------------
263
  disabled = not st.session_state.uploaded_file_name or not client
264
 
265
+ with st.form(key="chat_form", clear_on_submit=True):
266
  col1, col2 = st.columns([0.85, 0.15])
267
  with col1:
268
+ question = st.text_input(
269
  "",
270
  placeholder="Ask a question about the loaded PDF...",
271
  disabled=disabled
 
273
  with col2:
274
  send = st.form_submit_button("➤", disabled=disabled)
275
 
276
+ if send and question:
277
+ st.session_state.messages.append(("user", question))
278
  with st.spinner("Thinking..."):
279
+ answer = ask(question)
280
+ st.session_state.messages.append(("bot", answer))
281
  st.rerun()
282
 
283
+ # ---------------- CHAT HISTORY ----------------
284
+ for role, msg in reversed(st.session_state.messages):
285
  if role == "user":
286
  st.markdown(f"<div class='chat-user'>{msg}</div>", unsafe_allow_html=True)
287
  else: