Lokiiparihar commited on
Commit
24882c7
·
verified ·
1 Parent(s): 061baf1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -17
app.py CHANGED
@@ -1,37 +1,49 @@
1
  from flask import Flask, request, jsonify
 
2
  import joblib
3
- import numpy as np
4
- import traceback
5
 
6
- # Initialize Flask app
 
 
 
 
7
  app = Flask(__name__)
8
 
9
- # Load model
10
  model = joblib.load("tuned_gradient_boosting_model.pkl")
11
 
12
- # Home route
 
 
13
  @app.route('/')
 
14
  def home():
15
- return "Sales Prediction API is running."
16
 
17
- # Prediction route
 
 
 
 
18
  @app.route('/predict', methods=['POST'])
 
19
  def predict():
 
20
  try:
21
- # Parse JSON input
22
- data = request.get_json(force=True)
23
 
24
- # Extract features as a 2D array (expecting list or single sample dict)
25
- features = np.array(data['features']).reshape(1, -1)
 
26
 
27
- # Make prediction
28
- prediction = model.predict(features)
29
 
30
- # Return prediction as JSON
31
- return jsonify({"predicted_sales": prediction[0]})
32
 
33
  except Exception as e:
34
- return jsonify({"error": str(e), "trace": traceback.format_exc()})
 
 
 
 
35
 
36
  if __name__ == '__main__':
37
- app.run(debug=True, host='0.0.0.0', port=5000)
 
 
1
  from flask import Flask, request, jsonify
2
+
3
  import joblib
 
 
4
 
5
+ import pandas as pd
6
+
7
+
8
+
9
+
10
  app = Flask(__name__)
11
 
 
12
  model = joblib.load("tuned_gradient_boosting_model.pkl")
13
 
14
+
15
+
16
+
17
  @app.route('/')
18
+
19
  def home():
 
20
 
21
+ return "Welcome to SuperKart Sales Forecast API"
22
+
23
+
24
+
25
+
26
  @app.route('/predict', methods=['POST'])
27
+
28
  def predict():
29
+
30
  try:
 
 
31
 
32
+ data = request.get_json()
33
+
34
+ input_df = pd.DataFrame([data])
35
 
36
+ prediction = model.predict(input_df)[0]
 
37
 
38
+ return jsonify({'Predicted_Sales': round(float(prediction), 2)})
 
39
 
40
  except Exception as e:
41
+
42
+ return jsonify({'error': str(e)})
43
+
44
+
45
+
46
 
47
  if __name__ == '__main__':
48
+
49
+ app.run(host='0.0.0.0', port=7860)