Spaces:
Running
Running
Download utils.py from NbAiLab/goldenswag: direct link, hf CLI and curl.
- Browser
- Download file 1.38 kB
-
https://huggingface.co/spaces/NbAiLab/goldenswag/resolve/main/utils.py
- Command line
-
hf download hf://spaces/NbAiLab/goldenswag/utils.py
-
curl -L -o utils.py https://huggingface.co/spaces/NbAiLab/goldenswag/resolve/main/utils.py
1.38 kB
| import os | |
| import re | |
| import sys | |
| import datasets | |
| # noreval_metrics.py lives in tasks_base, two levels up | |
| sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "..")) | |
| from noreval_metrics import make_process_results | |
| def preprocess(text): | |
| text = text.strip() | |
| # NOTE: Brackets are artifacts of the WikiHow dataset portion of HellaSwag. | |
| # A mid-text [tittel] marks a sentence break; add a full stop only if one is missing. | |
| text = re.sub(r"([.!?:])? \[tittel\]", lambda m: (m.group(1) or ".") + " ", text) | |
| text = re.sub(r"\[.*?\]", "", text) | |
| # Removed brackets leave double or leading spaces behind. | |
| text = re.sub(r" {2,}", " ", text) | |
| return text.strip() | |
| def process_docs(dataset: datasets.Dataset) -> datasets.Dataset: | |
| def _process_doc(doc): | |
| out_doc = { | |
| "query": preprocess(doc["activity_label"] + ": " + doc["ctx"]), | |
| "choices": [preprocess(ending) for ending in doc["endings"]], | |
| "gold": int(doc["label"]), | |
| } | |
| return out_doc | |
| return dataset.map(_process_doc) | |
| def choices_text(doc): | |
| return doc["choices"] | |
| def choices_labels(doc): | |
| return ["A", "B", "C", "D"] | |
| def gold(doc): | |
| return [doc["gold"]] | |
| process_results = make_process_results(choices_text, gold) # cf / hybrid | |
| process_results_mcf = make_process_results(choices_labels, gold) | |