Shubham170793 commited on
Commit
1760b27
Β·
verified Β·
1 Parent(s): 30ffedc

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +18 -35
src/streamlit_app.py CHANGED
@@ -212,7 +212,7 @@ st.caption("Query SAP documentation and enterprise PDFs β€” powered by reasoning
212
  doc_choice = st.radio("Select a document:", ["-- Select --", "Sample PDF", "Upload Custom PDF"], index=0)
213
 
214
  # ==========================================================
215
- # πŸ“‚ DOCUMENT HANDLING β€” CLEAN + ACCURATE UI FLOW (final polished)
216
  # ==========================================================
217
  if doc_choice == "-- Select --":
218
  st.info("⬅️ Select or upload a document to begin.")
@@ -234,7 +234,7 @@ else:
234
  with open(temp_path, "wb") as f:
235
  f.write(uploaded_file.getbuffer())
236
  else:
237
- st.stop()
238
 
239
  # --- Real processing begins here ---
240
  if temp_path:
@@ -243,23 +243,22 @@ else:
243
  # Process only once per document
244
  if "doc_ready" not in st.session_state or st.session_state.get("last_doc") != doc_name:
245
  status = st.empty()
246
- status.info("πŸ“€ Upload complete β€” reading document...")
247
 
 
248
  text, toc, toc_source = extract_text_from_pdf(temp_path)
249
  status.info("πŸ“‘ Parsing and chunking document...")
250
  chunks = chunk_text(text, chunk_size=chunk_size, overlap=overlap)
251
 
 
252
  status.info("🧠 Building embeddings and search index...")
253
  embeddings = cache_embeddings(doc_name, chunks, embed_chunks)
254
  index = build_faiss_index(embeddings)
255
 
256
- # βœ… Smooth transition for ready state
257
- import time
258
- status.success("βœ… Document processed successfully.")
259
- time.sleep(1.2)
260
- status.empty()
261
 
262
- # βœ… Simplified final ready message for reruns
263
  st.session_state.update({
264
  "text": text,
265
  "toc": toc,
@@ -268,19 +267,21 @@ else:
268
  "index": index,
269
  "doc_ready": True,
270
  "last_doc": doc_name,
271
- "status_text": f"πŸ“„ {doc_name} ready β€” your AI assistant is standing by!"
272
  })
273
 
274
- # Build suggestions once
275
  query_suggestions = generate_dynamic_suggestions_from_toc(toc, chunks, doc_name)
276
  st.session_state["query_suggestions_fixed"] = query_suggestions
277
  st.session_state["user_query_input"] = ""
278
  st.session_state["selected_suggestion"] = None
279
  st.session_state["show_more"] = False
 
 
280
  st.rerun()
281
 
282
  else:
283
- # Reuse cached content and show final ready message with fade-in
284
  text = st.session_state["text"]
285
  toc = st.session_state["toc"]
286
  chunks = st.session_state["chunks"]
@@ -288,28 +289,9 @@ else:
288
  index = st.session_state["index"]
289
  query_suggestions = st.session_state.get("query_suggestions_fixed", [])
290
 
291
- # ✨ Fading "ready" message for better UX
292
- st.markdown(f"""
293
- <div style="
294
- animation: fadeIn 1.2s ease-in-out;
295
- background: #072541;
296
- border-left: 4px solid #10b981;
297
- padding: 10px 14px;
298
- border-radius: 6px;
299
- color: #d1fae5;
300
- margin-top: 8px;
301
- font-size: 15px;">
302
- βœ… {st.session_state.get('status_text', f"{doc_name} ready for queries.")}
303
- </div>
304
- <style>
305
- @keyframes fadeIn {{
306
- from {{opacity: 0; transform: translateY(-4px);}}
307
- to {{opacity: 1; transform: translateY(0);}}
308
- }}
309
- </style>
310
- """, unsafe_allow_html=True)
311
-
312
- # --- Ask section ---
313
  st.markdown("### πŸ’¬ Ask the Assistant")
314
  if query_suggestions:
315
  visible = query_suggestions if st.session_state["show_more"] else query_suggestions[:3]
@@ -323,6 +305,7 @@ else:
323
  st.session_state["show_more"] = not st.session_state["show_more"]
324
  st.rerun()
325
 
 
326
  user_query = st.text_input("Type your question or click one above:", key="user_query_input")
327
 
328
  if user_query.strip():
@@ -334,13 +317,13 @@ else:
334
 
335
  st.markdown("### πŸ€– Assistant’s Answer")
336
 
 
337
  if not reasoning_mode and not answer.startswith("⚠️"):
338
  answer = re.sub(r"\*\*(.*?)\*\*", r"\1", answer)
339
  answer = re.sub(r"(^|\n)-\s*", r"\1<br>β€’ ", answer)
340
  st.markdown(f"<div class='answer-box'>{answer}</div>", unsafe_allow_html=True)
341
 
342
 
343
-
344
  # ==========================================================
345
  # 🎨 Optional Sidebar Scroll Styling (keeps it clean)
346
  # ==========================================================
 
212
  doc_choice = st.radio("Select a document:", ["-- Select --", "Sample PDF", "Upload Custom PDF"], index=0)
213
 
214
  # ==========================================================
215
+ # πŸ“‚ DOCUMENT HANDLING β€” POLISHED UI FLOW
216
  # ==========================================================
217
  if doc_choice == "-- Select --":
218
  st.info("⬅️ Select or upload a document to begin.")
 
234
  with open(temp_path, "wb") as f:
235
  f.write(uploaded_file.getbuffer())
236
  else:
237
+ st.stop() # Wait until file is uploaded
238
 
239
  # --- Real processing begins here ---
240
  if temp_path:
 
243
  # Process only once per document
244
  if "doc_ready" not in st.session_state or st.session_state.get("last_doc") != doc_name:
245
  status = st.empty()
246
+ status.info("πŸ“€ Upload complete β€” hang tight while we process your document...")
247
 
248
+ # Step 1: Extract text
249
  text, toc, toc_source = extract_text_from_pdf(temp_path)
250
  status.info("πŸ“‘ Parsing and chunking document...")
251
  chunks = chunk_text(text, chunk_size=chunk_size, overlap=overlap)
252
 
253
+ # Step 2: Build embeddings + FAISS index
254
  status.info("🧠 Building embeddings and search index...")
255
  embeddings = cache_embeddings(doc_name, chunks, embed_chunks)
256
  index = build_faiss_index(embeddings)
257
 
258
+ # Step 3: Final ready state
259
+ status.success("βœ… All set β€” your AI assistant is ready to help.")
 
 
 
260
 
261
+ # Persist session data for reruns
262
  st.session_state.update({
263
  "text": text,
264
  "toc": toc,
 
267
  "index": index,
268
  "doc_ready": True,
269
  "last_doc": doc_name,
270
+ "status_text": "πŸ“„ Document is ready for queries."
271
  })
272
 
273
+ # Build question suggestions (once per doc)
274
  query_suggestions = generate_dynamic_suggestions_from_toc(toc, chunks, doc_name)
275
  st.session_state["query_suggestions_fixed"] = query_suggestions
276
  st.session_state["user_query_input"] = ""
277
  st.session_state["selected_suggestion"] = None
278
  st.session_state["show_more"] = False
279
+
280
+ # Refresh to cleanly show "ready" state
281
  st.rerun()
282
 
283
  else:
284
+ # --- Reuse existing state (rerun-safe) ---
285
  text = st.session_state["text"]
286
  toc = st.session_state["toc"]
287
  chunks = st.session_state["chunks"]
 
289
  index = st.session_state["index"]
290
  query_suggestions = st.session_state.get("query_suggestions_fixed", [])
291
 
292
+ st.info(st.session_state.get("status_text", f"πŸ“„ {doc_name} is ready for queries."))
293
+
294
+ # --- Ask the Assistant section ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
295
  st.markdown("### πŸ’¬ Ask the Assistant")
296
  if query_suggestions:
297
  visible = query_suggestions if st.session_state["show_more"] else query_suggestions[:3]
 
305
  st.session_state["show_more"] = not st.session_state["show_more"]
306
  st.rerun()
307
 
308
+ # --- Query input box ---
309
  user_query = st.text_input("Type your question or click one above:", key="user_query_input")
310
 
311
  if user_query.strip():
 
317
 
318
  st.markdown("### πŸ€– Assistant’s Answer")
319
 
320
+ # Clean up answer format (bullets, bold)
321
  if not reasoning_mode and not answer.startswith("⚠️"):
322
  answer = re.sub(r"\*\*(.*?)\*\*", r"\1", answer)
323
  answer = re.sub(r"(^|\n)-\s*", r"\1<br>β€’ ", answer)
324
  st.markdown(f"<div class='answer-box'>{answer}</div>", unsafe_allow_html=True)
325
 
326
 
 
327
  # ==========================================================
328
  # 🎨 Optional Sidebar Scroll Styling (keeps it clean)
329
  # ==========================================================