File size: 1,209 Bytes
7006533
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

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
@app.route('/', methods=['GET'])
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
@app.route('/predict', methods=['POST'])
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)