File size: 1,662 Bytes
c985b4c
 
487fb9f
 
 
 
c985b4c
02c45ef
c985b4c
 
02c45ef
c985b4c
 
 
02c45ef
c985b4c
 
02c45ef
c985b4c
 
 
02c45ef
c985b4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
02c45ef
487fb9f
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from flask import Flask, request, jsonify
from flask_cors import CORS
import os
import sys

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from api.predict import predict_review, load_resources

app = Flask(__name__)
CORS(app)

print("loading models on startup...")
try:
    load_resources()
    print("models loaded")
except Exception as e:
    print(f"couldnt preload models: {e}")

@app.route('/health', methods=['GET'])
def health():
    return jsonify({"status": "ok"}), 200

@app.route('/predict', methods=['POST'])
def predict():
    try:
        data = request.get_json()
        
        if not data or 'text' not in data:
            return jsonify({"error": "missing 'text' field"}), 400
        
        reviewText = data['text']
        
        if not isinstance(reviewText, str):
            return jsonify({"error": "'text' must be a string"}), 400
        
        if len(reviewText.strip()) == 0:
            return jsonify({"error": "text cannot be empty"}), 400
        
        result = predict_review(reviewText)
        
        if 'error' in result:
            return jsonify({"error": result['error']}), 400
        
        return jsonify({
            "prediction": result['prediction'],
            "confidence": result['confidence'],
            "is_fake": result['is_fake'],
            "length_category": result.get('length_category'),
            "token_count": result.get('token_count')
        }), 200
        
    except Exception as e:
        return jsonify({"error": str(e)}), 500

if __name__ == '__main__':
    print("starting api server")
    app.run(host='0.0.0.0', port=7860, debug=False)