File size: 3,788 Bytes
ae30ece
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"""Normalize the GenomeQA eukaryote_cls configuration to the mcq schema."""

import argparse
import ast
import json
from pathlib import Path

from datasets import load_dataset
from huggingface_hub import CommitOperationAdd, HfApi, hf_hub_download


DATASET_ID = "HuggingFaceBio/genomeqa"
PARQUET_PATH = "mcq/eukaryote_cls_mcq.parquet"


def main() -> None:
    parser = argparse.ArgumentParser()
    parser.add_argument("--source-revision", required=True)
    parser.add_argument("--output-dataset", default=DATASET_ID)
    parser.add_argument("--output-dir", type=Path, default=Path.cwd())
    parser.add_argument("--publish", action="store_true")
    args = parser.parse_args()

    reference = load_dataset(DATASET_ID, "mcq", split="train", revision=args.source_revision)
    source = load_dataset(DATASET_ID, "eukaryote_cls", split="train", revision=args.source_revision)
    normalized = source.map(lambda row: {"metadata": json.dumps(ast.literal_eval(row["metadata"]))})
    normalized = normalized.cast(reference.features)

    parquet_path = args.output_dir / PARQUET_PATH
    parquet_path.parent.mkdir(parents=True, exist_ok=True)
    normalized.to_parquet(parquet_path)

    api = HfApi()
    head = api.repo_info(args.output_dataset, repo_type="dataset").sha
    card = Path(hf_hub_download(args.output_dataset, "README.md", repo_type="dataset", revision=head)).read_text()
    if "- config_name: eukaryote_cls\n  features:" not in card:
        info = f"""- config_name: eukaryote_cls
  features:
  - name: id
    dtype: int64
  - name: subtask
    dtype: string
  - name: question
    dtype: string
  - name: options
    list: string
  - name: answer_index
    dtype: int64
  - name: answer_text
    dtype: string
  - name: metadata
    dtype: string
  splits:
  - name: train
    num_bytes: {normalized.data.nbytes}
    num_examples: {len(normalized)}
  download_size: {parquet_path.stat().st_size}
  dataset_size: {normalized.data.nbytes}
"""
        card = card.replace("- config_name: binary_600\n", info + "- config_name: binary_600\n", 1)

    if "The `eukaryote_cls` configuration uses the `mcq` feature schema" not in card:
        note = "The `eukaryote_cls` configuration uses the `mcq` feature schema, including JSON-serialized `metadata`.\n\n"
        card = card.replace("The `binary_600` and `mcq_600` configurations", note + "The `binary_600` and `mcq_600` configurations", 1)

    if "scripts/create_eukaryote_cls.py" not in card:
        card += f"""
## Reproducing

To rebuild and publish the normalized `eukaryote_cls` configuration from the pinned source revision, run:

```bash
python scripts/create_eukaryote_cls.py --source-revision {args.source_revision} --output-dataset {args.output_dataset} --publish
```

The script preserves the source rows, converts `metadata` from Python dictionary strings to JSON, and casts the
columns to the `mcq` feature schema. It publishes the Parquet file, this card, and the builder script in one commit.
"""

    card_path = args.output_dir / "README.md"
    card_path.write_text(card)
    print(f"Prepared {len(normalized)} rows at {parquet_path}")
    if args.publish:
        commit = api.create_commit(
            repo_id=args.output_dataset,
            repo_type="dataset",
            parent_commit=head,
            commit_message="Normalize eukaryote_cls schema and metadata",
            operations=[
                CommitOperationAdd(path_in_repo=PARQUET_PATH, path_or_fileobj=parquet_path),
                CommitOperationAdd(path_in_repo="README.md", path_or_fileobj=card_path),
                CommitOperationAdd(path_in_repo="scripts/create_eukaryote_cls.py", path_or_fileobj=__file__),
            ],
        )
        print(f"Published {commit.commit_url}")


if __name__ == "__main__":
    main()