lewtun HF Staff commited on
Commit
586a2d7
·
1 Parent(s): da80382

Add validation to conversion script

Browse files
Files changed (2) hide show
  1. convert_datasets_to_json.py +43 -7
  2. requirements.txt +3 -0
convert_datasets_to_json.py CHANGED
@@ -1,7 +1,13 @@
 
 
 
 
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]
@@ -13,27 +19,57 @@ def convert(dataset_id: str):
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()
 
1
+ import subprocess
2
+ import urllib
3
+
4
+ import typer
5
  from datasets import (get_dataset_config_names, get_dataset_split_names,
6
  load_dataset)
7
  from huggingface_hub import list_datasets
8
 
9
+ app = typer.Typer()
10
+
11
 
12
  def convert(dataset_id: str):
13
  dataset_name = dataset_id.split("/")[-1]
 
19
  for split in splits:
20
  columns_to_keep = ["gem_id", "gem_parent_id", "target"]
21
  dataset = load_dataset(dataset_id, name=config, split=split)
22
+ # For non-train splits, it seems we use the references column as the target
23
+ if "train" not in split:
24
+ dataset = dataset.map(lambda x: {"target": x["references"]})
25
+ else:
26
+ # Wrap references in list to match GEM schema
27
+ dataset = dataset.map(lambda x: {"target": [x["target"]]})
28
  # Delete unused columns
29
  # The test split doesn't have a parent ID
30
  if split == "test":
31
  columns_to_keep.remove("gem_parent_id")
32
  # The `datasets` JSON serializer is buggy - use `pandas` for now
33
  df = dataset.to_pandas()
34
+ reference_name = f"{dataset_name}_{config}_{split}"
35
+ df[columns_to_keep].to_json(f"{reference_name}.json", orient="records")
36
+ validate(reference_name)
37
+
38
+
39
+ def validate(reference_name: str):
40
+ # Download original references from GitHub repo
41
+ url = f"https://github.com/GEM-benchmark/GEM-metrics/releases/download/data/{reference_name}.json"
42
+ urllib.request.urlretrieve(
43
+ url,
44
+ f"github_references/{reference_name}.json",
45
+ )
46
+ # Run diff - requires `jq`
47
+ process = subprocess.run(
48
+ f"diff <(jq --sort-keys . {reference_name}.json) <(jq --sort-keys . ./github_references/{reference_name}.json)",
49
+ shell=True,
50
+ stdout=subprocess.PIPE,
51
+ executable="/bin/bash",
52
+ )
53
+ if process.stdout:
54
+ raise ValueError(f"❌ Validation failed for {reference_name}! New and original references do not agree 😭")
55
+ else:
56
+ typer.echo(f"✅ Validation successful for {reference_name}!")
57
 
58
 
59
+ @app.command()
60
  def main():
61
  all_datasets = list_datasets()
62
+ # Filter for GEM datasets
63
  gem_datasets = [dataset for dataset in all_datasets if dataset.id.startswith("GEM/")]
64
+ # Filter for blocklist
65
+ blocklist = ["test", "test-transform"]
66
+ gem_datasets = [dataset for dataset in gem_datasets if dataset.id not in blocklist]
67
+ # Focus on MLSUM for now - later remove this line to evaluate all datasets
68
  mlsum_datasets = [dataset for dataset in gem_datasets if dataset.id.startswith("GEM/mlsum")]
69
  for dataset in mlsum_datasets:
70
  convert(dataset.id)
71
+ typer.echo(f"🥳 All datasets converted!")
72
 
73
 
74
  if __name__ == "__main__":
75
+ app()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ datasets==1.16.1
2
+ huggingface-hub==0.2.1
3
+ typer==0.4.0