Spaces:
Runtime error
Runtime error
| import joblib | |
| import pandas as pd | |
| from flask import Flask, request, jsonify | |
| # Initialize Flask app with a clear name | |
| app = Flask("SuperKart Sales Forecaster") | |
| # Load the trained model | |
| model = joblib.load('model.joblib') | |
| # Define a route for the home page (Health Check) | |
| def home(): | |
| return "Welcome to the SuperKart Sales Forecasting API!" | |
| # Define the prediction endpoint | |
| def predict(): | |
| try: | |
| # Get JSON data from the request | |
| data = request.get_json() | |
| # Convert input to pandas DataFrame | |
| if isinstance(data, dict): | |
| df = pd.DataFrame([data]) | |
| else: | |
| df = pd.DataFrame(data) | |
| # Make prediction | |
| prediction = model.predict(df) | |
| # Return the result as JSON | |
| return jsonify({ | |
| 'status': 'success', | |
| 'prediction': prediction.tolist() | |
| }) | |
| except Exception as e: | |
| return jsonify({ | |
| 'status': 'error', | |
| 'message': str(e) | |
| }) | |
| # Run the app on port 7860 for Hugging Face Spaces | |
| if __name__ == '__main__': | |
| app.run(host='0.0.0.0', port=7860) | |