asahi417 commited on
Commit
3f25a55
1 Parent(s): d1cd512
Files changed (3) hide show
  1. delete_audio.py +15 -0
  2. download_audio.py +10 -14
  3. main.sh +14 -12
delete_audio.py ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from os.path import join as p_join
3
+ from glob import glob
4
+ from tqdm import tqdm
5
+
6
+ direction = os.getenv("DIRECTION", "enA-jaA")
7
+ cache_dir_audio = p_join("download", "audio", direction)
8
+ cache_dir_feature = p_join("download", "feature", direction)
9
+ line_no_start = int(os.getenv("LINE_NO_START", 0))
10
+ line_no_end = int(os.getenv("LINE_NO_END", 10000))
11
+ for i in tqdm(range(line_no_start, line_no_end), total=line_no_end-line_no_start):
12
+ for audio_file in glob(p_join(cache_dir_audio, "*", "*")):
13
+ os.remove(audio_file)
14
+ if os.path.exists(p_join(cache_dir_feature, f"{i}.json")):
15
+ os.remove(p_join(cache_dir_feature, f"{i}.json"))
download_audio.py CHANGED
@@ -29,7 +29,7 @@ os.makedirs(cache_dir_feature, exist_ok=True)
29
  for s in sides:
30
  os.makedirs(p_join(cache_dir_audio, s), exist_ok=True)
31
  # processor config
32
- n_pool = int(os.getenv("N_POOL", os.cpu_count()))
33
  wget_max_retry = os.getenv("MAX_RETRY", "1")
34
  wget_timeout = os.getenv("TIMEOUT", "10")
35
  line_no_start = int(os.getenv("LINE_NO_START", 0))
@@ -99,7 +99,8 @@ def get_audio(dataframe: pd.DataFrame):
99
  if not os.path.exists(features[f"{side}.path"]):
100
  flag = wget(features[f"{side}.url"], output_file=features[f"{side}.path"])
101
  if not flag:
102
- return False
 
103
  else:
104
  try:
105
  wav = audio_loader.decode_example({"path": features[f"{side}.path"], "bytes": None})
@@ -107,14 +108,14 @@ def get_audio(dataframe: pd.DataFrame):
107
  sf.write(features[f"{side}.path"], wav["array"][start:end], wav["sampling_rate"])
108
  else:
109
  os.remove(features[f"{side}.path"])
110
- return False
111
  except Exception as e:
112
  print(e)
113
  os.remove(features[f"{side}.path"])
114
- return False
115
  with open(p_join(cache_dir_feature, f'{features["line_no"]}.json'), "w") as f:
116
  json.dump(features, f)
117
- return True
118
 
119
 
120
  if __name__ == '__main__':
@@ -132,17 +133,12 @@ if __name__ == '__main__':
132
  print(f"removed side != 2: {len(inputs)}")
133
  if n_pool == 1:
134
  for g in tqdm(inputs, total=len(inputs)):
135
- flag = get_audio(g)
136
- if not flag:
137
- print(f"failed:\n{g['url']}")
138
  else:
139
- # for i in range(0, len(inputs), n_pool):
140
- # batch_inputs = inputs[i: min(len(inputs), i + n_pool)]
141
- # with Pool(n_pool) as pool:
142
- # pool.map_async(get_audio, tqdm(batch_inputs, total=len(batch_inputs)))
143
  with Pool(n_pool) as pool:
144
- for flag in pool.imap_unordered(get_audio, tqdm(inputs, total=len(inputs))):
145
- pass
 
146
 
147
  def loader(feature: str) -> Dict:
148
  with open(feature) as f_reader:
 
29
  for s in sides:
30
  os.makedirs(p_join(cache_dir_audio, s), exist_ok=True)
31
  # processor config
32
+ n_pool = int(os.getenv("N_POOL", 1))
33
  wget_max_retry = os.getenv("MAX_RETRY", "1")
34
  wget_timeout = os.getenv("TIMEOUT", "10")
35
  line_no_start = int(os.getenv("LINE_NO_START", 0))
 
99
  if not os.path.exists(features[f"{side}.path"]):
100
  flag = wget(features[f"{side}.url"], output_file=features[f"{side}.path"])
101
  if not flag:
102
+ # create a dummy file to avoid the url again
103
+ return None
104
  else:
105
  try:
106
  wav = audio_loader.decode_example({"path": features[f"{side}.path"], "bytes": None})
 
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 None
112
  except Exception as e:
113
  print(e)
114
  os.remove(features[f"{side}.path"])
115
+ return None
116
  with open(p_join(cache_dir_feature, f'{features["line_no"]}.json'), "w") as f:
117
  json.dump(features, f)
118
+ return features["line_no"]
119
 
120
 
121
  if __name__ == '__main__':
 
133
  print(f"removed side != 2: {len(inputs)}")
134
  if n_pool == 1:
135
  for g in tqdm(inputs, total=len(inputs)):
136
+ line_no = get_audio(g)
 
 
137
  else:
 
 
 
 
138
  with Pool(n_pool) as pool:
139
+ for line_no in pool.imap_unordered(get_audio, inputs):
140
+ if line_no:
141
+ print(line_no)
142
 
143
  def loader(feature: str) -> Dict:
144
  with open(feature) as f_reader:
main.sh CHANGED
@@ -44,14 +44,6 @@ export LINE_NO_START=$(((DATASET_ID-1) * 5000))
44
  export LINE_NO_END=$((DATASET_ID * 5000))
45
  python download_audio.py
46
 
47
-
48
- export DATASET_ID=15
49
- export DIRECTION="enA-jaA"
50
- export LINE_NO_START=$(((DATASET_ID-1) * 5000))
51
- export LINE_NO_END=$((DATASET_ID * 5000))
52
- python download_audio.py
53
-
54
-
55
  export DATASET_ID=16
56
  export DIRECTION="enA-jaA"
57
  export LINE_NO_START=$(((DATASET_ID-1) * 5000))
@@ -84,22 +76,32 @@ export LINE_NO_START=$(((DATASET_ID-1) * 5000))
84
  export LINE_NO_END=$((DATASET_ID * 5000))
85
  python download_audio.py
86
 
87
- export DATASET_ID=23
88
  export DIRECTION="enA-jaA"
89
  export LINE_NO_START=$(((DATASET_ID-1) * 5000))
90
  export LINE_NO_END=$((DATASET_ID * 5000))
91
  python download_audio.py
92
 
93
 
94
- # test
95
- for i in $(seq 14 72);
 
 
 
 
 
 
 
 
 
 
96
  do
97
  export DATASET_ID=${i}
98
  export DIRECTION="enA-jaA"
99
  export LINE_NO_START=$(((DATASET_ID-1) * 5000))
100
  export LINE_NO_END=$((DATASET_ID * 5000))
101
  echo ${LINE_NO_START}
102
- python download_audio.py
103
  done
104
 
105
 
 
44
  export LINE_NO_END=$((DATASET_ID * 5000))
45
  python download_audio.py
46
 
 
 
 
 
 
 
 
 
47
  export DATASET_ID=16
48
  export DIRECTION="enA-jaA"
49
  export LINE_NO_START=$(((DATASET_ID-1) * 5000))
 
76
  export LINE_NO_END=$((DATASET_ID * 5000))
77
  python download_audio.py
78
 
79
+ export DATASET_ID=7
80
  export DIRECTION="enA-jaA"
81
  export LINE_NO_START=$(((DATASET_ID-1) * 5000))
82
  export LINE_NO_END=$((DATASET_ID * 5000))
83
  python download_audio.py
84
 
85
 
86
+ for i in $(seq 60 64);
87
+ do
88
+ export DATASET_ID=${i}
89
+ export DIRECTION="enA-jaA"
90
+ export LINE_NO_START=$(((DATASET_ID-1) * 5000))
91
+ export LINE_NO_END=$((DATASET_ID * 5000))
92
+ echo ${LINE_NO_START}
93
+ python download_audio.py &
94
+ done
95
+
96
+
97
+ for i in $(seq 65 72);
98
  do
99
  export DATASET_ID=${i}
100
  export DIRECTION="enA-jaA"
101
  export LINE_NO_START=$(((DATASET_ID-1) * 5000))
102
  export LINE_NO_END=$((DATASET_ID * 5000))
103
  echo ${LINE_NO_START}
104
+ python download_audio.py &
105
  done
106
 
107