vamshf commited on
Commit
e526624
·
verified ·
1 Parent(s): 898275e

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask,jsonify
2
+
3
+ # Initialize Flask app
4
+ superKart_Sales_forecast = Flask("SuperKart Sales Forecast")
5
+
6
+ # Load the serialized model
7
+ try:
8
+ loaded_model = joblib.load('tuned_random_forest_model.pkl')
9
+ print("Model loaded successfully!")
10
+ except Exception as e:
11
+ print(f"Error loading model: {e}")
12
+ loaded_model = None # Set model to None if loading fails
13
+
14
+
15
+ @superKart_Sales_forecast.route('/predict', methods=['POST'])
16
+ def predict():
17
+ if loaded_model is None:
18
+ return jsonify({'error': 'Model not loaded'}), 500
19
+
20
+ try:
21
+ # Get data from the request
22
+ data = request.get_json(force=True)
23
+ # Convert the incoming data to a pandas DataFrame
24
+ # Assuming the incoming data is a list of dictionaries, where each dictionary is a row
25
+ # The columns should match the features used during training (excluding the target)
26
+ input_df = pd.DataFrame(data)
27
+
28
+ # Ensure the columns are in the same order as the training data
29
+ # You might need to store the order of columns from X_train during training
30
+ # For now, assuming input_df columns match X_train columns
31
+ # A more robust solution would involve saving and loading the column order
32
+ # For demonstration, let's assume the column order is consistent
33
+
34
+ # Make predictions
35
+ predictions = loaded_model.predict(input_df)
36
+
37
+ # Convert predictions to a list and return as JSON
38
+ return jsonify(predictions.tolist())
39
+
40
+ except Exception as e:
41
+ return jsonify({'error': str(e)}), 400
42
+
43
+ # To run the Flask superKart_Sales_forecast (for local testing)
44
+ #if __name__ == '__main__':
45
+ # # This will run the server locally on port 5000
46
+ # # In a production environment, you would use a production-ready server like Gunicorn or uWSGI
47
+ # superKart_Sales_forecast.run(debug=True, host='0.0.0.0', port=5000)