File size: 1,981 Bytes
0df774e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import joblib
import pandas as pd
from flask import Flask, request, jsonify

# Initialize Flask app with a name
Sales_predictor_api = Flask("SuperKart Sales Predictor")

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

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

# Define an endpoint to predict churn for a single customer
@Sales_predictor_api.post('/v1/sales')
def predict_sales():
    # Get JSON data from the request
    sales_data = request.get_json()

    # Extract relevant sales features from the input data
    sample = {
        'Product_Weight': sales_data['Product_Weight'],
        'Product_Allocated_Area': sales_data['Product_Allocated_Area'],
        'Product_MRP': sales_data['Product_MRP'],
        'Product_Type' : sales_data['Product_Type'],          
        'Product_MRP' : sales_data['Product_Type'] ,
        'Store_Size'  : sales_data['Store_Size'] ,  
        'Store_Location_City_Type' : sales_data['Store_Location_City_Type'] ,
        'Store_Type' : sales_data['Store_Type'],
        'Store_Age'  : sales_data['Store_Age'] ,                 
        'Avg_Sales_Per_Product' : sales_data['Avg_Sales_Per_Product'] ,      
        'Avg_Sales_Per_Store'   : sales_data['Avg_Sales_Per_Store'] ,       
        'Product_Sales_Rank'    : sales_data['Product_Sales_Rank']  ,       
        'Store_Product_Share'   : sales_data['Store_Product_Share']  ,       
        'Product_MRP_Band'      : sales_data['Product_MRP_Band']
    }

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

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

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

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