Datasets:

Modalities:
Audio
Text
ArXiv:
Libraries:
Datasets
License:
File size: 3,014 Bytes
9bd1d61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# /// script
# requires-python = ">=3.10"
# dependencies = ["pandas>=2"]
# ///
"""Assemble the Hugging Face Hub copy of FreeTZAN (https://huggingface.co/datasets/gclef-cmu/freetzan).

The Hub repo mirrors this repository plus the audio (audio/<split>/<genre>/<track_id>.mp3, not in git),
and adds:
  audio/<split>/metadata.csv  that split's rows of metadata.csv, with `file_name` relative to the folder;
                              the Hub dataset viewer and datasets.load_dataset read the clips through these
  README.md                   this README.md with a YAML dataset-card header that maps audio/train,
                              audio/valid, audio/test to the Hub splits train, validation, test

Writes into --out (a clone of the Hub repo), replacing audio/, scripts/ and resources/ there. Push with
git + git-lfs (mp3 files are LFS-tracked by the Hub's default .gitattributes).

Usage: uv run scripts/make_hf_dataset.py --out /path/to/hub/clone
"""
import argparse
import hashlib
import shutil
from pathlib import Path

import pandas as pd

REPO = Path(__file__).resolve().parent.parent
SPLITS = ['train', 'valid', 'test']
MIRROR = ['LICENSE', 'metadata.csv', 'checksums.sha1', 'scripts/build_freetzan.py', 'scripts/find_duplicates.py',
          'scripts/extract_features.py', 'scripts/run_baselines.py', 'scripts/make_hf_dataset.py',
          'resources/exclusions.csv', 'resources/duplicates.csv', 'resources/baselines.csv']
CARD = """---
pretty_name: FreeTZAN
license: other
license_name: per-track-creative-commons
license_link: LICENSE
task_categories:
- audio-classification
tags:
- music
- genre-classification
size_categories:
- 1K<n<10K
configs:
- config_name: default
  data_files:
  - split: train
    path: audio/train/**
  - split: validation
    path: audio/valid/**
  - split: test
    path: audio/test/**
---

"""


def main():
    ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
    ap.add_argument('--out', required=True)
    args = ap.parse_args()
    out = Path(args.out)
    md = pd.read_csv(REPO / 'metadata.csv')
    assert set(md.split) == set(SPLITS)

    for d in ('audio', 'data', 'scripts', 'resources'):
        shutil.rmtree(out / d, ignore_errors=True)
    for f in MIRROR:
        (out / f).parent.mkdir(parents=True, exist_ok=True)
        shutil.copy2(REPO / f, out / f)
    for p, sha1 in zip(md.path, md.sha1):
        b = (REPO / p).read_bytes()
        assert hashlib.sha1(b).hexdigest() == sha1, p
        (out / p).parent.mkdir(parents=True, exist_ok=True)
        (out / p).write_bytes(b)
    for split in SPLITS:
        rows = md.split == split
        d = md[rows].drop(columns='path')
        d.insert(0, 'file_name', md.path[rows].str.removeprefix(f'audio/{split}/'))
        d.to_csv(out / 'audio' / split / 'metadata.csv', index=False)
        print(f'{split}: {len(d)} clips')
    (out / 'README.md').write_text(CARD + (REPO / 'README.md').read_text())


if __name__ == '__main__':
    main()