Views
No views yet
1from transformers import AutoModel, AutoTokenizer
2import torch
3import torch.nn as nn
4import json
5
6# Load model components
7encoder = AutoModel.from_pretrained("path/to/model")
8
9with open("path/to/model/classifier_config.json", 'r') as f:
10 c_config = json.load(f)
11
12classifier = nn.Sequential(
13 nn.Linear(c_config['hidden_size'], 256),
14 nn.ReLU(),
15 nn.Dropout(0.1),
16 nn.Linear(256, c_config['num_labels'])
17)
18classifier.load_state_dict(torch.load("path/to/model/classifier.pt"))
19
20tokenizer = AutoTokenizer.from_pretrained("path/to/model")
21
22# Predict
23def predict(text):
24 inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=128)
25 with torch.no_grad():
26 outputs = encoder(**inputs)
27 cls_embedding = outputs.last_hidden_state[:, 0, :]
28 logits = classifier(cls_embedding)
29 prob = torch.sigmoid(logits).item()
30 return prob
31
32text = "আপনার বাংলা টেক্সট এখানে"
33prob = predict(text)
34print(f"Hate Speech Probability: {prob:.4f}")1@misc{bangla-hate-speech-model,
2 author = {Nabil},
3 title = {Bangla Hate Speech Detection Model},
4 year = {2026},
5 publisher = {HuggingFace},
6}