File size: 24,926 Bytes
a520e58 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 |
import torch
import torch.nn as nn
import torch.nn.functional as F
from transformers import (
AutoModel, AutoConfig, AutoTokenizer,
T5ForConditionalGeneration, T5Config,
AutoModelForSequenceClassification,
PreTrainedModel, PretrainedConfig
)
from transformers.modeling_utils import (
load_state_dict,
WEIGHTS_NAME,
SAFE_WEIGHTS_NAME,
SAFE_WEIGHTS_INDEX_NAME,
WEIGHTS_INDEX_NAME
)
from transformers.utils import (
is_safetensors_available,
is_torch_available,
logging,
EntryNotFoundError,
PushToHubMixin
)
import os
import json
import numpy as np
logger = logging.get_logger(__name__)
class BaseHateSpeechModel(nn.Module):
"""Base class cho tất cả các mô hình hate speech detection"""
def __init__(self, model_name: str, num_labels: int = 3):
super().__init__()
self.num_labels = num_labels
self.model_name = model_name
def forward(self, input_ids, attention_mask, labels=None):
raise NotImplementedError
@classmethod
def from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs):
"""
Load model từ pretrained checkpoint.
Transformers sẽ tự động load state_dict sau khi khởi tạo model.
"""
# Extract config từ kwargs (transformers sẽ pass config vào đây)
config = kwargs.pop("config", None)
# Load config nếu chưa có
if config is None:
try:
config = AutoConfig.from_pretrained(pretrained_model_name_or_path)
except Exception:
config = {}
# Get num_labels từ config hoặc kwargs
num_labels = kwargs.pop("num_labels", None)
if num_labels is None:
if hasattr(config, "num_labels"):
num_labels = config.num_labels
elif isinstance(config, dict) and "num_labels" in config:
num_labels = config["num_labels"]
else:
num_labels = 3
# Lấy base model name từ config
base_model_name = None
if hasattr(config, "_name_or_path"):
base_model_name = config._name_or_path
elif isinstance(config, dict) and "_name_or_path" in config:
base_model_name = config["_name_or_path"]
# Khởi tạo model với base model name
if base_model_name:
model = cls(model_name=base_model_name, num_labels=num_labels, **kwargs)
else:
# Fallback: dùng default model_name từ class
model = cls(num_labels=num_labels, **kwargs)
return model
class PhoBERTV2Model(BaseHateSpeechModel):
"""PhoBERT-V2 cho hate speech detection"""
def __init__(self, model_name: str = "vinai/phobert-base-v2", num_labels: int = 3):
super().__init__(model_name, num_labels)
self.config = AutoConfig.from_pretrained(model_name, ignore_mismatched_sizes=True)
self.encoder = AutoModel.from_pretrained(model_name, config=self.config, ignore_mismatched_sizes=True)
self.dropout = nn.Dropout(0.1)
self.classifier = nn.Linear(self.config.hidden_size, num_labels)
def forward(self, input_ids, attention_mask, labels=None):
outputs = self.encoder(input_ids=input_ids, attention_mask=attention_mask)
pooled_output = outputs.pooler_output
pooled_output = self.dropout(pooled_output)
logits = self.classifier(pooled_output)
loss = None
if labels is not None:
loss_fn = nn.CrossEntropyLoss()
loss = loss_fn(logits, labels)
return {"loss": loss, "logits": logits}
class BartPhoModel(BaseHateSpeechModel):
"""BART Pho cho hate speech detection"""
def __init__(self, model_name: str = "vinai/bartpho-syllable-base", num_labels: int = 3):
super().__init__(model_name, num_labels)
self.config = AutoConfig.from_pretrained(model_name, ignore_mismatched_sizes=True)
self.encoder = AutoModel.from_pretrained(model_name, config=self.config, ignore_mismatched_sizes=True)
self.dropout = nn.Dropout(0.1)
self.classifier = nn.Linear(self.config.d_model, num_labels)
def forward(self, input_ids, attention_mask, labels=None):
outputs = self.encoder(input_ids=input_ids, attention_mask=attention_mask)
# Sử dụng hidden state của token cuối cùng
last_hidden_states = outputs.last_hidden_state
pooled_output = last_hidden_states.mean(dim=1) # Mean pooling
pooled_output = self.dropout(pooled_output)
logits = self.classifier(pooled_output)
loss = None
if labels is not None:
loss_fn = nn.CrossEntropyLoss()
loss = loss_fn(logits, labels)
return {"loss": loss, "logits": logits}
class ViSoBERTModel(BaseHateSpeechModel):
"""ViSoBERT cho hate speech detection"""
def __init__(self, model_name: str = "uitnlp/visobert", num_labels: int = 3):
super().__init__(model_name, num_labels)
self.config = AutoConfig.from_pretrained(model_name, ignore_mismatched_sizes=True)
self.encoder = AutoModel.from_pretrained(model_name, config=self.config, ignore_mismatched_sizes=True)
self.dropout = nn.Dropout(0.1)
self.classifier = nn.Linear(self.config.hidden_size, num_labels)
def forward(self, input_ids, attention_mask, labels=None):
outputs = self.encoder(input_ids=input_ids, attention_mask=attention_mask)
# Kiểm tra xem có pooler_output không, nếu không thì dùng last_hidden_state
if hasattr(outputs, 'pooler_output') and outputs.pooler_output is not None:
pooled_output = outputs.pooler_output
else:
# Fallback: sử dụng mean pooling của last_hidden_state
pooled_output = outputs.last_hidden_state.mean(dim=1)
pooled_output = self.dropout(pooled_output)
logits = self.classifier(pooled_output)
loss = None
if labels is not None:
loss_fn = nn.CrossEntropyLoss()
loss = loss_fn(logits, labels)
return {"loss": loss, "logits": logits}
class PhoBERTV1Model(BaseHateSpeechModel):
"""PhoBERT-V1 cho hate speech detection"""
def __init__(self, model_name: str = "vinai/phobert-base", num_labels: int = 3):
super().__init__(model_name, num_labels)
self.config = AutoConfig.from_pretrained(model_name, ignore_mismatched_sizes=True)
self.encoder = AutoModel.from_pretrained(model_name, config=self.config, ignore_mismatched_sizes=True)
self.dropout = nn.Dropout(0.1)
self.classifier = nn.Linear(self.config.hidden_size, num_labels)
def forward(self, input_ids, attention_mask, labels=None):
outputs = self.encoder(input_ids=input_ids, attention_mask=attention_mask)
# Một số encoder không có pooler_output
if hasattr(outputs, 'pooler_output') and outputs.pooler_output is not None:
pooled_output = outputs.pooler_output
else:
pooled_output = outputs.last_hidden_state.mean(dim=1)
pooled_output = self.dropout(pooled_output)
logits = self.classifier(pooled_output)
loss = None
if labels is not None:
loss_fn = nn.CrossEntropyLoss()
loss = loss_fn(logits, labels)
return {"loss": loss, "logits": logits}
class MBERTModel(BaseHateSpeechModel):
"""mBERT (bert-base-multilingual-cased) cho hate speech detection"""
def __init__(self, model_name: str = "bert-base-multilingual-cased", num_labels: int = 3):
super().__init__(model_name, num_labels)
self.config = AutoConfig.from_pretrained(model_name, ignore_mismatched_sizes=True)
self.encoder = AutoModel.from_pretrained(model_name, config=self.config, ignore_mismatched_sizes=True)
self.dropout = nn.Dropout(0.1)
self.classifier = nn.Linear(self.config.hidden_size, num_labels)
def forward(self, input_ids, attention_mask, labels=None):
outputs = self.encoder(input_ids=input_ids, attention_mask=attention_mask)
if hasattr(outputs, 'pooler_output') and outputs.pooler_output is not None:
pooled_output = outputs.pooler_output
else:
pooled_output = outputs.last_hidden_state.mean(dim=1)
pooled_output = self.dropout(pooled_output)
logits = self.classifier(pooled_output)
loss = None
if labels is not None:
loss_fn = nn.CrossEntropyLoss()
loss = loss_fn(logits, labels)
return {"loss": loss, "logits": logits}
class SPhoBERTModel(BaseHateSpeechModel):
"""SPhoBERT (biến thể PhoBERT syllable-level) cho hate speech detection"""
def __init__(self, model_name: str = "vinai/phobert-base", num_labels: int = 3):
super().__init__(model_name, num_labels)
self.config = AutoConfig.from_pretrained(model_name, ignore_mismatched_sizes=True)
self.encoder = AutoModel.from_pretrained(model_name, config=self.config, ignore_mismatched_sizes=True)
self.dropout = nn.Dropout(0.1)
self.classifier = nn.Linear(self.config.hidden_size, num_labels)
def forward(self, input_ids, attention_mask, labels=None):
outputs = self.encoder(input_ids=input_ids, attention_mask=attention_mask)
if hasattr(outputs, 'pooler_output') and outputs.pooler_output is not None:
pooled_output = outputs.pooler_output
else:
pooled_output = outputs.last_hidden_state.mean(dim=1)
pooled_output = self.dropout(pooled_output)
logits = self.classifier(pooled_output)
loss = None
if labels is not None:
loss_fn = nn.CrossEntropyLoss()
loss = loss_fn(logits, labels)
return {"loss": loss, "logits": logits}
class ViHateT5Model(BaseHateSpeechModel):
"""ViHateT5 cho hate speech detection"""
def __init__(self, model_name: str = "VietAI/vit5-base", num_labels: int = 3):
super().__init__(model_name, num_labels)
self.config = T5Config.from_pretrained(model_name)
self.encoder = T5ForConditionalGeneration.from_pretrained(model_name, config=self.config)
self.dropout = nn.Dropout(0.1)
self.classifier = nn.Linear(self.config.d_model, num_labels)
def forward(self, input_ids, attention_mask, labels=None):
outputs = self.encoder.encoder(input_ids=input_ids, attention_mask=attention_mask)
# Sử dụng hidden state của token cuối cùng
last_hidden_states = outputs.last_hidden_state
pooled_output = last_hidden_states.mean(dim=1) # Mean pooling
pooled_output = self.dropout(pooled_output)
logits = self.classifier(pooled_output)
loss = None
if labels is not None:
loss_fn = nn.CrossEntropyLoss()
loss = loss_fn(logits, labels)
return {"loss": loss, "logits": logits}
class XLMRModel(BaseHateSpeechModel):
"""XLM-R Large cho hate speech detection"""
def __init__(self, model_name: str = "xlm-roberta-large", num_labels: int = 3):
super().__init__(model_name, num_labels)
self.config = AutoConfig.from_pretrained(model_name, ignore_mismatched_sizes=True)
self.encoder = AutoModel.from_pretrained(model_name, config=self.config, ignore_mismatched_sizes=True)
self.dropout = nn.Dropout(0.1)
self.classifier = nn.Linear(self.config.hidden_size, num_labels)
def forward(self, input_ids, attention_mask, labels=None):
outputs = self.encoder(input_ids=input_ids, attention_mask=attention_mask)
pooled_output = outputs.pooler_output
pooled_output = self.dropout(pooled_output)
logits = self.classifier(pooled_output)
loss = None
if labels is not None:
loss_fn = nn.CrossEntropyLoss()
loss = loss_fn(logits, labels)
return {"loss": loss, "logits": logits}
class RoBERTaGRUModel(BaseHateSpeechModel):
"""RoBERTa + GRU Hybrid model"""
def __init__(self, model_name: str = "vinai/phobert-base-v2", num_labels: int = 3, hidden_size: int = 256):
super().__init__(model_name, num_labels)
self.config = AutoConfig.from_pretrained(model_name, ignore_mismatched_sizes=True)
self.encoder = AutoModel.from_pretrained(model_name, config=self.config, ignore_mismatched_sizes=True)
self.gru = nn.GRU(
input_size=self.config.hidden_size,
hidden_size=hidden_size,
num_layers=2,
batch_first=True,
dropout=0.1,
bidirectional=True
)
self.dropout = nn.Dropout(0.1)
self.classifier = nn.Linear(hidden_size * 2, num_labels) # *2 for bidirectional
def forward(self, input_ids, attention_mask, labels=None):
outputs = self.encoder(input_ids=input_ids, attention_mask=attention_mask)
hidden_states = outputs.last_hidden_state # [batch_size, seq_len, hidden_size]
# GRU processing
gru_output, _ = self.gru(hidden_states) # [batch_size, seq_len, hidden_size*2]
# Global average pooling
pooled_output = gru_output.mean(dim=1) # [batch_size, hidden_size*2]
pooled_output = self.dropout(pooled_output)
logits = self.classifier(pooled_output)
loss = None
if labels is not None:
loss_fn = nn.CrossEntropyLoss()
loss = loss_fn(logits, labels)
return {"loss": loss, "logits": logits}
class TextCNNModel(BaseHateSpeechModel):
"""TextCNN cho hate speech detection"""
def __init__(self, vocab_size: int, embedding_dim: int = 128, num_labels: int = 3,
num_filters: int = 100, filter_sizes: list = [3, 4, 5], dropout: float = 0.5):
super().__init__("textcnn", num_labels)
self.embedding = nn.Embedding(vocab_size, embedding_dim)
self.convs = nn.ModuleList([
nn.Conv2d(1, num_filters, (filter_size, embedding_dim))
for filter_size in filter_sizes
])
self.dropout = nn.Dropout(dropout)
self.classifier = nn.Linear(num_filters * len(filter_sizes), num_labels)
@classmethod
def from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs):
"""Override để detect vocab_size từ state_dict hoặc checkpoint file"""
# Get vocab_size từ kwargs hoặc config
vocab_size = kwargs.pop("vocab_size", None)
config = kwargs.pop("config", None)
# Nếu chưa có vocab_size, thử detect từ checkpoint file
if vocab_size is None:
import os
state_dict = None
# Try to load state_dict từ local path để detect vocab_size
if os.path.isdir(pretrained_model_name_or_path):
if os.path.isfile(os.path.join(pretrained_model_name_or_path, SAFE_WEIGHTS_NAME)):
try:
from safetensors.torch import load_file
state_dict = load_file(os.path.join(pretrained_model_name_or_path, SAFE_WEIGHTS_NAME))
except Exception:
pass
elif os.path.isfile(os.path.join(pretrained_model_name_or_path, WEIGHTS_NAME)):
try:
state_dict = torch.load(os.path.join(pretrained_model_name_or_path, WEIGHTS_NAME), map_location="cpu")
except Exception:
pass
# Detect vocab_size từ embedding.weight
if state_dict is not None and "embedding.weight" in state_dict:
vocab_size = state_dict["embedding.weight"].shape[0]
else:
vocab_size = 30000 # Default
# Get num_labels
num_labels = kwargs.pop("num_labels", None)
if num_labels is None:
if config and hasattr(config, "num_labels"):
num_labels = config.num_labels
elif config and isinstance(config, dict) and "num_labels" in config:
num_labels = config["num_labels"]
else:
num_labels = 3
# Khởi tạo model
model = cls(vocab_size=vocab_size, num_labels=num_labels, **kwargs)
return model
def forward(self, input_ids, attention_mask, labels=None):
# Embedding
embedded = self.embedding(input_ids) # [batch_size, seq_len, embedding_dim]
# Add channel dimension for Conv2d
embedded = embedded.unsqueeze(1) # [batch_size, 1, seq_len, embedding_dim]
# Convolutional layers
conv_outputs = []
for conv in self.convs:
conv_out = F.relu(conv(embedded)) # [batch_size, num_filters, seq_len', 1]
conv_out = conv_out.squeeze(3) # [batch_size, num_filters, seq_len']
pooled = F.max_pool1d(conv_out, conv_out.size(2)) # [batch_size, num_filters, 1]
pooled = pooled.squeeze(2) # [batch_size, num_filters]
conv_outputs.append(pooled)
# Concatenate all conv outputs
concatenated = torch.cat(conv_outputs, dim=1) # [batch_size, num_filters * len(filter_sizes)]
# Classification
concatenated = self.dropout(concatenated)
logits = self.classifier(concatenated)
loss = None
if labels is not None:
loss_fn = nn.CrossEntropyLoss()
loss = loss_fn(logits, labels)
return {"loss": loss, "logits": logits}
class BiLSTMModel(BaseHateSpeechModel):
"""BiLSTM cho hate speech detection"""
def __init__(self, vocab_size: int, embedding_dim: int = 128, hidden_size: int = 256,
num_labels: int = 3, num_layers: int = 2, dropout: float = 0.5):
super().__init__("bilstm", num_labels)
self.embedding = nn.Embedding(vocab_size, embedding_dim)
self.lstm = nn.LSTM(
input_size=embedding_dim,
hidden_size=hidden_size,
num_layers=num_layers,
batch_first=True,
dropout=dropout if num_layers > 1 else 0,
bidirectional=True
)
self.dropout = nn.Dropout(dropout)
self.classifier = nn.Linear(hidden_size * 2, num_labels) # *2 for bidirectional
@classmethod
def from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs):
"""Override để detect vocab_size từ state_dict hoặc checkpoint file"""
# Get vocab_size từ kwargs hoặc config
vocab_size = kwargs.pop("vocab_size", None)
config = kwargs.pop("config", None)
# Nếu chưa có vocab_size, thử detect từ checkpoint file
if vocab_size is None:
import os
state_dict = None
# Try to load state_dict từ local path để detect vocab_size
if os.path.isdir(pretrained_model_name_or_path):
if os.path.isfile(os.path.join(pretrained_model_name_or_path, SAFE_WEIGHTS_NAME)):
try:
from safetensors.torch import load_file
state_dict = load_file(os.path.join(pretrained_model_name_or_path, SAFE_WEIGHTS_NAME))
except Exception:
pass
elif os.path.isfile(os.path.join(pretrained_model_name_or_path, WEIGHTS_NAME)):
try:
state_dict = torch.load(os.path.join(pretrained_model_name_or_path, WEIGHTS_NAME), map_location="cpu")
except Exception:
pass
# Detect vocab_size từ embedding.weight
if state_dict is not None and "embedding.weight" in state_dict:
vocab_size = state_dict["embedding.weight"].shape[0]
else:
vocab_size = 30000 # Default
# Get num_labels
num_labels = kwargs.pop("num_labels", None)
if num_labels is None:
if config and hasattr(config, "num_labels"):
num_labels = config.num_labels
elif config and isinstance(config, dict) and "num_labels" in config:
num_labels = config["num_labels"]
else:
num_labels = 3
# Khởi tạo model
model = cls(vocab_size=vocab_size, num_labels=num_labels, **kwargs)
return model
def forward(self, input_ids, attention_mask, labels=None):
# Embedding
embedded = self.embedding(input_ids) # [batch_size, seq_len, embedding_dim]
# BiLSTM
lstm_output, (hidden, cell) = self.lstm(embedded) # [batch_size, seq_len, hidden_size*2]
# Global average pooling (có thể thay bằng max pooling hoặc last hidden state)
# Option 1: Global average pooling
pooled_output = lstm_output.mean(dim=1) # [batch_size, hidden_size*2]
# Option 2: Last hidden state (uncomment if preferred)
# pooled_output = lstm_output[:, -1, :] # [batch_size, hidden_size*2]
# Option 3: Max pooling (uncomment if preferred)
# pooled_output = torch.max(lstm_output, dim=1)[0] # [batch_size, hidden_size*2]
pooled_output = self.dropout(pooled_output)
logits = self.classifier(pooled_output)
loss = None
if labels is not None:
loss_fn = nn.CrossEntropyLoss()
loss = loss_fn(logits, labels)
return {"loss": loss, "logits": logits}
class EnsembleModel(BaseHateSpeechModel):
"""Ensemble model kết hợp các mô hình deep learning"""
def __init__(self, models: list, num_labels: int = 3, weights: list = None):
super().__init__("ensemble", num_labels)
self.models = nn.ModuleList(models)
self.num_models = len(models)
self.weights = weights if weights else [1.0] * self.num_models
self.weights = torch.tensor(self.weights, dtype=torch.float32)
self.weights = self.weights / self.weights.sum() # Normalize weights
def forward(self, input_ids, attention_mask, labels=None):
all_logits = []
total_loss = 0
for i, model in enumerate(self.models):
model_output = model(input_ids, attention_mask, labels)
all_logits.append(model_output["logits"])
if model_output["loss"] is not None:
total_loss += self.weights[i] * model_output["loss"]
# Weighted average of logits
ensemble_logits = torch.zeros_like(all_logits[0])
for i, logits in enumerate(all_logits):
ensemble_logits += self.weights[i] * logits
return {
"loss": total_loss if total_loss > 0 else None,
"logits": ensemble_logits
}
def get_model(model_name: str, num_labels: int = 3, **kwargs):
"""
Factory function để tạo model dựa trên tên
Args:
model_name: Tên model ("phobert-v2", "bartpho", "visobert", "vihate-t5",
"xlm-r", "roberta-gru", "textcnn", "bilstm", "bilstm-crf", "ensemble")
num_labels: Số lượng nhãn (3 cho hate speech: CLEAN, OFFENSIVE, HATE)
**kwargs: Các tham số bổ sung cho model
Returns:
Model instance
"""
model_mapping = {
"phobert-v1": PhoBERTV1Model,
"phobert-v2": PhoBERTV2Model,
"bartpho": BartPhoModel,
"visobert": ViSoBERTModel,
"vihate-t5": ViHateT5Model,
"xlm-r": XLMRModel,
"mbert": MBERTModel,
"sphobert": SPhoBERTModel,
"roberta-gru": RoBERTaGRUModel,
"textcnn": TextCNNModel,
"bilstm": BiLSTMModel,
"ensemble": EnsembleModel
}
if model_name not in model_mapping:
raise ValueError(f"Unknown model: {model_name}. Available models: {list(model_mapping.keys())}")
model_class = model_mapping[model_name]
return model_class(num_labels=num_labels, **kwargs) |