HacksterT commited on
Commit
cd539e8
·
verified ·
1 Parent(s): 4ac14a0

Force upload app.py - 1754759382

Browse files
Files changed (1) hide show
  1. app.py +52 -40
app.py CHANGED
@@ -1,40 +1,52 @@
1
-
2
- from flask import Flask, request, jsonify
3
- from flask_cors import CORS
4
- import joblib
5
- import pandas as pd
6
- import numpy as np
7
- from datetime import datetime
8
-
9
- app = Flask(__name__)
10
- CORS(app)
11
-
12
- # Load model on startup
13
- deployment_package = joblib.load('models/superkart_deployment_package_20250809_122306.pkl')
14
- model = deployment_package['model']
15
-
16
- @app.route('/', methods=['GET'])
17
- def home():
18
- return jsonify({
19
- 'status': 'healthy',
20
- 'message': 'SuperKart Sales Forecasting API',
21
- 'model_loaded': True
22
- })
23
-
24
- @app.route('/predict', methods=['POST'])
25
- def predict_sales():
26
- try:
27
- data = request.get_json()
28
- input_df = pd.DataFrame([data])
29
- prediction = model.predict(input_df)
30
-
31
- return jsonify({
32
- 'prediction': float(prediction[0]),
33
- 'prediction_formatted': f"${prediction[0]:.2f}",
34
- 'status': 'success'
35
- })
36
- except Exception as e:
37
- return jsonify({'error': str(e), 'status': 'failed'}), 500
38
-
39
- if __name__ == '__main__':
40
- app.run(host='0.0.0.0', port=5000, debug=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, jsonify
2
+ from flask_cors import CORS
3
+ import joblib
4
+ import pandas as pd
5
+ import numpy as np
6
+ from datetime import datetime
7
+
8
+ app = Flask(__name__)
9
+ CORS(app)
10
+
11
+ # Load model components using joblib (class requirement)
12
+ print("Loading model components...")
13
+ try:
14
+ deployment_package = joblib.load('models/superkart_minimal.pkl')
15
+ model = deployment_package['model']
16
+ scaler = deployment_package['scaler']
17
+ metadata = deployment_package['metadata']
18
+ print("Model loaded successfully with joblib!")
19
+ except Exception as e:
20
+ print(f"Error loading model: {e}")
21
+ model = None
22
+ scaler = None
23
+ metadata = None
24
+
25
+ @app.route('/', methods=['GET'])
26
+ def home():
27
+ return jsonify({
28
+ 'status': 'healthy',
29
+ 'message': 'SuperKart Sales Forecasting API',
30
+ 'model_loaded': True
31
+ })
32
+
33
+ @app.route('/predict', methods=['POST'])
34
+ def predict_sales():
35
+ try:
36
+ data = request.get_json()
37
+ input_df = pd.DataFrame([data])
38
+
39
+ # Scale the input data (essential for correct predictions)
40
+ input_scaled = scaler.transform(input_df)
41
+ prediction = model.predict(input_scaled)
42
+
43
+ return jsonify({
44
+ 'prediction': float(prediction[0]),
45
+ 'prediction_formatted': f"${prediction[0]:.2f}",
46
+ 'status': 'success'
47
+ })
48
+ except Exception as e:
49
+ return jsonify({'error': str(e), 'status': 'failed'}), 500
50
+
51
+ if __name__ == '__main__':
52
+ app.run(host='0.0.0.0', port=5000, debug=True)