| |
| import numpy as np |
| import joblib |
| import pandas as pd |
| from flask import Flask, request, jsonify |
|
|
| |
| superMartSales_api = Flask("SuperMart_Sales_Revenue") |
|
|
| |
| model = joblib.load("supermart_sales_prediction_model_v1_0.joblib") |
|
|
|
|
| |
| @superMartSales_api.get('/') |
| def home(): |
| return "Welcome to SuperKart sales Revenue Prediction" |
|
|
|
|
| import traceback |
|
|
| @superMartSales_api.post('/v1/superKartSales') |
| def sale_pred_single(): |
| try: |
| sale_data = request.get_json() |
|
|
| sample = { |
| 'Product_Weight': sale_data.get('Product_Weight'), |
| 'Product_Sugar_Content': sale_data.get('Product_Sugar_Content'), |
| 'Product_Allocated_Area': sale_data.get('Product_Allocated_Area'), |
| 'Product_Type': sale_data.get('Product_Type'), |
| 'Product_MRP': sale_data.get('Product_MRP'), |
| 'Store_Id': sale_data.get('Store_Id'), |
| 'Store_Establishment_Year': sale_data.get('Store_Establishment_Year'), |
| 'Store_Size': sale_data.get('Store_Size'), |
| 'Store_Location_City_Type': sale_data.get('Store_Location_City_Type'), |
| 'Store_Type': sale_data.get('Store_Type'), |
| } |
|
|
| input_data = pd.DataFrame([sample]) |
|
|
| predicted_sale = model.predict(input_data)[0] |
|
|
| response = { |
| 'Store_Outlet': sample['Store_Id'], |
| "Sale": round(float(predicted_sale), 2) |
| } |
| return jsonify(response) |
|
|
| except Exception as e: |
| print("❌ Error in /v1/superKartSales:", e) |
| traceback.print_exc() |
| return jsonify({"error": str(e)}), 500 |
|
|
| |
| @superMartSales_api.post('/v1/superKartbatch') |
| def sale_pred_batch(): |
| file = request.files['file'] |
| print("File Received:", file.filename) |
|
|
| |
| input_data = pd.read_csv(file) |
|
|
| |
| predicted_sale = model.predict(input_data).tolist() |
|
|
| |
| input_data['Predicted_Sale'] = predicted_sale |
|
|
| |
| grouped_sales = input_data.groupby('Store_Id')['Predicted_Sale'].sum().to_dict() |
|
|
| |
| response = { |
| 'store_sales': { |
| store_id: round(float(sale), 2) |
| for store_id, sale in grouped_sales.items() |
| } |
| } |
| print("Final Response:", response) |
|
|
| return jsonify(response) |
|
|
|
|
| |
| if __name__ == '__main__': |
| superMartSales_api.run() |
|
|