codingcoolfun9ed commited on
Commit
5e99838
·
verified ·
1 Parent(s): e8622f5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -45
app.py CHANGED
@@ -1,57 +1,44 @@
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, 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
22
-
23
- @app.route('/predict', methods=['POST'])
24
- def predict():
25
  try:
26
- data = request.get_json()
27
-
28
- if not data or 'text' not in data:
29
- return jsonify({"error": "missing 'text' field"}), 400
 
 
 
30
 
31
- reviewText = data['text']
32
 
33
- if not isinstance(reviewText, str):
34
- return jsonify({"error": "'text' must be a string"}), 400
35
-
36
- if len(reviewText.strip()) == 0:
37
- return jsonify({"error": "text cannot be empty"}), 400
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:
53
- return jsonify({"error": str(e)}), 500
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
 
55
- if __name__ == '__main__':
56
- print("starting api server")
57
- app.run(host='0.0.0.0', port=7860, debug=False)
 
1
+ import gradio as gr
 
 
2
  import sys
3
+ import os
4
 
5
  sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
6
+ from api.predict import predict_review
 
 
 
7
 
8
+ def predict_api(text):
 
 
 
 
 
 
 
 
 
 
 
 
9
  try:
10
+ if not text or len(text.strip()) == 0:
11
+ return {
12
+ "error": "text cannot be empty",
13
+ "prediction": None,
14
+ "confidence": 0.0,
15
+ "is_fake": False
16
+ }
17
 
18
+ result = predict_review(text)
19
 
20
+ return {
 
 
 
 
 
 
 
 
 
 
 
21
  "prediction": result['prediction'],
22
  "confidence": result['confidence'],
23
+ "is_fake": result['is_fake']
24
+ }
 
 
25
 
26
  except Exception as e:
27
+ return {
28
+ "error": str(e),
29
+ "prediction": None,
30
+ "confidence": 0.0,
31
+ "is_fake": False
32
+ }
33
+
34
+ demo = gr.Interface(
35
+ fn=predict_api,
36
+ inputs=gr.Textbox(label="review text", lines=5, placeholder="enter review text here..."),
37
+ outputs=gr.JSON(label="result"),
38
+ title="sentinelcheck api",
39
+ description="check if review is fake or real",
40
+ api_name="predict"
41
+ )
42
 
43
+ if __name__ == "__main__":
44
+ demo.launch()