Views
No views yet
aubmindlab/bert-base-arabertv2 for Arabic hate speech detection.1from transformers import AutoTokenizer, AutoModelForSequenceClassification
2import torch
3
4# Load model and tokenizer
5model_name = "Ibracadabra13/arabic-bert-hate-speech-detection"
6tokenizer = AutoTokenizer.from_pretrained(model_name)
7model = AutoModelForSequenceClassification.from_pretrained(model_name)
8
9# Function to predict hate speech
10def predict_hate_speech(text):
11 inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=128)
12
13 with torch.no_grad():
14 outputs = model(**inputs)
15 predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
16 predicted_class = torch.argmax(predictions, dim=-1).item()
17 confidence = predictions[0][predicted_class].item()
18
19 label_map = {0: 'Normal', 1: 'Hate Speech'}
20 return {
21 'prediction': label_map[predicted_class],
22 'confidence': confidence,
23 'is_hate_speech': predicted_class == 1
24 }
25
26# Example usage
27result = predict_hate_speech("أنت حيوان حقير")
28print(result) # {'prediction': 'Hate Speech', 'confidence': 0.97, 'is_hate_speech': True}