ankanghosh commited on
Commit
7409714
·
verified ·
1 Parent(s): 3a645c3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -8
app.py CHANGED
@@ -19,6 +19,9 @@ 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 # This will help us reset the form
 
 
 
22
 
23
  # THEN: Import your modules
24
  from rag_engine import process_query, load_model
@@ -112,11 +115,15 @@ div.stInfo {
112
  <div class="main-title">Spirituality Q&A</div>
113
  """, unsafe_allow_html=True)
114
 
115
- # Function to handle query selection
116
  def set_query(query):
 
 
 
117
  st.session_state.last_query = query
118
  st.session_state.submit_clicked = True
119
- st.rerun()
 
120
 
121
  # Function to group questions into rows based on length
122
  def group_buttons(questions, max_chars_per_row=100):
@@ -159,7 +166,6 @@ if not st.session_state.initialized:
159
  init_message.error(f"Error initializing: {str(e)}")
160
 
161
  # Handle timing of success message disappearance
162
- # This will run on each rerender after initialization
163
  elif st.session_state.init_time is not None:
164
  elapsed_time = time.time() - st.session_state.init_time
165
  if elapsed_time >= 2.0:
@@ -186,27 +192,32 @@ st.markdown("### Common questions to try:")
186
  # Group questions into rows
187
  question_rows = group_buttons(common_questions, max_chars_per_row=80)
188
 
189
- # Create buttons for each row
190
  for row_idx, row in enumerate(question_rows):
191
  cols = st.columns(len(row))
192
  for i, (col, q) in enumerate(zip(cols, row)):
193
  with col:
194
- if st.button(q, key=f"r{row_idx}_q{i}", use_container_width=True):
195
  set_query(q)
196
 
197
  # Function to handle form submission
198
  def handle_form_submit():
 
 
 
199
  if st.session_state.query_input and st.session_state.query_input.strip():
200
  st.session_state.last_query = st.session_state.query_input.strip()
201
  st.session_state.submit_clicked = True
 
202
  # Increment the form key to force a reset
203
  st.session_state.form_key += 1
204
 
205
- # Create a form with dynamic key for resetting
 
206
  with st.form(key=f"query_form_{st.session_state.form_key}"):
207
  query = st.text_input("Ask your question:", key="query_input",
208
- placeholder="Press enter to submit your question")
209
- submit_button = st.form_submit_button("Get Answer", on_click=handle_form_submit)
210
 
211
  # Display the current question if there is one
212
  if st.session_state.last_query:
@@ -234,6 +245,8 @@ if st.session_state.submit_clicked and st.session_state.last_query:
234
  st.write(citation)
235
  except Exception as e:
236
  st.error(f"Error processing query: {str(e)}")
 
 
237
 
238
  # Add helpful information
239
  st.markdown("---")
 
19
  st.session_state.init_time = None
20
  if 'form_key' not in st.session_state:
21
  st.session_state.form_key = 0 # This will help us reset the form
22
+ # New variable for debouncing: whether processing is in progress
23
+ if 'is_processing' not in st.session_state:
24
+ st.session_state.is_processing = False
25
 
26
  # THEN: Import your modules
27
  from rag_engine import process_query, load_model
 
115
  <div class="main-title">Spirituality Q&A</div>
116
  """, unsafe_allow_html=True)
117
 
118
+ # Function to handle query selection from the common questions buttons
119
  def set_query(query):
120
+ # If already processing, ignore further input
121
+ if st.session_state.is_processing:
122
+ return
123
  st.session_state.last_query = query
124
  st.session_state.submit_clicked = True
125
+ st.session_state.is_processing = True
126
+ st.experimental_rerun()
127
 
128
  # Function to group questions into rows based on length
129
  def group_buttons(questions, max_chars_per_row=100):
 
166
  init_message.error(f"Error initializing: {str(e)}")
167
 
168
  # Handle timing of success message disappearance
 
169
  elif st.session_state.init_time is not None:
170
  elapsed_time = time.time() - st.session_state.init_time
171
  if elapsed_time >= 2.0:
 
192
  # Group questions into rows
193
  question_rows = group_buttons(common_questions, max_chars_per_row=80)
194
 
195
+ # Create buttons for each row. They are disabled if a query is processing.
196
  for row_idx, row in enumerate(question_rows):
197
  cols = st.columns(len(row))
198
  for i, (col, q) in enumerate(zip(cols, row)):
199
  with col:
200
+ if st.button(q, key=f"r{row_idx}_q{i}", use_container_width=True, disabled=st.session_state.is_processing):
201
  set_query(q)
202
 
203
  # Function to handle form submission
204
  def handle_form_submit():
205
+ # If already processing, ignore further input
206
+ if st.session_state.is_processing:
207
+ return
208
  if st.session_state.query_input and st.session_state.query_input.strip():
209
  st.session_state.last_query = st.session_state.query_input.strip()
210
  st.session_state.submit_clicked = True
211
+ st.session_state.is_processing = True
212
  # Increment the form key to force a reset
213
  st.session_state.form_key += 1
214
 
215
+ # Create a form with dynamic key for resetting.
216
+ # The form's submit button is also disabled when processing.
217
  with st.form(key=f"query_form_{st.session_state.form_key}"):
218
  query = st.text_input("Ask your question:", key="query_input",
219
+ placeholder="Press enter to submit your question", disabled=st.session_state.is_processing)
220
+ submit_button = st.form_submit_button("Get Answer", on_click=handle_form_submit, disabled=st.session_state.is_processing)
221
 
222
  # Display the current question if there is one
223
  if st.session_state.last_query:
 
245
  st.write(citation)
246
  except Exception as e:
247
  st.error(f"Error processing query: {str(e)}")
248
+ # Reset debouncing after the answer has been produced
249
+ st.session_state.is_processing = False
250
 
251
  # Add helpful information
252
  st.markdown("---")