File size: 1,129 Bytes
acd5ff6
 
ddeea80
 
 
 
d944941
ddeea80
 
 
d944941
 
 
 
 
 
acd5ff6
d944941
 
ddeea80
 
d944941
ddeea80
 
d944941
 
 
 
cfdb748
d944941
 
acd5ff6
 
 
cfdb748
d944941
 
acd5ff6
d944941
 
 
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
# Superkart Sales Forecasting Flask API 

from flask import Flask, request, jsonify
import pandas as pd
import numpy as np
import joblib
import traceback

app = Flask(__name__)

MODEL_PATH = "best_model.pkl"

try:
    model = joblib.load(MODEL_PATH)
    print("✅ Model loaded successfully.")
except Exception as e:
    print("❌ Model load error:", e)
    traceback.print_exc()

@app.route("/", methods=["GET"])
def health_check():
    return "✅ SuperKart backend is up and running!", 200

@app.route("/v1/forecast", methods=["POST"])
def predict_single():
    try:
        data = request.get_json()
        df = pd.DataFrame([data])

        df["Store_Age"] = 2025 - df["Store_Establishment_Year"]
        df["Price_per_kg"] = df["Product_MRP"] / df["Product_Weight"]
        df["MRP_Band"] = pd.cut(
            df["Product_MRP"], bins=[0, 100, 200, float("inf")], labels=["Low", "Mid", "High"]
        )

        pred_log = model.predict(df)[0]
        pred = np.expm1(pred_log)
        return jsonify({"Predicted_Sales": round(float(pred), 2)})

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