codingcoolfun9ed commited on
Commit
19782f6
·
verified ·
1 Parent(s): 5ed6f8f

completely forgot about gradio

Browse files
Files changed (1) hide show
  1. app.py +44 -49
app.py CHANGED
@@ -1,65 +1,60 @@
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():
22
  try:
23
- data = request.get_json()
24
-
25
- if not data or 'text' not in data:
26
- return jsonify({"error": "missing 'text' field"}), 400
27
-
28
- reviewText = data['text']
29
-
30
- if not isinstance(reviewText, str):
31
- return jsonify({"error": "'text' must be a string"}), 400
32
 
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":
46
- return jsonify(result), 400
 
 
 
 
 
 
 
47
 
48
- return jsonify({
49
- "prediction": result['prediction'],
50
- "confidence": result['confidence'],
51
- "is_fake": result['is_fake'],
52
- "model_agreement": result['model_agreement'],
53
- "fake_probability": result['fake_probability'],
54
- "genuine_probability": result['genuine_probability'],
55
- "length_category": result['length_category'],
56
- "token_count": result['token_count']
57
- }), 200
58
 
59
  except Exception as e:
60
- return jsonify({"error": str(e)}), 500
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)
 
1
+ import gradio as gr
 
2
  import os
3
  import sys
4
 
5
  sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
6
  from api.predict import predict_review, models_loaded
7
 
8
+ def analyze_review(text):
9
+ if not text or len(text.strip()) == 0:
10
+ return "error: please enter some text"
11
+
12
+ if not models_loaded:
13
+ return "models are loading for the first time, this will take 20-30 minutes. please wait..."
14
+
 
 
 
 
 
 
15
  try:
16
+ result = predict_review(text)
 
 
 
 
 
 
 
 
17
 
18
+ if "error" in result and result["prediction"] == "error":
19
+ return f"error: {result['error']}"
20
 
21
+ prediction = result['prediction']
22
+ confidence = result['confidence']
23
+ is_fake = result['is_fake']
 
 
 
24
 
25
+ status = "FAKE" if is_fake else "GENUINE"
26
 
27
+ output = f"""prediction: {status}
28
+ confidence: {confidence:.2%}
29
+
30
+ fake probability: {result['fake_probability']:.2%}
31
+ genuine probability: {result['genuine_probability']:.2%}
32
+
33
+ model agreement: {result['model_agreement']:.1f}%
34
+ length category: {result['length_category']}
35
+ token count: {result['token_count']}"""
36
 
37
+ return output
 
 
 
 
 
 
 
 
 
38
 
39
  except Exception as e:
40
+ return f"error: {str(e)}"
41
+
42
+ demo = gr.Interface(
43
+ fn=analyze_review,
44
+ inputs=gr.Textbox(
45
+ lines=5,
46
+ placeholder="paste review text here...",
47
+ label="review text"
48
+ ),
49
+ outputs=gr.Textbox(
50
+ lines=10,
51
+ label="analysis"
52
+ ),
53
+ title="review classifier",
54
+ description="ensemble model for detecting fake reviews"
55
+ )
56
 
57
+ if __name__ == "__main__":
58
+ print("starting gradio interface", flush=True)
59
  print("models will load on first prediction request", flush=True)
60
+ demo.launch(server_name="0.0.0.0", server_port=7860)