|
|
|
import datasets |
|
from huggingface_hub import HfApi |
|
from datasets import DownloadManager, DatasetInfo |
|
from datasets.data_files import DataFilesDict |
|
import os |
|
import json |
|
|
|
|
|
|
|
_NAME = "mickylan2367/LoadingScriptPractice" |
|
_EXTENSION = [".png"] |
|
_REVISION = "main" |
|
|
|
|
|
|
|
_HOMEPAGE = "https://huggingface.co/datasets/mickylan2367/spectrogram_musicCaps" |
|
|
|
_DESCRIPTION = f"""\ |
|
{_NAME} Datasets including spectrogram.png file from Google MusicCaps Datasets! |
|
Using for Project Learning... |
|
""" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class LoadingScriptPractice(datasets.GeneratorBasedBuilder): |
|
|
|
|
|
BUILDER_CONFIGS = [ |
|
datasets.BuilderConfig( |
|
name="train", |
|
description="this Datasets is personal practice for using loadingScript. Data is from Google/MusicCaps", |
|
|
|
|
|
|
|
|
|
), |
|
|
|
|
|
datasets.BuilderConfig( |
|
name="test", |
|
description="this Datasets is personal practice for using loadingScript. Data is from Google/MusicCaps", |
|
|
|
|
|
|
|
|
|
) |
|
] |
|
|
|
def _info(self) -> DatasetInfo: |
|
return datasets.DatasetInfo( |
|
description = self.config.description, |
|
features=datasets.Features( |
|
{ |
|
"image": datasets.Image(), |
|
"caption": datasets.Value("string"), |
|
"data_idx": datasets.Value("int32"), |
|
"number" : datasets.Value("int32"), |
|
"label" : datasets.ClassLabel( |
|
names=[ |
|
"blues", |
|
"classical", |
|
"country", |
|
"disco", |
|
"hiphop", |
|
"metal", |
|
"pop", |
|
"reggae", |
|
"rock", |
|
"jazz" |
|
] |
|
) |
|
} |
|
), |
|
supervised_keys=("image", "caption"), |
|
homepage=_HOMEPAGE, |
|
citation= "", |
|
|
|
|
|
) |
|
|
|
def _split_generators(self, dl_manager: DownloadManager): |
|
|
|
hfh_dataset_info = HfApi().dataset_info(_NAME, revision=_REVISION, timeout=100.0) |
|
|
|
train_metadata_paths = DataFilesDict.from_hf_repo( |
|
{datasets.Split.TRAIN: ["**"]}, |
|
dataset_info=hfh_dataset_info, |
|
allowed_extensions=["jsonl", ".jsonl"], |
|
) |
|
|
|
test_metadata_paths = DataFilesDict.from_hf_repo( |
|
{datasets.Split.TEST: ["**"]}, |
|
dataset_info=hfh_dataset_info, |
|
allowed_extensions=["jsonl", ".jsonl"], |
|
) |
|
|
|
|
|
train_data_url = DataFilesDict.from_hf_repo( |
|
{datasets.Split.TRAIN: ["**"]}, |
|
dataset_info=hfh_dataset_info, |
|
allowed_extensions=["zip", ".zip"], |
|
) |
|
|
|
test_data_url = DataFilesDict.from_hf_repo( |
|
{datasets.Split.test: ["**"]}, |
|
dataset_info=hfh_dataset_info, |
|
allowed_extensions=["zip", ".zip"], |
|
) |
|
|
|
|
|
data_path = { |
|
"train" : train_data_url["train"][0], |
|
"test" : test_data_url["test"][0] |
|
} |
|
|
|
split_metadata_paths= { |
|
"train" : train_metadata_paths["train"][0], |
|
"test" : test_metadata_paths["test"][0] |
|
} |
|
|
|
gs = [] |
|
for split, files in data_path.items(): |
|
''' |
|
split : "train" or "test" or "val" |
|
files : zip files |
|
''' |
|
|
|
split_metadata_path = dl_manager.download_and_extract(split_metadata_paths[split]) |
|
downloaded_files_path = dl_manager.download(files) |
|
|
|
|
|
gs.append( |
|
datasets.SplitGenerator( |
|
name = split, |
|
gen_kwargs={ |
|
"images" : dl_manager.iter_archive(downloaded_files_path), |
|
"metadata_path": split_metadata_path |
|
} |
|
) |
|
) |
|
return gs |
|
|
|
def _generate_examples(self, images, metadata_path): |
|
"""Generate images and captions for splits.""" |
|
|
|
|
|
file_list = list() |
|
caption_list = list() |
|
dataIDX_list = list() |
|
num_list = list() |
|
label_list = list() |
|
|
|
with open(metadata_path) as fin: |
|
for line in fin: |
|
data = json.loads(line) |
|
file_list.append(data["file_name"]) |
|
caption_list.append(data["caption"]) |
|
dataIDX_list.append(data["data_idx"]) |
|
num_list.append(data["number"]) |
|
label_list.append(data["label"]) |
|
|
|
for idx, (file_path, file_obj) in enumerate(images): |
|
yield file_path, { |
|
"image": { |
|
"path": file_path, |
|
"bytes": file_obj.read() |
|
}, |
|
"caption" : caption_list[idx], |
|
"data_idx" : dataIDX_list[idx], |
|
"number" : num_list[idx], |
|
"label": label_list[idx] |
|
} |
|
|
|
|
|
|