Spaces:
Running
Running
Update app/routes.py
Browse files- app/routes.py +10 -17
app/routes.py
CHANGED
|
@@ -15,14 +15,15 @@ import torch
|
|
| 15 |
from app.utils.model_cache import model_cache
|
| 16 |
from app.config import MAX_FILE_SIZE, ALLOWED_EXTENSIONS
|
| 17 |
|
| 18 |
-
# Import
|
| 19 |
try:
|
| 20 |
-
from training.resnet_train import visualize_attention
|
| 21 |
from training.efficient_train import generate_caption
|
| 22 |
except ImportError:
|
| 23 |
# Fallback for backward compatibility (before reorganization)
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
| 26 |
|
| 27 |
logger = logging.getLogger(__name__)
|
| 28 |
|
|
@@ -86,7 +87,6 @@ def health_check():
|
|
| 86 |
'status': 'healthy',
|
| 87 |
'timestamp': datetime.utcnow().isoformat(),
|
| 88 |
'models_loaded': {
|
| 89 |
-
'resnet': model_cache.is_resnet_loaded(),
|
| 90 |
'efficientnet': model_cache.is_efficientnet_loaded()
|
| 91 |
}
|
| 92 |
}), 200
|
|
@@ -94,9 +94,9 @@ def health_check():
|
|
| 94 |
|
| 95 |
@bp.route('/ready')
|
| 96 |
def readiness_check():
|
| 97 |
-
"""Readiness check - ensures
|
| 98 |
-
if not model_cache.
|
| 99 |
-
return jsonify({'status': 'not ready', 'reason': '
|
| 100 |
return jsonify({'status': 'ready'}), 200
|
| 101 |
|
| 102 |
|
|
@@ -160,15 +160,8 @@ def upload_file():
|
|
| 160 |
model_cache._device,
|
| 161 |
max_length=64
|
| 162 |
)
|
| 163 |
-
else: # resnet50
|
| 164 |
-
|
| 165 |
-
return jsonify({'error': 'ResNet model not available'}), 503
|
| 166 |
-
|
| 167 |
-
encoder, decoder, vocab = model_cache.get_resnet_models()
|
| 168 |
-
|
| 169 |
-
# Generate caption
|
| 170 |
-
with torch.no_grad():
|
| 171 |
-
caption = visualize_attention(filepath, encoder, decoder, model_cache._device)
|
| 172 |
|
| 173 |
inference_time = time.time() - start_time
|
| 174 |
logger.info(f"Caption generated in {inference_time:.3f}s using {model_choice}")
|
|
|
|
| 15 |
from app.utils.model_cache import model_cache
|
| 16 |
from app.config import MAX_FILE_SIZE, ALLOWED_EXTENSIONS
|
| 17 |
|
| 18 |
+
# Import EfficientNet caption generation function
|
| 19 |
try:
|
|
|
|
| 20 |
from training.efficient_train import generate_caption
|
| 21 |
except ImportError:
|
| 22 |
# Fallback for backward compatibility (before reorganization)
|
| 23 |
+
try:
|
| 24 |
+
from efficient_train import generate_caption
|
| 25 |
+
except ImportError:
|
| 26 |
+
raise ImportError("Could not import generate_caption from training.efficient_train or efficient_train")
|
| 27 |
|
| 28 |
logger = logging.getLogger(__name__)
|
| 29 |
|
|
|
|
| 87 |
'status': 'healthy',
|
| 88 |
'timestamp': datetime.utcnow().isoformat(),
|
| 89 |
'models_loaded': {
|
|
|
|
| 90 |
'efficientnet': model_cache.is_efficientnet_loaded()
|
| 91 |
}
|
| 92 |
}), 200
|
|
|
|
| 94 |
|
| 95 |
@bp.route('/ready')
|
| 96 |
def readiness_check():
|
| 97 |
+
"""Readiness check - ensures EfficientNet model is loaded."""
|
| 98 |
+
if not model_cache.is_efficientnet_loaded():
|
| 99 |
+
return jsonify({'status': 'not ready', 'reason': 'EfficientNet model not loaded'}), 503
|
| 100 |
return jsonify({'status': 'ready'}), 200
|
| 101 |
|
| 102 |
|
|
|
|
| 160 |
model_cache._device,
|
| 161 |
max_length=64
|
| 162 |
)
|
| 163 |
+
else: # resnet50 (not supported - EfficientNet only)
|
| 164 |
+
return jsonify({'error': 'ResNet model is not available. Please use EfficientNet.'}), 400
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
|
| 166 |
inference_time = time.time() - start_time
|
| 167 |
logger.info(f"Caption generated in {inference_time:.3f}s using {model_choice}")
|