| import os |
| from datasets import DatasetDict, Audio |
| import pandas as pd |
| from datasets.table import embed_table_storage |
| import argparse |
|
|
|
|
| if __name__ == "__main__": |
| parser = argparse.ArgumentParser() |
| |
| |
| parser.add_argument("main_folder_path", type=str, help="Path of the base mls folder") |
| parser.add_argument("configuration", type=str, help="Dataset configuration to use, if necessary. Here corresponds to the language name.") |
| parser.add_argument("output_dir", type=str, help="Save the dataset on disk with this path.") |
|
|
| parser.add_argument("--cpu_num_workers", default=1, type=int, help="Number of CPU workers.") |
| parser.add_argument("--csv_folder_path", default=None, type=str, help="Path where to save intermediate csv, by default will be main_foldr_path") |
| parser.add_argument("--repo_id", default="facebook/multilingual_librispeech", type=str, help="Push the dataset to the hub.") |
|
|
|
|
| args = parser.parse_args() |
|
|
| main_folder_path = args.main_folder_path |
| csv_folder_path = args.csv_folder_path if args.csv_folder_path is not None else main_folder_path |
| if not os.path.exists(csv_folder_path): |
| os.makedirs(csv_folder_path) |
|
|
| splits = ["dev", "test", "train"] |
|
|
| |
|
|
| csv_dict = {} |
| for split in splits: |
| segment_path = os.path.join(main_folder_path, split, "segments.txt") |
| transcript_path = os.path.join(main_folder_path, split, "transcripts.txt") |
|
|
| segments = pd.read_csv(segment_path, sep='\t', names=["audio", "original_path", "begin_time", "end_time"], |
| index_col="audio") |
| transcripts = pd.read_csv(transcript_path, sep='\t', names=["audio", "transcript"], index_col="audio") |
|
|
| df = pd.concat([segments, transcripts], axis=1, join="inner") |
| print( |
| f"Segments and transcripts of {split} has been joined: new length {len(df)}, old lengths {(len(segments), len(transcripts))}") |
|
|
| |
| df["audio_duration"] = df["end_time"] - df["begin_time"] |
| df["split"] = split |
|
|
| print(f"len df {len(df)}") |
|
|
| df.to_csv(os.path.join(csv_folder_path, f"{split}.csv")) |
| csv_dict[split] = os.path.join(csv_folder_path, f"{split}.csv") |
| |
| |
| if split == "train": |
| nine_hours_segment_path = os.path.join(main_folder_path, "train/limited_supervision/9hr/handles.txt") |
| nine_hours_segment = pd.read_csv(nine_hours_segment_path, sep='\t', names=["audio"], index_col="audio").index |
| nine_hours_df = df.filter(items=nine_hours_segment, axis=0) |
| nine_hours_df.to_csv(os.path.join(csv_folder_path, f"9_hours.csv")) |
| csv_dict["9_hours"] = os.path.join(csv_folder_path, f"9_hours.csv") |
| |
| one_hours_segments = [ os.path.join(f.path, "handles.txt") for f in os.scandir( os.path.join(main_folder_path, "train/limited_supervision/1hr")) if f.is_dir()] |
| one_hours_segments = pd.concat([pd.read_csv(one, sep='\t', names=["audio"], index_col="audio") for one in one_hours_segments], axis=0).index |
| one_hours_df = df.filter(items=one_hours_segments, axis=0) |
| one_hours_df.to_csv(os.path.join(csv_folder_path, f"1_hours.csv")) |
| csv_dict["1_hours"] = os.path.join(csv_folder_path, f"1_hours.csv") |
|
|
| |
| |
|
|
| dataset = DatasetDict.from_csv(csv_dict) |
|
|
| def extract_speaker_id_and_format_path(audio, split): |
| speaker_id = audio.split("_")[0] |
| chapter_id = audio.split("_")[1] |
| file = f"{audio}.opus" |
|
|
| path = os.path.join(main_folder_path, split, "audio", speaker_id, chapter_id, file) |
| return {"audio": path, "speaker_id": speaker_id, "chapter_id": chapter_id, "file": file, "id": audio} |
|
|
| |
| dataset = dataset.map(extract_speaker_id_and_format_path, input_columns=["audio", "split"], num_proc=args.cpu_num_workers, remove_columns=["split"]) |
| dataset = dataset.cast_column("audio", Audio()) |
|
|
| print(dataset) |
| print(dataset["dev"][0]) |
|
|
| print("Embed table storage") |
|
|
| |
| format = dataset["train"].format |
| dataset = dataset.with_format("arrow") |
| dataset = dataset.map(embed_table_storage, batched=True, num_proc=args.cpu_num_workers) |
| dataset = dataset.with_format(**format) |
|
|
| |
| dataset.save_to_disk(args.output_dir, num_proc=args.cpu_num_workers) |
|
|
| if args.repo_id: |
| pushed = False |
| while not pushed: |
| try: |
| dataset.push_to_hub(args.repo_id, args.configuration, revision="refs/pr/15") |
| pushed = True |
| except: |
| pass |
|
|