|
15 | 15 | import shutil |
16 | 16 | from pathlib import Path |
17 | 17 | import hashlib |
| 18 | +from tqdm import tqdm |
18 | 19 |
|
19 | 20 |
|
20 | 21 | # inspired by https://github.com/pydantic/pydantic/issues/7580 |
@@ -125,7 +126,6 @@ def __init__(self, initializer=None, **data): |
125 | 126 | @model_validator(mode='before') |
126 | 127 | @classmethod |
127 | 128 | def convert_str_to_file(cls, values): |
128 | | - print(f"check_uri_or_path input: {values}") |
129 | 129 | if isinstance(values, str): # Only accept strings |
130 | 130 | return {"uri": values} |
131 | 131 | elif isinstance(values, dict): |
@@ -176,11 +176,22 @@ def _download_url(self) -> None: |
176 | 176 | } |
177 | 177 | req = urllib.request.Request(original_url, headers=headers) |
178 | 178 |
|
179 | | - # Download the file |
| 179 | + # Download the file with progress bar |
180 | 180 | print(f"Downloading URL: {original_url} to {self._tmp_path}") |
181 | 181 | try: |
182 | | - with urllib.request.urlopen(req) as response, open(self._tmp_path, 'wb') as out_file: |
183 | | - out_file.write(response.read()) |
| 182 | + with urllib.request.urlopen(req) as response: |
| 183 | + total_size = int(response.headers.get('content-length', 0)) |
| 184 | + block_size = 1024 # 1 Kibibyte |
| 185 | + |
| 186 | + with tqdm(total=total_size, unit='iB', unit_scale=True) as pbar: |
| 187 | + with open(self._tmp_path, 'wb') as out_file: |
| 188 | + while True: |
| 189 | + buffer = response.read(block_size) |
| 190 | + if not buffer: |
| 191 | + break |
| 192 | + out_file.write(buffer) |
| 193 | + pbar.update(len(buffer)) |
| 194 | + |
184 | 195 | self.path = self._tmp_path |
185 | 196 | except (urllib.error.URLError, urllib.error.HTTPError) as e: |
186 | 197 | raise RuntimeError(f"Failed to download URL {original_url}: {str(e)}") |
|
0 commit comments