# Import necessary libraries import numpy as np import joblib import pandas as pd from flask import Flask, request, jsonify # Initialize the Flask application Superkart_sales_predictor_api = Flask("SuperKart Store Sales Prediction") # Load the trained machine learning model model = joblib.load("best_random_forest_model.joblib") # Home route @Superkart_sales_predictor_api.get('/') def home(): return "Welcome to the SuperKart Store Sales Prediction API!" # Single prediction endpoint @Superkart_sales_predictor_api.post('/v1/sales') def predict_sales_price(): try: store_data = request.get_json() sample = { 'Product_Weight': store_data['Product_Weight'], 'Product_Sugar_Content': store_data['Product_Sugar_Content'], 'Product_Allocated_Area': store_data['Product_Allocated_Area'], 'Product_Type': store_data['Product_Type'], 'Product_MRP': store_data['Product_MRP'], 'Store_Id': store_data['Store_Id'], 'Store_Establishment_Year': store_data['Store_Establishment_Year'], 'Store_Size': store_data['Store_Size'], 'Store_Type': store_data['Store_Type'], 'Store_Location_City_Type': store_data['Store_Location_City_Type'] } input_data = pd.DataFrame([sample]) predicted_sale = model.predict(input_data)[0] predicted_sale = round(float(predicted_sale), 2) return jsonify({'Predicted Sales (in dollars)': predicted_sale}) except Exception as e: print("ERROR in /v1/sales:", str(e)) return jsonify({'error': str(e)}), 500 # Batch prediction endpoint @Superkart_sales_predictor_api.post('/v1/salesbatch') def predict_sales_price_batch(): try: file = request.files['file'] input_data = pd.read_csv(file) predicted_sale = model.predict(input_data).tolist() store_ids = input_data['Store_Id'].tolist() output_dict = dict(zip(store_ids, predicted_sale)) return jsonify(output_dict) except Exception as e: print("ERROR in /v1/salesbatch:", str(e)) return jsonify({'error': str(e)}), 500 # Run the app if __name__ == '__main__': Superkart_sales_predictor_api.run(debug=True)