Download tokenize_dataset.py from tmutton/wcag-accessibility-issues: direct link, hf CLI and curl.
- Browser
- Download file 1.07 kB
-
https://huggingface.co/datasets/tmutton/wcag-accessibility-issues/resolve/main/tokenize_dataset.py
- Command line
-
hf download hf://datasets/tmutton/wcag-accessibility-issues/tokenize_dataset.py
-
curl -L -o tokenize_dataset.py https://huggingface.co/datasets/tmutton/wcag-accessibility-issues/resolve/main/tokenize_dataset.py
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}") |