Skip to content

Commit c08b5b7

Browse files
committed
tests, file improvements, download and dir helpers
1 parent 1182fc1 commit c08b5b7

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ classifiers = [
1818
]
1919
dependencies = [
2020
"pydantic>=2.0.0",
21+
"tqdm>=4.67.0",
2122
]
2223

2324
[project.urls]

src/inferencesh/sdk.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import shutil
1616
from pathlib import Path
1717
import hashlib
18+
from tqdm import tqdm
1819

1920

2021
# inspired by https://github.com/pydantic/pydantic/issues/7580
@@ -125,7 +126,6 @@ def __init__(self, initializer=None, **data):
125126
@model_validator(mode='before')
126127
@classmethod
127128
def convert_str_to_file(cls, values):
128-
print(f"check_uri_or_path input: {values}")
129129
if isinstance(values, str): # Only accept strings
130130
return {"uri": values}
131131
elif isinstance(values, dict):
@@ -176,11 +176,22 @@ def _download_url(self) -> None:
176176
}
177177
req = urllib.request.Request(original_url, headers=headers)
178178

179-
# Download the file
179+
# Download the file with progress bar
180180
print(f"Downloading URL: {original_url} to {self._tmp_path}")
181181
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+
184195
self.path = self._tmp_path
185196
except (urllib.error.URLError, urllib.error.HTTPError) as e:
186197
raise RuntimeError(f"Failed to download URL {original_url}: {str(e)}")

0 commit comments

Comments
 (0)