asahi417 commited on
Commit
3524295
·
1 Parent(s): 404c102

nit refactoring

Browse files
Files changed (3) hide show
  1. download_audio.py +49 -3
  2. filter_audio.py +1 -1
  3. main.sh +69 -17
download_audio.py CHANGED
@@ -4,13 +4,18 @@ import tarfile
4
  import zipfile
5
  import gzip
6
  import subprocess
 
7
  from os.path import join as p_join
8
  from tqdm import tqdm
9
  from multiprocessing import Pool
10
- from typing import Optional
 
11
 
12
  import pandas as pd
 
 
13
 
 
14
  # dataset config
15
  url_metadata_dict = {
16
  "enA-jaA": "https://dl.fbaipublicfiles.com/seamless/data/seamless_align_nov2023_extension/seamless.dataset.metadata.public.enA-jaA.tsv.gz",
@@ -25,10 +30,14 @@ for s in sides:
25
  os.makedirs(p_join(cache_dir_audio, s), exist_ok=True)
26
  # processor config
27
  n_pool = int(os.getenv("N_POOL", 8))
28
- wget_max_retry = os.getenv("MAX_RETRY", "1")
29
- wget_timeout = os.getenv("TIMEOUT", "20")
30
  line_no_start = int(os.getenv("LINE_NO_START", 0))
31
  line_no_end = int(os.getenv("LINE_NO_END", 10000))
 
 
 
 
32
 
33
 
34
  def wget(url: str, output_file: Optional[str] = None):
@@ -87,10 +96,23 @@ def get_audio(dataframe: pd.DataFrame):
87
  features.update({f"{side}.{k}": to_json_serializable(v) for k, v in df.iloc[0].to_dict().items()})
88
  identifier = os.path.basename(features[f"{side}.url"]).split(".")[-1]
89
  features[f"{side}.path"] = str(p_join(cache_dir_audio, side, f"{features['line_no']}.{identifier}"))
 
90
  if not os.path.exists(features[f"{side}.path"]):
91
  flag = wget(features[f"{side}.url"], output_file=features[f"{side}.path"])
92
  if not flag:
93
  return False
 
 
 
 
 
 
 
 
 
 
 
 
94
  with open(p_join(cache_dir_feature, f'{features["line_no"]}.json'), "w") as f:
95
  json.dump(features, f)
96
  return True
@@ -119,6 +141,30 @@ def process_dataset():
119
  pool.map(get_audio, tqdm(inputs, total=len(inputs)))
120
 
121
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122
  if __name__ == '__main__':
123
  process_dataset()
124
 
 
4
  import zipfile
5
  import gzip
6
  import subprocess
7
+ import time
8
  from os.path import join as p_join
9
  from tqdm import tqdm
10
  from multiprocessing import Pool
11
+ from typing import Optional, Dict
12
+ from glob import glob
13
 
14
  import pandas as pd
15
+ import soundfile as sf
16
+ from datasets import Dataset, Audio, DatasetDict
17
 
18
+ audio_loader = Audio()
19
  # dataset config
20
  url_metadata_dict = {
21
  "enA-jaA": "https://dl.fbaipublicfiles.com/seamless/data/seamless_align_nov2023_extension/seamless.dataset.metadata.public.enA-jaA.tsv.gz",
 
30
  os.makedirs(p_join(cache_dir_audio, s), exist_ok=True)
31
  # processor config
32
  n_pool = int(os.getenv("N_POOL", 8))
33
+ wget_max_retry = os.getenv("MAX_RETRY", "2")
34
+ wget_timeout = os.getenv("TIMEOUT", "30")
35
  line_no_start = int(os.getenv("LINE_NO_START", 0))
36
  line_no_end = int(os.getenv("LINE_NO_END", 10000))
37
+ dataset_id = int(os.getenv("DATASET_ID", 0))
38
+ hf_org = "kotoba-tech"
39
+ hf_dataset = f"seamless-align-{direction}"
40
+
41
 
42
 
43
  def wget(url: str, output_file: Optional[str] = None):
 
96
  features.update({f"{side}.{k}": to_json_serializable(v) for k, v in df.iloc[0].to_dict().items()})
97
  identifier = os.path.basename(features[f"{side}.url"]).split(".")[-1]
98
  features[f"{side}.path"] = str(p_join(cache_dir_audio, side, f"{features['line_no']}.{identifier}"))
99
+ start, end = features[f"{side}.duration_start"], features[f"{side}.duration_end"]
100
  if not os.path.exists(features[f"{side}.path"]):
101
  flag = wget(features[f"{side}.url"], output_file=features[f"{side}.path"])
102
  if not flag:
103
  return False
104
+ else:
105
+ try:
106
+ wav = audio_loader.decode_example({"path": features[f"{side}.path"], "bytes": None})
107
+ if start < end < len(wav["array"]):
108
+ sf.write(features[f"{side}.path"], wav["array"][start:end], wav["sampling_rate"])
109
+ else:
110
+ os.remove(features[f"{side}.path"])
111
+ return False
112
+ except Exception as e:
113
+ print(e)
114
+ os.remove(features[f"{side}.path"])
115
+ return False
116
  with open(p_join(cache_dir_feature, f'{features["line_no"]}.json'), "w") as f:
117
  json.dump(features, f)
118
  return True
 
141
  pool.map(get_audio, tqdm(inputs, total=len(inputs)))
142
 
143
 
144
+ def loader(feature: str) -> Dict:
145
+ with open(feature) as f_reader:
146
+ return json.load(f_reader)
147
+
148
+ features = [loader(i) for i in glob(p_join(cache_dir_feature, '*.json'))]
149
+ print(f"push {len(features)} records to hub")
150
+ data_dict = {}
151
+ for side in sides:
152
+ data_dict.update({f"{side}.audio": [i.pop(f"{side}.path") for i in features]})
153
+ data_dict.update({k: [i[k] for i in features] for k in features[0].keys()})
154
+ audio_dataset = Dataset.from_dict(data_dict)
155
+ for side in sides:
156
+ audio_dataset = audio_dataset.cast_column(f"{side}.audio", Audio())
157
+ dataset_to_push = DatasetDict({"train": audio_dataset})
158
+ repo_name = f"{hf_org}/{hf_dataset}"
159
+ while True:
160
+ try:
161
+ dataset_to_push.push_to_hub(repo_name, config_name=f"subset_{dataset_id}")
162
+ break
163
+ except Exception:
164
+ print(f"FAILED: push_to_hub on {repo_name} failed. wait 60 sec and retry soon...")
165
+ time.sleep(60)
166
+
167
+
168
  if __name__ == '__main__':
169
  process_dataset()
170
 
filter_audio.py CHANGED
@@ -48,4 +48,4 @@ for i in tqdm(index_list):
48
  sf.write(p_join(cache_dir_audio_fixed, f"{i}.mp3"), wav["array"][start:end], wav["sampling_rate"])
49
  except Exception as e:
50
  print(e)
51
- os.remove(audio_file)
 
48
  sf.write(p_join(cache_dir_audio_fixed, f"{i}.mp3"), wav["array"][start:end], wav["sampling_rate"])
49
  except Exception as e:
50
  print(e)
51
+ os.remove(audio_file)
main.sh CHANGED
@@ -2,82 +2,134 @@
2
  # enA-jaA: 718_606 #
3
  ####################
4
  export DIRECTION="enA-jaA"
5
-
6
- export DATASET_ID=1
7
  export LINE_NO_START=0
8
  export LINE_NO_END=25000
9
  python download_audio.py
10
 
11
- export DATASET_ID=2
12
  export LINE_NO_START=25000
13
  export LINE_NO_END=50000
14
  python download_audio.py
15
 
16
- export DATASET_ID=3
17
  export LINE_NO_START=50000
18
  export LINE_NO_END=75000
19
  python download_audio.py
20
 
21
- export DATASET_ID=4
22
  export LINE_NO_START=75000
23
  export LINE_NO_END=100000
24
  python download_audio.py
25
 
26
- export DATASET_ID=5
27
  export LINE_NO_START=100000
28
  export LINE_NO_END=125000
29
  python download_audio.py
30
 
31
- export DATASET_ID=6
32
  export LINE_NO_START=125000
33
  export LINE_NO_END=150000
34
  python download_audio.py
35
 
36
- export DATASET_ID=7
37
  export LINE_NO_START=150000
38
  export LINE_NO_END=175000
39
  python download_audio.py
40
 
41
- export DATASET_ID=8
42
  export LINE_NO_START=175000
43
  export LINE_NO_END=200000
44
  python download_audio.py
45
 
46
- export DATASET_ID=9
47
  export LINE_NO_START=200000
48
  export LINE_NO_END=225000
49
  python download_audio.py
50
 
51
- export DATASET_ID=10
52
  export LINE_NO_START=225000
53
  export LINE_NO_END=250000
54
  python download_audio.py
55
 
56
- export DATASET_ID=11
57
  export LINE_NO_START=250000
58
  export LINE_NO_END=275000
59
  python download_audio.py
60
 
61
- export DATASET_ID=12
62
  export LINE_NO_START=275000
63
  export LINE_NO_END=300000
64
  python download_audio.py
65
 
66
- export DATASET_ID=13
67
  export LINE_NO_START=300000
68
  export LINE_NO_END=325000
69
  python download_audio.py
70
 
71
- export DATASET_ID=14
72
  export LINE_NO_START=325000
73
  export LINE_NO_END=350000
74
  python download_audio.py
75
 
76
- export DATASET_ID=15
77
  export LINE_NO_START=350000
78
  export LINE_NO_END=360000
79
  python download_audio.py
80
- python push_s2s_translation.py
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
 
82
  ######################
83
  # enA-jpn: 1_468_292 #
 
2
  # enA-jaA: 718_606 #
3
  ####################
4
  export DIRECTION="enA-jaA"
 
 
5
  export LINE_NO_START=0
6
  export LINE_NO_END=25000
7
  python download_audio.py
8
 
9
+ export DIRECTION="enA-jaA"
10
  export LINE_NO_START=25000
11
  export LINE_NO_END=50000
12
  python download_audio.py
13
 
14
+ export DIRECTION="enA-jaA"
15
  export LINE_NO_START=50000
16
  export LINE_NO_END=75000
17
  python download_audio.py
18
 
19
+ export DIRECTION="enA-jaA"
20
  export LINE_NO_START=75000
21
  export LINE_NO_END=100000
22
  python download_audio.py
23
 
24
+ export DIRECTION="enA-jaA"
25
  export LINE_NO_START=100000
26
  export LINE_NO_END=125000
27
  python download_audio.py
28
 
29
+ export DIRECTION="enA-jaA"
30
  export LINE_NO_START=125000
31
  export LINE_NO_END=150000
32
  python download_audio.py
33
 
34
+ export DIRECTION="enA-jaA"
35
  export LINE_NO_START=150000
36
  export LINE_NO_END=175000
37
  python download_audio.py
38
 
39
+ export DIRECTION="enA-jaA"
40
  export LINE_NO_START=175000
41
  export LINE_NO_END=200000
42
  python download_audio.py
43
 
44
+ export DIRECTION="enA-jaA"
45
  export LINE_NO_START=200000
46
  export LINE_NO_END=225000
47
  python download_audio.py
48
 
49
+ export DIRECTION="enA-jaA"
50
  export LINE_NO_START=225000
51
  export LINE_NO_END=250000
52
  python download_audio.py
53
 
54
+ export DIRECTION="enA-jaA"
55
  export LINE_NO_START=250000
56
  export LINE_NO_END=275000
57
  python download_audio.py
58
 
59
+ export DIRECTION="enA-jaA"
60
  export LINE_NO_START=275000
61
  export LINE_NO_END=300000
62
  python download_audio.py
63
 
64
+ export DIRECTION="enA-jaA"
65
  export LINE_NO_START=300000
66
  export LINE_NO_END=325000
67
  python download_audio.py
68
 
69
+ export DIRECTION="enA-jaA"
70
  export LINE_NO_START=325000
71
  export LINE_NO_END=350000
72
  python download_audio.py
73
 
74
+ export DIRECTION="enA-jaA"
75
  export LINE_NO_START=350000
76
  export LINE_NO_END=360000
77
  python download_audio.py
78
+
79
+ # FILTER AUDIO
80
+ export DIRECTION="enA-jaA"
81
+ export DIRECTION_SPEECH="enA"
82
+ export LINE_NO_START=0
83
+ export LINE_NO_END=25000
84
+ python filter_audio.py
85
+
86
+ export DIRECTION="enA-jaA"
87
+ export DIRECTION_SPEECH="enA"
88
+ export LINE_NO_START=25000
89
+ export LINE_NO_END=50000
90
+ python filter_audio.py
91
+
92
+ export DIRECTION="enA-jaA"
93
+ export DIRECTION_SPEECH="enA"
94
+ export LINE_NO_START=50000
95
+ export LINE_NO_END=75000
96
+ python filter_audio.py
97
+
98
+ export DIRECTION="enA-jaA"
99
+ export DIRECTION_SPEECH="enA"
100
+ export LINE_NO_START=75000
101
+ export LINE_NO_END=100000
102
+ python filter_audio.py
103
+
104
+ export DIRECTION="enA-jaA"
105
+ export DIRECTION_SPEECH="enA"
106
+ export LINE_NO_START=100000
107
+ export LINE_NO_END=125000
108
+ python filter_audio.py
109
+
110
+ export DIRECTION="enA-jaA"
111
+ export DIRECTION_SPEECH="enA"
112
+ export LINE_NO_START=125000
113
+ export LINE_NO_END=150000
114
+ python filter_audio.py
115
+
116
+ export DIRECTION="enA-jaA"
117
+ export DIRECTION_SPEECH="enA"
118
+ export LINE_NO_START=150000
119
+ export LINE_NO_END=175000
120
+ python filter_audio.py
121
+
122
+ export DIRECTION="enA-jaA"
123
+ export DIRECTION_SPEECH="enA"
124
+ export LINE_NO_START=175000
125
+ export LINE_NO_END=200000
126
+ python filter_audio.py
127
+
128
+ export DIRECTION="enA-jaA"
129
+ export DIRECTION_SPEECH="enA"
130
+ export LINE_NO_START=200000
131
+ export LINE_NO_END=225000
132
+ python filter_audio.py
133
 
134
  ######################
135
  # enA-jpn: 1_468_292 #