Skip to content
Snippets Groups Projects
Commit f3f0c4a8 authored by Mateusz Klimaszewski's avatar Mateusz Klimaszewski
Browse files

Cleaner download bar.

parent 8d0fd6c9
Branches
Tags
2 merge requests!20Release 1.0.0b2.,!19Release 1.0.0b2.
import errno import errno
import logging import logging
import math
import os import os
import requests import requests
...@@ -23,18 +22,18 @@ def download_file(model_name, force=False): ...@@ -23,18 +22,18 @@ def download_file(model_name, force=False):
if os.path.exists(location) and not force: if os.path.exists(location) and not force:
logger.debug("Using cached model.") logger.debug("Using cached model.")
return location return location
chunk_size = 1024 * 10 chunk_size = 1024
logger.info(url) logger.info(url)
try: try:
with _requests_retry_session(retries=2).get(url, stream=True) as r: with _requests_retry_session(retries=2).get(url, stream=True) as r:
total_length = math.ceil(int(r.headers.get("content-length")) / chunk_size) pbar = tqdm.tqdm(unit="B", total=int(r.headers.get("content-length")),
unit_divisor=chunk_size, unit_scale=True)
with open(location, "wb") as f: with open(location, "wb") as f:
with tqdm.tqdm(total=total_length) as pbar: with pbar:
for chunk in r.raw.stream(chunk_size, decode_content=False): for chunk in r.iter_content(chunk_size):
if chunk: if chunk:
f.write(chunk) f.write(chunk)
f.flush() pbar.update(len(chunk))
pbar.update(1)
except exceptions.RetryError: except exceptions.RetryError:
raise ConnectionError(f"Couldn't find or download model {model_name}.tar.gz. " raise ConnectionError(f"Couldn't find or download model {model_name}.tar.gz. "
"Check if model name is correct or try again later!") "Check if model name is correct or try again later!")
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment