ylacombe commited on
Commit
8a439ef
1 Parent(s): 326dcbd

Create create_dataset.py

Browse files
Files changed (1) hide show
  1. create_dataset.py +106 -0
create_dataset.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from datasets import DatasetDict, Audio
3
+ import pandas as pd
4
+ from datasets.table import embed_table_storage
5
+ import argparse
6
+
7
+
8
+ if __name__ == "__main__":
9
+ parser = argparse.ArgumentParser()
10
+
11
+
12
+ parser.add_argument("main_folder_path", type=str, help="Path of the base mls folder")
13
+ parser.add_argument("configuration", type=str, help="Dataset configuration to use, if necessary. Here corresponds to the language name.")
14
+ parser.add_argument("output_dir", type=str, help="Save the dataset on disk with this path.")
15
+
16
+ parser.add_argument("--cpu_num_workers", default=1, type=int, help="Number of CPU workers.")
17
+ parser.add_argument("--csv_folder_path", default=None, type=str, help="Path where to save intermediate csv, by default will be main_foldr_path")
18
+ parser.add_argument("--repo_id", default="facebook/multilingual_librispeech", type=str, help="Push the dataset to the hub.")
19
+
20
+
21
+ args = parser.parse_args()
22
+
23
+ main_folder_path = args.main_folder_path
24
+ csv_folder_path = args.csv_folder_path if args.csv_folder_path is not None else main_folder_path
25
+ if not os.path.exists(csv_folder_path):
26
+ os.makedirs(csv_folder_path)
27
+
28
+ splits = ["dev", "test", "train"]
29
+
30
+ # total_length_per_split = 10_000 * 60 * 60 # in sec -> 10k hours
31
+
32
+ csv_dict = {}
33
+ for split in splits:
34
+ segment_path = os.path.join(main_folder_path, split, "segments.txt")
35
+ transcript_path = os.path.join(main_folder_path, split, "transcripts.txt")
36
+
37
+ segments = pd.read_csv(segment_path, sep='\t', names=["audio", "original_path", "begin_time", "end_time"],
38
+ index_col="audio")
39
+ transcripts = pd.read_csv(transcript_path, sep='\t', names=["audio", "transcript"], index_col="audio")
40
+
41
+ df = pd.concat([segments, transcripts], axis=1, join="inner")
42
+ print(
43
+ f"Segments and transcripts of {split} has been joined: new length {len(df)}, old lengths {(len(segments), len(transcripts))}")
44
+
45
+ # add audio duration
46
+ df["audio_duration"] = df["end_time"] - df["begin_time"]
47
+ df["split"] = split
48
+
49
+ print(f"len df {len(df)}")
50
+
51
+ df.to_csv(os.path.join(csv_folder_path, f"{split}.csv"))
52
+ csv_dict[split] = os.path.join(csv_folder_path, f"{split}.csv")
53
+
54
+ # take care of /limited_supervision
55
+ if split == "train":
56
+ nine_hours_segment_path = os.path.join(main_folder_path, "train/limited_supervision/9hr/handles.txt")
57
+ nine_hours_segment = pd.read_csv(nine_hours_segment_path, sep='\t', names=["audio"], index_col="audio").index
58
+ nine_hours_df = df.filter(items=nine_hours_segment, axis=0)
59
+ nine_hours_df.to_csv(os.path.join(csv_folder_path, f"9_hours.csv"))
60
+ csv_dict["9_hours"] = os.path.join(csv_folder_path, f"9_hours.csv")
61
+
62
+ 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()]
63
+ 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
64
+ one_hours_df = df.filter(items=one_hours_segments, axis=0)
65
+ one_hours_df.to_csv(os.path.join(csv_folder_path, f"1_hours.csv"))
66
+ csv_dict["1_hours"] = os.path.join(csv_folder_path, f"1_hours.csv")
67
+
68
+
69
+
70
+
71
+ dataset = DatasetDict.from_csv(csv_dict)
72
+
73
+ def extract_speaker_id_and_format_path(audio, split):
74
+ speaker_id = audio.split("_")[0]
75
+ chapter_id = audio.split("_")[1]
76
+ file = f"{audio}.opus"
77
+
78
+ path = os.path.join(main_folder_path, split, "audio", speaker_id, chapter_id, file)
79
+ return {"audio": path, "speaker_id": speaker_id, "chapter_id": chapter_id, "file": file, "id": audio}
80
+
81
+ # correct audio path
82
+ dataset = dataset.map(extract_speaker_id_and_format_path, input_columns=["audio", "split"], num_proc=args.cpu_num_workers, remove_columns=["split"])
83
+ dataset = dataset.cast_column("audio", Audio())
84
+
85
+ print(dataset)
86
+ print(dataset["dev"][0])
87
+
88
+ print("Embed table storage")
89
+
90
+ # load_dataset(...)
91
+ format = dataset["train"].format
92
+ dataset = dataset.with_format("arrow")
93
+ dataset = dataset.map(embed_table_storage, batched=True, num_proc=args.cpu_num_workers)
94
+ dataset = dataset.with_format(**format)
95
+
96
+
97
+ dataset.save_to_disk(args.output_dir, num_proc=args.cpu_num_workers)
98
+
99
+ if args.repo_id:
100
+ pushed = False
101
+ while not pushed:
102
+ try:
103
+ dataset.push_to_hub(args.repo_id, args.configuration, revision="refs/pr/15")
104
+ pushed = True
105
+ except:
106
+ pass