File size: 991 Bytes
1efdefb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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}