File size: 2,584 Bytes
d31ac8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import joblib
import pandas as pd
from flask import Flask, request, jsonify

# Initialize Flask app with a name
app = Flask("SuperKart product sales revenue Prediction")

# Load the trained SuperKart sales revenue prediction model
model_superkart = joblib.load("superkart_revenue_prediction_model_v1_0.joblib")

# Define a route for the home page
@app.get('/')
def home():
    return "Welcome to the SuperKart Revenue Prediction API, created by Vrundav Gamit"

# Define an endpoint to predict revenue for single data point
# POST endpoint
# v1 in the url is a industry practise for versioning of endpoint urls
# For example v1 urls can be used to testing, v2 urls can be used for validation testing
@app.post('/v1/predictrevenue')
# @app.route('/v1/predictrevenue', methods=['POST'])
def predict_revenue():
    # Get JSON data from the request
    sales_data = request.get_json()

    # Extract relevant store and product features from the input data
    payload = {
        'Product_Weight': sales_data['Product_Weight'],
        'Product_Allocated_Area': sales_data['Product_Allocated_Area'],
        'Product_MRP': sales_data['Product_MRP'],
        'Store_Size': sales_data['Store_Size'],
        'Store_Location_City_Type': sales_data['Store_Location_City_Type'],
        'Store_Type': sales_data['Store_Type'],
        'Product_Sugar_Content': sales_data['Product_Sugar_Content'],
        'Product_Type': sales_data['Product_Type']
    }

    # Convert the extracted data into a DataFrame
    input_data = pd.DataFrame([payload])

    # Make a sales revenue prediction using the trained model
    prediction = model_superkart.predict(input_data).tolist()[0]

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

# Define an endpoint to predict revenue for a batch of store and product data
@app.post('/v1/predictrevenuebatch')
def predict_revenue_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)

    # Drop Product_Id from the input before performing a predict
    # As Product_Id is not one of the input features of the model
    predictions = model_superkart.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, predictions))

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