HacksterT commited on
Commit
192f654
·
verified ·
1 Parent(s): 156a504

Update backend code

Browse files
Files changed (1) hide show
  1. app.py +40 -0
app.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)