| |
| import numpy as np |
| import joblib |
| import pandas as pd |
| from flask import Flask, request, jsonify |
|
|
| |
| product_revenue_predictor_api = Flask("SuperKart Product Revenue Predictor") |
|
|
| |
| model = joblib.load("super_kart_revenue_prediction_model_v1_0.joblib") |
|
|
| |
| @product_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 Revenue Prediction API!" |
|
|
| |
| @product_revenue_predictor_api.post('/v1/product') |
| def predict_product_revenue(): |
| """ |
| This function handles POST requests to the '/v1/product' endpoint. |
| It expects a JSON payload containing product details and returns |
| the predicted revenue of that particular product in the given store |
| as a JSON response. |
| """ |
| |
| property_data = request.get_json() |
|
|
| |
| sample = { |
| 'Product_Weight': property_data['Product_Weight'], |
| 'Product_Allocated_Area': property_data['Product_Allocated_Area'], |
| 'Product_MRP': property_data['Product_MRP'], |
| 'Product_Sugar_Content': property_data['Product_Sugar_Content'], |
| 'Store_Id': property_data['Store_Id'], |
| 'Store_Establishment_Year': property_data['Store_Establishment_Year'], |
| 'Store_Size': property_data['Store_Size'], |
| 'Store_Location_City_Type': property_data['Store_Location_City_Type'], |
| 'Store_Type': property_data['Store_Type'], |
| 'Product_Id_Code': property_data['Product_Id_Code'] |
| } |
|
|
| |
| input_data = pd.DataFrame([sample]) |
|
|
| |
| predicted_revenue = model.predict(input_data)[0] |
|
|
| |
| return jsonify({'Predicted revenue (in rupees)': round(float(predicted_revenue), 2)}) |
|
|
| |
| @product_revenue_predictor_api.post('/v1/productBatch') |
| def predict_product_revenue_batch(): |
| """ |
| This function handles POST requests to the '/v1/productBatch' endpoint. |
| It expects a CSV file containing product/store details for multiple products |
| and returns the predicted product revenue as a dictionary in the JSON response. |
| """ |
| |
| file = request.files['file'] |
|
|
| |
| input_data = pd.read_csv(file) |
|
|
| |
| predicted_revenues = model.predict(input_data).tolist() |
|
|
| |
| |
| |
|
|
| |
| return predicted_revenues |
|
|
| |
| if __name__ == '__main__': |
| product_revenue_predictor_api.run(debug=True) |
|
|