yangwang825 commited on
Commit
046aa08
·
verified ·
1 Parent(s): 90a171f

Update audioset.py

Browse files
Files changed (1) hide show
  1. audioset.py +26 -22
audioset.py CHANGED
@@ -10,6 +10,7 @@ import joblib
10
  import shutil
11
  import pathlib
12
  import logging
 
13
  import textwrap
14
  import datasets
15
  import itertools
@@ -152,8 +153,11 @@ class AudioSet(datasets.GeneratorBasedBuilder):
152
  HF_DATASETS_CACHE, 'confit___audioset/unbalanced', VERSION, f'unbalanced_train_segments_{partxx}_partial.*'
153
  )
154
  os.system(f"rm -rf {part_zip_files}")
155
-
156
- train_archive_path = dl_manager.extract(_output_file)
 
 
 
157
  logger.info(f"`{concat_zip_filename}` is extracted to {train_archive_path}")
158
 
159
  zip_file_url = f'https://huggingface.co/datasets/confit/audioset/resolve/main/{_EVAL_FILENAME}'
@@ -339,26 +343,26 @@ def download_file(
339
  set_writing_permissions(dest_unpack)
340
 
341
 
342
- def unpack(source, dest):
343
- dest_unpack = os.path.dirname(dest)
344
- if os.path.exists(dest_unpack):
345
- logger.info(f"{dest_unpack} already exists. Skipping extraction")
346
- else:
347
- logger.info(f"Extracting {source} to {dest_unpack}")
348
- # shutil unpack_archive does not work with tar.gz files
349
- if (
350
- source.endswith(".tar.gz")
351
- or source.endswith(".tgz")
352
- or source.endswith(".gz")
353
- ):
354
- out = dest.replace(".gz", "")
355
- with gzip.open(dest, "rb") as f_in:
356
- with open(out, "wb") as f_out:
357
- shutil.copyfileobj(f_in, f_out)
358
- else:
359
- shutil.unpack_archive(dest, dest_unpack)
360
- if write_permissions:
361
- set_writing_permissions(dest_unpack)
362
 
363
 
364
  def set_writing_permissions(folder_path):
 
10
  import shutil
11
  import pathlib
12
  import logging
13
+ import zipfile
14
  import textwrap
15
  import datasets
16
  import itertools
 
153
  HF_DATASETS_CACHE, 'confit___audioset/unbalanced', VERSION, f'unbalanced_train_segments_{partxx}_partial.*'
154
  )
155
  os.system(f"rm -rf {part_zip_files}")
156
+
157
+ train_archive_path = os.path.join(
158
+ HF_DATASETS_CACHE, 'confit___audioset/unbalanced', VERSION, 'extracted'
159
+ )
160
+ unzip_file(zip_path=_output_file, extract_to=train_archive_path)
161
  logger.info(f"`{concat_zip_filename}` is extracted to {train_archive_path}")
162
 
163
  zip_file_url = f'https://huggingface.co/datasets/confit/audioset/resolve/main/{_EVAL_FILENAME}'
 
343
  set_writing_permissions(dest_unpack)
344
 
345
 
346
+ def unzip_file(zip_path, extract_to):
347
+ """
348
+ Unzips a given zip file to a specified directory.
349
+
350
+ Parameters:
351
+ zip_path (str): The path to the zip file.
352
+ extract_to (str): The directory to extract the files to.
353
+ """
354
+ if os.path.exists(extract_to):
355
+ logger.info(f"{extract_to} already exists. Skipping extraction")
356
+
357
+ if not os.path.exists(zip_path):
358
+ raise FileNotFoundError(f"The file {zip_path} does not exist.")
359
+
360
+ if not os.path.exists(extract_to):
361
+ os.makedirs(extract_to)
362
+
363
+ with zipfile.ZipFile(zip_path, 'r') as zip_ref:
364
+ zip_ref.extractall(extract_to)
365
+ logger.info(f"Extracted {zip_path} to {extract_to}")
366
 
367
 
368
  def set_writing_permissions(folder_path):