Add dataset conversion script for MLSUM
Browse files- convert_datasets_to_json.py +39 -0
convert_datasets_to_json.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from datasets import (get_dataset_config_names, get_dataset_split_names,
|
2 |
+
load_dataset)
|
3 |
+
from huggingface_hub import list_datasets
|
4 |
+
|
5 |
+
|
6 |
+
def convert(dataset_id: str):
|
7 |
+
dataset_name = dataset_id.split("/")[-1]
|
8 |
+
configs = get_dataset_config_names(dataset_id)
|
9 |
+
|
10 |
+
for config in configs:
|
11 |
+
splits = get_dataset_split_names(dataset_id, config)
|
12 |
+
splits = [split for split in splits if split not in ["train", "validation"]]
|
13 |
+
for split in splits:
|
14 |
+
columns_to_keep = ["gem_id", "gem_parent_id", "target"]
|
15 |
+
dataset = load_dataset(dataset_id, name=config, split=split)
|
16 |
+
# It seems like we store the references column as the target one
|
17 |
+
dataset = dataset.map(lambda x: {"target": x["references"]})
|
18 |
+
# Delete unused columns
|
19 |
+
# The test split doesn't have a parent ID
|
20 |
+
if split == "test":
|
21 |
+
columns_to_keep.remove("gem_parent_id")
|
22 |
+
# The `datasets` JSON serializer is buggy - use `pandas` for now
|
23 |
+
df = dataset.to_pandas()
|
24 |
+
df[columns_to_keep].to_json(f"{dataset_name}_{config}_{split}.json", orient="records")
|
25 |
+
# TODO: validate against existing references on GitHub
|
26 |
+
# diff <(jq --sort-keys . mlsum_de_challenge_test_covid.json) <(jq --sort-keys . ~/git/GEM-metrics/data/references/mlsum_de_challenge_test_covid.json)
|
27 |
+
|
28 |
+
|
29 |
+
def main():
|
30 |
+
all_datasets = list_datasets()
|
31 |
+
gem_datasets = [dataset for dataset in all_datasets if dataset.id.startswith("GEM/")]
|
32 |
+
# Test run with MLSUM
|
33 |
+
mlsum_datasets = [dataset for dataset in gem_datasets if dataset.id.startswith("GEM/mlsum")]
|
34 |
+
for dataset in mlsum_datasets:
|
35 |
+
convert(dataset.id)
|
36 |
+
|
37 |
+
|
38 |
+
if __name__ == "__main__":
|
39 |
+
main()
|