File size: 768 Bytes
5dd10f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43acd02
 
 
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
# 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

@app.get("/health")
def health():
    return {"status": "ok"}

@app.post("/predict")
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)))