wcag-accessibility-issues / tokenize_dataset.py
tmutton's picture
Add WCAG classification training pipeline
e0cfb62
Raw History Blame Contribute Delete
1.07 kB
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}")