File size: 2,332 Bytes
4fd7ca0
 
 
 
0856265
4fd7ca0
 
0856265
66a887c
4fd7ca0
d6e0139
 
 
c6874ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0856265
4fd7ca0
 
 
 
0856265
4fd7ca0
 
0856265
 
 
 
 
 
 
 
 
 
 
195cdab
0856265
 
c6874ad
 
0856265
 
 
 
 
 
 
 
 
 
4fd7ca0
0856265
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
import joblib
import pandas as pd
from flask import Flask, request, jsonify

# Initialize Flask app
sales_predictor_api = Flask("Product_Store_Total_Sales_Predictor")

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

#Label encoding Store Size as this is ordinal
size_map = {'Small': 1, 'Medium': 2, 'High': 3}

feasibility_map = {
    'Fruits and Vegetables': 'Perishable',
    'Hard Drinks': 'Non Perishable',
    'Snack Foods': 'Perishable',
    'Dairy': 'Perishable',
    'Canned': 'Perishable',
    'Baking Goods': 'Perishable',
    'Breads': 'Perishable',
    'Breakfast': 'Perishable',
    'Frozen Foods': 'Perishable',
    'Health and Hygiene': 'Perishable',
    'Household': 'Perishable',
    'Seafood': 'Perishable',
    'Starchy Foods': 'Perishable',
    'Others': 'Non Perishable',
    'Meat': 'Perishable',
    'Soft Drinks': 'Non Perishable',
}

# Home endpoint
@sales_predictor_api.get('/')
def home():
    return "Welcome to the Product Store Total Sales Prediction API!"

# Prediction endpoint
@sales_predictor_api.post('/v1/product')
def predict_product_sales():
    try:
        product_data = request.get_json()

        # Construct input DataFrame
        input_data = pd.DataFrame([{
            'Product_Weight': product_data['Product_Weight'],
            'Product_Sugar_Content': product_data['Product_Sugar_Content'],
            'Product_Allocated_Area': product_data['Product_Allocated_Area'],
            'Product_Type': product_data['Product_Type'],
            'Product_MRP': product_data['Product_MRP'],
            'Store_Establishment_Year': product_data['Store_Establishment_Year'],
            'Store_Size':  size_map[product_data['Store_Size']],
            'Store_Location_City_Type': product_data['Store_Location_City_Type'],
            'Store_Type': product_data['Store_Type'],
            'Store_Age': 2025 - product_data['Store_Establishment_Year'],
            'Product_Perishability': feasibility_map[product_data['Product_Type']]
        }])

        # Predict
        prediction = model.predict(input_data).tolist()[0]
        return jsonify({'Product Store Sales Total': prediction})

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

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