Spaces:
Sleeping
Sleeping
| import os | |
| from flask import Flask, request, jsonify | |
| import joblib | |
| import pandas as pd | |
| # Load the trained RandomForest pipeline | |
| model = joblib.load('final_model_pipeline.pkl') | |
| app = Flask(__name__) | |
| # Health check / welcome endpoint | |
| def home(): | |
| return jsonify({ | |
| "status": "success", | |
| "message": "Welcome to SuperKart Sales Forecasting API by Satyarth! \nSubmit your data as JSON to the /predict endpoint and get instant, AI-powered sales predictions. Let's make smart business moves together!" | |
| }), 200 | |
| # Prediction endpoint | |
| def predict(): | |
| try: | |
| data = request.get_json(force=True) | |
| df = pd.DataFrame([data]) | |
| prediction = model.predict(df)[0] | |
| return jsonify({ | |
| "status": "success", | |
| "Predicted_Sales": round(float(prediction), 2) | |
| }), 200 | |
| except Exception as e: | |
| return jsonify({ | |
| "status": "error", | |
| "message": str(e) | |
| }), 400 | |
| if __name__ == '__main__': | |
| # Use the PORT environment variable set by Hugging Face Spaces | |
| port = int(os.environ.get('PORT', 7860)) | |
| app.run(host='0.0.0.0', port=port) | |