File size: 1,113 Bytes
58c1d87
 
 
 
 
107ed62
 
58c1d87
107ed62
 
d552013
5d1b892
107ed62
58c1d87
 
b2e90ef
58c1d87
 
 
 
 
 
 
 
 
40c3981
58c1d87
40c3981
 
 
 
 
58c1d87
40c3981
 
 
 
58c1d87
 
 
 
40c3981
b2e90ef
58c1d87
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
38
39
40
41
42
43
44
45

from flask import Flask, request, jsonify
import joblib
import numpy as np
import os


from huggingface_hub import hf_hub_download
import joblib

# Load the trained model
model = joblib.load("model_compressed.joblib")


# Initialize app
app = Flask(__name__)

@app.route("/")
def home():
    return jsonify({"message": "Supermarket Revenue Prediction API is running!"})

@app.route("/predict", methods=["POST"])
def predict():
    try:
        data = request.get_json(force=True)
        features = np.array(data["features"])

        # Case 1: single row
        if features.ndim == 1:
            features = features.reshape(1, -1)
            prediction = model.predict(features)[0]
            return jsonify({"predicted_revenue": float(prediction)})

        # Case 2: multiple rows
        else:
            predictions = model.predict(features).tolist()
            return jsonify({"predictions": predictions})

    except Exception as e:
        return jsonify({"error": str(e)})


if __name__ == "__main__":
    app.run(host="0.0.0.0", port=7860, debug=True)