| |
| import joblib |
| import pandas as pd |
| from flask import Flask, request, jsonify |
| from datetime import datetime |
|
|
| |
| sales_revenue_predictor_api = Flask("SuperKart Sales Revenue Predictor") |
|
|
| |
| model = joblib.load("superkart_sales_revenue_prediction_model_v1_0.joblib") |
|
|
| |
| @sales_revenue_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 Product Sales Revenue Prediction API !" |
|
|
| |
| @sales_revenue_predictor_api.post('/v1/revenue') |
| def predict_sales_revenue(): |
| """ |
| This function handles POST requests to the '/v1/revenue' endpoint. |
| It expects a JSON payload containing property details and returns |
| the predicted product sales revenue as a JSON response. |
| """ |
| |
| property_data = request.get_json() |
|
|
| |
| sample = { |
| 'Product_Weight': property_data['Product_Weight'], |
| 'Product_Sugar_Content': property_data['Product_Sugar_Content'], |
| 'Product_Allocated_Area': property_data['Product_Allocated_Area'], |
| 'Product_Type': property_data['Product_Type'], |
| 'Product_MRP': property_data['Product_MRP'], |
| 'Store_Size': property_data['Store_Size'], |
| 'Store_Location_City_Type': property_data['Store_Location_City_Type'], |
| 'Store_Type': property_data['Store_Type'], |
| 'Store_Establishment_Year': property_data['Store_Establishment_Year'] |
| } |
|
|
| |
| record_input_data = pd.DataFrame([sample]) |
|
|
| |
| current_year_value = datetime.now().year |
| record_input_data['Store_Age'] = current_year_value - record_input_data['Store_Establishment_Year'] |
|
|
| |
| age_bins = [0, 10, 20, 30, float("inf")] |
| age_labels = ["<10 Years", "10β20 Years", "20β30 Years", "30+ Years"] |
|
|
| |
| record_input_data["Store_Age_Binned"] = pd.cut( |
| record_input_data["Store_Age"], |
| bins=age_bins, |
| labels=age_labels, |
| right=False |
| ) |
|
|
| |
| predicted_store_revenue = model.predict(record_input_data)[0] |
|
|
| |
| predicted_store_revenue = round(float(predicted_store_revenue), 2) |
| |
|
|
| |
| return jsonify({'Predicted Product_Store_Sales_Total': predicted_store_revenue}) |
|
|
|
|
| |
| @sales_revenue_predictor_api.post('/v1/revenuebatch') |
| def predict_sales_revenue_batch(): |
| """ |
| This function handles POST requests to the '/v1/revenuebatch' endpoint. |
| It expects a CSV file containing property details for multiple properties |
| and returns the predicted product sales revenue as a dictionary in the JSON response. |
| """ |
| |
| file = request.files['file'] |
|
|
| |
| csv_input_data = pd.read_csv(file) |
|
|
| |
| current_year_value = datetime.now().year |
| csv_input_data['Store_Age'] = current_year_value - csv_input_data['Store_Establishment_Year'] |
| |
| age_bins = [0, 10, 20, 30, float("inf")] |
| age_labels = ["<10 Years", "10β20 Years", "20β30 Years", "30+ Years"] |
|
|
| |
| csv_input_data["Store_Age_Binned"] = pd.cut( |
| csv_input_data["Store_Age"], |
| bins=age_bins, |
| labels=age_labels, |
| right=False |
| ) |
|
|
| |
| predicted_store_revenues = model.predict(csv_input_data).tolist() |
|
|
| |
| if 'Product_Id' not in csv_input_data.columns: |
| return jsonify({"error": "Input file must contain a 'Product_Id' column"}), 400 |
|
|
| |
| product_ids = csv_input_data['Product_Id'].tolist() |
| output_dict = dict(zip(product_ids, predicted_store_revenues)) |
|
|
| |
| return jsonify(output_dict) |
|
|
| |
| if __name__ == '__main__': |
| sales_revenue_predictor_api.run(debug=True) |
|
|