Spaces:
Runtime error
Runtime error
Download handler.py from rafadhii/rd-indralaya: direct link, hf CLI and curl.
- Browser
- Download file 991 Bytes
-
https://huggingface.co/spaces/rafadhii/rd-indralaya/resolve/main/handler.py
- Command line
-
hf download hf://spaces/rafadhii/rd-indralaya/handler.py
-
curl -L -o handler.py https://huggingface.co/spaces/rafadhii/rd-indralaya/resolve/main/handler.py
991 Bytes
| import json | |
| import os | |
| import joblib | |
| import pandas as pd | |
| class EndpointHandler: | |
| def __init__(self, path: str = ""): | |
| model_path = os.path.join(path, "random_forest_best.pkl") | |
| features_path = os.path.join(path, "feature_columns.json") | |
| self.model = joblib.load(model_path) | |
| with open(features_path, "r", encoding="utf-8") as f: | |
| self.features = json.load(f) | |
| def __call__(self, data): | |
| # Expected payload format: | |
| # {"inputs": [{"feature_a": value, ...}, {...}]} | |
| rows = data.get("inputs", []) | |
| if not rows: | |
| return {"error": "Payload must contain non-empty 'inputs' list."} | |
| X = pd.DataFrame(rows) | |
| missing = [c for c in self.features if c not in X.columns] | |
| if missing: | |
| return {"error": "Missing required features.", "missing_features": missing} | |
| X = X[self.features] | |
| preds = self.model.predict(X).tolist() | |
| return {"predictions": preds} | |