File size: 1,809 Bytes
b656d19 | 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 |
# Model Loader Script - Compatible with Python 3.13.9
import torch
import torch.nn as nn
from transformers import AutoModel, AutoTokenizer
import json
import os
class MultiHeadClassifier(nn.Module):
def __init__(self, bert_model_name="aubmindlab/bert-base-arabertv2", num_heads=5, num_classes=4):
super().__init__()
self.bert = AutoModel.from_pretrained(bert_model_name)
hidden_size = self.bert.config.hidden_size
self.heads = nn.ModuleList([
nn.Sequential(
nn.Linear(hidden_size, 256),
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(256, num_classes)
) for _ in range(num_heads)
])
def forward(self, input_ids, attention_mask):
outputs = self.bert(input_ids=input_ids, attention_mask=attention_mask)
cls_embedding = outputs.last_hidden_state[:, 0, :]
logits = [head(cls_embedding) for head in self.heads]
return logits
def load_feedback_model(model_path, tokenizer_path):
"""Load the trained model for inference"""
# Load config
with open(os.path.join(os.path.dirname(model_path), "model_config.json"), "r") as f:
config = json.load(f)
# Initialize model
model = MultiHeadClassifier(
bert_model_name=config["bert_model_name"],
num_heads=config["num_heads"],
num_classes=config["num_classes"]
)
# Load weights
model.load_state_dict(torch.load(model_path, map_location="cpu"))
model.eval()
# Load tokenizer
tokenizer = AutoTokenizer.from_pretrained(tokenizer_path)
return model, tokenizer, config
# Example usage:
# model, tokenizer, config = load_feedback_model(
# "saved_model_python313/feedback_classifier.pt",
# "saved_model_python313/tokenizer"
# )
|