| from __future__ import annotations | |
| from typing import Any | |
| from transformers import AutoModelForSequenceClassification, AutoTokenizer | |
| def sanitize_model_name(model_name: str) -> str: | |
| return model_name.replace("/", "__").replace(" ", "_") | |
| def load_tokenizer(model_name: str) -> Any: | |
| return AutoTokenizer.from_pretrained(model_name, trust_remote_code=True, use_fast=True) | |
| def load_sequence_classifier(model_name: str, num_labels: int, label_set: list[str]) -> Any: | |
| id2label = {idx: label for idx, label in enumerate(label_set)} | |
| label2id = {label: idx for idx, label in id2label.items()} | |
| return AutoModelForSequenceClassification.from_pretrained( | |
| model_name, | |
| num_labels=num_labels, | |
| id2label=id2label, | |
| label2id=label2id, | |
| trust_remote_code=True, | |
| ) | |