File size: 1,654 Bytes
4587a57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
238f5f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4587a57
22d1001
 
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
54
55
56
57
58
import numpy as np
import pandas as pd
from flask import Flask, request, jsonify
import joblib

# Initialize Flask app
sales_prediction_api = Flask(__name__)

# 👇 REQUIRED for Hugging Face Gunicorn
application = sales_prediction_api

# Load models
dt_model = joblib.load("decision_tree_model.pkl")
xgb_model = joblib.load("xgboost_model.pkl")

# Home route
@sales_prediction_api.route("/")
def home():
    return "✅ SuperKart Sales Prediction API is running"

# Prediction route
@sales_prediction_api.route("/predict", methods=["POST"])
def predict():
    data = request.get_json()

    # Model choice
    model_choice = data.get("model", "dt")

    # Extract features (MATCHES STREAMLIT KEYS)
    sample = {
        "Product_Weight": data["Product_Weight"],
        "Product_Sugar_Content": data["Product_Sugar_Content"],
        "Product_Allocated_Area": data["Product_Allocated_Area"],
        "Product_Type": data["Product_Type"],
        "Product_MRP": data["Product_MRP"],
        "Store_Size": data["Store_Size"],
        "Store_Location_City_Type": data["Store_Location_City_Type"],
        "Store_Type": data["Store_Type"],
        "Store_Age": data["Store_Age"]
    }

    sample_df = pd.DataFrame([sample])

    # Select model
    if model_choice == "dt":
        prediction = dt_model.predict(sample_df)[0]
    elif model_choice == "xgb":
        prediction = xgb_model.predict(sample_df)[0]
    else:
        return jsonify({"error": "Invalid model choice"}), 400

    return jsonify({
        "Prediction": float(prediction),
        "ModelUsed": model_choice
    })

if __name__ == '__main__':
    sales_prediction_api.run(debug=True)