| |
| import numpy as np |
| import joblib |
| import pandas as pd |
| from flask import Flask, request, jsonify |
|
|
| |
| total_sales_predictor_api = Flask("SuperKart Product Total Sales Prediction") |
|
|
| |
| model = joblib.load("product_store_sales_total_prediction_model_v1_0.joblib") |
|
|
| |
| |
| |
| @total_sales_predictor_api.get('/') |
| def home(): |
| """ |
| Handles GET requests to the root URL ('/'). |
| Returns a simple welcome message to confirm the API is running. |
| """ |
| return "Welcome to the SuperKart Product Total Sales Prediction API!" |
|
|
| |
| |
| |
| @total_sales_predictor_api.post('/v1/totalsales') |
| def predict_total_sales(): |
| """ |
| Handles POST requests to '/v1/totalsales' endpoint. |
| Accepts JSON input with product and store features, |
| and returns the predicted 'Product_Store_Sales_Total' as JSON. |
| """ |
| |
| input_data = request.get_json() |
|
|
| |
| sample = { |
| 'Product_Weight': input_data['Product_Weight'], |
| 'Product_Allocated_Area': input_data['Product_Allocated_Area'], |
| 'Product_MRP': input_data['Product_MRP'], |
| 'Store_Establishment_Year': input_data['Store_Establishment_Year'], |
| 'Product_Sugar_Content': input_data['Product_Sugar_Content'], |
| 'Product_Type': input_data['Product_Type'], |
| 'Store_Id': input_data['Store_Id'], |
| 'Store_Size': input_data['Store_Size'], |
| 'Store_Location_City_Type': input_data['Store_Location_City_Type'], |
| 'Store_Type': input_data['Store_Type'] |
| } |
|
|
| |
| input_df = pd.DataFrame([sample]) |
|
|
| |
| predicted_sales = model.predict(input_df)[0] |
|
|
| |
| predicted_sales = round(float(predicted_sales), 2) |
|
|
| |
| return jsonify({'Predicted Product Store Sales Total': predicted_sales}) |
|
|
| |
| |
| |
| @total_sales_predictor_api.post('/v1/totalsalesbatch') |
| def predict_total_sales_batch(): |
| """ |
| Handles POST requests to '/v1/totalsalesbatch' endpoint. |
| Accepts a CSV file of multiple records and returns a dictionary |
| mapping 'Product_Id' to predicted total sales value. |
| """ |
| |
| file = request.files['file'] |
|
|
| |
| input_df = pd.read_csv(file) |
|
|
| |
| predicted_prices = model.predict(input_df).tolist() |
|
|
| |
| product_ids = input_df['Product_Id'].tolist() |
|
|
| |
| output_dict = dict(zip(product_ids, [round(float(p), 2) for p in predicted_prices])) |
|
|
| |
| return output_dict |
|
|
| |
| |
| |
| if __name__ == '__main__': |
| total_sales_predictor_api.run(debug=True) |
|
|