devjas1 commited on
Commit
d7065c4
Β·
1 Parent(s): 62a4041

(REFACTOR): improve model loading and reset behavior

Browse files

- Enhances error handling for model loading by clearly raising a 'ValueError' when the model is not loaded.
- Adds functionality to reset results and update state messages when selecting new files or samples, ensuring the UI reflects the current state accurately.
- Resets previous upload widget values and clears inference artifacts on mode changes for better UX

Files changed (1) hide show
  1. app.py +21 -5
app.py CHANGED
@@ -132,7 +132,8 @@ def load_model(model_name):
132
  if state_dict:
133
  model.load_state_dict(state_dict, strict=True)
134
  if model is None:
135
- raise ValueError("Model is not loaded. Please check the model configuration or weights.")
 
136
  model.eval()
137
  return model, True
138
  else:
@@ -296,6 +297,9 @@ def on_upload_change():
296
  st.session_state["input_text"] = text
297
  st.session_state["filename"] = getattr(up, "name", "uploaded.txt")
298
  st.session_state["input_source"] = "upload"
 
 
 
299
  st.session_state["status_message"] = f"πŸ“ File '{st.session_state['filename']}' ready for analysis"
300
  st.session_state["status_type"] = "success"
301
 
@@ -304,13 +308,14 @@ def on_sample_change():
304
  """Read selected sample once and persist as text."""
305
  sel = st.session_state.get("sample_select", "-- Select Sample --")
306
  if sel == "-- Select Sample --":
307
- # Do nothing; leave current input intact (prevents clobbering uploads)
308
  return
309
  try:
310
- text = (Path(SAMPLE_DATA_DIR) / sel).read_text(encoding="utf-8")
311
  st.session_state["input_text"] = text
312
  st.session_state["filename"] = sel
313
  st.session_state["input_source"] = "sample"
 
 
314
  st.session_state["status_message"] = f"πŸ“ Sample '{sel}' ready for analysis"
315
  st.session_state["status_type"] = "success"
316
  except (FileNotFoundError, IOError) as e:
@@ -319,9 +324,19 @@ def on_sample_change():
319
 
320
 
321
  def on_input_mode_change():
322
- """reset sample when switching to Upload"""
323
  if st.session_state["input_mode"] == "Upload File":
324
  st.session_state["sample_select"] = "-- Select Sample --"
 
 
 
 
 
 
 
 
 
 
325
 
326
  def reset_results(reason: str = ""):
327
  """Clear previous inference artifacts so the right column returns to initial state."""
@@ -395,7 +410,8 @@ def main():
395
  st.subheader("🧠 Model Selection")
396
  model_labels = [
397
  f"{MODEL_CONFIG[name]['emoji']} {name}" for name in MODEL_CONFIG.keys()]
398
- selected_label = st.selectbox("Choose AI model:", model_labels)
 
399
  model_choice = selected_label.split(" ", 1)[1]
400
 
401
  # Model info
 
132
  if state_dict:
133
  model.load_state_dict(state_dict, strict=True)
134
  if model is None:
135
+ raise ValueError(
136
+ "Model is not loaded. Please check the model configuration or weights.")
137
  model.eval()
138
  return model, True
139
  else:
 
297
  st.session_state["input_text"] = text
298
  st.session_state["filename"] = getattr(up, "name", "uploaded.txt")
299
  st.session_state["input_source"] = "upload"
300
+
301
+ # πŸ”§ Clear previous results so the right column resets immediately
302
+ reset_results("New file selected")
303
  st.session_state["status_message"] = f"πŸ“ File '{st.session_state['filename']}' ready for analysis"
304
  st.session_state["status_type"] = "success"
305
 
 
308
  """Read selected sample once and persist as text."""
309
  sel = st.session_state.get("sample_select", "-- Select Sample --")
310
  if sel == "-- Select Sample --":
 
311
  return
312
  try:
313
+ text = (Path(SAMPLE_DATA_DIR / sel).read_text(encoding="utf-8"))
314
  st.session_state["input_text"] = text
315
  st.session_state["filename"] = sel
316
  st.session_state["input_source"] = "sample"
317
+ # πŸ”§ Clear previous results so right column resets immediately
318
+ reset_results("New sample selected")
319
  st.session_state["status_message"] = f"πŸ“ Sample '{sel}' ready for analysis"
320
  st.session_state["status_type"] = "success"
321
  except (FileNotFoundError, IOError) as e:
 
324
 
325
 
326
  def on_input_mode_change():
327
+ """Reset sample when switching to Upload"""
328
  if st.session_state["input_mode"] == "Upload File":
329
  st.session_state["sample_select"] = "-- Select Sample --"
330
+ # πŸ”§ Reset when switching modes to prevent stale right-column visuals
331
+ reset_results("Switched input mode")
332
+ # Also clear te previous upload widget value to avoid confusion
333
+ st.session_state["upload_txt"] = None
334
+
335
+
336
+ def on_model_change():
337
+ """Force the right column back to init state when the model changes"""
338
+ reset_results("Model changed")
339
+
340
 
341
  def reset_results(reason: str = ""):
342
  """Clear previous inference artifacts so the right column returns to initial state."""
 
410
  st.subheader("🧠 Model Selection")
411
  model_labels = [
412
  f"{MODEL_CONFIG[name]['emoji']} {name}" for name in MODEL_CONFIG.keys()]
413
+ selected_label = st.selectbox("Choose AI model:", model_labels,
414
+ key="model_select", on_change=on_model_change)
415
  model_choice = selected_label.split(" ", 1)[1]
416
 
417
  # Model info