| import json |
| import joblib |
| import numpy as np |
| from lightgbm import LGBMRegressor |
|
|
| |
| model = joblib.load("lgbm_model.joblib") |
|
|
| def preprocess(inputs): |
| """ |
| Convert JSON input into the appropriate format for LightGBM model |
| inputs: dict or list of dicts |
| """ |
| |
| if isinstance(inputs, dict): |
| inputs = [inputs] |
| |
| |
| feature_order = sorted(inputs[0].keys()) |
| X = np.array([[sample[f] for f in feature_order] for sample in inputs]) |
| return X |
|
|
| def predict(inputs): |
| X = preprocess(inputs) |
| preds = model.predict(X) |
| |
| return preds.tolist() |
|
|
| def handle(inputs): |
| """ |
| Function called by Hugging Face Inference Endpoint |
| """ |
| |
| if isinstance(inputs, str): |
| inputs = json.loads(inputs) |
| |
| preds = predict(inputs) |
| return {"predictions": preds} |
|
|