| import joblib |
| import pandas as pd |
| from flask import Flask, request, jsonify |
|
|
| |
| |
| |
| sales_prediction_api = Flask('Superkart Sales Prediction') |
|
|
| |
| |
| |
| model = joblib.load("gradient_tuned.joblib") |
|
|
| |
| |
| |
| @sales_prediction_api.get("/") |
| def home(): |
| """Simple health check endpoint""" |
| return 'Welcome to the SuperKart Sales Prediction API' |
|
|
| |
| |
| |
| @sales_prediction_api.post("/v1/salesdata") |
| def predict_sales(): |
| """Predict sales for a single product/store record""" |
| try: |
| |
| sales_data = request.get_json() |
|
|
| |
| sample = { |
| 'Product_Weight': sales_data['Product_Weight'], |
| 'Product_Allocated_Area': sales_data['Product_Allocated_Area'], |
| 'Product_MRP': sales_data['Product_MRP'], |
| 'Product_Sugar_Content': sales_data['Product_Sugar_Content'], |
| 'Product_Type': sales_data['Product_Type'], |
| 'Store_Id': sales_data['Store_Id'], |
| 'Store_Size': sales_data['Store_Size'], |
| 'Store_Location_City_Type': sales_data['Store_Location_City_Type'], |
| } |
|
|
| |
| input_df = pd.DataFrame([sample]) |
|
|
| |
| predicted_sales = model.predict(input_df)[0] |
| predicted_sales = round(float(predicted_sales), 2) |
|
|
| |
| return jsonify({"predicted_sales": predicted_sales}) |
|
|
| except Exception as e: |
| return jsonify({"error": str(e)}) |
|
|
| |
| |
| |
| @sales_prediction_api.post("/v1/salesdatabatch") |
| def predict_sales_batch(): |
| """Predict sales for multiple product/store records from a CSV file""" |
| try: |
| |
| file = request.files['file'] |
| input_df = pd.read_csv(file) |
|
|
| |
| features = [ |
| 'Product_Weight', 'Product_Allocated_Area', 'Product_MRP', |
| 'Product_Sugar_Content', 'Product_Type', 'Store_Id', |
| 'Store_Size', 'Store_Location_City_Type' |
| ] |
| input_features = input_df[features] |
|
|
| |
| predicted_sales = model.predict(input_features) |
| predicted_sales = [round(float(sale), 2) for sale in predicted_sales] |
|
|
| |
| if 'Product_Id' in input_df.columns: |
| product_ids = input_df['Product_Id'].tolist() |
| output_dict = dict(zip(product_ids, predicted_sales)) |
| else: |
| output_dict = {"predicted_sales": predicted_sales} |
|
|
| return jsonify(output_dict) |
|
|
| except Exception as e: |
| return jsonify({"error": str(e)}) |
|
|
| |
| |
| |
| if __name__ == '__main__': |
| sales_prediction_api.run(debug=True) |
|
|