from flask import Flask, request, jsonify import os import joblib import pandas as pd app = Flask(__name__) # Load the serialized preprocessing+model pipeline once at startup model = joblib.load("best_model.pkl") @app.route("/", methods=["GET"]) def home(): # Prevent 404 on root; quick sanity-check endpoint return jsonify({ "message": "✅ ExtraaLeanBackend is up and running!", "routes": ["/predict", "/health"] }) @app.route("/health", methods=["GET"]) def health(): # Health check for HF Spaces return jsonify({"status": "ok"}) @app.route("/predict", methods=["POST"]) def predict(): # Parse JSON body into a DataFrame data = request.get_json() df = pd.DataFrame([data]) # Predict class and probability pred = int(model.predict(df)[0]) prob = float(model.predict_proba(df)[0, 1]) return jsonify({"prediction": pred, "probability": prob}) if __name__ == "__main__": # Listen on PORT env var (provided by HF Spaces) or default 7860 port = int(os.getenv("PORT", 7860)) app.run(host="0.0.0.0", port=port)