| |
| import numpy as np |
| import joblib |
| import pandas as pd |
| from flask import Flask, request, jsonify |
|
|
| |
| superkart_sales_predictor_api = Flask("SuperKart Sales Predictor") |
|
|
| |
| model = joblib.load("superkart_sales_prediction_model.joblib") |
|
|
| |
| @superkart_sales_predictor_api.get('/') |
| def home(): |
| """ |
| This function handles GET requests to the root URL ("/") of the API. |
| It returns a simple welcome message. |
| """ |
| return "Welcome to the SuperKart Sales Prediction API!" |
|
|
| |
| @superkart_sales_predictor_api.post('/v1/predict-sales') |
| def predict_sales(): |
| """ |
| This function handles POST requests to the '/v1/predict-sales' endpoint. |
| It expects a JSON payload containing product and store details and returns |
| the predicted sales as a JSON response. |
| """ |
| |
| product_data = request.get_json() |
|
|
| |
| sample = { |
| 'Product_Weight': product_data['Product_Weight'], |
| 'Product_Sugar_Content': product_data['Product_Sugar_Content'], |
| 'Product_Allocated_Area': product_data['Product_Allocated_Area'], |
| 'Product_Type': product_data['Product_Type'], |
| 'Product_MRP': product_data['Product_MRP'], |
| 'Store_Establishment_Year': product_data['Store_Establishment_Year'], |
| 'Store_Size': product_data['Store_Size'], |
| 'Store_Location_City_Type': product_data['Store_Location_City_Type'], |
| 'Store_Type': product_data['Store_Type'] |
| } |
|
|
| |
| input_data = pd.DataFrame([sample]) |
|
|
| |
| predicted_sales = model.predict(input_data)[0] |
|
|
| |
| predicted_sales = round(float(predicted_sales), 2) |
|
|
| |
| return jsonify({'Predicted Product Store Sales Total': predicted_sales}) |
|
|
|
|
| |
| @superkart_sales_predictor_api.post('/v1/predict-sales-batch') |
| def predict_sales_batch(): |
| """ |
| This function handles POST requests to the '/v1/predict-sales-batch' endpoint. |
| It expects a CSV file containing product and store details for multiple entries |
| and returns the predicted sales as a dictionary in the JSON response. |
| """ |
| |
| file = request.files['file'] |
|
|
| |
| input_data = pd.read_csv(file) |
|
|
| |
| predicted_sales = model.predict(input_data).tolist() |
|
|
| |
| predicted_sales = [round(float(sales), 2) for sales in predicted_sales] |
|
|
| |
| |
| |
| output_dict = {f"Prediction_{i+1}": sales for i, sales in enumerate(predicted_sales)} |
|
|
| |
| return output_dict |
|
|
| |
| if __name__ == '__main__': |
| superkart_sales_predictor_api.run(debug=True) |
|
|