Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 4 |
+
|
| 5 |
+
MODEL_ID = "gorkem371/toxicity-classifier-xlmr-base-v3"
|
| 6 |
+
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 8 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL_ID)
|
| 9 |
+
model.eval()
|
| 10 |
+
|
| 11 |
+
LABELS = ["Not Toxic", "Toxic"]
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def classify(text: str) -> dict:
|
| 15 |
+
"""Return probability distribution over Not Toxic / Toxic."""
|
| 16 |
+
if not text or not text.strip():
|
| 17 |
+
return {label: 0.0 for label in LABELS}
|
| 18 |
+
|
| 19 |
+
inputs = tokenizer(
|
| 20 |
+
text,
|
| 21 |
+
return_tensors="pt",
|
| 22 |
+
truncation=True,
|
| 23 |
+
max_length=256,
|
| 24 |
+
padding=True,
|
| 25 |
+
)
|
| 26 |
+
with torch.no_grad():
|
| 27 |
+
logits = model(**inputs).logits
|
| 28 |
+
probs = torch.softmax(logits, dim=-1)[0]
|
| 29 |
+
return {LABELS[i]: float(probs[i]) for i in range(len(LABELS))}
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
examples = [
|
| 33 |
+
# English
|
| 34 |
+
["You are such a wonderful person, keep up the great work!"],
|
| 35 |
+
["I hope you suffer and never find happiness in your life."],
|
| 36 |
+
["The weather is beautiful today, perfect for a walk."],
|
| 37 |
+
# Turkish
|
| 38 |
+
["Harika bir insansınız, paylaşımlarınız çok değerli."],
|
| 39 |
+
["Seni gördüğüm yerde döverim, haddini bil."],
|
| 40 |
+
["Bugün hava çok güzel, yürüyüşe çıkalım."],
|
| 41 |
+
# Arabic
|
| 42 |
+
["أنت إنسان رائع ومشاركاتك مفيدة جداً."],
|
| 43 |
+
["اخرس يا حيوان، لا أحد يهتم برأيك."],
|
| 44 |
+
["اليوم طقس جميل، فلنخرج للتنزه."],
|
| 45 |
+
]
|
| 46 |
+
|
| 47 |
+
demo = gr.Interface(
|
| 48 |
+
fn=classify,
|
| 49 |
+
inputs=gr.Textbox(
|
| 50 |
+
label="Enter text",
|
| 51 |
+
placeholder="Type or paste text in Turkish, Arabic, or English...",
|
| 52 |
+
lines=3,
|
| 53 |
+
),
|
| 54 |
+
outputs=gr.Label(label="Prediction"),
|
| 55 |
+
title="Multilingual Toxicity Classifier",
|
| 56 |
+
description=(
|
| 57 |
+
"Binary toxicity classifier for Turkish, Arabic, and English. "
|
| 58 |
+
"Built with XLM-RoBERTa-base, fine-tuned on 120K balanced samples."
|
| 59 |
+
),
|
| 60 |
+
examples=examples,
|
| 61 |
+
theme=gr.themes.Soft(),
|
| 62 |
+
allow_flagging="never",
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
if __name__ == "__main__":
|
| 66 |
+
demo.launch()
|