Repository navigation
fix: download readme and respect subfolder #392
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,6 +46,37 @@ def test_local_loading(mock_static_model: StaticModel) -> None: | |
| assert mock_snapshot.call_count == 3 | ||
|
|
||
|
|
||
| def test_hub_loading_downloads_readme(tmp_path: Path, mock_tokenizer: Tokenizer) -> None: | ||
| """Test that loading from the hub fetches the README and reads the language.""" | ||
| vectors = np.random.RandomState(0).randn(len(mock_tokenizer.get_vocab()), 8) | ||
| StaticModel(vectors=vectors, tokenizer=mock_tokenizer, language=["en", "nl"]).save_pretrained(tmp_path) | ||
|
|
||
| with patch("model2vec.persistence.persistence.huggingface_hub.snapshot_download") as mock_snapshot: | ||
| mock_snapshot.return_value = tmp_path | ||
| model = StaticModel.from_pretrained("my_org/haha", force_download=True) | ||
|
|
||
| assert "README.md" in mock_snapshot.call_args.kwargs["allow_patterns"] | ||
| assert model.language == ["en", "nl"] | ||
|
|
||
|
|
||
| def test_hub_subfolder_loading(tmp_path: Path, mock_static_model: StaticModel) -> None: | ||
| """Test that loading a subfolder from the hub fetches the files in that subfolder.""" | ||
| mock_static_model.save_pretrained(tmp_path / "subfolder") | ||
|
|
||
| with patch("model2vec.persistence.persistence.huggingface_hub.snapshot_download") as mock_snapshot: | ||
| mock_snapshot.return_value = tmp_path | ||
| with patch("model2vec.persistence.persistence.maybe_get_cached_model_path") as cache: | ||
| # Cached snapshot without the subfolder files | ||
| cache.return_value = tmp_path / "other" | ||
| model = StaticModel.from_pretrained("my_org/haha", subfolder="subfolder") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Pass |
||
|
|
||
| allow_patterns = mock_snapshot.call_args.kwargs["allow_patterns"] | ||
| assert "subfolder/README.md" in allow_patterns | ||
| assert "subfolder/config.json" in allow_patterns | ||
| assert all(pattern.startswith("subfolder/") for pattern in allow_patterns) | ||
| assert model.tokens == mock_static_model.tokens | ||
|
|
||
|
|
||
| def test_garbage(mock_static_model: StaticModel) -> None: | ||
| """Test that garbage loading crashes.""" | ||
| with TemporaryDirectory() as dir_name: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With
force_download=False, a snapshot downloaded by the old code can contain all model files but noREADME.md._has_valid_layoutaccepts it, so_resolve_folderreturns before fetching the newly included README.model.languagestaysNoneeven when the hub README declares a language.Fetch missing README metadata without making a README mandatory for models that have none.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, and?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The actionable fix is to avoid returning a valid cached snapshot when its model directory lacks
README.md; let it fall through tosnapshot_downloadso the README can be fetched. Keep_has_valid_layoutunchanged, so models that genuinely have no README remain valid.This preserves the cache fast path when the README is already present, while allowing older cached snapshots to acquire it. Repositories without a README may be rechecked on later loads unless the cache layer records that the README is absent.