Binary classifier for short Kazakh comments and reviews: 0 = no Kazakh profanity, 1 = profanity present. Russian obscenities used inside Kazakh text count as profanity (code-switching).
import json
import torch
from huggingface_hub import hf_hub_download
from transformers import AutoModelForSequenceClassification, AutoTokenizer
MODEL_ID = "karlygaw/kazakh-profanity-xlmr"
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
model = AutoModelForSequenceClassification.from_pretrained(MODEL_ID).eval()
config = json.load(open(hf_hub_download(MODEL_ID, "inference_config.json"))) # threshold 0.70, max_len 128
def predict(text):
enc = tokenizer(text, truncation=True, max_length=config["max_len"], return_tensors="pt")
with torch.no_grad():
prob = torch.softmax(model(**enc).logits, -1)[0, 1].item()
return {"label": int(prob >= config["threshold"]), "probability": round(prob, 3)}
print(predict("Өте жақсы қосымша, маған ұнады"))
inference_config.json stores the decision threshold (0.70, chosen on the validation set) and the maximum length (128 tokens). During training, phone numbers, links, e-mails and @-handles were replaced with <PHONE>, <URL>, <EMAIL>, <USER>; do the same for best results (see the notebooks in the GitHub repository).
Reviews from KazSAnDRA (ISSAI, CC BY 4.0). The dataset has star ratings but no profanity labels, so the profanity label was created by the author: candidates were found with a lexicon of profane roots, verified by manual audit (60/60 precision on a random sample), and negatives were sampled from the same domains, including hard negatives. 6,728 examples (2,239 positive, 4,489 negative): train 4,714 / validation 1,015 / test 999. All examples are real; none are synthetic. Labeling rules are described in the GitHub README.
Fine-tuned from FacebookAI/xlm-roberta-base: AdamW, learning rate 2e-5, weight decay 0.01, 10% warm-up, batch size 32, 3 epochs, maximum length 128, seed 42, one T4 GPU.
| Precision | Recall | F1 |
|---|---|---|
| 0.945 | 0.928 | 0.936 |
Confusion matrix (true rows, predicted columns): TN 648, FP 18, FN 24, TP 309.
FacebookAI/xlm-roberta-base