Spaces:
Running
Running
Update app/utils/model_cache.py
Browse files- app/utils/model_cache.py +44 -0
app/utils/model_cache.py
CHANGED
|
@@ -112,6 +112,21 @@ class ModelCache:
|
|
| 112 |
efficientnet_path.replace('efficient_best_model.pth', 'efficientnet_efficient_best_model_quantized.pth'),
|
| 113 |
]
|
| 114 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
# Find optimized EfficientNet model
|
| 116 |
for opt_path in optimized_efficient_paths:
|
| 117 |
if os.path.exists(opt_path):
|
|
@@ -309,12 +324,41 @@ class ModelCache:
|
|
| 309 |
logger.info(f"Found model at: {alt_path}")
|
| 310 |
return alt_path
|
| 311 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 312 |
# Try in root directory (backward compatibility)
|
| 313 |
alt_path = str(BASE_DIR / os.path.basename(checkpoint_path))
|
| 314 |
if os.path.exists(alt_path):
|
| 315 |
logger.info(f"Found model at: {alt_path}")
|
| 316 |
return alt_path
|
| 317 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 318 |
# Return original path (will fail with clear error)
|
| 319 |
return checkpoint_path
|
| 320 |
|
|
|
|
| 112 |
efficientnet_path.replace('efficient_best_model.pth', 'efficientnet_efficient_best_model_quantized.pth'),
|
| 113 |
]
|
| 114 |
|
| 115 |
+
# Also search in HF Hub cache directories (nested structure)
|
| 116 |
+
hf_cache_base = MODELS_DIR / "optimized_models"
|
| 117 |
+
if hf_cache_base.exists():
|
| 118 |
+
# Search for HF Hub cache structure: models--*--*/snapshots/*/model.pth
|
| 119 |
+
for cache_dir in hf_cache_base.glob("models--*"):
|
| 120 |
+
if cache_dir.is_dir():
|
| 121 |
+
snapshots_dir = cache_dir / "snapshots"
|
| 122 |
+
if snapshots_dir.exists():
|
| 123 |
+
for snapshot_dir in snapshots_dir.glob("*"):
|
| 124 |
+
if snapshot_dir.is_dir():
|
| 125 |
+
hf_model_path = snapshot_dir / "efficientnet_efficient_best_model_quantized.pth"
|
| 126 |
+
if hf_model_path.exists():
|
| 127 |
+
optimized_efficient_paths.insert(0, str(hf_model_path))
|
| 128 |
+
logger.info(f"Found model in HF Hub cache: {hf_model_path}")
|
| 129 |
+
|
| 130 |
# Find optimized EfficientNet model
|
| 131 |
for opt_path in optimized_efficient_paths:
|
| 132 |
if os.path.exists(opt_path):
|
|
|
|
| 324 |
logger.info(f"Found model at: {alt_path}")
|
| 325 |
return alt_path
|
| 326 |
|
| 327 |
+
# Try in optimized_models directory
|
| 328 |
+
alt_path = str(MODELS_DIR / "optimized_models" / os.path.basename(checkpoint_path))
|
| 329 |
+
if os.path.exists(alt_path):
|
| 330 |
+
logger.info(f"Found model at: {alt_path}")
|
| 331 |
+
return alt_path
|
| 332 |
+
|
| 333 |
# Try in root directory (backward compatibility)
|
| 334 |
alt_path = str(BASE_DIR / os.path.basename(checkpoint_path))
|
| 335 |
if os.path.exists(alt_path):
|
| 336 |
logger.info(f"Found model at: {alt_path}")
|
| 337 |
return alt_path
|
| 338 |
|
| 339 |
+
# Search in HF Hub cache directories (nested structure)
|
| 340 |
+
hf_cache_base = MODELS_DIR / "optimized_models"
|
| 341 |
+
if hf_cache_base.exists():
|
| 342 |
+
model_filename = os.path.basename(checkpoint_path)
|
| 343 |
+
# Also try quantized variant if looking for base model
|
| 344 |
+
search_filenames = [model_filename]
|
| 345 |
+
if 'efficient_best_model.pth' in model_filename and 'quantized' not in model_filename:
|
| 346 |
+
search_filenames.append('efficientnet_efficient_best_model_quantized.pth')
|
| 347 |
+
search_filenames.append(model_filename.replace('efficient_best_model.pth', 'efficientnet_efficient_best_model_quantized.pth'))
|
| 348 |
+
|
| 349 |
+
# Search for HF Hub cache structure: models--*--*/snapshots/*/model.pth
|
| 350 |
+
for cache_dir in hf_cache_base.glob("models--*"):
|
| 351 |
+
if cache_dir.is_dir():
|
| 352 |
+
snapshots_dir = cache_dir / "snapshots"
|
| 353 |
+
if snapshots_dir.exists():
|
| 354 |
+
for snapshot_dir in snapshots_dir.glob("*"):
|
| 355 |
+
if snapshot_dir.is_dir():
|
| 356 |
+
for search_filename in search_filenames:
|
| 357 |
+
hf_model_path = snapshot_dir / search_filename
|
| 358 |
+
if hf_model_path.exists():
|
| 359 |
+
logger.info(f"Found model in HF Hub cache: {hf_model_path}")
|
| 360 |
+
return str(hf_model_path)
|
| 361 |
+
|
| 362 |
# Return original path (will fail with clear error)
|
| 363 |
return checkpoint_path
|
| 364 |
|