from flask import Flask, request, jsonify from transformers import pipeline app = Flask(__name__) labels = { "LABEL_0": "not_bullying", "LABEL_1": "bullying", } # Load the text classification pipeline with label mappings classifier = pipeline( "text-classification", model="Davephoenix/bert-bullying-detector", ) @app.route("/classify/text", methods=["POST"]) def classify_text(): data = request.get_json() if not data or "text" not in data: return jsonify({"error": "Missing 'text' in request body"}), 400 try: result = classifier(data["text"]) # Map the labels to their meanings for item in result: item["label"] = labels.get(item["label"], item["label"]) return jsonify(result) except Exception as e: return jsonify({"error": str(e)}), 500 @app.route("/classify/audio", methods=["POST"]) def classify_audio(): return jsonify({"error": "Audio classification not implemented yet"}), 501 @app.route("/classify/image", methods=["POST"]) def classify_image(): return jsonify({"error": "Image classification not implemented yet"}), 501 if __name__ == "__main__": app.run(host="0.0.0.0", port=7860)