new app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 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():
|
| 18 |
+
try:
|
| 19 |
+
data = request.get_json()
|
| 20 |
+
|
| 21 |
+
if not data or 'text' not in data:
|
| 22 |
+
return jsonify({"error": "missing 'text' field"}), 400
|
| 23 |
+
|
| 24 |
+
reviewText = data['text']
|
| 25 |
+
|
| 26 |
+
if not isinstance(reviewText, str):
|
| 27 |
+
return jsonify({"error": "'text' must be a string"}), 400
|
| 28 |
+
|
| 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":
|
| 35 |
+
return jsonify(result), 400
|
| 36 |
+
|
| 37 |
+
return jsonify({
|
| 38 |
+
"prediction": result['prediction'],
|
| 39 |
+
"confidence": result['confidence'],
|
| 40 |
+
"is_fake": result['is_fake'],
|
| 41 |
+
"model_agreement": result['model_agreement'],
|
| 42 |
+
"fake_probability": result['fake_probability'],
|
| 43 |
+
"genuine_probability": result['genuine_probability'],
|
| 44 |
+
"length_category": result['length_category'],
|
| 45 |
+
"token_count": result['token_count']
|
| 46 |
+
}), 200
|
| 47 |
+
|
| 48 |
+
except Exception as e:
|
| 49 |
+
return jsonify({"error": str(e)}), 500
|
| 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)from flask import Flask, request, jsonify
|
| 55 |
+
from flask_cors import CORS
|
| 56 |
+
import os
|
| 57 |
+
import sys
|
| 58 |
+
|
| 59 |
+
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 60 |
+
from api.predict import predict_review
|
| 61 |
+
|
| 62 |
+
app = Flask(__name__)
|
| 63 |
+
CORS(app)
|
| 64 |
+
|
| 65 |
+
@app.route('/health', methods=['GET'])
|
| 66 |
+
def health():
|
| 67 |
+
return jsonify({"status": "ok"}), 200
|
| 68 |
+
|
| 69 |
+
@app.route('/predict', methods=['POST'])
|
| 70 |
+
def predict():
|
| 71 |
+
try:
|
| 72 |
+
data = request.get_json()
|
| 73 |
+
|
| 74 |
+
if not data or 'text' not in data:
|
| 75 |
+
return jsonify({"error": "missing 'text' field"}), 400
|
| 76 |
+
|
| 77 |
+
reviewText = data['text']
|
| 78 |
+
|
| 79 |
+
if not isinstance(reviewText, str):
|
| 80 |
+
return jsonify({"error": "'text' must be a string"}), 400
|
| 81 |
+
|
| 82 |
+
if len(reviewText.strip()) == 0:
|
| 83 |
+
return jsonify({"error": "text cannot be empty"}), 400
|
| 84 |
+
|
| 85 |
+
result = predict_review(reviewText)
|
| 86 |
+
|
| 87 |
+
return jsonify({
|
| 88 |
+
"prediction": result['prediction'],
|
| 89 |
+
"confidence": result['confidence'],
|
| 90 |
+
"is_fake": result['is_fake'],
|
| 91 |
+
"model_agreement": result['model_agreement'],
|
| 92 |
+
"fake_probability": result['fake_probability'],
|
| 93 |
+
"genuine_probability": result['genuine_probability'],
|
| 94 |
+
"length_category": result['length_category'],
|
| 95 |
+
"token_count": result['token_count']
|
| 96 |
+
}), 200
|
| 97 |
+
|
| 98 |
+
except Exception as e:
|
| 99 |
+
return jsonify({"error": str(e)}), 500
|
| 100 |
+
|
| 101 |
+
if __name__ == '__main__':
|
| 102 |
+
print("starting ensemble api server")
|
| 103 |
+
print("loading models on first request...")
|
| 104 |
+
app.run(host='0.0.0.0', port=5000, debug=False)
|