Spaces:
Running
Running
Commit
·
8823b14
1
Parent(s):
44be10f
Add Hugging Face Space config
Browse files- README.md +6 -5
- app.py +23 -0
- requirements.txt +4 -0
README.md
CHANGED
|
@@ -1,10 +1,11 @@
|
|
| 1 |
---
|
| 2 |
title: ToxiShield
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: docker
|
|
|
|
| 7 |
pinned: false
|
| 8 |
---
|
| 9 |
-
|
| 10 |
-
|
|
|
|
| 1 |
---
|
| 2 |
title: ToxiShield
|
| 3 |
+
emoji: 🛡️
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: purple
|
| 6 |
sdk: docker
|
| 7 |
+
app_file: app.py
|
| 8 |
pinned: false
|
| 9 |
---
|
| 10 |
+
# ToxiShield
|
| 11 |
+
A toxicity detection backend API built using Flask and Hugging Face Transformers.
|
app.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
from flask_cors import CORS
|
| 3 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer, TextClassificationPipeline
|
| 4 |
+
|
| 5 |
+
app = Flask(__name__)
|
| 6 |
+
CORS(app)
|
| 7 |
+
|
| 8 |
+
model_path = "textdetox/xlmr-large-toxicity-classifier"
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 10 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_path)
|
| 11 |
+
pipeline = TextClassificationPipeline(model=model, tokenizer=tokenizer)
|
| 12 |
+
|
| 13 |
+
@app.route("/predict", methods=["POST"])
|
| 14 |
+
def predict():
|
| 15 |
+
data = request.get_json()
|
| 16 |
+
text = data.get("text", "")
|
| 17 |
+
if not text:
|
| 18 |
+
return jsonify({"error": "No text provided"}), 400
|
| 19 |
+
result = pipeline(text)[0]
|
| 20 |
+
return jsonify({"label": result["label"]})
|
| 21 |
+
|
| 22 |
+
if __name__ == "__main__":
|
| 23 |
+
app.run(host="0.0.0.0", port=7860)
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
flask
|
| 2 |
+
flask-cors
|
| 3 |
+
transformers
|
| 4 |
+
torch
|