ankanghosh commited on
Commit
60d5b14
·
verified ·
1 Parent(s): 93ca283

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -18
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import streamlit as st
2
  import time
 
3
 
4
  # FIRST: Set page config before ANY other Streamlit command
5
  st.set_page_config(page_title="Spirituality Q&A")
@@ -18,9 +19,13 @@ if 'submit_clicked' not in st.session_state:
18
  if 'init_time' not in st.session_state:
19
  st.session_state.init_time = None
20
  if 'form_key' not in st.session_state:
21
- st.session_state.form_key = 0
22
- if 'processing' not in st.session_state:
23
- st.session_state.processing = False
 
 
 
 
24
 
25
  # THEN: Import your modules
26
  from rag_engine import process_query, load_model
@@ -114,12 +119,15 @@ div.stInfo {
114
  <div class="main-title">Spirituality Q&A</div>
115
  """, unsafe_allow_html=True)
116
 
117
- # Function to handle query selection
118
  def set_query(query):
119
- st.session_state.last_query = query
120
- st.session_state.submit_clicked = True
121
- st.session_state.processing = True
122
- st.rerun()
 
 
 
123
 
124
  # Function to group questions into rows based on length
125
  def group_buttons(questions, max_chars_per_row=100):
@@ -189,28 +197,34 @@ st.markdown("### Common questions to try:")
189
  # Group questions into rows
190
  question_rows = group_buttons(common_questions, max_chars_per_row=80)
191
 
192
- # Create buttons for each row - disable if already processing
193
  for row_idx, row in enumerate(question_rows):
194
  cols = st.columns(len(row))
195
  for i, (col, q) in enumerate(zip(cols, row)):
196
  with col:
197
- button_disabled = st.session_state.processing
198
- if st.button(q, key=f"r{row_idx}_q{i}", use_container_width=True, disabled=button_disabled):
199
  set_query(q)
200
 
201
- # Function to handle form submission
202
  def handle_form_submit():
203
- if st.session_state.query_input and st.session_state.query_input.strip():
 
 
 
 
 
204
  st.session_state.last_query = st.session_state.query_input.strip()
205
  st.session_state.submit_clicked = True
206
- st.session_state.processing = True
 
207
  st.session_state.form_key += 1
208
 
209
  # Create a form with dynamic key for resetting
210
  with st.form(key=f"query_form_{st.session_state.form_key}"):
211
  query = st.text_input("Ask your question:", key="query_input",
212
  placeholder="Press enter to submit your question")
213
- submit_button = st.form_submit_button("Get Answer", on_click=handle_form_submit, disabled=st.session_state.processing)
 
214
 
215
  # Display the current question if there is one
216
  if st.session_state.last_query:
@@ -231,6 +245,7 @@ if st.session_state.submit_clicked and st.session_state.last_query:
231
  with st.spinner("Processing your question..."):
232
  try:
233
  result = process_query(st.session_state.last_query, top_k=top_k, word_limit=word_limit)
 
234
  st.subheader("Answer:")
235
  st.write(result["answer_with_rag"])
236
  st.subheader("Sources:")
@@ -238,9 +253,6 @@ if st.session_state.submit_clicked and st.session_state.last_query:
238
  st.write(citation)
239
  except Exception as e:
240
  st.error(f"Error processing query: {str(e)}")
241
- finally:
242
- # Reset processing flag when done
243
- st.session_state.processing = False
244
 
245
  # Add helpful information
246
  st.markdown("---")
 
1
  import streamlit as st
2
  import time
3
+ from datetime import datetime
4
 
5
  # FIRST: Set page config before ANY other Streamlit command
6
  st.set_page_config(page_title="Spirituality Q&A")
 
19
  if 'init_time' not in st.session_state:
20
  st.session_state.init_time = None
21
  if 'form_key' not in st.session_state:
22
+ st.session_state.form_key = 0 # This will help us reset the form
23
+ if 'last_click_time' not in st.session_state:
24
+ st.session_state.last_click_time = 0
25
+ if 'debounce_time' not in st.session_state:
26
+ st.session_state.debounce_time = 0.5 # Debounce time in seconds
27
+ if 'debounce_active' not in st.session_state:
28
+ st.session_state.debounce_active = True
29
 
30
  # THEN: Import your modules
31
  from rag_engine import process_query, load_model
 
119
  <div class="main-title">Spirituality Q&A</div>
120
  """, unsafe_allow_html=True)
121
 
122
+ # Function to handle query selection with debouncing
123
  def set_query(query):
124
+ current_time = datetime.now().timestamp()
125
+ if not st.session_state.debounce_active or current_time - st.session_state.last_click_time > st.session_state.debounce_time:
126
+ st.session_state.last_click_time = current_time
127
+ st.session_state.last_query = query
128
+ st.session_state.submit_clicked = True
129
+ st.session_state.debounce_active = True # Reactivate debouncing for next query
130
+ st.rerun()
131
 
132
  # Function to group questions into rows based on length
133
  def group_buttons(questions, max_chars_per_row=100):
 
197
  # Group questions into rows
198
  question_rows = group_buttons(common_questions, max_chars_per_row=80)
199
 
200
+ # Create buttons for each row
201
  for row_idx, row in enumerate(question_rows):
202
  cols = st.columns(len(row))
203
  for i, (col, q) in enumerate(zip(cols, row)):
204
  with col:
205
+ if st.button(q, key=f"r{row_idx}_q{i}", use_container_width=True):
 
206
  set_query(q)
207
 
208
+ # Function to handle form submission with debouncing
209
  def handle_form_submit():
210
+ current_time = datetime.now().timestamp()
211
+ if (st.session_state.query_input and
212
+ st.session_state.query_input.strip() and
213
+ (not st.session_state.debounce_active or current_time - st.session_state.last_click_time > st.session_state.debounce_time)):
214
+
215
+ st.session_state.last_click_time = current_time
216
  st.session_state.last_query = st.session_state.query_input.strip()
217
  st.session_state.submit_clicked = True
218
+ st.session_state.debounce_active = True # Reactivate debouncing for next query
219
+ # Increment the form key to force a reset
220
  st.session_state.form_key += 1
221
 
222
  # Create a form with dynamic key for resetting
223
  with st.form(key=f"query_form_{st.session_state.form_key}"):
224
  query = st.text_input("Ask your question:", key="query_input",
225
  placeholder="Press enter to submit your question")
226
+ # When enter is pressed, the form is submitted which triggers handle_form_submit
227
+ submit_button = st.form_submit_button("Get Answer", on_click=handle_form_submit)
228
 
229
  # Display the current question if there is one
230
  if st.session_state.last_query:
 
245
  with st.spinner("Processing your question..."):
246
  try:
247
  result = process_query(st.session_state.last_query, top_k=top_k, word_limit=word_limit)
248
+ st.session_state.debounce_active = False # Disable debouncing after answer is displayed
249
  st.subheader("Answer:")
250
  st.write(result["answer_with_rag"])
251
  st.subheader("Sources:")
 
253
  st.write(citation)
254
  except Exception as e:
255
  st.error(f"Error processing query: {str(e)}")
 
 
 
256
 
257
  # Add helpful information
258
  st.markdown("---")