Download model_loader.py from abbasszahreddine/feedback_analysis_streamlit_app: direct link, hf CLI and curl.
- Browser
- Download file 1.81 kB
-
https://huggingface.co/abbasszahreddine/feedback_analysis_streamlit_app/resolve/main/model_loader.py
- Command line
-
hf download hf://abbasszahreddine/feedback_analysis_streamlit_app/model_loader.py
-
curl -L -o model_loader.py https://huggingface.co/abbasszahreddine/feedback_analysis_streamlit_app/resolve/main/model_loader.py
1.81 kB
| # 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" | |
| # ) | |