File size: 3,400 Bytes
84fd116
 
f28d976
 
 
84fd116
 
 
 
 
095c856
84fd116
 
 
 
a9a69f4
84fd116
539a652
 
84fd116
539a652
84fd116
 
 
f28d976
84fd116
 
 
 
 
539a652
84fd116
 
 
 
 
 
 
 
 
 
ccf4ece
84fd116
 
 
f28d976
9464159
 
f28d976
9464159
84fd116
539a652
f28d976
539a652
 
 
 
8e61dc6
539a652
 
8e61dc6
f28d976
 
8e61dc6
539a652
f28d976
9464159
539a652
 
 
 
 
84fd116
 
 
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
import joblib
import pandas as pd  ## To Read around the CSV Files.
from flask import Flask, request, jsonify  # Flask to create the backwend API using FLASK, request to use and cater the influx requests,
                                           # and jsonify to convert the python object to JSON String,as Python object CANNOT be transmitted over 
                                           # HTTP CALLS.  

# Initialize Flask app with a name
app = Flask("Product Store Sales Revenue Predictor")

# Load the trained churn prediction model
model = joblib.load("product_store_prediction_model_v1_0.joblib")

# Define a route for the home page
@app.get('/')
def home():
    return "Welcome to the Product Store Sales Revenue Predictor API.!"

# Single Prediction
# Define an endpoint to predict Sales revenue for a given Product in a store.
@app.post('/v1/salesTotal')
def predict_sales():
    # Get JSON data from the request
    product_store_data = request.get_json()

    # Extract relevant product and store features from the input http request data.
    sample = {
        'Product_Weight': product_store_data['Product_Weight'],
        'Product_Allocated_Area': product_store_data['Product_Allocated_Area'],
        'Product_MRP': product_store_data['Product_MRP'],
        'Store_Establishment_Year': product_store_data['Store_Establishment_Year'],
        'Product_Category' : product_store_data['Product_Category'],
        'Product_Sugar_Content': product_store_data['Product_Sugar_Content'],
        'Product_Type': product_store_data['Product_Type'],
        'Store_Id': product_store_data['Store_Id'],
        'Store_Size': product_store_data['Store_Size'],
        'Store_Location_City_Type': product_store_data['Store_Location_City_Type'],
        'Store_Type': product_store_data['Store_Type']
    }

    # Convert the extracted data into a DataFrame
    input_data = pd.DataFrame([sample])
    
    # Make a Sales prediction using the trained model
    salesPrediction = model.predict(input_data).tolist()[0]

    # round off the value to 2 decimal places.
    predicted_revenue = round(float(salesPrediction), 2)

    # Return the predicted_revenue
    return jsonify({'Predicted Sales Revenue Price': predicted_revenue})

# Batch Prediction
# Define an endpoint to predict Sales revenue for a given batch of Product among the given stores.
@app.post('/v1/salesTotalBatch')
def predict_sales_batch():
    # Get the uploaded CSV file from the request
    file = request.files['file']
    
    # Read the file into a DataFrame
    input_data = pd.read_csv(file)
    
    # Adding Product_Category as part of Feature Engineering which was missing from exisiting csv file. It is derived using first 2 chars of 
    # Product_Id before dropping Product_id field in itself as being Unique in nature.
    input_data["Product_Category"] = input_data["Product_Id"].str[:2]

    # Make predictions for the batch data and convert raw predictions into a readable format.
    salesPredictions = [round(pred, 2) for pred in model.predict(input_data.drop("Product_Id", axis=1)).tolist()]
    product_id_list = input_data.Product_Id.values.tolist()
    output_dict = dict(zip(product_id_list, salesPredictions))

    return output_dict

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