File size: 4,502 Bytes
26379d2
 
 
 
5d32c2b
26379d2
14ba173
5d32c2b
26379d2
14ba173
5d32c2b
26379d2
14ba173
 
26379d2
 
 
 
14ba173
5d32c2b
26379d2
14ba173
 
26379d2
 
 
 
 
 
14ba173
5d32c2b
ce1d05c
 
14ba173
 
 
 
 
3e72608
14ba173
 
 
 
ce1d05c
14ba173
5d32c2b
ce1d05c
 
 
 
 
 
 
 
 
 
 
 
 
5d32c2b
2397a50
 
5d32c2b
14ba173
 
5d32c2b
26379d2
 
14ba173
 
26379d2
 
 
 
 
14ba173
3e72608
5d32c2b
3e72608
 
5d32c2b
22408be
 
 
 
 
 
 
 
 
 
 
2397a50
 
5d32c2b
14ba173
 
2e6200f
5d32c2b
3e72608
7953292
5d32c2b
7953292
14ba173
26379d2
14ba173
 
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# Import necessary libraries
import joblib  # For loading the serialized model
import pandas as pd  # For data manipulation
from flask import Flask, request, jsonify  # For creating the Flask API

# Initialize the Flask application
superkart_sales_api = Flask("SuperKart Product Store Sales Predictor")

# Load the trained machine learning model
model = joblib.load("SuperKart_model_v1_0.joblib")

# Define a route for the home page (GET request)
@superkart_sales_api.get('/')
def home():
    """
    This function handles GET requests to the root URL ('/') of the API.
    It returns a simple welcome message.
    """
    return "Welcome to the SuperKart Product Store Sales Prediction API!"

# Define an endpoint for single product prediction (POST request)
@superkart_sales_api.post('/v1/product')
def predict_product_sales():
    """
    This function handles POST requests to the '/v1/product' endpoint.
    It expects a JSON payload containing product and store details and returns
    the predicted sales as a JSON response.
    """
    # Get the JSON data from the request body
    product_data = request.get_json()

    # Extract original features from the JSON data
    original_features = {
        'Product_Id': product_data['Product_Id'],
        '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_Id': product_data['Store_Id'],
        'Store_Establishment_Year': product_data['Store_Establishment_Year'],
        'Store_Size': product_data['Store_Size'],
        'Store_Location_City_Type': product_data['Store_Location_City_Type'],
        'Store_Type': product_data['Store_Type']
    }

    # Convert to DataFrame to easily calculate engineered features
    input_data = pd.DataFrame([original_features])

    # Calculate engineered features
    current_year = pd.to_datetime('now').year
    input_data['Store_Age'] = current_year - input_data['Store_Establishment_Year']

    perishables = ['Fruits and Vegetables', 'Dairy', 'Meat', 'Seafood', 'Breads', 'Breakfast']
    input_data['Product_Category_Type'] = input_data['Product_Type'].apply(lambda x: 'Perishables' if x in perishables else 'Non Perishables')

    # Assuming Product_Id is in the format 'XX####' and we need the first two characters
    input_data['Product_Category_from_ID'] = input_data['Product_Id'].apply(lambda x: x[:2])


    # Make prediction and round to 2 decimal places
    prediction = round(model.predict(input_data).tolist()[0], 2)

    # Return the prediction as a JSON response
    return jsonify({'Predicted_Product_Store_Sales_Total': prediction})


# Define an endpoint for batch prediction (POST request)
@superkart_sales_api.post('/v1/productbatch')
def predict_product_batch():
    """
    This function handles POST requests to the '/v1/productbatch' endpoint.
    It expects a CSV file containing product and store details for multiple entries
    and returns the predicted sales as a dictionary in the JSON response.
    """
    # Get the uploaded CSV file from the request
    file = request.files['file']

    # Read the CSV file into a Pandas DataFrame
    input_data = pd.read_csv(file)

    # Calculate engineered features for batch prediction
    current_year = pd.to_datetime('now').year
    input_data['Store_Age'] = current_year - input_data['Store_Establishment_Year']

    perishables = ['Fruits and Vegetables', 'Dairy', 'Meat', 'Seafood', 'Breads', 'Breakfast']
    input_data['Product_Category_Type'] = input_data['Product_Type'].apply(lambda x: 'Perishables' if x in perishables else 'Non Perishables')

    # Assuming Product_Id is in the format 'XX####' and we need the first two characters
    input_data['Product_Category_from_ID'] = input_data['Product_Id'].apply(lambda x: x[:2])


    # Make predictions for the batch data and round to 2 decimal places
    predictions = [round(pred, 2) for pred in model.predict(input_data).tolist()]

    # Add predictions to the DataFrame
    input_data['Predicted_Product_Store_Sales_Total'] = predictions


    # Convert results to dictionary
    result = input_data.to_dict(orient="records")

    return jsonify(result)

# Run the Flask application in debug mode if this script is executed directly
if __name__ == '__main__':
    superkart_sales_api.run(debug=True)