codingcoolfun9ed commited on
Commit
e1f7e22
·
verified ·
1 Parent(s): c985b4c

Update api/app.py

Browse files
Files changed (1) hide show
  1. api/app.py +15 -3
api/app.py CHANGED
@@ -4,11 +4,18 @@ 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
8
 
9
  app = Flask(__name__)
10
  CORS(app)
11
 
 
 
 
 
 
 
 
12
  @app.route('/health', methods=['GET'])
13
  def health():
14
  return jsonify({"status": "ok"}), 200
@@ -31,10 +38,15 @@ def predict():
31
 
32
  result = predict_review(reviewText)
33
 
 
 
 
34
  return jsonify({
35
  "prediction": result['prediction'],
36
  "confidence": result['confidence'],
37
- "is_fake": result['is_fake']
 
 
38
  }), 200
39
 
40
  except Exception as e:
@@ -42,4 +54,4 @@ def predict():
42
 
43
  if __name__ == '__main__':
44
  print("starting api server")
45
- app.run(host='0.0.0.0', port=5000, debug=False)
 
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, load_resources
8
 
9
  app = Flask(__name__)
10
  CORS(app)
11
 
12
+ print("loading models on startup...")
13
+ try:
14
+ load_resources()
15
+ print("models loaded")
16
+ except Exception as e:
17
+ print(f"couldnt preload models: {e}")
18
+
19
  @app.route('/health', methods=['GET'])
20
  def health():
21
  return jsonify({"status": "ok"}), 200
 
38
 
39
  result = predict_review(reviewText)
40
 
41
+ if 'error' in result:
42
+ return jsonify({"error": result['error']}), 400
43
+
44
  return jsonify({
45
  "prediction": result['prediction'],
46
  "confidence": result['confidence'],
47
+ "is_fake": result['is_fake'],
48
+ "length_category": result.get('length_category'),
49
+ "token_count": result.get('token_count')
50
  }), 200
51
 
52
  except Exception as e:
 
54
 
55
  if __name__ == '__main__':
56
  print("starting api server")
57
+ app.run(host='0.0.0.0', port=5000, debug=False)