File size: 1,087 Bytes
09df773
a64d5fc
 
 
09df773
 
a64d5fc
09df773
 
a64d5fc
 
 
 
 
 
 
 
 
 
 
 
 
09df773
 
a64d5fc
09df773
 
 
 
 
 
 
 
a64d5fc
 
09df773
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
30
31
32
33
34
35
36
37
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)