codingcoolfun9ed commited on
Commit
5ed6f8f
·
verified ·
1 Parent(s): 528529c

telling the user to wait a bit

Browse files

the models load whenever the first request is made (20-30 minutes) but subsequent requests will work much faster; can be fixed with a pro subscription.

Files changed (1) hide show
  1. app.py +16 -5
app.py CHANGED
@@ -1,17 +1,21 @@
1
  from flask import Flask, request, jsonify
2
  from flask_cors import CORS
3
- import sys
4
  import os
 
5
 
6
  sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
7
- from api.predict import predict_review
8
 
9
  app = Flask(__name__)
10
  CORS(app)
11
 
12
  @app.route('/health', methods=['GET'])
13
  def health():
14
- return jsonify({"status": "ok", "model": "ensemble-v1"}), 200
 
 
 
 
15
 
16
  @app.route('/predict', methods=['POST'])
17
  def predict():
@@ -29,6 +33,13 @@ def predict():
29
  if len(reviewText.strip()) == 0:
30
  return jsonify({"error": "text cannot be empty"}), 400
31
 
 
 
 
 
 
 
 
32
  result = predict_review(reviewText)
33
 
34
  if "error" in result and result["prediction"] == "error":
@@ -50,5 +61,5 @@ def predict():
50
 
51
  if __name__ == '__main__':
52
  print("starting ensemble api server", flush=True)
53
- print("loading models on first request...", flush=True)
54
- app.run(host='0.0.0.0', port=5000, debug=False)
 
1
  from flask import Flask, request, jsonify
2
  from flask_cors import CORS
 
3
  import os
4
+ import sys
5
 
6
  sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
7
+ from api.predict import predict_review, models_loaded
8
 
9
  app = Flask(__name__)
10
  CORS(app)
11
 
12
  @app.route('/health', methods=['GET'])
13
  def health():
14
+ return jsonify({
15
+ "status": "ok",
16
+ "model": "ensemble-v1",
17
+ "models_loaded": models_loaded
18
+ }), 200
19
 
20
  @app.route('/predict', methods=['POST'])
21
  def predict():
 
33
  if len(reviewText.strip()) == 0:
34
  return jsonify({"error": "text cannot be empty"}), 400
35
 
36
+ if not models_loaded:
37
+ return jsonify({
38
+ "status": "loading",
39
+ "message": "models are loading for the first time, this will take 20-30 minutes. please wait...",
40
+ "models_loaded": False
41
+ }), 202
42
+
43
  result = predict_review(reviewText)
44
 
45
  if "error" in result and result["prediction"] == "error":
 
61
 
62
  if __name__ == '__main__':
63
  print("starting ensemble api server", flush=True)
64
+ print("models will load on first prediction request", flush=True)
65
+ app.run(host='0.0.0.0', port=7860, debug=False)