esilver commited on
Commit
7e8e84e
·
1 Parent(s): 5e72e96

Persist UI results in session state

Browse files
Files changed (1) hide show
  1. ui.py +52 -11
ui.py CHANGED
@@ -25,6 +25,17 @@ def render_ui():
25
  st.session_state.openai_input = ""
26
  if 'compare_input' not in st.session_state:
27
  st.session_state.compare_input = ""
 
 
 
 
 
 
 
 
 
 
 
28
 
29
  # Page config is now set in app.py
30
  st.title("Product Categorization Tool")
@@ -79,6 +90,7 @@ def render_ui():
79
  # Results section
80
  st.subheader("Results")
81
  results_placeholder_ingredient = st.container() # Use container for results area
 
82
  if find_ingredients_btn:
83
  if st.session_state.ingredient_input: # Check state value
84
  with st.spinner("Finding similar ingredients..."):
@@ -89,12 +101,17 @@ def render_ui():
89
  top_n,
90
  confidence
91
  )
92
- results_placeholder_ingredient.markdown(results_html, unsafe_allow_html=True)
 
93
  else:
 
94
  results_placeholder_ingredient.warning("Please enter product names.")
 
 
 
95
  else:
96
- # Keep the placeholder visible even when no button is clicked yet
97
- results_placeholder_ingredient.write("Results will appear here.")
98
 
99
  # --- Category Matching Tab ---
100
  with tab_category:
@@ -132,6 +149,7 @@ def render_ui():
132
  with st.container(border=True):
133
  st.subheader("Results")
134
  results_placeholder_category = st.container() # Use container
 
135
  if match_categories_btn:
136
  if st.session_state.category_input:
137
  with st.spinner("Matching to categories..."):
@@ -142,11 +160,17 @@ def render_ui():
142
  category_top_n,
143
  category_confidence
144
  )
145
- results_placeholder_category.markdown(results_html, unsafe_allow_html=True)
 
146
  else:
 
147
  results_placeholder_category.warning("Please enter product names.")
 
 
 
148
  else:
149
- results_placeholder_category.write("Results will appear here.")
 
150
 
151
  # --- Common function for Reranking Tabs ---
152
  def create_reranking_ui(tab, tab_key_prefix, tab_name, backend_function, default_match="categories"):
@@ -195,11 +219,15 @@ def render_ui():
195
  with st.container(border=True):
196
  st.subheader("Results")
197
  results_placeholder_rerank = st.container() # Use container
 
 
 
 
198
  if tab_match_btn:
199
- if st.session_state[f"{tab_key_prefix}_input"]:
200
  with st.spinner(f"Matching using {tab_name}..."):
201
  results_html = backend_function(
202
- st.session_state[f"{tab_key_prefix}_input"],
203
  False,
204
  tab_expansion,
205
  tab_emb_top_n,
@@ -207,11 +235,17 @@ def render_ui():
207
  tab_confidence,
208
  tab_match_type
209
  )
210
- results_placeholder_rerank.markdown(results_html, unsafe_allow_html=True)
 
211
  else:
 
212
  results_placeholder_rerank.warning("Please enter product names.")
 
 
 
213
  else:
214
- results_placeholder_rerank.write("Results will appear here.")
 
215
 
216
  # Create the reranking tabs
217
  create_reranking_ui(tab_voyage, "voyage", "Voyage AI Reranking", categorize_products_with_voyage_reranking, "categories")
@@ -276,6 +310,7 @@ def render_ui():
276
  with st.container(border=True):
277
  st.subheader("Comparison Results")
278
  results_placeholder_compare = st.container() # Use container
 
279
  if compare_btn:
280
  if st.session_state.compare_input:
281
  with st.spinner("Comparing methods..."):
@@ -287,11 +322,17 @@ def render_ui():
287
  compare_match_type,
288
  compare_expansion
289
  )
290
- results_placeholder_compare.markdown(results_html, unsafe_allow_html=True)
 
291
  else:
 
292
  results_placeholder_compare.warning("Please enter product names.")
 
 
 
293
  else:
294
- results_placeholder_compare.write("Results will appear here.")
 
295
 
296
  st.markdown("---")
297
  st.markdown("Powered by Voyage AI embeddings • Built with Streamlit")
 
25
  st.session_state.openai_input = ""
26
  if 'compare_input' not in st.session_state:
27
  st.session_state.compare_input = ""
28
+ # Add state for results persistence
29
+ if 'ingredient_results_html' not in st.session_state:
30
+ st.session_state.ingredient_results_html = None
31
+ if 'category_results_html' not in st.session_state:
32
+ st.session_state.category_results_html = None
33
+ if 'voyage_results_html' not in st.session_state:
34
+ st.session_state.voyage_results_html = None
35
+ if 'openai_results_html' not in st.session_state:
36
+ st.session_state.openai_results_html = None
37
+ if 'compare_results_html' not in st.session_state:
38
+ st.session_state.compare_results_html = None
39
 
40
  # Page config is now set in app.py
41
  st.title("Product Categorization Tool")
 
90
  # Results section
91
  st.subheader("Results")
92
  results_placeholder_ingredient = st.container() # Use container for results area
93
+ # --- Results Display Logic (Ingredient Tab) ---
94
  if find_ingredients_btn:
95
  if st.session_state.ingredient_input: # Check state value
96
  with st.spinner("Finding similar ingredients..."):
 
101
  top_n,
102
  confidence
103
  )
104
+ st.session_state.ingredient_results_html = results_html # Store results
105
+ results_placeholder_ingredient.markdown(results_html, unsafe_allow_html=True) # Display immediately
106
  else:
107
+ st.session_state.ingredient_results_html = None # Clear results if input is empty
108
  results_placeholder_ingredient.warning("Please enter product names.")
109
+ # Display stored results if button wasn't clicked but results exist
110
+ elif 'ingredient_results_html' in st.session_state and st.session_state.ingredient_results_html:
111
+ results_placeholder_ingredient.markdown(st.session_state.ingredient_results_html, unsafe_allow_html=True)
112
  else:
113
+ # Initial state or after clearing
114
+ results_placeholder_ingredient.write("Results will appear here.")
115
 
116
  # --- Category Matching Tab ---
117
  with tab_category:
 
149
  with st.container(border=True):
150
  st.subheader("Results")
151
  results_placeholder_category = st.container() # Use container
152
+ # --- Results Display Logic (Category Tab) ---
153
  if match_categories_btn:
154
  if st.session_state.category_input:
155
  with st.spinner("Matching to categories..."):
 
160
  category_top_n,
161
  category_confidence
162
  )
163
+ st.session_state.category_results_html = results_html # Store results
164
+ results_placeholder_category.markdown(results_html, unsafe_allow_html=True) # Display immediately
165
  else:
166
+ st.session_state.category_results_html = None # Clear results if input is empty
167
  results_placeholder_category.warning("Please enter product names.")
168
+ # Display stored results if button wasn't clicked but results exist
169
+ elif 'category_results_html' in st.session_state and st.session_state.category_results_html:
170
+ results_placeholder_category.markdown(st.session_state.category_results_html, unsafe_allow_html=True)
171
  else:
172
+ # Initial state or after clearing
173
+ results_placeholder_category.write("Results will appear here.")
174
 
175
  # --- Common function for Reranking Tabs ---
176
  def create_reranking_ui(tab, tab_key_prefix, tab_name, backend_function, default_match="categories"):
 
219
  with st.container(border=True):
220
  st.subheader("Results")
221
  results_placeholder_rerank = st.container() # Use container
222
+ # --- Results Display Logic (Reranking Tabs) ---
223
+ results_key = f"{tab_key_prefix}_results_html"
224
+ input_key = f"{tab_key_prefix}_input"
225
+
226
  if tab_match_btn:
227
+ if st.session_state[input_key]:
228
  with st.spinner(f"Matching using {tab_name}..."):
229
  results_html = backend_function(
230
+ st.session_state[input_key],
231
  False,
232
  tab_expansion,
233
  tab_emb_top_n,
 
235
  tab_confidence,
236
  tab_match_type
237
  )
238
+ st.session_state[results_key] = results_html # Store results
239
+ results_placeholder_rerank.markdown(results_html, unsafe_allow_html=True) # Display immediately
240
  else:
241
+ st.session_state[results_key] = None # Clear results if input is empty
242
  results_placeholder_rerank.warning("Please enter product names.")
243
+ # Display stored results if button wasn't clicked but results exist
244
+ elif results_key in st.session_state and st.session_state[results_key]:
245
+ results_placeholder_rerank.markdown(st.session_state[results_key], unsafe_allow_html=True)
246
  else:
247
+ # Initial state or after clearing
248
+ results_placeholder_rerank.write("Results will appear here.")
249
 
250
  # Create the reranking tabs
251
  create_reranking_ui(tab_voyage, "voyage", "Voyage AI Reranking", categorize_products_with_voyage_reranking, "categories")
 
310
  with st.container(border=True):
311
  st.subheader("Comparison Results")
312
  results_placeholder_compare = st.container() # Use container
313
+ # --- Results Display Logic (Compare Tab) ---
314
  if compare_btn:
315
  if st.session_state.compare_input:
316
  with st.spinner("Comparing methods..."):
 
322
  compare_match_type,
323
  compare_expansion
324
  )
325
+ st.session_state.compare_results_html = results_html # Store results
326
+ results_placeholder_compare.markdown(results_html, unsafe_allow_html=True) # Display immediately
327
  else:
328
+ st.session_state.compare_results_html = None # Clear results if input is empty
329
  results_placeholder_compare.warning("Please enter product names.")
330
+ # Display stored results if button wasn't clicked but results exist
331
+ elif 'compare_results_html' in st.session_state and st.session_state.compare_results_html:
332
+ results_placeholder_compare.markdown(st.session_state.compare_results_html, unsafe_allow_html=True)
333
  else:
334
+ # Initial state or after clearing
335
+ results_placeholder_compare.write("Results will appear here.")
336
 
337
  st.markdown("---")
338
  st.markdown("Powered by Voyage AI embeddings • Built with Streamlit")