jam-alt / create_hf_dataset.py
cifkao's picture
Update create_hf_dataset.py script to work with the new version
0e839d1
from pathlib import Path
import shutil
import argparse
import datasets
NUM_SONGS = 79
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"--move-audio",
action="store_true",
help="Move and link audio files (default: only create metadata.jsonl files)",
)
args = parser.parse_args()
datasets.disable_caching()
subsets_dir = Path("subsets")
if args.move_audio:
# Divide the MP3 files by language. Hugging Face requires each subset and its metadata to be in
# a separate directory. However, for backwards compatibility, we also want to keep the top-level
# "audio" directory and replace the MP3 files with symlinks into the subsets.
if subsets_dir.exists():
shutil.rmtree(subsets_dir)
subsets_dir.mkdir()
# Back up the directory with the original MP3 files
if not Path("audio_orig").exists():
Path("audio").rename("audio_orig")
elif Path("audio").exists():
shutil.rmtree("audio")
Path("audio").mkdir(exist_ok=True)
else:
num_audio_files = sum(1 for _ in subsets_dir.glob("*/audio/*.mp3"))
if num_audio_files != NUM_SONGS:
raise RuntimeError(
f"Expected '{subsets_dir}' to exist and contain all {NUM_SONGS} audio files in "
f"subdirectories by language. Found {num_audio_files} files matching the pattern. "
"Use --move-audio if the audio files are found in 'audio' instead."
)
# Create language subsets and:
# - hard link the files from mp3_orig to subsets
# - add symlinks from mp3 into subsets
for config_name in ["all", "en", "es", "de", "fr"]:
subset_dir = Path(".") if config_name == "all" else subsets_dir / config_name
subset_dir.mkdir(exist_ok=True)
dataset = datasets.load_dataset(
"./loader.py",
config_name,
trust_remote_code=True,
split="test",
)
if config_name == "all":
dataset = dataset.add_column(
"file_name",
[
str(Path("subsets") / lg / "audio" / f"{n}.mp3")
for lg, n in zip(dataset["language"], dataset["name"])
],
)
else:
dataset = dataset.add_column(
"file_name", [str(Path("audio") / f"{n}.mp3") for n in dataset["name"]]
)
dataset = dataset.remove_columns("audio")
dataset.to_json(subset_dir / "metadata.jsonl")
if args.move_audio and config_name != "all":
(subset_dir / "audio").mkdir()
for name in dataset["name"]:
(subset_dir / "audio" / f"{name}.mp3").hardlink_to(
Path("audio_orig") / f"{name}.mp3"
)
(Path("audio") / f"{name}.mp3").symlink_to(
Path("..") / subset_dir / "audio" / f"{name}.mp3"
)
if __name__ == "__main__":
main()