Skip to content

Commit

Permalink
Improved Downloading Functionality (#6)
Browse files Browse the repository at this point in the history
Used context manager to open the file and download the file in chunks
in a session. Added a progress bar to show the progress of the download.
  • Loading branch information
MSameerAbbas authored Nov 16, 2023
1 parent 81a2c0f commit 63a88ed
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
9 changes: 7 additions & 2 deletions pranaam/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import tarfile
import requests
from .logging import get_logger
from tqdm.auto import tqdm

logger = get_logger()

Expand All @@ -16,8 +17,12 @@ def download_file(url, target, file_name):
try:
print("Download models from dataverse...")
# download the file
r = requests.get(url, allow_redirects=True)
open(file_path, "wb").write(r.content)
with requests.Session() as s:
r = s.get(REPO_BASE_URL, stream=True, allow_redirects=True)
with tqdm(total=int(r.headers['Content-Length']), unit='iB', unit_scale=True, desc=file_name, initial=0, miniters=1, ascii=True, colour='cyan', leave=True) as pbar:
with open(file_path, 'wb') as fd:
for chunk in r.iter_content(chunk_size=1024**2):
pbar.update(fd.write(chunk))
# untar
with tarfile.open(file_path, "r:gz") as tar_ref:
def is_within_directory(directory, target):
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ pandas
numpy
setuptools>=65.5.1 # not directly required, pinned by Snyk to avoid a vulnerability
wheel>=0.38.0 # not directly required, pinned by Snyk to avoid a vulnerability
tqdm

0 comments on commit 63a88ed

Please sign in to comment.