|
5 | 5 |
|
6 | 6 |
|
7 | 7 | import os |
8 | | -import urllib.request |
9 | 8 |
|
10 | | -# import requests |
| 9 | +import requests |
11 | 10 | import json |
12 | 11 | import numpy as np |
13 | 12 | import tensorflow as tf |
@@ -48,44 +47,40 @@ def download_and_load_gpt2(model_size, models_dir): |
48 | 47 |
|
49 | 48 | def download_file(url, destination, backup_url=None): |
50 | 49 | def _attempt_download(download_url): |
51 | | - with urllib.request.urlopen(download_url) as response: |
52 | | - # Get the total file size from headers, defaulting to 0 if not present |
53 | | - file_size = int(response.headers.get("Content-Length", 0)) |
54 | | - |
55 | | - # Check if file exists and has the same size |
56 | | - if os.path.exists(destination): |
57 | | - file_size_local = os.path.getsize(destination) |
58 | | - if file_size == file_size_local: |
59 | | - print(f"File already exists and is up-to-date: {destination}") |
60 | | - return True # Indicate success without re-downloading |
61 | | - |
62 | | - block_size = 1024 # 1 Kilobyte |
63 | | - |
64 | | - # Initialize the progress bar with total file size |
65 | | - progress_bar_description = os.path.basename(download_url) |
66 | | - with tqdm(total=file_size, unit="iB", unit_scale=True, desc=progress_bar_description) as progress_bar: |
67 | | - with open(destination, "wb") as file: |
68 | | - while True: |
69 | | - chunk = response.read(block_size) |
70 | | - if not chunk: |
71 | | - break |
| 50 | + response = requests.get(download_url, stream=True, timeout=60) |
| 51 | + response.raise_for_status() |
| 52 | + |
| 53 | + file_size = int(response.headers.get("Content-Length", 0)) |
| 54 | + |
| 55 | + # Check if file exists and has same size |
| 56 | + if os.path.exists(destination): |
| 57 | + file_size_local = os.path.getsize(destination) |
| 58 | + if file_size and file_size == file_size_local: |
| 59 | + print(f"File already exists and is up-to-date: {destination}") |
| 60 | + return True |
| 61 | + |
| 62 | + block_size = 1024 # 1 KB |
| 63 | + desc = os.path.basename(download_url) |
| 64 | + with tqdm(total=file_size, unit="iB", unit_scale=True, desc=desc) as progress_bar: |
| 65 | + with open(destination, "wb") as file: |
| 66 | + for chunk in response.iter_content(chunk_size=block_size): |
| 67 | + if chunk: |
72 | 68 | file.write(chunk) |
73 | 69 | progress_bar.update(len(chunk)) |
74 | | - return True |
| 70 | + return True |
75 | 71 |
|
76 | 72 | try: |
77 | 73 | if _attempt_download(url): |
78 | 74 | return |
79 | | - except (urllib.error.HTTPError, urllib.error.URLError): |
| 75 | + except requests.exceptions.RequestException: |
80 | 76 | if backup_url is not None: |
81 | 77 | print(f"Primary URL ({url}) failed. Attempting backup URL: {backup_url}") |
82 | 78 | try: |
83 | 79 | if _attempt_download(backup_url): |
84 | 80 | return |
85 | | - except urllib.error.HTTPError: |
| 81 | + except requests.exceptions.RequestException: |
86 | 82 | pass |
87 | 83 |
|
88 | | - # If we reach here, both attempts have failed |
89 | 84 | error_message = ( |
90 | 85 | f"Failed to download from both primary URL ({url})" |
91 | 86 | f"{' and backup URL (' + backup_url + ')' if backup_url else ''}." |
|
0 commit comments