File size: 7,556 Bytes
4116085
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8747487
4116085
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e93e38
4116085
 
 
 
 
 
 
 
 
 
 
6eaa0b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4116085
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6eaa0b2
4116085
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6eaa0b2
 
4116085
 
6eaa0b2
 
4116085
 
 
 
 
6eaa0b2
 
 
 
4116085
 
 
 
6eaa0b2
4116085
 
6eaa0b2
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# MIT License

# Copyright (c) 2019 AudioCaps authors

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.


import datasets
from pathlib import Path
import pandas as pd
import os
from typing import Optional, List
from multiprocessing import Lock, Process
from tqdm import tqdm

try:
    import yt_dlp
except ImportError:
    raise ImportError("this code must need yt-dlp package, please run `pip install yt-dlp`")


_CITATION = """\
@inproceedings{audiocaps,
  title={AudioCaps: Generating Captions for Audios in The Wild},
  author={Kim, Chris Dongjoo and Kim, Byeongchang and Lee, Hyunmin and Kim, Gunhee},
  booktitle={NAACL-HLT},
  year={2019}
}
"""

_DESCRIPTION = """\
We explore audio captioning: generating natural language description for any kind of audio in the wild. We contribute AudioCaps, a large-scale dataset of about 46K audio clips to human-written text pairs collected via crowdsourcing on the  AudioSet dataset. The collected captions of AudioCaps are indeed faithful for audio inputs. We provide the source code of the models to explore what forms of audio representation and captioning models are effective for the audio captioning.
"""

_HOMEPAGE = "https://github.com/cdjkim/audiocaps"

_LICENSE = "MIT License"


_URL = "https://raw.githubusercontent.com/cdjkim/audiocaps/master/dataset/"
_URLs = {
    "train": _URL + "train.csv",
    "valid": _URL + "val.csv",
    "test": _URL + "test.csv",
}

YT_URL = "https://www.youtube.com/watch?v="
DURATION = os.getenv("AUDIOCAPS_DURATION", 10)


# TODO: Name of the dataset usually match the script name with CamelCase instead of snake_case
class AudioCaps(datasets.GeneratorBasedBuilder):
    """Korean Naver movie review dataset."""

    VERSION = datasets.Version("1.0.0")

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    "audiocap_id": datasets.Value("int32"),
                    "youtube_id": datasets.Value("string"),
                    "start_time": datasets.Value("int32"),
                    "audio": datasets.Audio(48000),
                    "caption": datasets.Value("string"),
                }
            ),
            supervised_keys=None,
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        downloaded_files = dl_manager.download_and_extract(_URLs)
        num_workers = os.getenv("AUDIOCAPS_NUM_WORKER", 4)

        for split, filepath in downloaded_files.items():
            download_dir = Path(filepath).parent
            save_dir = download_dir.joinpath("AudioCaps", split)

            df = pd.read_csv(filepath)

            df_chunks = self.df_to_n_chunks(df, num_workers)
            ps_ls = list()
            for i in range(num_workers):
                process = Process(
                    target=self.yt_dlp_processor,
                    args=(df_chunks[i], save_dir, i),
                )
                process.start()
                ps_ls.append(process)

            try:
                for ps in ps_ls:
                    ps.join()
            except KeyboardInterrupt:
                for ps in ps_ls:
                    ps.terminate()
                    ps.join()

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "filepath": downloaded_files["train"],
                    "split": "train",
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={
                    "filepath": downloaded_files["valid"],
                    "split": "valid",
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={
                    "filepath": downloaded_files["test"],
                    "split": "test",
                },
            ),
        ]

    # copied from https://github.com/prompteus/audio-captioning/blob/main/audiocap/download_audiocaps.py
    def yt_dlp_processor(
        self,
        df: pd.DataFrame,
        audios_dir: Path,
        pid: Optional[int] = 0,
    ):
        """
        download yt videos specified in df, can be used in a multiprocess manner,
        just specify lock parameter, so logging goes safe
        """
        print(f"### Download process number {pid} started")
        for row in tqdm(list(df.iterrows()), desc=f"dw_ps: {pid}"):
            wav_file = audios_dir.joinpath(f"""{row[1]["youtube_id"]}.wav""")
            if wav_file.exists():
                continue

            youtube_id = row[1]["youtube_id"]
            start_time = row[1]["start_time"]
            end_time = start_time + DURATION

            dw_url = f"{YT_URL}{youtube_id}"

            # Download full video of whatever format
            audio_name = os.path.join(audios_dir, f"{youtube_id}")
            os.system(
                f"""yt-dlp -S "asr:48000" -x --quiet --audio-format wav --external-downloader aria2c --external-downloader-args 'ffmpeg_i:-ss {start_time} -to {end_time}' -o '{audio_name}.%(ext)s' {dw_url}"""
            )

    def df_to_n_chunks(self, df: pd.DataFrame, n: int) -> List[pd.DataFrame]:
        chunk_size = len(df) // n + 1
        dfs = []
        for i in range(n):
            new_df = df.iloc[i * chunk_size : (i + 1) * chunk_size]
            dfs.append(new_df)
        return dfs

    def _generate_examples(self, filepath, split):
        rm_flag = os.getenv("AUDIOCAPS_RM_AUDIO_FILE", False)
        save_flag = os.getenv("AUDIOCAPS_SAVE_TO_BYTE", False)

        df = pd.read_csv(filepath)
        download_dir = Path(filepath).parent
        save_dir = download_dir.joinpath("AudioCaps", split)

        for id_, row in df.iterrows():
            wav_file = save_dir.joinpath(f"""{row["youtube_id"]}.wav""")
            if not wav_file.exists():
                continue
            audio = wav_file.read_bytes() if save_flag else str(wav_file)
            if rm_flag:
                # rm file
                os.remove(wav_file)
            yield id_, {
                "audiocap_id": row["audiocap_id"],
                "youtube_id": row["youtube_id"],
                "start_time": row["start_time"],
                "audio": audio,
                "caption": row["caption"],
            }
        if rm_flag:
            # rm dir
            os.remove(save_dir)