File size: 1,639 Bytes
015bfea |
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 |
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_model_v1_0.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/productstore')
def predict_sales():
# Get JSON data from the request
Prodstore_data = request.get_json()
# Extract relevant customer features from the input data
sample = {
'Product_Weight': Prodstore_data['Product_Weight'],
'Product_Allocated_Area': Prodstore_data['Product_Allocated_Area'],
'Product_MRP': Prodstore_data['Product_MRP'],
'Store_Establishment_Year': Prodstore_data['Store_Establishment_Year'],
'Product_Sugar_Content': Prodstore_data['Product_Sugar_Content'],
'Product_Type': Prodstore_data['Product_Type'],
'Store_Size': Prodstore_data['Store_Size'],
'Store_Location_City_Type': Prodstore_data['Store_Location_City_Type'],
'Store_Type': Prodstore_data['Store_Type']
}
# 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
app = Flask(__name__)
if __name__ == '__main__':
app.run(debug=True)
|