File size: 814 Bytes
715cc5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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,
    )