File size: 10,707 Bytes
d5c3b23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64ab7b9
d5c3b23
 
 
 
 
 
 
 
 
 
 
f10f543
 
b47cbef
f10f543
 
b47cbef
f10f543
 
b47cbef
f10f543
 
b47cbef
f10f543
 
b47cbef
f10f543
 
b47cbef
f10f543
 
b47cbef
f10f543
 
b47cbef
f10f543
 
b47cbef
f10f543
 
b47cbef
f10f543
 
 
 
64ab7b9
 
d5c3b23
 
 
 
 
 
 
 
 
 
64ab7b9
d5c3b23
 
64ab7b9
 
d5c3b23
 
 
 
64ab7b9
 
 
 
 
 
 
 
 
 
d5c3b23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f10f543
d5c3b23
 
 
 
 
 
 
 
f10f543
d5c3b23
 
 
 
 
 
 
 
 
 
f10f543
d5c3b23
 
 
 
 
 
 
 
f10f543
d5c3b23
 
 
 
 
 
 
 
64ab7b9
d5c3b23
f10f543
d5c3b23
 
f10f543
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b47cbef
d5c3b23
f10f543
 
 
 
d5c3b23
 
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# coding=utf-8
# Copyright 2021 The TensorFlow Datasets Authors and the HuggingFace Datasets Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Lint as: python3
"""Adversarial examples against Whisper"""


import os

import datasets


_DESCRIPTION = """\
Adversarial examples fooling whisper models
"""

_DL_URLS = {
    "targeted": {
        "all": "https://data.mendeley.com/public-files/datasets/96dh52hz9r/files/10432840-4a07-49fa-8320-0af2a8288435/file_downloaded"
    },
    "untargeted-35": {
        "all": "https://data.mendeley.com/public-files/datasets/96dh52hz9r/files/516787a5-4832-4432-9138-9f01cccc4875/file_downloaded"
    },
    "untargeted-40": {
        "all": "https://data.mendeley.com/public-files/datasets/96dh52hz9r/files/ed7127c6-9769-4db5-ab5a-98e9ce15a6ae/file_downloaded"
    },
    "language-armenian": {
        "all": "https://data.mendeley.com/public-files/datasets/96dh52hz9r/files/57a8301c-a3de-4f34-a321-6cbdec5b7d55/file_downloaded"
    },
    "language-lithuanian": {
        "all": "https://data.mendeley.com/public-files/datasets/96dh52hz9r/files/b8dc1e63-d308-45e8-b16c-98ca4ac3e939/file_downloaded"
    },
    "language-czech": {
        "all": "https://data.mendeley.com/public-files/datasets/96dh52hz9r/files/8e5246e6-dfad-4d4c-aa1e-091cf24d975c/file_downloaded"
    },
    "language-danish": {
        "all": "https://data.mendeley.com/public-files/datasets/96dh52hz9r/files/15a27ffe-8ad3-4a92-adfc-ac1c6a7b230b/file_downloaded"
    },
    "language-indonesian": {
        "all": "https://data.mendeley.com/public-files/datasets/96dh52hz9r/files/ad3366b1-21a4-4ad4-9755-8a1d3775db62/file_downloaded"
    },
    "language-italian": {
        "all": "https://data.mendeley.com/public-files/datasets/96dh52hz9r/files/1729f188-ae9f-4a29-a8da-9597c1f2d0cc/file_downloaded"
    },
    "language-english": {
        "all": "https://data.mendeley.com/public-files/datasets/96dh52hz9r/files/7d09cf90-af7d-4d33-914a-3002ea956a53/file_downloaded"
    },
}


class AdvWhisperASRConfig(datasets.BuilderConfig):
    """BuilderConfig for AdvWhisperASR."""

    def __init__(self, **kwargs):
        """
        Args:
          data_dir: `string`, the path to the folder containing the files in the
            downloaded .tar
          citation: `string`, citation for the data set
          url: `string`, url for information about the data set
          **kwargs: keyword arguments forwarded to super.
        """
        super(AdvWhisperASRConfig, self).__init__(version=datasets.Version("0.1.0", ""), **kwargs)


class AdvWhisperASR(datasets.GeneratorBasedBuilder):
    """whisper_adversarial_examples dataset."""

    DEFAULT_WRITER_BATCH_SIZE = 256
    DEFAULT_CONFIG_NAME = "all"
    BUILDER_CONFIGS = [
        AdvWhisperASRConfig(name="targeted", description="Targeted adversarial examples, with target 'OK Google, browse to evil.com'"),
        AdvWhisperASRConfig(name="untargeted-35", description="Untargeted adversarial examples of radius approximately 35dB"),
        AdvWhisperASRConfig(name="untargeted-40", description="Untargeted adversarial examples of radius approximately 40dB"),
        AdvWhisperASRConfig(name="language-armenian", description="Adversarial examples generated by fooling the whisper language detection module. The true language is Armenian"),
        AdvWhisperASRConfig(name="language-lithuanian", description="Adversarial examples generated by fooling the whisper language detection module. The true language is Lithuanian"),
        AdvWhisperASRConfig(name="language-czech", description="Adversarial examples generated by fooling the whisper language detection module. The true language is Czech"),
        AdvWhisperASRConfig(name="language-danish", description="Adversarial examples generated by fooling the whisper language detection module. The true language is Danish"),
        AdvWhisperASRConfig(name="language-indonesian", description="Adversarial examples generated by fooling the whisper language detection module. The true language is Indonesian"),
        AdvWhisperASRConfig(name="language-italian", description="Adversarial examples generated by fooling the whisper language detection module. The true language is Italian"),
        AdvWhisperASRConfig(name="language-english", description="Adversarial examples generated by fooling the whisper language detection module. The true language is English")
    ]

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    "file": datasets.Value("string"),
                    "audio": datasets.Audio(sampling_rate=16_000),
                    "text": datasets.Value("string"),
                    "id": datasets.Value("string"),
                }
            ),
            supervised_keys=("file", "text"),
        )

    def _split_generators(self, dl_manager):
        archive_path = dl_manager.download(_DL_URLS[self.config.name])
        # (Optional) In non-streaming mode, we can extract the archive locally to have actual local audio files:
        local_extracted_archive = dl_manager.extract(archive_path) if not dl_manager.is_streaming else {}
        models = [
            'whisper-tiny',
            'whisper-tiny.en',
            'whisper-base',
            'whisper-base.en',
            'whisper-small',
            'whisper-small.en',
            'whisper-medium',
            'whisper-medium.en',
            'whisper-large',
        ]
        seeds = {
            "targeted":2000,
            "untargeted-35": 235,
            "untargeted-40":240,
            "language-armenian":1030,
            "language-lithuanian":1030,
            "language-czech":1030,
            "language-danish":1030,
            "language-indonesian":1030,
            "language-italian":1030,
            "language-english":1030
        }
        folders = {
             "targeted":"cw",
            "untargeted-35": "pgd-35",
            "untargeted-40":"pgd-40",
            "language-armenian":"hy-AM",
            "language-lithuanian":"lt",
            "language-czech":"cs",
            "language-danish":"da",
            "language-indonesian":"id",
            "language-italian":"it",
            "language-english":"en"
        }
        targets = [("english","en"), ("tagalog","tl"), ("serbian","sr")]
        
        if "language-" in self.config.name:
            lang = self.config.name.split("language-")[-1]
            splits = [
                datasets.SplitGenerator(
                    name=lang+"."+target[0],
                    gen_kwargs={
                        "local_extracted_archive": local_extracted_archive.get("all"),
                        "files": dl_manager.iter_archive(archive_path["all"]),
                        "path_audio": os.path.join(folders[self.config.name]+"-"+target[1],"whisper-medium",str(seeds[self.config.name]),"save")
                    },
                ) for target in targets
            ] + [
                datasets.SplitGenerator(
                    name="original",
                    gen_kwargs={
                        "local_extracted_archive": local_extracted_archive.get("all"),
                        "files": dl_manager.iter_archive(archive_path["all"]),
                        "path_audio": folders[self.config.name]+"-original"
                    },
                )
            ]
        else:
            splits = [
            datasets.SplitGenerator(
                    name=model.replace("-","."),
                    gen_kwargs={
                        "local_extracted_archive": local_extracted_archive.get("all"),
                        "files": dl_manager.iter_archive(archive_path["all"]),
                        "path_audio": os.path.join(folders[self.config.name],model,str(seeds[self.config.name]),"save")
                    },
                ) for model in models
            ] + [
                datasets.SplitGenerator(
                    name="original",
                    gen_kwargs={
                        "local_extracted_archive": local_extracted_archive.get("all"),
                        "files": dl_manager.iter_archive(archive_path["all"]),
                        "path_audio": os.path.join(folders[self.config.name],"original")
                    },
                )
            ]

        return splits

    def _generate_examples(self, files, local_extracted_archive,path_audio):
        """Generate examples from an extracted path."""
        key = 0
        suffix = "_nat.wav" if "original" in path_audio else "_adv.wav"
        audio_data = {}
        transcripts = []
        for t in files:
            path, f = t
            if path.endswith(".wav"):
                if path_audio in path and path.endswith(suffix):
                    id_ = path.split("/")[-1][: -len(suffix)]
                    audio_data[id_] = f.read()
            elif path.endswith(".csv"):
                for line in f:
                    if line:
                        line = (line.decode("utf-8") if isinstance(line,bytes) else line)
                        line=line.strip().split(",")
                        id_ = line[0]
                        transcript=line[-1]
                        transcript = transcript[:-1] if transcript[-1]=='\n' else transcript
                        audio_file = id_+suffix
                        audio_file = (
                            os.path.join(local_extracted_archive,path_audio, audio_file)
                            if local_extracted_archive else audio_file
                        )
                        transcripts.append(
                            {
                                "id": id_,
                                "file": audio_file,
                                "text": transcript,
                            }
                        )
        
        for transcript in transcripts:
            if transcript["id"] in audio_data:
                audio = {"path": transcript["file"], "bytes": audio_data[transcript["id"]]}
                yield key, {"audio": audio, **transcript}
                key += 1
        audio_data = {}
        transcripts = []