File size: 2,769 Bytes
244d09a
2ea6547
 
 
 
 
244d09a
2ea6547
 
244d09a
2ea6547
 
244d09a
2ea6547
 
 
 
244d09a
2ea6547
 
 
 
 
244d09a
2ea6547
 
 
 
 
 
 
 
 
 
 
 
 
244d09a
2ea6547
 
 
 
 
244d09a
2ea6547
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
244d09a
2ea6547
 
 
 
 
 
 
 
 
 
 
 
 
244d09a
2ea6547
 
244d09a
2ea6547
 
244d09a
2ea6547
244d09a
 
2ea6547
 
 
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90

# Import necessary libraries
import numpy as np
import joblib  # For loading the serialized model
import polars as pl  # Polars instead of pandas
from flask import Flask, request, jsonify  # Flask API

# Initialize Flask app with a name
superkart_api = Flask("SuperKart_API")

# Load trained model pipeline
model = joblib.load("Gradient-Boosting-Regressor-Tuned.joblib")

# Home route
@superkart_api.get('/')
def home():
    return "SuperKart Sales Prediction API is running"

# Prediction endpoint
@superkart_api.post('/v1/predict')
def predict_sales():
    # Get JSON input
    data = request.get_json()

    # Extract features
    required_fields = [
        'Product_Weight',
        'Product_Sugar_Content',
        'Product_Allocated_Area',
        'Product_MRP',
        'Store_Size',
        'Store_Location_City_Type',
        'Store_Type',
        'Product_Id_char',
        'Store_Age_Years',
        'Product_Type_Category'
    ]

    # Check for missing or null values
    missing_fields = [
        field for field in required_fields
        if field not in data or data[field] is None or data[field] == ""
    ]

    # If any field is missing → return error
    if missing_fields:
        return jsonify({
            "error": "Missing or empty fields in request body",
            "missing_fields": missing_fields,
            "expected_format": {
                "Product_Weight": "float",
                "Product_Sugar_Content": "string",
                "Product_Allocated_Area": "float",
                "Product_MRP": "float",
                "Store_Size": "string",
                "Store_Location_City_Type": "string",
                "Store_Type": "string",
                "Product_Id_char": "string",
                "Store_Age_Years": "float",
                "Product_Type_Category": "string"
            }
        }), 400

    # Safe to build sample
    sample = {
        'Product_Weight': data['Product_Weight'],
        'Product_Sugar_Content': data['Product_Sugar_Content'],
        'Product_Allocated_Area': data['Product_Allocated_Area'],
        'Product_MRP': data['Product_MRP'],
        'Store_Size': data['Store_Size'],
        'Store_Location_City_Type': data['Store_Location_City_Type'],
        'Store_Type': data['Store_Type'],
        'Product_Id_char': data['Product_Id_char'],
        'Store_Age_Years': data['Store_Age_Years'],
        'Product_Type_Category': data['Product_Type_Category']
    }

    # Convert to Polars DataFrame
    input_data = pl.DataFrame([sample])

    # Convert to numpy for sklearn compatibility (safe step)
    prediction = model.predict(input_data.to_pandas()).tolist()[0]

    return jsonify({'Sales': prediction})


# Run app
if __name__ == '__main__':
    superkart_api.run(debug=True)