From f7b154f866046f3d029e309c39747e10c6830319 Mon Sep 17 00:00:00 2001 From: Marko Djordjevic Date: Sun, 15 Feb 2026 22:18:16 +0100 Subject: [PATCH] fix(ml): extract class labels from model when metadata is missing The model info returned empty labels array because the pkl file has no metadata dict. Now extracts labels from model.classes_ or model.model.classes_ as fallback. --- services/ml/app/main.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/services/ml/app/main.py b/services/ml/app/main.py index 65b0f67..c6119b9 100644 --- a/services/ml/app/main.py +++ b/services/ml/app/main.py @@ -257,6 +257,15 @@ def load_model_from_local(model_path: str) -> tuple[Any, Dict[str, Any]]: model = model_data metadata = {} + # Extract labels from model if not in metadata + labels = metadata.get("labels", []) + if not labels: + # Try to get class labels from the model itself + if hasattr(model, 'classes_'): + labels = [str(c) for c in model.classes_] + elif hasattr(model, 'model') and hasattr(model.model, 'classes_'): + labels = [str(c) for c in model.model.classes_] + # Build model info model_info = { "model_name": model_path.stem, @@ -265,7 +274,7 @@ def load_model_from_local(model_path: str) -> tuple[Any, Dict[str, Any]]: "trained_at": metadata.get("trained_at", None), "dataset_version": metadata.get("dataset_version", None), "feature_engineering_enabled": metadata.get("feature_engineering_enabled", True), - "labels": metadata.get("labels", []), + "labels": labels, "per_class_metrics": metadata.get("per_class_metrics", []) }