Skip to content
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion brainscore_language/models/glove/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ def _prepare_weights(name):
if not weights_file.is_file():
# download zip
zip_filename = f"{name}.zip"
url = f'https://nlp.stanford.edu/data/{zip_filename}'
# Stanford's official mirror hosts the original pretrained vectors.
url = f'https://huggingface.co/stanfordnlp/glove/resolve/main/{zip_filename}'
_logger.info(f"Downloading weights zip from {url} to {zip_filename}")
urlretrieve(url, zip_filename)
# unzip
Expand Down
43 changes: 43 additions & 0 deletions tests/test_model_helpers/test_glove_download.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""Check download routing with a tiny archive, without model inference."""
import importlib.util
from pathlib import Path
import zipfile

import pytest


@pytest.fixture
def glove_module(tmp_path, monkeypatch):
source = Path(__file__).parents[2] / 'brainscore_language/models/glove/model.py'
spec = importlib.util.spec_from_file_location('glove_download_under_test', source)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
monkeypatch.setattr(module, '__file__', str(tmp_path / 'model.py'))
monkeypatch.chdir(tmp_path)
return module


def test_download_original_vectors_from_official_host(glove_module, monkeypatch):
calls = []

def download(url, filename):
calls.append(url)
with zipfile.ZipFile(filename, 'w') as archive:
archive.writestr('glove.840B.300d.txt', 'test 0.1 0.2\n')

monkeypatch.setattr(glove_module, 'urlretrieve', download)
result = glove_module._prepare_weights('glove.840B.300d')
assert result.read_text() == 'test 0.1 0.2\n'
assert calls == ['https://huggingface.co/stanfordnlp/glove/resolve/main/glove.840B.300d.zip']
assert not Path('glove.840B.300d.zip').exists()
assert glove_module._prepare_weights('glove.840B.300d') == result
assert len(calls) == 1


def test_download_failure_remains_a_failure(glove_module, monkeypatch):
def download(*args):
raise OSError('unreachable')

monkeypatch.setattr(glove_module, 'urlretrieve', download)
with pytest.raises(OSError, match='unreachable'):
glove_module._prepare_weights('glove.840B.300d')
Loading