File size: 1,210 Bytes
4991c1b
 
 
 
 
3c061e1
 
 
 
7afa047
 
 
 
 
 
4991c1b
 
 
 
 
 
 
 
 
3c061e1
 
 
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
44
45
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)