Spaces:
Sleeping
Sleeping
File size: 1,161 Bytes
4991c1b 7afa047 4991c1b |
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 |
from flask import Flask, request, jsonify
from transformers import pipeline
app = Flask(__name__)
# Define label mappings
id2label = {0: "not_bullying", 1: "bullying"}
label2id = {"not_bullying": 0, "bullying": 1}
# Load the text classification pipeline with label mappings
classifier = pipeline(
"text-classification",
model="Davephoenix/bert-bullying-detector",
id2label=id2label,
label2id=label2id,
)
@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"])
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)
|