File size: 1,726 Bytes
421e0cf
 
 
 
 
 
 
 
cdae9fb
421e0cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
727674a
 
 
 
0aa530f
727674a
 
421e0cf
 
 
 
cdae9fb
421e0cf
 
9d59ba1
421e0cf
 
 
 
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
# Import necessary libraries
import numpy as np
import joblib  
import pandas as pd
from flask import Flask, request, jsonify

# Initialize Flask app with a name
app = Flask("Super Kart Product Pricing Predictor")
model = joblib.load("super_kart_product_pricing_model.joblib")

# Define a route for the home page, used to validate backend is functional and accessible
@app.get('/')
def home():
    return "Welcome to the Super Kart Product Pricing Predictor API!"

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

    # Extract relevant customer features from the input data. The order of the column names matters.
    sample = {
        'Product_Weight': data['Product_Weight'],
        'Product_Allocated_Area': data['Product_Allocated_Area'],
        'Product_MRP': data['Product_MRP'],
        'Store_Establishment_Year': data['Store_Age_Years'],
        'Product_Sugar_Content_Mapping': data['Product_Sugar_Content'],
        'Store_Size_Mapping': data['Store_Size'],
        'Store_Location_City_Type_Mapping': data['Store_Location_City_Type'],
        'Product_Type_Mapping': data['Product_Type'],
        'Store_Id_Mapping': data['Store_Id'],
        'Store_Type_Mapping': data['Store_Type'],
    }

    # Convert the extracted data into a DataFrame and preprocess it
    input_data = pd.DataFrame([sample])
    prediction = model.predict(input_data).tolist()[0]

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

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