Download MELD_Text_Audio.py from chuhaojie/MELD_Text_Audio: direct link, hf CLI and curl.
- Browser
- Download file 4.64 kB
-
https://huggingface.co/datasets/chuhaojie/MELD_Text_Audio/resolve/main/MELD_Text_Audio.py
- Command line
-
hf download hf://datasets/chuhaojie/MELD_Text_Audio/MELD_Text_Audio.py
-
curl -L -o MELD_Text_Audio.py https://huggingface.co/datasets/chuhaojie/MELD_Text_Audio/resolve/main/MELD_Text_Audio.py
4.64 kB
| import datasets | |
| import pandas as pd | |
| from datasets import ClassLabel | |
| import os | |
| class MELD_Text(datasets.GeneratorBasedBuilder): | |
| """TODO: Short description of my dataset.""" | |
| VERSION = datasets.Version("0.0.1") | |
| BUILDER_CONFIGS = [ # noqa: RUF012 | |
| datasets.BuilderConfig(name="MELD_Text", version=VERSION, description="MELD text"), | |
| ] | |
| def _info(self): | |
| return datasets.DatasetInfo( | |
| description="", | |
| features=datasets.Features( | |
| { | |
| "text": datasets.Value("string"), | |
| "path": datasets.Value("string"), | |
| "audio": datasets.Audio(16000), | |
| "emotion": ClassLabel(names=["neutral", "joy", "sadness", "anger", "fear", "disgust", "surprise"]), | |
| "sentiment": ClassLabel(names=["neutral", "positive", "negative"]), | |
| } | |
| ), | |
| supervised_keys=None, | |
| homepage="", | |
| license="", | |
| citation="", | |
| ) | |
| def _split_generators(self, dl_manager): | |
| """Returns SplitGenerators.""" | |
| metadata_dir: dict[str, str] = dl_manager.download_and_extract( | |
| {"train": "train_sent_emo.csv", "validation": "dev_sent_emo.csv", "test": "test_sent_emo.csv"} | |
| ) # type: ignore # noqa: PGH003 | |
| data_path: dict[str, str] = dl_manager.download( | |
| { | |
| "audios_train": "archive/audios_train.tgz", | |
| "audios_validation": "archive/audios_validation.tgz", | |
| "audios_test": "archive/audios_test.tgz", | |
| } | |
| ) # type: ignore # noqa: PGH003 | |
| path_to_clips = "MELD_Text_Audio" | |
| local_extracted_archive: dict[str, str] = ( | |
| dl_manager.extract(data_path) | |
| if not dl_manager.is_streaming | |
| else { | |
| "audios_train": None, | |
| "audios_validation": None, | |
| "audios_test": None, | |
| } | |
| ) # type: ignore # noqa: PGH003 | |
| return [ | |
| datasets.SplitGenerator( | |
| name=datasets.Split.TRAIN, # type: ignore # noqa: PGH003 | |
| gen_kwargs={ | |
| "filepath": metadata_dir["train"], | |
| "split": "train", | |
| "local_extracted_archive": local_extracted_archive["audios_train"], | |
| "audio_files": dl_manager.iter_archive(data_path["audios_train"]), | |
| "path_to_clips": path_to_clips, | |
| }, | |
| ), | |
| datasets.SplitGenerator( | |
| name=datasets.Split.VALIDATION, # type: ignore # noqa: PGH003 | |
| gen_kwargs={ | |
| "filepath": metadata_dir["validation"], | |
| "split": "validation", | |
| "local_extracted_archive": local_extracted_archive["audios_validation"], | |
| "audio_files": dl_manager.iter_archive(data_path["audios_validation"]), | |
| "path_to_clips": path_to_clips, | |
| }, | |
| ), | |
| datasets.SplitGenerator( | |
| name=datasets.Split.TEST, # type: ignore # noqa: PGH003 | |
| gen_kwargs={ | |
| "filepath": metadata_dir["test"], | |
| "split": "test", | |
| "local_extracted_archive": local_extracted_archive["audios_test"], | |
| "audio_files": dl_manager.iter_archive(data_path["audios_test"]), | |
| "path_to_clips": path_to_clips, | |
| }, | |
| ), | |
| ] | |
| def _generate_examples(self, filepath, split, local_extracted_archive, audio_files, path_to_clips): | |
| """Yields examples.""" | |
| metadata_df = pd.read_csv(filepath, sep=",", index_col=0, header=0) | |
| metadata = {} | |
| for _, row in metadata_df.iterrows(): | |
| id_ = f"dia{row['Dialogue_ID']}_utt{row['Utterance_ID']}" | |
| audio_path = f"{split}/{id_}.flac" | |
| metadata[audio_path] = row | |
| id_ = 0 | |
| for path, f in audio_files: | |
| if path in metadata: | |
| row = metadata[path] | |
| path = os.path.join(local_extracted_archive, path) if local_extracted_archive else path | |
| audio = {"path": path, bytes: f.read()} | |
| yield ( | |
| id_, | |
| { | |
| "text": row["Utterance"], | |
| "path": path, | |
| "audio": audio, | |
| "emotion": row["Emotion"], | |
| "sentiment": row["Sentiment"], | |
| }, | |
| ) | |
| id_ += 1 | |