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 app = Flask(__name__) CORS(app) @app.route('/health', methods=['GET']) def health(): return jsonify({"status": "ok", "model": "ensemble-v1"}), 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 and result["prediction"] == "error": return jsonify(result), 400 return jsonify({ "prediction": result['prediction'], "confidence": result['confidence'], "is_fake": result['is_fake'], "model_agreement": result['model_agreement'], "fake_probability": result['fake_probability'], "genuine_probability": result['genuine_probability'], "length_category": result['length_category'], "token_count": result['token_count'] }), 200 except Exception as e: return jsonify({"error": str(e)}), 500 if __name__ == '__main__': print("starting ensemble api server", flush=True) print("loading models on first request...", flush=True) app.run(host='0.0.0.0', port=5000, debug=False)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 app = Flask(__name__) CORS(app) @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) return jsonify({ "prediction": result['prediction'], "confidence": result['confidence'], "is_fake": result['is_fake'], "model_agreement": result['model_agreement'], "fake_probability": result['fake_probability'], "genuine_probability": result['genuine_probability'], "length_category": result['length_category'], "token_count": result['token_count'] }), 200 except Exception as e: return jsonify({"error": str(e)}), 500 if __name__ == '__main__': print("starting ensemble api server") print("loading models on first request...") app.run(host='0.0.0.0', port=5000, debug=False)