File size: 2,571 Bytes
a3ff0e4
 
 
 
 
 
 
 
46b9a4f
a3ff0e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
future_sale_predictor_api = Flask("SuperKart Sales Predictor")

# Load the trained sales prediction model
model = joblib.load("xgb_tuned_model.joblib")

# Define a route for the home page
@future_sale_predictor_api.get('/')
def home():
    return "Welcome to the SuperKart Sales Prediction API!, Created by Kumar Utkarsh"

# Define an endpoint to predict sales for a single product-store combination
@future_sale_predictor_api.post('/v1/predict_sale')
def predict_sale():
    # Get JSON data from the request
    sale_data = request.get_json()

    # Extract relevant product-store information from the input data
    # Ensure these keys match the expected input features for your trained model
    sample = {
        'Product_Weight': sale_data['Product_Weight'],
        'Product_Sugar_Content': sale_data['Product_Sugar_Content'],
        'Product_Allocated_Area': sale_data['Product_Allocated_Area'],
        'Product_Type': sale_data['Product_Type'],
        'Product_MRP': sale_data['Product_MRP'],
        'Store_Id': sale_data['Store_Id'],
        'Store_Establishment_Year': sale_data['Store_Establishment_Year'],
        'Store_Size': sale_data['Store_Size'],
        'Store_Location_City_Type': sale_data['Store_Location_City_Type'],
        'Store_Type': sale_data['Store_Type']
    }

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

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

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

# Define an endpoint to predict sales for a batch of product-store combinations
@future_sale_predictor_api.post('/v1/predict_sales_batch')
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)

    # Make predictions for the batch data
    # Assuming the input CSV for batch prediction has the same columns as the training data
    predictions = model.predict(input_data).tolist()

    # You might want to return predictions linked to an identifier if available in the batch input
    # For simplicity, returning a list of predictions
    return jsonify({'Predicted_Sales_Batch': predictions})


# Run the Flask app in debug mode
if __name__ == '__main__':
    # Port 7860 is used
    app.run(debug=True, host='0.0.0.0', port=7860)