ankanghosh commited on
Commit
dffc83c
·
verified ·
1 Parent(s): 05a1de8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -42
app.py CHANGED
@@ -1,6 +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")
6
 
@@ -27,9 +27,31 @@ if 'last_answer' not in st.session_state:
27
  st.session_state.last_answer = None
28
 
29
  # THEN: Import your modules
30
- from rag_engine import process_query, load_model
31
  from utils import setup_all_auth
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  # Custom styling (pure CSS)
34
  st.markdown("""
35
  <style>
@@ -133,11 +155,9 @@ def group_buttons(questions, max_chars_per_row=100):
133
  rows = []
134
  current_row = []
135
  current_length = 0
136
-
137
  for q in questions:
138
  # Add some buffer for button padding/margins
139
  q_length = len(q) + 5
140
-
141
  if current_length + q_length > max_chars_per_row and current_row:
142
  rows.append(current_row)
143
  current_row = [q]
@@ -145,36 +165,10 @@ def group_buttons(questions, max_chars_per_row=100):
145
  else:
146
  current_row.append(q)
147
  current_length += q_length
148
-
149
  if current_row:
150
  rows.append(current_row)
151
-
152
  return rows
153
 
154
- # Handle initialization and success message timing
155
- init_message = st.empty()
156
-
157
- if not st.session_state.initialized:
158
- init_message.info("Hang in there! We are setting the system up for you. 😊")
159
-
160
- try:
161
- setup_all_auth()
162
- load_model()
163
- st.session_state.initialized = True
164
- st.session_state.init_time = time.time()
165
- init_message.success("System initialized successfully!")
166
- time.sleep(2)
167
- init_message.empty()
168
- except Exception as e:
169
- init_message.error(f"Error initializing: {str(e)}")
170
-
171
- # Handle timing of success message disappearance
172
- elif st.session_state.init_time is not None:
173
- elapsed_time = time.time() - st.session_state.init_time
174
- if elapsed_time >= 2.0:
175
- init_message.empty()
176
- st.session_state.init_time = None
177
-
178
  # All common questions in a single list
179
  common_questions = [
180
  "What is the Atman or the soul?",
@@ -192,10 +186,8 @@ common_questions = [
192
  # Display heading for common questions
193
  st.markdown("### Common questions to try:")
194
 
195
- # Group questions into rows
196
  question_rows = group_buttons(common_questions, max_chars_per_row=80)
197
-
198
- # Create buttons for each row. They are disabled if a query is processing.
199
  for row_idx, row in enumerate(question_rows):
200
  cols = st.columns(len(row))
201
  for i, (col, q) in enumerate(zip(cols, row)):
@@ -215,14 +207,13 @@ def handle_form_submit():
215
  # Increment the form key to force a reset
216
  st.session_state.form_key += 1
217
 
218
- # Create a form with dynamic key for resetting.
219
- # The form's submit button is also disabled when processing.
220
  with st.form(key=f"query_form_{st.session_state.form_key}"):
221
  query = st.text_input("Ask your question:", key="query_input",
222
- placeholder="Press enter to submit your question", disabled=st.session_state.is_processing)
223
  submit_button = st.form_submit_button("Get Answer", on_click=handle_form_submit, disabled=st.session_state.is_processing)
224
 
225
- # Display the current question if there is one
226
  if st.session_state.last_query:
227
  st.markdown("### Current Question:")
228
  st.info(st.session_state.last_query)
@@ -234,19 +225,17 @@ with col1:
234
  with col2:
235
  word_limit = st.slider("Word limit:", 50, 500, 200)
236
 
237
- # Process the query only if explicitly submitted
238
  if st.session_state.submit_clicked and st.session_state.last_query:
239
  st.session_state.submit_clicked = False
240
-
241
  with st.spinner("Processing your question..."):
242
  try:
243
  result = process_query(st.session_state.last_query, top_k=top_k, word_limit=word_limit)
244
- st.session_state.last_answer = result # Store the result in session state
245
  except Exception as e:
246
  st.session_state.last_answer = {"answer_with_rag": f"Error processing query: {str(e)}", "citations": ""}
247
- # Reset debouncing after processing
248
  st.session_state.is_processing = False
249
- # Force a rerun so that buttons are re-rendered in enabled state and the answer is re-displayed
250
  st.experimental_rerun()
251
 
252
  # Display the answer if available
 
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")
6
 
 
27
  st.session_state.last_answer = None
28
 
29
  # THEN: Import your modules
30
+ from rag_engine import process_query, load_model, cached_load_data_files
31
  from utils import setup_all_auth
32
 
33
+ # Preload resources during initialization
34
+ init_message = st.empty()
35
+ if not st.session_state.initialized:
36
+ init_message.info("Hang in there! We are setting the system up for you. 😊")
37
+ try:
38
+ # Setup authentication and preload heavy resources
39
+ setup_all_auth()
40
+ load_model() # This uses cached_load_model via alias
41
+ cached_load_data_files() # Preload FAISS index, text chunks, and metadata
42
+ st.session_state.initialized = True
43
+ st.session_state.init_time = time.time()
44
+ init_message.success("System initialized successfully!")
45
+ time.sleep(2)
46
+ init_message.empty()
47
+ except Exception as e:
48
+ init_message.error(f"Error initializing: {str(e)}")
49
+ elif st.session_state.init_time is not None:
50
+ elapsed_time = time.time() - st.session_state.init_time
51
+ if elapsed_time >= 2.0:
52
+ init_message.empty()
53
+ st.session_state.init_time = None
54
+
55
  # Custom styling (pure CSS)
56
  st.markdown("""
57
  <style>
 
155
  rows = []
156
  current_row = []
157
  current_length = 0
 
158
  for q in questions:
159
  # Add some buffer for button padding/margins
160
  q_length = len(q) + 5
 
161
  if current_length + q_length > max_chars_per_row and current_row:
162
  rows.append(current_row)
163
  current_row = [q]
 
165
  else:
166
  current_row.append(q)
167
  current_length += q_length
 
168
  if current_row:
169
  rows.append(current_row)
 
170
  return rows
171
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
172
  # All common questions in a single list
173
  common_questions = [
174
  "What is the Atman or the soul?",
 
186
  # Display heading for common questions
187
  st.markdown("### Common questions to try:")
188
 
189
+ # Group questions into rows and create buttons (disabled if processing)
190
  question_rows = group_buttons(common_questions, max_chars_per_row=80)
 
 
191
  for row_idx, row in enumerate(question_rows):
192
  cols = st.columns(len(row))
193
  for i, (col, q) in enumerate(zip(cols, row)):
 
207
  # Increment the form key to force a reset
208
  st.session_state.form_key += 1
209
 
210
+ # Create a form with a dynamic key (to allow resetting)
 
211
  with st.form(key=f"query_form_{st.session_state.form_key}"):
212
  query = st.text_input("Ask your question:", key="query_input",
213
+ placeholder="Press enter to submit your question", disabled=st.session_state.is_processing)
214
  submit_button = st.form_submit_button("Get Answer", on_click=handle_form_submit, disabled=st.session_state.is_processing)
215
 
216
+ # Display the current question if available
217
  if st.session_state.last_query:
218
  st.markdown("### Current Question:")
219
  st.info(st.session_state.last_query)
 
225
  with col2:
226
  word_limit = st.slider("Word limit:", 50, 500, 200)
227
 
228
+ # Process the query only if it has been explicitly submitted
229
  if st.session_state.submit_clicked and st.session_state.last_query:
230
  st.session_state.submit_clicked = False
 
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.session_state.last_answer = result # Store result in session state
235
  except Exception as e:
236
  st.session_state.last_answer = {"answer_with_rag": f"Error processing query: {str(e)}", "citations": ""}
237
+ # Reset debouncing after processing and force a rerun to re-enable buttons
238
  st.session_state.is_processing = False
 
239
  st.experimental_rerun()
240
 
241
  # Display the answer if available