from flask import Flask, request, jsonify import joblib import pandas as pd import numpy as np # Import numpy to handle numpy data types # Load the saved model pipeline model = joblib.load("SuperKart_prediction_model_v1_0.joblib") app = Flask(__name__) @app.route('/') def home(): return "SuperKart Sales Prediction API" @app.route('/predict', methods=['POST']) def predict(): try: # Get the data from the request data = request.get_json(force=True) # Convert the data to a pandas DataFrame # Ensure the column order matches the training data used for the pipeline # The keys in the input JSON should match the original column names in X_train input_data = pd.DataFrame([data]) # Make prediction using the loaded model pipeline prediction = model.predict(input_data) # Convert the prediction (which is a NumPy float) to a standard Python float predicted_value = float(prediction[0]) # Return the prediction as JSON return jsonify({'prediction': predicted_value}) except Exception as e: return jsonify({'error': str(e)}) if __name__ == '__main__': # You can run this locally for testing # app.run(debug=True) pass # This is for deployment, Flask will be run by the serving environment