Spaces:
Sleeping
Sleeping
Download app/services/model_service.py from otito254/epl-predictor-api: direct link, hf CLI and curl.
- Browser
- Download file 943 Bytes
-
https://huggingface.co/spaces/otito254/epl-predictor-api/resolve/main/app/services/model_service.py
- Command line
-
hf download hf://spaces/otito254/epl-predictor-api/app/services/model_service.py
-
curl -L -o model_service.py https://huggingface.co/spaces/otito254/epl-predictor-api/resolve/main/app/services/model_service.py
943 Bytes
| import joblib | |
| import numpy as np | |
| import pandas as pd | |
| from pathlib import Path | |
| MODEL_DIR = Path("models/saved") | |
| def load_model(): | |
| """Load the main ensemble model.""" | |
| return joblib.load(MODEL_DIR / "ensemble.pkl") | |
| def load_imputer(): | |
| """Load the feature imputer.""" | |
| return joblib.load(MODEL_DIR / "imputer.pkl") | |
| def load_feature_names() -> list: | |
| """Load expected feature names in correct order.""" | |
| return joblib.load(MODEL_DIR / "feature_names.pkl") | |
| def predict_proba(model, features: pd.DataFrame) -> list[float]: | |
| imputer = load_imputer() | |
| feature_names = load_feature_names() | |
| features = features.reindex(columns=feature_names) | |
| # ✅ Keep as DataFrame with column names — fixes sklearn warning | |
| features_imputed = pd.DataFrame( | |
| imputer.transform(features), | |
| columns=feature_names | |
| ) | |
| probs = model.predict_proba(features_imputed)[0] | |
| return probs.tolist() |