Skip to content

Use progress bar for downloads #425

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

Merged
merged 1 commit into from
Sep 27, 2018
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
24 changes: 16 additions & 8 deletions torchtext/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import six
import requests
import csv
from tqdm import tqdm


def reporthook(t):
Expand All @@ -25,11 +26,22 @@ def inner(b=1, bsize=1, tsize=None):

def download_from_url(url, path):
"""Download file, with logic (from tensor2tensor) for Google Drive"""
if 'drive.google.com' not in url:
r = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'})
def process_response(r):
chunk_size = 16 * 1024
total_size = int(r.headers.get('Content-length', 0))
with open(path, "wb") as file:
file.write(r.content)
with tqdm(total=total_size, unit='B',
unit_scale=1, desc=path.split('/')[-1]) as t:
for chunk in r.iter_content(chunk_size):
if chunk:
file.write(chunk)
t.update(len(chunk))

if 'drive.google.com' not in url:
response = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'}, stream=True)
process_response(response)
return

print('downloading from Google Drive; may take a few minutes')
confirm_token = None
session = requests.Session()
Expand All @@ -42,11 +54,7 @@ def download_from_url(url, path):
url = url + "&confirm=" + confirm_token
response = session.get(url, stream=True)

chunk_size = 16 * 1024
with open(path, "wb") as f:
for chunk in response.iter_content(chunk_size):
if chunk:
f.write(chunk)
process_response(response)


def unicode_csv_reader(unicode_csv_data, **kwargs):
Expand Down