TobiasLogic commited on
Commit
c63ec25
·
verified ·
1 Parent(s): 7a76816

Upload fetch_clotho.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. fetch_clotho.py +71 -0
fetch_clotho.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import argparse
4
+ import os
5
+
6
+ import py7zr
7
+ import requests
8
+
9
+ RECORD = "4783391"
10
+ FILES = {
11
+ "development": ("clotho_audio_development.7z", "clotho_captions_development.csv"),
12
+ "validation": ("clotho_audio_validation.7z", "clotho_captions_validation.csv"),
13
+ "evaluation": ("clotho_audio_evaluation.7z", "clotho_captions_evaluation.csv"),
14
+ }
15
+
16
+ def zenodo_files():
17
+ r = requests.get(f"https://zenodo.org/api/records/{RECORD}", timeout=30)
18
+ r.raise_for_status()
19
+ return {f["key"]: (f["links"]["self"], f["size"]) for f in r.json()["files"]}
20
+
21
+ def download(url, dest, expected_size=None):
22
+ if os.path.exists(dest) and (expected_size is None or os.path.getsize(dest) == expected_size):
23
+ print(f"[fetch] {dest} already present ({os.path.getsize(dest)} bytes), skipping", flush=True)
24
+ return
25
+ with requests.get(url, stream=True, timeout=60) as r:
26
+ r.raise_for_status()
27
+ total = int(r.headers.get("content-length", 0))
28
+ done = 0
29
+ with open(dest + ".part", "wb") as f:
30
+ for chunk in r.iter_content(chunk_size=1 << 20):
31
+ f.write(chunk)
32
+ done += len(chunk)
33
+ if total:
34
+ print(f"\r[fetch] {os.path.basename(dest)} {done/1e6:.0f}/{total/1e6:.0f} MB",
35
+ end="", flush=True)
36
+ print(flush=True)
37
+ os.rename(dest + ".part", dest)
38
+ if expected_size is not None and os.path.getsize(dest) != expected_size:
39
+ raise RuntimeError(f"{dest}: size {os.path.getsize(dest)} != expected {expected_size}")
40
+
41
+ def main():
42
+ ap = argparse.ArgumentParser()
43
+ ap.add_argument("--root", default="/root/clotho_raw")
44
+ ap.add_argument("--splits", nargs="+", default=["development", "validation", "evaluation"])
45
+ args = ap.parse_args()
46
+ os.makedirs(args.root, exist_ok=True)
47
+
48
+ links = zenodo_files()
49
+ for split in args.splits:
50
+ audio_key, caption_key = FILES[split]
51
+ audio_path = os.path.join(args.root, audio_key)
52
+ caption_path = os.path.join(args.root, caption_key)
53
+
54
+ audio_url, audio_size = links[audio_key]
55
+ caption_url, caption_size = links[caption_key]
56
+ print(f"[fetch] {split}: {audio_key} ({audio_size/1e6:.0f} MB)", flush=True)
57
+ download(audio_url, audio_path, audio_size)
58
+ download(caption_url, caption_path, caption_size)
59
+
60
+ extract_dir = os.path.join(args.root, split)
61
+ if not os.path.isdir(extract_dir):
62
+ print(f"[fetch] extracting {audio_key}", flush=True)
63
+ with py7zr.SevenZipFile(audio_path, mode="r") as z:
64
+ z.extractall(path=args.root)
65
+ wavs = [f for f in os.listdir(extract_dir) if f.endswith(".wav")]
66
+ print(f"[fetch] {split}: {len(wavs)} wav files in {extract_dir}", flush=True)
67
+
68
+ print("FETCHDONE", flush=True)
69
+
70
+ if __name__ == "__main__":
71
+ main()