File size: 1,653 Bytes
0fa1c54
41935d5
 
0fa1c54
 
b65f39a
0fa1c54
53c37fa
0fa1c54
 
 
 
 
 
 
b65f39a
0fa1c54
 
 
b65f39a
b569e15
 
 
0fa1c54
 
b133196
b569e15
0fa1c54
 
 
b133196
 
b65f39a
b133196
 
 
 
 
 
 
 
 
b65f39a
b133196
b65f39a
b133196
 
0fa1c54
 
099d2ed
0bab8cd
0fa1c54
1c65e07
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
46
47
48
49
50
51
52
53
import os
import joblib
import pandas as pd
from flask import Flask, request, jsonify

app = Flask(__name__)

MODEL_PATH = "superkart_model_v1_0.joblib"
model = None


def load_model():
    global model
    if model is None:
        if not os.path.exists(MODEL_PATH):
            raise FileNotFoundError(f"Model file not found: {MODEL_PATH}")
        model = joblib.load(MODEL_PATH)


# Health check (important for deployment)
@app.route("/", methods=["GET"])
def health():
    return "SuperKart Backend is running"


@app.route("/predict", methods=["POST"]) # Changed from /v1/predict to match frontend
def predict():
    try:
        load_model()
        data = request.get_json(force=True)
        
        # Keys must be strings to match the JSON sent by Streamlit
        sample = {
            "Product_Weight": data["Product_Weight"][0],
            "Product_Sugar_Content": data["Product_Sugar_Content"][0],
            "Product_Allocated_Area": data["Product_Allocated_Area"][0],
            "Product_Type": data["Product_Type"][0],
            "Product_MRP": data["Product_MRP"][0],
            "Store_Establishment_Year": data["Store_Establishment_Year"][0],
            "Store_Size": data["Store_Size"][0],
            "Store_Location_City_Type": data["Store_Location_City_Type"][0],
            "Store_Type": data["Store_Type"][0]
        }
        
        query_df = pd.DataFrame([sample])
        prediction = model.predict(query_df).tolist()
        return jsonify({"predictions": prediction}) 
    except Exception as e:
        return jsonify({"error": str(e)}), 500


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