righthook75 commited on
Commit
1a09b17
·
verified ·
1 Parent(s): cdd497b

Commit - 2025-12-10 20:22:17

Browse files
Files changed (1) hide show
  1. app.py +10 -17
app.py CHANGED
@@ -6,24 +6,17 @@ from flask import Flask, request, jsonify # For creating the Flask API
6
  from flask_cors import CORS
7
 
8
  superkart_api = Flask(__name__)
9
- # Enable CORS for all domains with explicit configuration
10
- CORS(superkart_api,
11
- resources={r"/*": {
12
- "origins": "*",
13
- "methods": ["GET", "POST", "OPTIONS"],
14
- "allow_headers": ["Content-Type", "Authorization"]
15
- }})
16
  model = joblib.load('superkart.joblib')
17
 
18
- @superkart_api.route('/', methods=['GET'])
 
19
  def home():
20
  return "Welcome to the SuperKart Sales Prediction API!"
21
 
22
- @superkart_api.route('/health', methods=['GET'])
23
- def health():
24
- return jsonify({'status': 'healthy', 'model_loaded': model is not None})
25
-
26
- @superkart_api.route('/predict', methods=['POST', 'OPTIONS'])
27
  def predict():
28
  """
29
  API endpoint to predict sales using the trained model.
@@ -45,7 +38,8 @@ def predict():
45
  except Exception as e:
46
  return jsonify({'error': str(e)}), 400
47
 
48
- @superkart_api.route('/predict/batch', methods=['POST', 'OPTIONS'])
 
49
  def predict_batch():
50
  """
51
  API endpoint to predict sales for a batch of inputs using the trained model.
@@ -66,7 +60,6 @@ def predict_batch():
66
  except Exception as e:
67
  return jsonify({'error': str(e)}), 400
68
 
 
69
  if __name__ == '__main__':
70
- import os
71
- port = int(os.environ.get('PORT', 7860)) # HF Spaces uses port 7860 by default
72
- superkart_api.run(host='0.0.0.0', port=port, debug=False)
 
6
  from flask_cors import CORS
7
 
8
  superkart_api = Flask(__name__)
9
+ CORS(superkart_api)
10
+ # Load the trained model at the start of the application
 
 
 
 
 
11
  model = joblib.load('superkart.joblib')
12
 
13
+ # Define an endpoint for the home route. This method tells flask that when a user goes to the root URL, this function should be called.
14
+ @superkart_api.get('/')
15
  def home():
16
  return "Welcome to the SuperKart Sales Prediction API!"
17
 
18
+ # A method to take a single prediction request in JSON format and return the results in JSON
19
+ @superkart_api.post('/predict')
 
 
 
20
  def predict():
21
  """
22
  API endpoint to predict sales using the trained model.
 
38
  except Exception as e:
39
  return jsonify({'error': str(e)}), 400
40
 
41
+ # A method to take a batch prediction request in CSV file and return the results in JSON
42
+ @superkart_api.post('/predict/batch')
43
  def predict_batch():
44
  """
45
  API endpoint to predict sales for a batch of inputs using the trained model.
 
60
  except Exception as e:
61
  return jsonify({'error': str(e)}), 400
62
 
63
+ # We can run this app locally with just the python command. This block enables that behavior.
64
  if __name__ == '__main__':
65
+ superkart_api.run(host='0.0.0.0', port=5001, debug=True)