| from flask import Flask, request, jsonify | |
| import joblib | |
| import numpy as np | |
| import pandas as pd | |
| # Initialize Flask app | |
| app = Flask(__name__) | |
| # Load the trained model (serialized earlier) | |
| model = joblib.load("final_gradient_boosting_model.pkl") # update filename if different | |
| def home(): | |
| return "SuperKart ML Model API is running ๐" | |
| # ----------------- Prediction Endpoint ----------------- | |
| def predict(): | |
| try: | |
| # Get JSON data from request | |
| data = request.get_json(force=True) | |
| # Convert to DataFrame (ensures compatibility with preprocessing pipeline) | |
| df = pd.DataFrame([data]) | |
| # Predict | |
| prediction = model.predict(df) | |
| # Return response | |
| return jsonify({ | |
| "input": data, | |
| "prediction": float(prediction[0]) | |
| }) | |
| except Exception as e: | |
| return jsonify({"error": str(e)}), 400 | |
| if __name__ == "__main__": | |
| app.run(debug=True) | |