Spaces:
Sleeping
Sleeping
Commit
·
4991c1b
1
Parent(s):
781a663
Add Flask app for bullying classification
Browse files- Dockerfile +20 -0
- README.md +33 -5
- app.py +33 -0
- requirements.txt +3 -0
Dockerfile
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.9-slim
|
2 |
+
|
3 |
+
# Create a non-root user
|
4 |
+
RUN useradd -m -u 1000 user
|
5 |
+
USER user
|
6 |
+
ENV HOME=/home/user
|
7 |
+
WORKDIR $HOME/app
|
8 |
+
|
9 |
+
# Copy and install dependencies
|
10 |
+
COPY --chown=user requirements.txt requirements.txt
|
11 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
12 |
+
|
13 |
+
# Copy application files
|
14 |
+
COPY --chown=user . .
|
15 |
+
|
16 |
+
# Expose the application port
|
17 |
+
EXPOSE 7860
|
18 |
+
|
19 |
+
# Run the application
|
20 |
+
CMD ["python", "app.py"]
|
README.md
CHANGED
@@ -1,10 +1,38 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: docker
|
|
|
7 |
pinned: false
|
8 |
---
|
9 |
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
+
title: Bullying Classifier API
|
3 |
+
emoji: 🛡️
|
4 |
+
colorFrom: indigo
|
5 |
+
colorTo: blue
|
6 |
sdk: docker
|
7 |
+
app_port: 7860
|
8 |
pinned: false
|
9 |
---
|
10 |
|
11 |
+
# Bullying Classifier API
|
12 |
+
|
13 |
+
This Flask application provides a RESTful API for classifying text inputs as bullying or not using a BERT-based model.
|
14 |
+
|
15 |
+
## Endpoints
|
16 |
+
|
17 |
+
- `POST /classify/text` – Classifies text as bullying or not.
|
18 |
+
- `POST /classify/audio` – Placeholder for audio classification.
|
19 |
+
- `POST /classify/image` – Placeholder for image classification.
|
20 |
+
|
21 |
+
## Usage
|
22 |
+
|
23 |
+
Send a POST request to `/classify/text` with a JSON body:
|
24 |
+
|
25 |
+
```json
|
26 |
+
{
|
27 |
+
"text": "your text here"
|
28 |
+
}
|
29 |
+
```
|
30 |
+
|
31 |
+
## Docker
|
32 |
+
|
33 |
+
Build and run the container:
|
34 |
+
|
35 |
+
```sh
|
36 |
+
docker build -t bullying-classifier .
|
37 |
+
docker run -p 7860:7860 bullying-classifier
|
38 |
+
```
|
app.py
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, jsonify
|
2 |
+
from transformers import pipeline
|
3 |
+
|
4 |
+
app = Flask(__name__)
|
5 |
+
|
6 |
+
# Load the text classification pipeline
|
7 |
+
classifier = pipeline("text-classification", model="Davephoenix/bert-bullying-detector")
|
8 |
+
|
9 |
+
|
10 |
+
@app.route("/classify/text", methods=["POST"])
|
11 |
+
def classify_text():
|
12 |
+
data = request.get_json()
|
13 |
+
if not data or "text" not in data:
|
14 |
+
return jsonify({"error": "Missing 'text' in request body"}), 400
|
15 |
+
try:
|
16 |
+
result = classifier(data["text"])
|
17 |
+
return jsonify(result)
|
18 |
+
except Exception as e:
|
19 |
+
return jsonify({"error": str(e)}), 500
|
20 |
+
|
21 |
+
|
22 |
+
@app.route("/classify/audio", methods=["POST"])
|
23 |
+
def classify_audio():
|
24 |
+
return jsonify({"error": "Audio classification not implemented yet"}), 501
|
25 |
+
|
26 |
+
|
27 |
+
@app.route("/classify/image", methods=["POST"])
|
28 |
+
def classify_image():
|
29 |
+
return jsonify({"error": "Image classification not implemented yet"}), 501
|
30 |
+
|
31 |
+
|
32 |
+
if __name__ == "__main__":
|
33 |
+
app.run(host="0.0.0.0", port=7860)
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
flask
|
2 |
+
transformers
|
3 |
+
torch
|