Skip to content
This repository has been archived by the owner on Jan 15, 2024. It is now read-only.

Commit

Permalink
Remove gluon.utils.download copy (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
leezu authored Jul 26, 2018
1 parent 2151103 commit 6fa4e62
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 71 deletions.
2 changes: 1 addition & 1 deletion env/doc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ dependencies:
- nltk
- pandoc==2.2.1
- pip:
- mxnet-cu80>=1.3.0b20180629
- mxnet-cu80>=1.3.0b20180725
- notedown
2 changes: 1 addition & 1 deletion env/py2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ dependencies:
- flaky
- pytest-cov
- pip:
- mxnet-cu80>=1.3.0b20180629
- mxnet-cu80>=1.3.0b20180725
2 changes: 1 addition & 1 deletion env/py3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ dependencies:
- flaky
- pytest-cov
- pip:
- mxnet-cu80>=1.3.0b20180629
- mxnet-cu80>=1.3.0b20180725
70 changes: 2 additions & 68 deletions gluonnlp/data/word_embedding_evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,13 @@
import zipfile

from mxnet.gluon.data.dataset import SimpleDataset
from mxnet.gluon.utils import check_sha1, _get_repo_file_url
from mxnet.gluon.utils import check_sha1, _get_repo_file_url, download

from .. import _constants as C
from .dataset import CorpusDataset
from .registry import register
from .utils import _get_home_dir

try:
import requests
except ImportError:

class requests_failed_to_import(object):
pass

requests = requests_failed_to_import

base_datasets = [
'WordSimilarityEvaluationDataset', 'WordAnalogyEvaluationDataset'
]
Expand All @@ -52,63 +43,6 @@ class requests_failed_to_import(object):
__all__ = base_datasets + word_similarity_datasets + word_analogy_datasets


# TODO Remove once verify support is merged in mxnet.gluon.utils.download
def download(url, path=None, overwrite=False, sha1_hash=None, verify=True):
"""Download an given URL
Parameters
----------
url : str
URL to download
path : str, optional
Destination path to store downloaded file. By default stores to the
current directory with same name as in url.
overwrite : bool, optional
Whether to overwrite destination file if already exists.
sha1_hash : str, optional
Expected sha1 hash in hexadecimal digits. Will ignore existing file when hash is specified
but doesn't match.
verify : bool
Toggle verification of SSL certificates.
Returns
-------
str
The file path of the downloaded file.
"""
if path is None:
fname = url.split('/')[-1]
else:
path = os.path.expanduser(path)
if os.path.isdir(path):
fname = os.path.join(path, url.split('/')[-1])
else:
fname = path

if overwrite or not os.path.exists(fname) or (
sha1_hash and not check_sha1(fname, sha1_hash)):
dirname = os.path.dirname(os.path.abspath(os.path.expanduser(fname)))
if not os.path.exists(dirname):
os.makedirs(dirname)

print('Downloading %s from %s...' % (fname, url))
r = requests.get(url, stream=True, verify=verify)
if r.status_code != 200:
raise RuntimeError('Failed downloading url %s' % url)
with open(fname, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk: # filter out keep-alive new chunks
f.write(chunk)

if sha1_hash and not check_sha1(fname, sha1_hash):
raise UserWarning('File {} is downloaded but the content hash does not match. ' \
'The repo may be outdated or download may be incomplete. ' \
'If the "repo_url" is overridden, consider switching to ' \
'the default repo.'.format(fname))

return fname


class _Dataset(SimpleDataset):
_url = None # Dataset is retrieved from here if not cached
_archive_file = (None, None) # Archive name and checksum
Expand Down Expand Up @@ -136,7 +70,7 @@ def _download_data(self):
url = self._url
downloaded_file_path = download(url, path=self.root,
sha1_hash=archive_hash,
verify=self._verify_ssl)
verify_ssl=self._verify_ssl)

if downloaded_file_path.lower().endswith('zip'):
with zipfile.ZipFile(downloaded_file_path, 'r') as zf:
Expand Down

0 comments on commit 6fa4e62

Please sign in to comment.