Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cli] Uploads: add progress bar #2078

Merged
merged 3 commits into from
Dec 6, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion transformers/hf_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@

import os
from os.path import expanduser
import six

import requests
import six
from requests.exceptions import HTTPError
from tqdm import tqdm

ENDPOINT = "https://huggingface.co"

Expand Down Expand Up @@ -129,10 +130,13 @@ def presign_and_upload(self, token, filename, filepath):
# Even though we presign with the correct content-type,
# the client still has to specify it when uploading the file.
with open(filepath, "rb") as f:
pf = TqdmProgressFileReader(f)

r = requests.put(urls.write, data=f, headers={
"content-type": urls.type,
})
r.raise_for_status()
pf.close()
return urls.access

def list_objs(self, token):
Expand All @@ -148,6 +152,34 @@ def list_objs(self, token):



class TqdmProgressFileReader:
"""
Wrap an io.BufferedReader `f` (such as the output of `open(…, "rb")`)
and override `f.read()` so as to display a tqdm progress bar.

see github.com/huggingface/transformers/pull/2078#discussion_r354739608
for implementation details.
"""
def __init__(
self,
f # type: io.BufferedReader
):
self.f = f
self.total_size = os.fstat(f.fileno()).st_size # type: int
self.pbar = tqdm(total=self.total_size, leave=False)
if six.PY3:
# does not work unless PY3
# no big deal as the CLI does not currently support PY2 anyways.
self.read = f.read
f.read = self._read

def _read(self, n=-1):
self.pbar.update(n)
return self.read(n)

def close(self):
self.pbar.close()



class HfFolder:
Expand Down