Download a file with progress bar
(url, destination)
| 112 | |
| 113 | |
| 114 | def download_file(url, destination): |
| 115 | """Download a file with progress bar""" |
| 116 | try: |
| 117 | response = requests.get(url, stream=True) |
| 118 | response.raise_for_status() # Raise an exception for bad status codes |
| 119 | |
| 120 | total_size = int(response.headers.get("content-length", 0)) |
| 121 | |
| 122 | with ( |
| 123 | open(destination, "wb") as f, |
| 124 | tqdm( |
| 125 | desc=os.path.basename(destination), |
| 126 | total=total_size, |
| 127 | unit="iB", |
| 128 | unit_scale=True, |
| 129 | unit_divisor=1024, |
| 130 | ) as pbar, |
| 131 | ): |
| 132 | for data in response.iter_content(chunk_size=1024): |
| 133 | size = f.write(data) |
| 134 | pbar.update(size) |
| 135 | except requests.exceptions.RequestException as e: |
| 136 | print(f"Error downloading file: {e}") |
| 137 | if os.path.exists(destination): |
| 138 | os.remove(destination) |
| 139 | raise |
| 140 | |
| 141 | |
| 142 | def prepare_glove_dataset(dataset_dir): |
no test coverage detected