Spaces:
Runtime error
Runtime error
| # app.py | |
| from flask import Flask, request, jsonify | |
| import joblib, os | |
| app = Flask(__name__) | |
| _model = None | |
| def get_model(): | |
| global _model | |
| if _model is None: | |
| _model = joblib.load(os.path.join("models", "best_model.joblib")) | |
| return _model | |
| def health(): | |
| return {"status": "ok"} | |
| def predict(): | |
| data = request.get_json(force=True) # expects dict with feature names | |
| model = get_model() | |
| # model was trained with a ColumnTransformer pipeline; pass DataFrame-like rows | |
| import pandas as pd | |
| X = pd.DataFrame([data]) | |
| yhat = model.predict(X) | |
| return jsonify({"prediction": float(yhat[0])}) | |
| if __name__ == "__main__": | |
| app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 7860))) |