RaphaelOlivier commited on
Commit
f10f543
·
1 Parent(s): 64ab7b9

Make streaming compatible

Browse files
Files changed (1) hide show
  1. whisper_adversarial_examples.py +70 -31
whisper_adversarial_examples.py CHANGED
@@ -28,7 +28,7 @@ Adversarial examples fooling whisper models
28
  """
29
 
30
 
31
- _DL_URLS = {
32
  "targeted": {
33
  "all": "https://data.mendeley.com/api/datasets/96dh52hz9r/draft/files/75f06ad3-4f86-4f4b-b748-ea0e94f23379?a=ee30841f-1832-41ec-bdac-bf3e5b67073c"
34
  },
@@ -62,6 +62,40 @@ _DL_URLS = {
62
  }
63
 
64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  class AdvWhisperASRConfig(datasets.BuilderConfig):
66
  """BuilderConfig for AdvWhisperASR."""
67
 
@@ -158,7 +192,7 @@ class AdvWhisperASR(datasets.GeneratorBasedBuilder):
158
  name=lang+"."+target[0],
159
  gen_kwargs={
160
  "local_extracted_archive": local_extracted_archive.get("all"),
161
- "files": dl_manager.iter_files(local_extracted_archive.get("all")),
162
  "path_audio": os.path.join(folders[self.config.name]+"-"+target[1],"whisper-medium",str(seeds[self.config.name]),"save")
163
  },
164
  ) for target in targets
@@ -167,7 +201,7 @@ class AdvWhisperASR(datasets.GeneratorBasedBuilder):
167
  name="original",
168
  gen_kwargs={
169
  "local_extracted_archive": local_extracted_archive.get("all"),
170
- "files": dl_manager.iter_files(local_extracted_archive.get("all")),
171
  "path_audio": folders[self.config.name]+"-original"
172
  },
173
  )
@@ -178,7 +212,7 @@ class AdvWhisperASR(datasets.GeneratorBasedBuilder):
178
  name=model.replace("-","."),
179
  gen_kwargs={
180
  "local_extracted_archive": local_extracted_archive.get("all"),
181
- "files": dl_manager.iter_files(local_extracted_archive.get("all")),
182
  "path_audio": os.path.join(folders[self.config.name],model,str(seeds[self.config.name]),"save")
183
  },
184
  ) for model in models
@@ -187,7 +221,7 @@ class AdvWhisperASR(datasets.GeneratorBasedBuilder):
187
  name="original",
188
  gen_kwargs={
189
  "local_extracted_archive": local_extracted_archive.get("all"),
190
- "files": dl_manager.iter_files(local_extracted_archive.get("all")),
191
  "path_audio": os.path.join(folders[self.config.name],"original")
192
  },
193
  )
@@ -198,34 +232,39 @@ class AdvWhisperASR(datasets.GeneratorBasedBuilder):
198
  def _generate_examples(self, files, local_extracted_archive,path_audio):
199
  """Generate examples from an extracted path."""
200
  key = 0
 
201
  audio_data = {}
202
  transcripts = []
203
- for path in files:
204
- if path.endswith(".csv"):
205
- with open(path,'r') as f:
206
- for line in f:
207
- if line:
208
- line = (line.decode("utf-8") if isinstance(line,bytes) else line)
209
- line=line.strip().split(",")
210
- id_ = line[0]
211
- transcript=line[-1]
212
- transcript = transcript[:-1] if transcript[-1]=='\n' else transcript
213
- suffix = "_nat.wav" if "original" in path_audio else "_adv.wav"
214
- audio_file = id_+suffix
215
- audio_file = os.path.join(local_extracted_archive,path_audio, audio_file)
216
- if os.path.exists(audio_file):
217
- with open(audio_file,"rb") as f:
218
- audio_data[id_] = f.read()
219
- transcripts.append(
220
- {
221
- "id": id_,
222
- "file": audio_file,
223
- "text": transcript,
224
- }
225
- )
 
 
 
226
  for transcript in transcripts:
227
- audio = {"path": transcript["file"], "bytes": audio_data[transcript["id"]]}
228
- yield key, {"audio": audio, **transcript}
229
- key += 1
 
230
  audio_data = {}
231
  transcripts = []
 
28
  """
29
 
30
 
31
+ _DL_URLS0 = {
32
  "targeted": {
33
  "all": "https://data.mendeley.com/api/datasets/96dh52hz9r/draft/files/75f06ad3-4f86-4f4b-b748-ea0e94f23379?a=ee30841f-1832-41ec-bdac-bf3e5b67073c"
34
  },
 
62
  }
63
 
64
 
65
+ _DL_URLS = {
66
+ "targeted": {
67
+ "all": "https://data.mendeley.com/api/datasets/96dh52hz9r/draft/files/ca70a5b6-ec76-4e6f-8a67-982e411da265?a=ee30841f-1832-41ec-bdac-bf3e5b67073c"
68
+ },
69
+ "untargeted-35": {
70
+ "all": "https://data.mendeley.com/api/datasets/96dh52hz9r/draft/files/516787a5-4832-4432-9138-9f01cccc4875?a=ee30841f-1832-41ec-bdac-bf3e5b67073c"
71
+ },
72
+ "untargeted-40": {
73
+ "all": "https://data.mendeley.com/api/datasets/96dh52hz9r/draft/files/ed7127c6-9769-4db5-ab5a-98e9ce15a6ae?a=ee30841f-1832-41ec-bdac-bf3e5b67073c"
74
+ },
75
+ "language-armenian": {
76
+ "all": "https://data.mendeley.com/api/datasets/96dh52hz9r/draft/files/57a8301c-a3de-4f34-a321-6cbdec5b7d55?a=ee30841f-1832-41ec-bdac-bf3e5b67073c"
77
+ },
78
+ "language-lithuanian": {
79
+ "all": "https://data.mendeley.com/api/datasets/96dh52hz9r/draft/files/b8dc1e63-d308-45e8-b16c-98ca4ac3e939?a=ee30841f-1832-41ec-bdac-bf3e5b67073c"
80
+ },
81
+ "language-czech": {
82
+ "all": "https://data.mendeley.com/api/datasets/96dh52hz9r/draft/files/8e5246e6-dfad-4d4c-aa1e-091cf24d975c?a=ee30841f-1832-41ec-bdac-bf3e5b67073c"
83
+ },
84
+ "language-danish": {
85
+ "all": "https://data.mendeley.com/api/datasets/96dh52hz9r/draft/files/15a27ffe-8ad3-4a92-adfc-ac1c6a7b230b?a=ee30841f-1832-41ec-bdac-bf3e5b67073c"
86
+ },
87
+ "language-indonesian": {
88
+ "all": "https://data.mendeley.com/api/datasets/96dh52hz9r/draft/files/ad3366b1-21a4-4ad4-9755-8a1d3775db62?a=ee30841f-1832-41ec-bdac-bf3e5b67073c"
89
+ },
90
+ "language-italian": {
91
+ "all": "https://data.mendeley.com/api/datasets/96dh52hz9r/draft/files/1729f188-ae9f-4a29-a8da-9597c1f2d0cc?a=ee30841f-1832-41ec-bdac-bf3e5b67073c"
92
+ },
93
+ "language-english": {
94
+ "all": "https://data.mendeley.com/api/datasets/96dh52hz9r/draft/files/7d09cf90-af7d-4d33-914a-3002ea956a53?a=ee30841f-1832-41ec-bdac-bf3e5b67073c"
95
+ },
96
+ }
97
+
98
+
99
  class AdvWhisperASRConfig(datasets.BuilderConfig):
100
  """BuilderConfig for AdvWhisperASR."""
101
 
 
192
  name=lang+"."+target[0],
193
  gen_kwargs={
194
  "local_extracted_archive": local_extracted_archive.get("all"),
195
+ "files": dl_manager.iter_archive(archive_path["all"]),
196
  "path_audio": os.path.join(folders[self.config.name]+"-"+target[1],"whisper-medium",str(seeds[self.config.name]),"save")
197
  },
198
  ) for target in targets
 
201
  name="original",
202
  gen_kwargs={
203
  "local_extracted_archive": local_extracted_archive.get("all"),
204
+ "files": dl_manager.iter_archive(archive_path["all"]),
205
  "path_audio": folders[self.config.name]+"-original"
206
  },
207
  )
 
212
  name=model.replace("-","."),
213
  gen_kwargs={
214
  "local_extracted_archive": local_extracted_archive.get("all"),
215
+ "files": dl_manager.iter_archive(archive_path["all"]),
216
  "path_audio": os.path.join(folders[self.config.name],model,str(seeds[self.config.name]),"save")
217
  },
218
  ) for model in models
 
221
  name="original",
222
  gen_kwargs={
223
  "local_extracted_archive": local_extracted_archive.get("all"),
224
+ "files": dl_manager.iter_archive(archive_path["all"]),
225
  "path_audio": os.path.join(folders[self.config.name],"original")
226
  },
227
  )
 
232
  def _generate_examples(self, files, local_extracted_archive,path_audio):
233
  """Generate examples from an extracted path."""
234
  key = 0
235
+ suffix = "_nat.wav" if "original" in path_audio else "_adv.wav"
236
  audio_data = {}
237
  transcripts = []
238
+ for t in files:
239
+ path, f = t
240
+ if path.endswith(".wav"):
241
+ if path_audio in path and path.endswith(suffix):
242
+ id_ = path.split("/")[-1][: -len(suffix)]
243
+ audio_data[id_] = f.read()
244
+ elif path.endswith(".csv"):
245
+ for line in f:
246
+ if line:
247
+ line = (line.decode("utf-8") if isinstance(line,bytes) else line)
248
+ line=line.strip().split(",")
249
+ id_ = line[0]
250
+ transcript=line[-1]
251
+ transcript = transcript[:-1] if transcript[-1]=='\n' else transcript
252
+ audio_file = id_+suffix
253
+ audio_file = (
254
+ os.path.join(local_extracted_archive,path_audio, audio_file)
255
+ if local_extracted_archive else audio_file
256
+ )
257
+ transcripts.append(
258
+ {
259
+ "id": id_,
260
+ "file": audio_file,
261
+ "text": transcript,
262
+ }
263
+ )
264
  for transcript in transcripts:
265
+ if transcript["id"] in audio_data:
266
+ audio = {"path": transcript["file"], "bytes": audio_data[transcript["id"]]}
267
+ yield key, {"audio": audio, **transcript}
268
+ key += 1
269
  audio_data = {}
270
  transcripts = []