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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -48
app.py CHANGED
@@ -35,6 +35,9 @@ if 'page_loaded' not in st.session_state:
35
  # Reset query state when returning to home page
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
@@ -207,10 +210,37 @@ div.stInfo {
207
  .source-link:hover {
208
  color: #6a1b9a;
209
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
210
  </style>
211
  <div class="main-title">Spirituality Q&A</div>
212
  """, unsafe_allow_html=True)
213
 
 
 
 
 
 
 
 
 
214
  # Centered button layout without columns
215
  _, center_col, _ = st.columns([1, 2, 1]) # Create a wider center column
216
  with center_col: # Put everything in the center column
@@ -279,6 +309,8 @@ def set_query(query):
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,8 +365,10 @@ def handle_form_submit():
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
 
339
  # Create a form with a dynamic key (to allow resetting)
340
  with st.form(key=f"query_form_{st.session_state.form_key}"):
@@ -342,62 +376,52 @@ with st.form(key=f"query_form_{st.session_state.form_key}"):
342
  placeholder="Press enter to submit your question", disabled=st.session_state.is_processing)
343
  submit_button = st.form_submit_button("Get Answer", on_click=handle_form_submit, disabled=st.session_state.is_processing)
344
 
345
- # Display the current question if available
346
  if st.session_state.last_query:
347
- st.markdown("### Current Question:")
348
- st.info(st.session_state.last_query)
 
 
 
349
 
350
- # Sliders for customization
351
- col1, col2 = st.columns(2)
352
- with col1:
353
- top_k = st.slider("Number of sources:", 3, 10, 5)
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("---")
 
35
  # Reset query state when returning to home page
36
  st.session_state.last_query = ""
37
  st.session_state.last_answer = None
38
+ # Add placeholder initialization tracking
39
+ if 'placeholders_initialized' not in st.session_state:
40
+ st.session_state.placeholders_initialized = False
41
 
42
  # THEN: Import your modules
43
  from rag_engine import process_query, load_model, cached_load_data_files
 
210
  .source-link:hover {
211
  color: #6a1b9a;
212
  }
213
+ /* Prevent layout shifts during loading */
214
+ .stApp {
215
+ min-height: 100vh;
216
+ }
217
+ /* Add smooth transitions to reduce perceived jerkiness */
218
+ * {
219
+ transition: opacity 0.15s ease-in-out;
220
+ }
221
+ /* Fix heights for containers that might change during loading */
222
+ div.stSpinner {
223
+ min-height: 50px;
224
+ display: flex;
225
+ align-items: center;
226
+ justify-content: center;
227
+ }
228
+ /* Ensure question and answer containers have consistent height */
229
+ div.stInfo, .answer-container {
230
+ min-height: 30px;
231
+ }
232
  </style>
233
  <div class="main-title">Spirituality Q&A</div>
234
  """, unsafe_allow_html=True)
235
 
236
+ # Initialize placeholders at the start
237
+ if not st.session_state.placeholders_initialized:
238
+ st.session_state.question_placeholder = st.empty()
239
+ st.session_state.slider_placeholder = st.empty()
240
+ st.session_state.answer_placeholder = st.empty()
241
+ st.session_state.sources_placeholder = st.empty()
242
+ st.session_state.placeholders_initialized = True
243
+
244
  # Centered button layout without columns
245
  _, center_col, _ = st.columns([1, 2, 1]) # Create a wider center column
246
  with center_col: # Put everything in the center column
 
309
  return
310
  st.session_state.last_query = query
311
  st.session_state.submit_clicked = True
312
+ st.session_state.is_processing = True
313
+ st.rerun()
314
 
315
  # Function to group questions into rows based on length
316
  def group_buttons(questions, max_chars_per_row=100):
 
365
  if st.session_state.query_input and st.session_state.query_input.strip():
366
  st.session_state.last_query = st.session_state.query_input.strip()
367
  st.session_state.submit_clicked = True
368
+ st.session_state.is_processing = True
369
  # Increment the form key to force a reset
370
  st.session_state.form_key += 1
371
+ st.rerun()
372
 
373
  # Create a form with a dynamic key (to allow resetting)
374
  with st.form(key=f"query_form_{st.session_state.form_key}"):
 
376
  placeholder="Press enter to submit your question", disabled=st.session_state.is_processing)
377
  submit_button = st.form_submit_button("Get Answer", on_click=handle_form_submit, disabled=st.session_state.is_processing)
378
 
379
+ # Display the current question if available using placeholders
380
  if st.session_state.last_query:
381
+ with st.session_state.question_placeholder.container():
382
+ st.markdown("### Current Question:")
383
+ st.info(st.session_state.last_query)
384
+ else:
385
+ st.session_state.question_placeholder.empty()
386
 
387
+ # Sliders for customization using placeholder
388
+ with st.session_state.slider_placeholder.container():
389
+ col1, col2 = st.columns(2)
390
+ with col1:
391
+ top_k = st.slider("Number of sources:", 3, 10, 5)
392
+ with col2:
393
+ word_limit = st.slider("Word limit:", 50, 500, 200)
 
 
 
 
 
 
394
 
395
  # Process the query only if it has been explicitly submitted
396
  if st.session_state.submit_clicked and st.session_state.last_query:
397
+ st.session_state.submit_clicked = False
 
 
 
 
 
 
 
 
398
 
399
+ # Use the answer placeholder for the spinner and answer display
400
+ with st.session_state.answer_placeholder.container():
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
405
+ except Exception as e:
406
+ st.session_state.last_answer = {"answer_with_rag": f"Error processing query: {str(e)}", "citations": ""}
 
 
 
 
 
 
 
 
 
 
 
 
 
407
 
408
+ # Reset processing flag
 
409
  st.session_state.is_processing = False
410
+ st.rerun() # Still using rerun, but the layout is more stable now
411
+
412
+ # Display the answer if available using placeholders
413
+ if st.session_state.last_answer is not None:
414
+ with st.session_state.answer_placeholder.container():
415
+ st.subheader("Answer:")
416
+ st.write(st.session_state.last_answer["answer_with_rag"])
417
+
418
+ with st.session_state.sources_placeholder.container():
419
+ st.subheader("Sources:")
420
+ for citation in st.session_state.last_answer["citations"].split("\n"):
421
+ st.write(citation)
422
+ else:
423
+ st.session_state.answer_placeholder.empty()
424
+ st.session_state.sources_placeholder.empty()
425
 
426
  # Add helpful information
427
  st.markdown("---")