| # app.py — SuperKart Sales Forecaster Backend | |
| from flask import Flask, request, jsonify | |
| import joblib | |
| import numpy as np | |
| import pandas as pd | |
| # Initialize Flask app | |
| app = Flask(__name__) | |
| # === Load model === | |
| model = joblib.load("tuned_xgb_sales_forecaster.pkl") | |
| def home(): | |
| return jsonify({"message": "SuperKart Sales Forecasting API is running!"}) | |
| def predict(): | |
| try: | |
| # Expecting JSON input with "features" list | |
| data = request.get_json() | |
| features = np.array(data["features"]).reshape(1, -1) | |
| prediction_log = model.predict(features)[0] | |
| prediction_original = float(np.expm1(prediction_log)) | |
| return jsonify({ | |
| "predicted_sales": prediction_original, | |
| "status": "success" | |
| }) | |
| except Exception as e: | |
| return jsonify({ | |
| "error": str(e), | |
| "status": "failed" | |
| }), 400 | |
| if __name__ == "__main__": | |
| app.run(host="0.0.0.0", port=7860) | |