alpercagann commited on
Commit
3e50399
·
1 Parent(s): 8c0dbae

Improve download_assets.py for Google Drive handling

Browse files
Files changed (1) hide show
  1. download_assets.py +54 -37
download_assets.py CHANGED
@@ -8,7 +8,7 @@ def get_gdrive_file_id(url):
8
  match = re.search(r"d/([a-zA-Z0-9_-]+)", url) or re.search(r"id=([a-zA-Z0-9_-]+)", url)
9
  if match:
10
  return match.group(1)
11
- return None
12
 
13
  def download_gdrive_file(file_id, destination, force=False):
14
  """Download a file from Google Drive with support for large files"""
@@ -19,52 +19,69 @@ def download_gdrive_file(file_id, destination, force=False):
19
  # Make the directory if it doesn't exist
20
  os.makedirs(os.path.dirname(destination), exist_ok=True)
21
 
22
- # Setup URL
23
  url = f"https://drive.google.com/uc?export=download&id={file_id}"
24
 
25
  # Set up a session to handle cookies
26
  session = requests.Session()
27
 
28
- try:
29
- # First request to get the confirmation token for large files
 
 
 
 
 
 
30
  response = session.get(url, stream=True)
31
-
32
- # Check if there's a download confirmation page
33
- if "confirm" in response.url:
34
- # Extract confirmation token
35
- token = response.url.split("confirm=")[1].split("&")[0]
36
- url = f"{url}&confirm={token}"
37
- response = session.get(url, stream=True)
38
-
39
- # Get file size for progress bar
40
- total_size = int(response.headers.get('content-length', 0))
41
-
42
- # Download the file with progress bar
43
- print(f"Downloading to {destination} ({total_size/(1024*1024):.1f} MB)...")
44
- with open(destination, 'wb') as f:
45
- with tqdm(total=total_size, unit='B', unit_scale=True) as pbar:
46
- for chunk in response.iter_content(chunk_size=1024*1024):
47
- if chunk:
48
- f.write(chunk)
49
- pbar.update(len(chunk))
50
-
51
- print(f"Downloaded {destination} successfully!")
52
- return True
53
 
54
- except Exception as e:
55
- print(f"Error downloading {destination}: {e}")
56
- return False
 
 
 
 
 
 
 
 
 
 
 
57
 
58
- def download_test_file():
59
- """Download a small test file to verify the download functionality"""
60
- # Create test directories
 
 
 
 
 
 
 
 
 
 
 
61
  os.makedirs("assets", exist_ok=True)
 
62
 
63
- # Small test file - this is a public file ID from a small test image
64
- test_file_id = "1vOAZcbkpo_hre2g26n--lUXdwbTQp22k" # One of your fire_crackling.wav ID
 
 
 
 
 
 
 
 
 
65
 
66
- result = download_gdrive_file(test_file_id, "assets/test_download.wav")
67
- return result
68
 
69
  if __name__ == "__main__":
70
- download_test_file()
 
 
 
8
  match = re.search(r"d/([a-zA-Z0-9_-]+)", url) or re.search(r"id=([a-zA-Z0-9_-]+)", url)
9
  if match:
10
  return match.group(1)
11
+ return url # If no match, assume it's already a file ID
12
 
13
  def download_gdrive_file(file_id, destination, force=False):
14
  """Download a file from Google Drive with support for large files"""
 
19
  # Make the directory if it doesn't exist
20
  os.makedirs(os.path.dirname(destination), exist_ok=True)
21
 
22
+ # First, try the direct download URL
23
  url = f"https://drive.google.com/uc?export=download&id={file_id}"
24
 
25
  # Set up a session to handle cookies
26
  session = requests.Session()
27
 
28
+ # First request to get the confirmation token for large files
29
+ response = session.get(url, stream=True)
30
+
31
+ # Check if there's a download confirmation page
32
+ if "confirm" in response.url:
33
+ # Extract confirmation token
34
+ token = response.url.split("confirm=")[1].split("&")[0]
35
+ url = f"{url}&confirm={token}"
36
  response = session.get(url, stream=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
+ # Get file size for progress bar
39
+ total_size = int(response.headers.get('content-length', 0))
40
+
41
+ # Download the file with progress bar
42
+ print(f"Downloading to {destination} ({total_size/(1024*1024):.1f} MB)...")
43
+ with open(destination, 'wb') as f:
44
+ with tqdm(total=total_size, unit='B', unit_scale=True) as pbar:
45
+ for chunk in response.iter_content(chunk_size=1024*1024):
46
+ if chunk:
47
+ f.write(chunk)
48
+ pbar.update(len(chunk))
49
+
50
+ print(f"Downloaded {destination} successfully!")
51
+ return True
52
 
53
+ def download_all_assets():
54
+ """Download all required assets"""
55
+ # Asset file IDs from Google Drive
56
+ assets = {
57
+ "ckpts/landscape.pt": "1-oTNIjCZq3_mGI1XRfzDyCnmjXCvd0Vh",
58
+ "ckpts/greatest_hits.pt": "1wGDCB4iRFi4kf7bsFXV3qkc9_jvyNrCa",
59
+ "ckpts/audio_projector_landscape.pth": "1BdjzRJOC8bvyPgrAkJJcCaN3EEJg3STm",
60
+ "ckpts/audio_projector_gh.pth": "19Uk68PXVOjE3TJl86H-IlMaM1URhU33a",
61
+ "ckpts/CLAP_weights_2022.pth": "1VK22jxHkFwpxknxQBLd6kIgO5WxQdLFP",
62
+ "assets/fire_crackling.wav": "1vOAZcbkpo_hre2g26n--lUXdwbTQp22k",
63
+ "assets/plastic_bag.wav": "15igeDor7a47a-oluSCfO6GeUvFVl2ttb"
64
+ }
65
+
66
+ # Create directories
67
  os.makedirs("assets", exist_ok=True)
68
+ os.makedirs("ckpts", exist_ok=True)
69
 
70
+ # Download each asset
71
+ results = []
72
+ for destination, file_id in assets.items():
73
+ if not os.path.exists(destination):
74
+ try:
75
+ success = download_gdrive_file(file_id, destination)
76
+ results.append(f"{destination}: {'Success' if success else 'Failed'}")
77
+ except Exception as e:
78
+ results.append(f"{destination}: Error - {str(e)}")
79
+ else:
80
+ results.append(f"{destination}: Already exists")
81
 
82
+ return results
 
83
 
84
  if __name__ == "__main__":
85
+ results = download_all_assets()
86
+ for result in results:
87
+ print(result)