Skip to content

Commit e8c8603

Browse files
authored
fix(inference): pass the token through when loading the encoder (#362)
* fix(inference): pass the token to the encoder download * test: cover token passthrough on both pipeline load paths
1 parent 7892d9b commit e8c8603

2 files changed

Lines changed: 45 additions & 2 deletions

File tree

model2vec/inference/model.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ def _load_pipeline(folder_or_repo_path: PathLike, token: str | None = None) -> t
221221
except EntryNotFoundError:
222222
return _load_legacy_pipeline(folder_or_repo_path, token)
223223

224-
model = StaticModel.from_pretrained(folder_or_repo_path)
224+
model = StaticModel.from_pretrained(folder_or_repo_path, token=token)
225225

226226
head_config = model.config.get("head_config", {})
227227
activation = Activation(head_config.get("activation", Activation.IDENTITY.value))
@@ -291,7 +291,7 @@ def convert_legacy_pipeline(
291291
folder_or_repo_path.as_posix(), _LEGACY_HEAD_FILENAME, token=token
292292
)
293293

294-
model = StaticModel.from_pretrained(folder_or_repo_path)
294+
model = StaticModel.from_pretrained(folder_or_repo_path, token=token)
295295
model.embedding = np.nan_to_num(model.embedding)
296296

297297
untrusted_types = skops.io.get_untrusted_types(file=legacy_head_path)

tests/test_inference.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,24 @@ def test_load_pipeline_from_hub(mock_inference_pipeline: StaticModelPipeline) ->
135135
assert loaded.predict(["dog"]).tolist() == mock_inference_pipeline.predict(["dog"]).tolist()
136136

137137

138+
def test_load_pipeline_from_hub_with_token(mock_inference_pipeline: StaticModelPipeline) -> None:
139+
"""Test that the token also reaches the encoder download, not just the head download."""
140+
with TemporaryDirectory() as temp_dir:
141+
mock_inference_pipeline.save_pretrained(temp_dir)
142+
downloaded_model = StaticModel.from_pretrained(temp_dir)
143+
head_path = os.path.join(temp_dir, "head.safetensors")
144+
145+
with (
146+
patch("model2vec.inference.model.huggingface_hub.hf_hub_download", return_value=head_path),
147+
patch(
148+
"model2vec.inference.model.StaticModel.from_pretrained", return_value=downloaded_model
149+
) as mock_from_pretrained,
150+
):
151+
StaticModelPipeline.from_pretrained("fake/repo-id", token="secret")
152+
153+
assert mock_from_pretrained.call_args.kwargs.get("token") == "secret"
154+
155+
138156
def test_push_to_hub(mock_inference_pipeline: StaticModelPipeline) -> None:
139157
"""Test that push_to_hub saves the pipeline to a temp folder before pushing it to the hub."""
140158
captured: dict[str, object] = {}
@@ -278,6 +296,31 @@ def _fake_download(repo_id: str, filename: str, token: str | None = None) -> str
278296
assert np.allclose(loaded.predict(["dog", "cat"]), legacy_pipeline.predict(encoded))
279297

280298

299+
def test_convert_legacy_pipeline_with_token(mock_static_model: StaticModel) -> None:
300+
"""Test that the token also reaches the encoder download on the legacy path."""
301+
rng = np.random.RandomState(0)
302+
X = rng.randn(30, mock_static_model.dim)
303+
y = rng.randn(30, 4)
304+
legacy_pipeline = make_pipeline(MLPRegressor(hidden_layer_sizes=(8,), max_iter=1, random_state=0).fit(X, y))
305+
306+
with TemporaryDirectory() as temp_dir:
307+
_dump_legacy_pipeline(temp_dir, mock_static_model, legacy_pipeline)
308+
downloaded_model = StaticModel.from_pretrained(temp_dir)
309+
310+
with (
311+
patch(
312+
"model2vec.inference.model.huggingface_hub.hf_hub_download",
313+
return_value=os.path.join(temp_dir, "pipeline.skops"),
314+
),
315+
patch(
316+
"model2vec.inference.model.StaticModel.from_pretrained", return_value=downloaded_model
317+
) as mock_from_pretrained,
318+
):
319+
convert_legacy_pipeline("fake/repo-id", token="secret")
320+
321+
assert mock_from_pretrained.call_args.kwargs.get("token") == "secret"
322+
323+
281324
def test_convert_legacy_pipeline_untrusted_type(mock_static_model: StaticModel) -> None:
282325
"""Test that an untrusted type in the legacy pipeline is rejected by default."""
283326
rng = np.random.RandomState(0)

0 commit comments

Comments
 (0)