ankanghosh commited on
Commit
67ebf1f
·
verified ·
1 Parent(s): eedb5ab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -54
app.py CHANGED
@@ -36,44 +36,6 @@ if 'page_loaded' not in st.session_state:
36
  st.session_state.last_query = ""
37
  st.session_state.last_answer = None
38
 
39
- # Add a flag to track if we need to check for navigation
40
- if 'check_navigation' not in st.session_state:
41
- st.session_state.check_navigation = True
42
-
43
- # Check if we need to reset state on page load
44
- if st.session_state.check_navigation:
45
- # Reset key query-related state variables
46
- st.session_state.last_query = ""
47
- st.session_state.last_answer = None
48
- st.session_state.submit_clicked = False
49
- # If you have any other state to reset, do it here
50
-
51
- # Mark that we've checked navigation for this page load
52
- st.session_state.check_navigation = False
53
-
54
- # Add JavaScript to reset the flag when navigating away
55
- components.html(
56
- """
57
- <script>
58
- // Set up beforeunload event to prepare for next page load
59
- window.addEventListener('beforeunload', function() {
60
- // This will be executed when the user navigates away
61
- // We'll use sessionStorage which persists across page loads
62
- sessionStorage.setItem('resetOnReturn', 'true');
63
- });
64
-
65
- // Check if we need to reload on return
66
- if (sessionStorage.getItem('resetOnReturn') === 'true') {
67
- // Clear the flag
68
- sessionStorage.removeItem('resetOnReturn');
69
- // Force page reload to reset state
70
- window.location.reload();
71
- }
72
- </script>
73
- """,
74
- height=0
75
- )
76
-
77
  # THEN: Import your modules
78
  from rag_engine import process_query, load_model, cached_load_data_files
79
  from utils import setup_all_auth
@@ -317,8 +279,6 @@ def set_query(query):
317
  return
318
  st.session_state.last_query = query
319
  st.session_state.submit_clicked = True
320
- st.session_state.is_processing = True
321
- st.rerun()
322
 
323
  # Function to group questions into rows based on length
324
  def group_buttons(questions, max_chars_per_row=100):
@@ -373,7 +333,6 @@ def handle_form_submit():
373
  if st.session_state.query_input and st.session_state.query_input.strip():
374
  st.session_state.last_query = st.session_state.query_input.strip()
375
  st.session_state.submit_clicked = True
376
- st.session_state.is_processing = True
377
  # Increment the form key to force a reset
378
  st.session_state.form_key += 1
379
 
@@ -395,26 +354,50 @@ with col1:
395
  with col2:
396
  word_limit = st.slider("Word limit:", 50, 500, 200)
397
 
 
 
 
 
 
 
 
398
  # Process the query only if it has been explicitly submitted
399
  if st.session_state.submit_clicked and st.session_state.last_query:
400
- st.session_state.submit_clicked = False
 
 
 
 
 
 
 
 
 
 
401
  with st.spinner("Processing your question..."):
402
  try:
403
  result = process_query(st.session_state.last_query, top_k=top_k, word_limit=word_limit)
404
- st.session_state.last_answer = result # Store result in session state
 
 
 
 
 
 
 
405
  except Exception as e:
406
- st.session_state.last_answer = {"answer_with_rag": f"Error processing query: {str(e)}", "citations": ""}
407
- # Reset debouncing after processing and force a rerun to re-enable buttons
 
 
 
 
 
 
 
 
 
408
  st.session_state.is_processing = False
409
- st.rerun()
410
-
411
- # Display the answer if available
412
- if st.session_state.last_answer is not None:
413
- st.subheader("Answer:")
414
- st.write(st.session_state.last_answer["answer_with_rag"])
415
- st.subheader("Sources:")
416
- for citation in st.session_state.last_answer["citations"].split("\n"):
417
- st.write(citation)
418
 
419
  # Add helpful information
420
  st.markdown("---")
 
36
  st.session_state.last_query = ""
37
  st.session_state.last_answer = None
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  # THEN: Import your modules
40
  from rag_engine import process_query, load_model, cached_load_data_files
41
  from utils import setup_all_auth
 
279
  return
280
  st.session_state.last_query = query
281
  st.session_state.submit_clicked = True
 
 
282
 
283
  # Function to group questions into rows based on length
284
  def group_buttons(questions, max_chars_per_row=100):
 
333
  if st.session_state.query_input and st.session_state.query_input.strip():
334
  st.session_state.last_query = st.session_state.query_input.strip()
335
  st.session_state.submit_clicked = True
 
336
  # Increment the form key to force a reset
337
  st.session_state.form_key += 1
338
 
 
354
  with col2:
355
  word_limit = st.slider("Word limit:", 50, 500, 200)
356
 
357
+ # Create placeholders for answer sections
358
+ answer_container = st.container()
359
+ answer_header = answer_container.empty()
360
+ answer_text = answer_container.empty()
361
+ sources_header = answer_container.empty()
362
+ sources_text = answer_container.empty()
363
+
364
  # Process the query only if it has been explicitly submitted
365
  if st.session_state.submit_clicked and st.session_state.last_query:
366
+ # Set processing flag first
367
+ st.session_state.is_processing = True
368
+
369
+ # Check if we already have an answer to display
370
+ if st.session_state.last_answer is not None:
371
+ answer_header.subheader("Answer:")
372
+ answer_text.write(st.session_state.last_answer["answer_with_rag"])
373
+ sources_header.subheader("Sources:")
374
+ sources_text.write(st.session_state.last_answer["citations"])
375
+
376
+ # Process in the same page load without rerun
377
  with st.spinner("Processing your question..."):
378
  try:
379
  result = process_query(st.session_state.last_query, top_k=top_k, word_limit=word_limit)
380
+ st.session_state.last_answer = result
381
+
382
+ # Update the display with new results
383
+ answer_header.subheader("Answer:")
384
+ answer_text.write(result["answer_with_rag"])
385
+ sources_header.subheader("Sources:")
386
+ sources_text.write(result["citations"])
387
+
388
  except Exception as e:
389
+ error_result = {"answer_with_rag": f"Error processing query: {str(e)}", "citations": ""}
390
+ st.session_state.last_answer = error_result
391
+
392
+ # Display the error
393
+ answer_header.subheader("Answer:")
394
+ answer_text.write(error_result["answer_with_rag"])
395
+ sources_header.subheader("Sources:")
396
+ sources_text.write(error_result["citations"])
397
+
398
+ # Reset flags after processing is complete
399
+ st.session_state.submit_clicked = False
400
  st.session_state.is_processing = False
 
 
 
 
 
 
 
 
 
401
 
402
  # Add helpful information
403
  st.markdown("---")