singhina commited on
Commit
a64d5fc
·
1 Parent(s): 09df773

Deploy Flask backend to ExtraaLeanBackend

Browse files
Files changed (1) hide show
  1. app.py +20 -9
app.py CHANGED
@@ -1,13 +1,28 @@
1
  from flask import Flask, request, jsonify
2
- import joblib, pandas as pd, os
 
 
3
 
4
  app = Flask(__name__)
5
- # Load the serialized preprocessing+model pipeline
6
  model = joblib.load("best_model.pkl")
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  @app.route("/predict", methods=["POST"])
9
  def predict():
10
- # Expect JSON body with feature keys matching training data
11
  data = request.get_json()
12
  df = pd.DataFrame([data])
13
  # Predict class and probability
@@ -15,11 +30,7 @@ def predict():
15
  prob = float(model.predict_proba(df)[0, 1])
16
  return jsonify({"prediction": pred, "probability": prob})
17
 
18
- @app.route("/health", methods=["GET"])
19
- def health():
20
- return jsonify({"status": "ok"})
21
-
22
  if __name__ == "__main__":
23
- # Use PORT env var if provided, otherwise default to 7860
24
- port = int(os.environ.get("PORT", 7860))
25
  app.run(host="0.0.0.0", port=port)
 
1
  from flask import Flask, request, jsonify
2
+ import os
3
+ import joblib
4
+ import pandas as pd
5
 
6
  app = Flask(__name__)
7
+ # Load the serialized preprocessing+model pipeline once at startup
8
  model = joblib.load("best_model.pkl")
9
 
10
+ @app.route("/", methods=["GET"])
11
+ def home():
12
+ # Prevent 404 on root; quick sanity-check endpoint
13
+ return jsonify({
14
+ "message": "✅ ExtraaLeanBackend is up and running!",
15
+ "routes": ["/predict", "/health"]
16
+ })
17
+
18
+ @app.route("/health", methods=["GET"])
19
+ def health():
20
+ # Health check for HF Spaces
21
+ return jsonify({"status": "ok"})
22
+
23
  @app.route("/predict", methods=["POST"])
24
  def predict():
25
+ # Parse JSON body into a DataFrame
26
  data = request.get_json()
27
  df = pd.DataFrame([data])
28
  # Predict class and probability
 
30
  prob = float(model.predict_proba(df)[0, 1])
31
  return jsonify({"prediction": pred, "probability": prob})
32
 
 
 
 
 
33
  if __name__ == "__main__":
34
+ # Listen on PORT env var (provided by HF Spaces) or default 7860
35
+ port = int(os.getenv("PORT", 7860))
36
  app.run(host="0.0.0.0", port=port)