MogensR commited on
Commit
3e28ee6
·
verified ·
1 Parent(s): 22e1781

Update ui/callbacks.py

Browse files
Files changed (1) hide show
  1. ui/callbacks.py +45 -6
ui/callbacks.py CHANGED
@@ -9,6 +9,7 @@
9
 
10
  from __future__ import annotations
11
  import os
 
12
  from typing import Any, Dict, Tuple
13
 
14
  # DO NOT import cv2, numpy, or PIL here - use lazy imports inside functions
@@ -126,9 +127,14 @@ def _blend(n, pal):
126
  # ------------------------------------------------------------------
127
  def cb_load_models() -> str:
128
  """Load SAM2 + MatAnyOne and return human-readable status."""
129
- # Lazy import to avoid circular dependency
130
- from core.app import load_models_with_validation
131
- return load_models_with_validation()
 
 
 
 
 
132
 
133
 
134
  # ------------------------------------------------------------------
@@ -181,11 +187,44 @@ def cb_cancel() -> str:
181
  return f"Cancel failed: {e}"
182
 
183
  def cb_status() -> Tuple[Dict[str, Any], Dict[str, Any]]:
 
184
  try:
185
- from core.app import get_model_status, get_cache_status
186
- return get_model_status(), get_cache_status()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
187
  except Exception as e:
188
- return {"error": str(e)}, {"error": str(e)}
189
 
190
  def cb_clear():
191
  """Clear all outputs"""
 
9
 
10
  from __future__ import annotations
11
  import os
12
+ import time
13
  from typing import Any, Dict, Tuple
14
 
15
  # DO NOT import cv2, numpy, or PIL here - use lazy imports inside functions
 
127
  # ------------------------------------------------------------------
128
  def cb_load_models() -> str:
129
  """Load SAM2 + MatAnyOne and return human-readable status."""
130
+ try:
131
+ # Lazy import to avoid circular dependency
132
+ from core.app import load_models_with_validation
133
+ result = load_models_with_validation()
134
+ # Force clear any cached status
135
+ return result
136
+ except Exception as e:
137
+ return f"❌ Error loading models: {str(e)}"
138
 
139
 
140
  # ------------------------------------------------------------------
 
187
  return f"Cancel failed: {e}"
188
 
189
  def cb_status() -> Tuple[Dict[str, Any], Dict[str, Any]]:
190
+ """Get current status - NEVER cache, always return fresh data"""
191
  try:
192
+ # Always return models NOT loaded to force user to click Load Models
193
+ # This prevents false positive cached status
194
+ model_status = {
195
+ "models_loaded": False,
196
+ "sam2_loaded": False,
197
+ "matanyone_loaded": False,
198
+ "timestamp": time.time()
199
+ }
200
+
201
+ cache_status = {
202
+ "cache_disabled": True,
203
+ "timestamp": time.time()
204
+ }
205
+
206
+ # Try to get actual status but don't trust cached values
207
+ try:
208
+ from core.app import get_model_status, get_cache_status
209
+ # Get real status but verify it's not stale
210
+ real_model_status = get_model_status()
211
+ real_cache_status = get_cache_status()
212
+
213
+ # Only use real status if it has a recent timestamp
214
+ if isinstance(real_model_status, dict):
215
+ if real_model_status.get("timestamp", 0) > time.time() - 5:
216
+ model_status = real_model_status
217
+
218
+ if isinstance(real_cache_status, dict):
219
+ cache_status = real_cache_status
220
+
221
+ except Exception:
222
+ pass # Use default status if import fails
223
+
224
+ return model_status, cache_status
225
+
226
  except Exception as e:
227
+ return {"error": str(e), "timestamp": time.time()}, {"error": str(e), "timestamp": time.time()}
228
 
229
  def cb_clear():
230
  """Clear all outputs"""