File size: 1,881 Bytes
e526624
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
42
43
44
45
46
47
48
from flask import Flask,jsonify

# Initialize Flask app
superKart_Sales_forecast = Flask("SuperKart Sales Forecast")

# Load the serialized model
try:
    loaded_model = joblib.load('tuned_random_forest_model.pkl')
    print("Model loaded successfully!")
except Exception as e:
    print(f"Error loading model: {e}")
    loaded_model = None # Set model to None if loading fails


@superKart_Sales_forecast.route('/predict', methods=['POST'])
def predict():
    if loaded_model is None:
        return jsonify({'error': 'Model not loaded'}), 500

    try:
        # Get data from the request
        data = request.get_json(force=True)
        # Convert the incoming data to a pandas DataFrame
        # Assuming the incoming data is a list of dictionaries, where each dictionary is a row
        # The columns should match the features used during training (excluding the target)
        input_df = pd.DataFrame(data)

        # Ensure the columns are in the same order as the training data
        # You might need to store the order of columns from X_train during training
        # For now, assuming input_df columns match X_train columns
        # A more robust solution would involve saving and loading the column order
        # For demonstration, let's assume the column order is consistent

        # Make predictions
        predictions = loaded_model.predict(input_df)

        # Convert predictions to a list and return as JSON
        return jsonify(predictions.tolist())

    except Exception as e:
        return jsonify({'error': str(e)}), 400

# To run the Flask superKart_Sales_forecast (for local testing)
#if __name__ == '__main__':
#     # This will run the server locally on port 5000
#     # In a production environment, you would use a production-ready server like Gunicorn or uWSGI
#      superKart_Sales_forecast.run(debug=True, host='0.0.0.0', port=5000)