SuperKartModel / streamlit_app.py
psr1992's picture
Update streamlit_app.py
43acd02 verified
raw
history blame contribute delete
768 Bytes
# 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)))