| |
| import joblib |
| import pandas as pd |
| from flask import Flask, request, jsonify |
|
|
| |
| superkart_sales_api = Flask("SuperKart Sales Revenue Predictor") |
|
|
| |
| model = joblib.load("superkart_prediction_model_v1_0.joblib") |
|
|
| |
| @superkart_sales_api.get('/') |
| def home(): |
| return "Welcome to the SuperKart Sales Revenue Prediction API!" |
|
|
| |
| @superkart_sales_api.post('/v1/sales') |
| def predict_sales_single(): |
| """ |
| Handles single prediction for SuperKart product revenue. |
| """ |
| try: |
| product_data = request.get_json() |
|
|
| |
| required_fields = [ |
| 'Product_Weight', 'Product_Sugar_Content', 'Product_Allocated_Area', |
| 'Product_Type', 'Product_MRP', 'Store_Size', |
| 'Store_Location_City_Type', 'Store_Type', 'Store_Establishment_Year' |
| ] |
| for field in required_fields: |
| if field not in product_data: |
| return jsonify({"error": f"Missing required field: {field}"}), 400 |
|
|
| |
| store_age = 2025 - int(product_data['Store_Establishment_Year']) |
| mrp_per_sqm = float(product_data['Product_MRP']) / float(product_data['Product_Allocated_Area']) |
| mrp_x_weight = float(product_data['Product_MRP']) * float(product_data['Product_Weight']) |
|
|
| |
| 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_Size': product_data['Store_Size'], |
| 'Store_Location_City_Type': product_data['Store_Location_City_Type'], |
| 'Store_Type': product_data['Store_Type'], |
| 'Store_Age': store_age, |
| 'MRP_per_sqm': mrp_per_sqm, |
| 'MRP_x_Weight': mrp_x_weight |
| } |
|
|
| input_df = pd.DataFrame([sample]) |
| predicted_sales = round(float(model.predict(input_df)[0]), 2) |
|
|
| return jsonify({'Predicted Sales (in dollars)': predicted_sales}) |
|
|
| except Exception as e: |
| print("Error in /v1/sales:", e) |
| return jsonify({"error": str(e)}), 500 |
|
|
| |
| @superkart_sales_api.post('/v1/salesbatch') |
| def predict_sales_batch(): |
| """ |
| Handles batch prediction for SuperKart product revenue from uploaded CSV. |
| """ |
| try: |
| file = request.files['file'] |
| input_data = pd.read_csv(file) |
|
|
| |
| input_data["Store_Age"] = 2025 - input_data["Store_Establishment_Year"] |
| input_data["MRP_per_sqm"] = input_data["Product_MRP"] / input_data["Product_Allocated_Area"] |
| input_data["MRP_x_Weight"] = input_data["Product_MRP"] * input_data["Product_Weight"] |
|
|
| |
| input_data.drop(columns=["Product_Id", "Store_Id", "Store_Establishment_Year"], errors='ignore', inplace=True) |
|
|
| |
| predicted_sales = [round(float(p), 2) for p in model.predict(input_data)] |
|
|
| |
| row_indices = input_data.index.tolist() |
| output_dict = dict(zip(row_indices, predicted_sales)) |
|
|
| return jsonify(output_dict) |
|
|
| except Exception as e: |
| print("Error in /v1/salesbatch:", e) |
| return jsonify({"error": str(e)}), 500 |
|
|
| |
| if __name__ == '__main__': |
| superkart_sales_api.run(debug=True) |
|
|
| |
| app = superkart_sales_api |
|
|