# # Import necessary libraries # import numpy as np # import joblib # For loading the serialized model # import pandas as pd # For data manipulation # from flask import Flask, request, jsonify # For creating the Flask API # # Initialize the Flask application # superkart_sales_api = Flask("SuperKart Sales Predictor") # # Load the trained machine learning model # model = joblib.load("superkart_sales_prediction_model_v1_0.joblib") # # Define a route for the home page (GET request) # @superkart_sales_api.get('/') # def home(): # return "Welcome to the SuperKart Sales Prediction API!" # # Define an endpoint for single product prediction (POST request) # @superkart_sales_api.post('/v1/sales') # def predict_sales(): # product_data = request.get_json() # 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_Establishment_Year': product_data['Store_Establishment_Year'], # 'Store_Size': product_data['Store_Size'], # 'Store_Location_City_Type': product_data['Store_Location_City_Type'], # 'Store_Type': product_data['Store_Type'] # } # input_data = pd.DataFrame([sample]) # predicted_sales = model.predict(input_data)[0] # return jsonify({'Predicted Sales (in INR)': round(float(predicted_sales), 2)}) # # Define an endpoint for batch prediction (POST request) # @superkart_sales_api.post('/v1/salesbatch') # def predict_sales_batch(): # file = request.files['file'] # input_data = pd.read_csv(file) # predicted_sales = model.predict(input_data).tolist() # product_ids = input_data['Product_Id'].tolist() # Assuming 'Product_Id' column exists # output_dict = dict(zip(product_ids, [round(float(p), 2) for p in predicted_sales])) # return jsonify(output_dict) # # Run the Flask application in debug mode # if __name__ == '__main__': # superkart_sales_api.run(debug=True) from flask import Flask, request, jsonify from flask_cors import CORS import pandas as pd app = Flask(__name__) CORS(app) # Dummy model logic for demo purposes def predict_sales(features): # Replace this logic with your trained model return round( features.get("Product_MRP", 100) * 1.5 + features.get("Product_Allocated_Area", 0.1) * 2000 - features.get("Product_Weight", 0.0) * 5, 2 ) @app.route("/") def health(): return "✅ SuperKart Sales Prediction API is running." @app.route("/v1/sales", methods=["POST"]) def online_prediction(): data = request.get_json() prediction = predict_sales(data) return jsonify({"predicted_sales": prediction}) @app.route("/v1/salesbatch", methods=["POST"]) def batch_prediction(): if "file" not in request.files: return jsonify({"error": "CSV file missing"}), 400 file = request.files["file"] try: df = pd.read_csv(file) results = { str(i): predict_sales(row) for i, row in df.iterrows() } return jsonify(results) except Exception as e: return jsonify({"error": str(e)}), 500