File size: 1,068 Bytes
e0cfb62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from datasets import load_dataset
from transformers import AutoTokenizer

MODEL_NAME = "distilbert-base-uncased"

LABELS = [
    "1.3.1 Info and Relationships",
    "2.1.1 Keyboard",
    "2.4.3 Focus Order",
    "2.4.7 Focus Visible",
    "4.1.2 Name, Role, Value",
]

label2id = {label: index for index, label in enumerate(LABELS)}
id2label = {index: label for index, label in enumerate(LABELS)}

dataset = load_dataset(
    "csv",
    data_files={
        "train": "train.csv",
        "validation": "validation.csv",
        "test": "test.csv",
    }
)

tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)


def preprocess(example):
    encoded = tokenizer(
        example["text"],
        truncation=True
    )

    encoded["label"] = label2id[example["label"]]

    return encoded


tokenized_dataset = dataset.map(preprocess)

print(tokenized_dataset)

print("\nOriginal example:")
print(dataset["train"][0])

print("\nTokenized example:")
print(tokenized_dataset["train"][0])

print("\nLabels:")
for label, id in label2id.items():
    print(f"{id}: {label}")