# Import necessary libraries from flask import Flask, request, jsonify import joblib import pandas as pd import numpy as np # Initialize the Flask application api = Flask("Superkart Sales Predictor") # Load the trained machine learning model model = joblib.load('superkart_prediction_model_v1_0.joblib') # Define a route for the home page (GET request) # Health check (important for deployment) @api.route("/", methods=["GET"]) def health(): return "SuperKart Backend is running" @api.route("/predict", methods=["POST"]) def predict(): try: data = request.get_json(force=True) sample = { "Product_Weight": data["Product_Weight"][0], "Product_Sugar_Content": data["Product_Sugar_Content"][0], "Product_Allocated_Area": data["Product_Allocated_Area"][0], "Product_Type": data["Product_Type"][0], "Product_MRP": data["Product_MRP"][0], "Store_Establishment_Year": data["Store_Establishment_Year"][0], "Store_Size": data["Store_Size"][0], "Store_Location_City_Type": data["Store_Location_City_Type"][0], "Store_Type": data["Store_Type"][0] } query_df = pd.DataFrame([sample]) prediction = model.predict(query_df).tolist() return jsonify({"predictions": prediction}) except Exception as e: return jsonify({"error": str(e)}), 500 if __name__ == "__main__": app.api(debug=True)