| |
| import numpy as np |
| import joblib |
| import pandas as pd |
| from flask import Flask, request, jsonify |
|
|
| |
| superkart_api = Flask("SuperKart Sales Predictor") |
|
|
| |
| model = joblib.load("superkart_sales_prediction_model_v1_0.joblib") |
|
|
|
|
| |
| @superkart_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 Sales Prediction API!" |
|
|
|
|
| |
| @superkart_api.post('/v1/sales') |
| def predict_sales(): |
| """ |
| POST endpoint to predict sales for a single product-store combination. |
| Expects JSON input with product and store attributes. |
| """ |
| try: |
| |
| data = request.get_json() |
| print("Raw incoming data:", data) |
|
|
| |
| required_fields = [ |
| "Product_Weight", |
| "Product_Allocated_Area", |
| "Product_MRP", |
| "Store_Age", |
| "Product_Sugar_Content", |
| "Product_Type", |
| "Store_Size", |
| "Store_Location_City_Type", |
| "Store_Type", |
| "Store_Id" |
| ] |
| missing_fields = [f for f in required_fields if f not in data] |
| if missing_fields: |
| return jsonify({ |
| "error": f"Missing required fields: {missing_fields}" |
| }), 400 |
|
|
| |
| sample = { |
| "Product_Weight": data["Product_Weight"], |
| "Product_Allocated_Area": data["Product_Allocated_Area"], |
| "Product_MRP": data["Product_MRP"], |
| "Store_Age": data["Store_Age"], |
| "Product_Sugar_Content": data["Product_Sugar_Content"], |
| "Product_Type": data["Product_Type"], |
| "Store_Size": data["Store_Size"], |
| "Store_Location_City_Type": data["Store_Location_City_Type"], |
| "Store_Type": data["Store_Type"], |
| "Store_Id": data["Store_Id"] |
| } |
|
|
| |
| sample = {f: data[f] for f in required_fields} |
| |
| input_df = pd.DataFrame([sample]) |
|
|
| |
| prediction = model.predict(input_df)[0] |
|
|
| |
| predicted_sales = round(float(prediction), 2) |
|
|
| |
| return jsonify({"Predicted_Sales_Total": predicted_sales}) |
|
|
| except Exception as e: |
| |
| return jsonify({"error": str(e)}), 500 |
|
|
|
|
| |
| if __name__ == '__main__': |
| superkart_api.run(debug=True) |
|
|