Views
No views yet
1from transformers import AutoTokenizer, AutoModelForSequenceClassification
2import torch
3
4# Load model and tokenizer
5tokenizer = AutoTokenizer.from_pretrained("archich/hate-speech-detector")
6model = AutoModelForSequenceClassification.from_pretrained("archich/hate-speech-detector")
7
8# Example text
9text = "Your text here"
10
11# Tokenize
12inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=256)
13
14# Predict
15with torch.no_grad():
16 outputs = model(**inputs)
17 probs = torch.softmax(outputs.logits, dim=1)
18 prediction = torch.argmax(probs, dim=1).item()
19
20labels = ["NOT_HATE_SPEECH", "HATE_SPEECH"]
21print(f"Prediction: {labels[prediction]} ({probs[0][prediction].item():.2%} confidence)")0: NOT_HATE_SPEECH - Normal, non-offensive content1: HATE_SPEECH - Hateful or offensive content (HOF)@misc{hate-speech-detector,
author = {archich},
title = {Multilingual Hate Speech Detector},
year = {2024},
publisher = {HuggingFace},
howpublished = {\url{https://huggingface.co/archich/hate-speech-detector}}
}