alpercagann commited on
Commit
1cac1fa
·
1 Parent(s): 9df780c

Add simplified download_assets.py

Browse files
Files changed (1) hide show
  1. download_assets.py +40 -54
download_assets.py CHANGED
@@ -1,7 +1,7 @@
1
  import os
2
- import re
3
  import requests
4
  from tqdm import tqdm
 
5
 
6
  def get_gdrive_file_id(url):
7
  """Extract file ID from Google Drive URL"""
@@ -10,75 +10,61 @@ def get_gdrive_file_id(url):
10
  return match.group(1)
11
  return None
12
 
13
- def download_gdrive_file(file_id, destination):
14
  """Download a file from Google Drive with support for large files"""
15
-
16
- if os.path.exists(destination):
17
  print(f"File already exists: {destination}")
18
- return
19
 
20
  # Make the directory if it doesn't exist
21
  os.makedirs(os.path.dirname(destination), exist_ok=True)
22
 
23
- # First, try the direct download URL
24
  url = f"https://drive.google.com/uc?export=download&id={file_id}"
25
 
26
  # Set up a session to handle cookies
27
  session = requests.Session()
28
 
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
 
53
- def main():
54
- """Download all required assets"""
55
- # Original sharing URLs
56
- gdrive_urls = {
57
- "assets/fire_crackling.wav": "https://drive.google.com/file/d/1vOAZcbkpo_hre2g26n--lUXdwbTQp22k/view?usp=drive_link",
58
- "assets/plastic_bag.wav": "https://drive.google.com/file/d/15igeDor7a47a-oluSCfO6GeUvFVl2ttb/view?usp=sharing",
59
- "ckpts/landscape.pt": "https://drive.google.com/file/d/1-oTNIjCZq3_mGI1XRfzDyCnmjXCvd0Vh/view?usp=drive_link",
60
- "ckpts/greatest_hits.pt": "https://drive.google.com/file/d/1wGDCB4iRFi4kf7bsFXV3qkc9_jvyNrCa/view?usp=drive_link",
61
- "ckpts/audio_projector_landscape.pth": "https://drive.google.com/file/d/1BdjzRJOC8bvyPgrAkJJcCaN3EEJg3STm/view?usp=sharing",
62
- "ckpts/audio_projector_gh.pth": "https://drive.google.com/file/d/19Uk68PXVOjE3TJl86H-IlMaM1URhU33a/view?usp=sharing",
63
- "ckpts/CLAP_weights_2022.pth": "https://drive.google.com/file/d/1VK22jxHkFwpxknxQBLd6kIgO5WxQdLFP/view?usp=sharing"
64
- }
65
-
66
- # Create necessary directories
67
  os.makedirs("assets", exist_ok=True)
68
- os.makedirs("ckpts", exist_ok=True)
69
 
70
- # Download each file
71
- for destination, url in gdrive_urls.items():
72
- file_id = get_gdrive_file_id(url)
73
- if file_id:
74
- try:
75
- download_gdrive_file(file_id, destination)
76
- except Exception as e:
77
- print(f"Error downloading {destination}: {e}")
78
- else:
79
- print(f"Could not extract file ID from {url}")
80
 
81
- print("Download process completed!")
 
82
 
83
  if __name__ == "__main__":
84
- main()
 
1
  import os
 
2
  import requests
3
  from tqdm import tqdm
4
+ import re
5
 
6
  def get_gdrive_file_id(url):
7
  """Extract file ID from Google Drive URL"""
 
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"""
15
+ if os.path.exists(destination) and not force:
 
16
  print(f"File already exists: {destination}")
17
+ return True
18
 
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()