import os import joblib from flask import Flask, request, jsonify import pandas as pd import numpy as np import warnings warnings.filterwarnings("ignore") # Define the path to the serialised model MODEL_PATH = "/content/Backend_files/SuperKart_Sales_Prediction_Model.joblib" # Load the trained model pipeline try: model_pipeline = joblib.load(MODEL_PATH) print(f"Model loaded successfully from {MODEL_PATH}") except Exception as e: model_pipeline = None print(f"Error loading model: {e}") # Initialize the Flask application app = Flask(__name__) # Define a route for the home page @app.route("/", methods=["GET"]) def home(): return "Welcome to the SuperKart Sales Prediction App!" # Define an endpoint for making predictions @app.route("/predict", methods=["POST"]) def predict(): if model_pipeline is None: return jsonify({"error": "Model not loaded"}), 500 try: # Get JSON data from the request data = request.get_json() if not data: return jsonify({"error": "No data provided"}), 400 # Extract features from the JSON data input_df = pd.DataFrame([data]) prediction = model_pipeline.predict(input_df) return jsonify({"prediction": prediction.tolist()}) except Exception as e: return jsonify({"error": f'Error during prediction: {e}'}), 500 if __name__ == "__main__": # Correct indentation port = int(os.environ.get("PORT", 5000)) app.run(host="0.0.0.0", port=5000, debug=True)