Update AudioCaps.py
Browse files- AudioCaps.py +41 -19
AudioCaps.py
CHANGED
@@ -28,6 +28,8 @@ import os
|
|
28 |
from typing import Optional, List
|
29 |
from multiprocessing import Lock, Process
|
30 |
from tqdm import tqdm
|
|
|
|
|
31 |
|
32 |
try:
|
33 |
import yt_dlp
|
@@ -90,6 +92,32 @@ class AudioCaps(datasets.GeneratorBasedBuilder):
|
|
90 |
|
91 |
def _split_generators(self, dl_manager):
|
92 |
downloaded_files = dl_manager.download_and_extract(_URLs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
return [
|
94 |
datasets.SplitGenerator(
|
95 |
name=datasets.Split.TRAIN,
|
@@ -115,12 +143,11 @@ class AudioCaps(datasets.GeneratorBasedBuilder):
|
|
115 |
]
|
116 |
|
117 |
# copied from https://github.com/prompteus/audio-captioning/blob/main/audiocap/download_audiocaps.py
|
118 |
-
def
|
119 |
self,
|
120 |
df: pd.DataFrame,
|
121 |
audios_dir: Path,
|
122 |
pid: Optional[int] = 0,
|
123 |
-
lock: Optional[Lock] = None,
|
124 |
):
|
125 |
"""
|
126 |
download yt videos specified in df, can be used in a multiprocess manner,
|
@@ -153,33 +180,28 @@ class AudioCaps(datasets.GeneratorBasedBuilder):
|
|
153 |
return dfs
|
154 |
|
155 |
def _generate_examples(self, filepath, split):
|
156 |
-
|
157 |
-
|
158 |
-
num_workers = os.getenv("AUDIO_CAPS_WORKER", 4)
|
159 |
|
160 |
df = pd.read_csv(filepath)
|
161 |
-
|
162 |
-
|
163 |
-
ps_ls = list()
|
164 |
-
for i in range(num_workers):
|
165 |
-
process = Process(
|
166 |
-
target=self.download,
|
167 |
-
args=(df_chunks[i], save_dir, i),
|
168 |
-
)
|
169 |
-
process.start()
|
170 |
-
ps_ls.append(process)
|
171 |
-
|
172 |
-
for ps in ps_ls:
|
173 |
-
ps.join()
|
174 |
|
175 |
for id_, row in df.iterrows():
|
176 |
wav_file = save_dir.joinpath(f"""{row["youtube_id"]}.wav""")
|
177 |
if not wav_file.exists():
|
178 |
continue
|
|
|
|
|
|
|
|
|
179 |
yield id_, {
|
180 |
"audiocap_id": row["audiocap_id"],
|
181 |
"youtube_id": row["youtube_id"],
|
182 |
"start_time": row["start_time"],
|
183 |
-
"audio":
|
184 |
"caption": row["caption"],
|
185 |
}
|
|
|
|
|
|
|
|
28 |
from typing import Optional, List
|
29 |
from multiprocessing import Lock, Process
|
30 |
from tqdm import tqdm
|
31 |
+
import io
|
32 |
+
import librosa
|
33 |
|
34 |
try:
|
35 |
import yt_dlp
|
|
|
92 |
|
93 |
def _split_generators(self, dl_manager):
|
94 |
downloaded_files = dl_manager.download_and_extract(_URLs)
|
95 |
+
num_workers = os.getenv("AUDIOCAPS_NUM_WORKER", 4)
|
96 |
+
|
97 |
+
for split, filepath in downloaded_files.items():
|
98 |
+
download_dir = Path(filepath).parent
|
99 |
+
save_dir = download_dir.joinpath("AudioCaps", split)
|
100 |
+
|
101 |
+
df = pd.read_csv(filepath)
|
102 |
+
|
103 |
+
df_chunks = self.df_to_n_chunks(df, num_workers)
|
104 |
+
ps_ls = list()
|
105 |
+
for i in range(num_workers):
|
106 |
+
process = Process(
|
107 |
+
target=self.yt_dlp_processor,
|
108 |
+
args=(df_chunks[i], save_dir, i),
|
109 |
+
)
|
110 |
+
process.start()
|
111 |
+
ps_ls.append(process)
|
112 |
+
|
113 |
+
try:
|
114 |
+
for ps in ps_ls:
|
115 |
+
ps.join()
|
116 |
+
except KeyboardInterrupt:
|
117 |
+
for ps in ps_ls:
|
118 |
+
ps.terminate()
|
119 |
+
ps.join()
|
120 |
+
|
121 |
return [
|
122 |
datasets.SplitGenerator(
|
123 |
name=datasets.Split.TRAIN,
|
|
|
143 |
]
|
144 |
|
145 |
# copied from https://github.com/prompteus/audio-captioning/blob/main/audiocap/download_audiocaps.py
|
146 |
+
def yt_dlp_processor(
|
147 |
self,
|
148 |
df: pd.DataFrame,
|
149 |
audios_dir: Path,
|
150 |
pid: Optional[int] = 0,
|
|
|
151 |
):
|
152 |
"""
|
153 |
download yt videos specified in df, can be used in a multiprocess manner,
|
|
|
180 |
return dfs
|
181 |
|
182 |
def _generate_examples(self, filepath, split):
|
183 |
+
rm_flag = os.getenv("AUDIOCAPS_RM_AUDIO_FILE", False)
|
184 |
+
save_flag = os.getenv("AUDIOCAPS_SAVE_TO_BYTE", False)
|
|
|
185 |
|
186 |
df = pd.read_csv(filepath)
|
187 |
+
download_dir = Path(filepath).parent
|
188 |
+
save_dir = download_dir.joinpath("AudioCaps", split)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
189 |
|
190 |
for id_, row in df.iterrows():
|
191 |
wav_file = save_dir.joinpath(f"""{row["youtube_id"]}.wav""")
|
192 |
if not wav_file.exists():
|
193 |
continue
|
194 |
+
audio = wav_file.read_bytes() if save_flag else str(wav_file)
|
195 |
+
if rm_flag:
|
196 |
+
# rm file
|
197 |
+
os.remove(wav_file)
|
198 |
yield id_, {
|
199 |
"audiocap_id": row["audiocap_id"],
|
200 |
"youtube_id": row["youtube_id"],
|
201 |
"start_time": row["start_time"],
|
202 |
+
"audio": audio,
|
203 |
"caption": row["caption"],
|
204 |
}
|
205 |
+
if rm_flag:
|
206 |
+
# rm dir
|
207 |
+
os.remove(save_dir)
|