Skip to content

Commit 55a410e

Browse files
committed
fix readme, subfolder, add test
1 parent 1cb6c79 commit 55a410e

2 files changed

Lines changed: 19 additions & 7 deletions

File tree

model2vec/persistence/persistence.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,13 @@ def load_pretrained(
101101
folder_or_repo_path = Path(folder_or_repo_path)
102102

103103
# We resolve a folder or repo path to an actual local folder.
104-
folder = _resolve_folder(
105-
folder_or_repo_path=folder_or_repo_path, token=token, force_download=force_download, subfolder=subfolder
106-
)
104+
folder = _resolve_folder(folder_or_repo_path=folder_or_repo_path, token=token, force_download=force_download)
105+
106+
if subfolder:
107+
folder = folder / subfolder
107108

108109
selected_layout = _get_paths(folder)
109-
readme_path = folder_or_repo_path / "README.md"
110+
readme_path = folder / "README.md"
110111

111112
opened_tensor_file = cast(SafeOpenProtocol, safetensors.safe_open(selected_layout.embeddings, framework="numpy"))
112113
embedding_name = "embedding.weight" if selected_layout.is_sentence_transformers else "embeddings"
@@ -133,7 +134,7 @@ def load_pretrained(
133134
return embeddings, tokenizer, config, metadata, weights, mapping
134135

135136

136-
def _resolve_folder(folder_or_repo_path: Path, subfolder: str | None, token: str | None, force_download: bool) -> Path:
137+
def _resolve_folder(folder_or_repo_path: Path, token: str | None, force_download: bool) -> Path:
137138
"""Resolve a folder locally or from hugging face hub."""
138139
if folder_or_repo_path.exists():
139140
return folder_or_repo_path

tests/test_persistence.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ def test_local_loading(mock_static_model: StaticModel) -> None:
1717
with patch(
1818
"model2vec.persistence.persistence.huggingface_hub.snapshot_download",
1919
) as mock_snapshot:
20-
mock_snapshot.return_value = dir_name
20+
mock_snapshot.return_value = Path(dir_name)
2121
# Patch the cache to return the local path, simulate a cache hit
2222
# we pass a fake model name to actually hit the cache and snapshot download paths
2323
with patch("model2vec.persistence.persistence.maybe_get_cached_model_path") as cache:
2424
# Simulate cache hit
25-
cache.return_value = dir_name
25+
cache.return_value = Path(dir_name)
2626
s = StaticModel.from_pretrained("haha", force_download=True)
2727
assert s.tokens == mock_static_model.tokens
2828
s = StaticModel.from_pretrained("haha", force_download=False)
@@ -52,6 +52,17 @@ def test_garbage(mock_static_model: StaticModel) -> None:
5252
StaticModel.from_pretrained(dir_name)
5353

5454

55+
def test_subfolder_loading(mock_static_model: StaticModel) -> None:
56+
"""Test that garbage loading crashes."""
57+
with TemporaryDirectory() as dir_name:
58+
dir_name_path = Path(dir_name)
59+
mock_static_model.save_pretrained(dir_name_path / "subfolder")
60+
with pytest.raises(ValueError):
61+
StaticModel.from_pretrained(dir_name)
62+
model = StaticModel.from_pretrained(dir_name, subfolder="subfolder")
63+
assert isinstance(model, StaticModel)
64+
65+
5566
def test_maybe_get_cached_model_path() -> None:
5667
"""Test cached model path."""
5768
model_id = "t/t"

0 commit comments

Comments
 (0)