44from collections .abc import Sequence
55from pathlib import Path
66from tempfile import TemporaryDirectory
7- from typing import Any , TypeVar , cast
7+ from typing import TypeVar , cast
88
99import huggingface_hub
1010import numpy as np
1515from model2vec .inference .mlp import Activation , Layer , MLPHead
1616from model2vec .model import PathLike , StaticModel
1717from model2vec .persistence import save_pretrained
18+ from model2vec .types import _UNSET , StaticModelConfig , _UnsetType
1819
1920_DEFAULT_HEAD_FILENAME = "head.safetensors"
2021_LEGACY_HEAD_FILENAME = "pipeline.skops"
@@ -74,7 +75,7 @@ def _encode_and_coerce_to_2d(
7475 self ,
7576 X : Sequence [str ],
7677 show_progress_bar : bool ,
77- max_length : int | None ,
78+ max_length : int | None | _UnsetType ,
7879 batch_size : int ,
7980 use_multiprocessing : bool ,
8081 multiprocessing_threshold : int ,
@@ -97,7 +98,7 @@ def predict(
9798 self ,
9899 X : Sequence [str ],
99100 show_progress_bar : bool = False ,
100- max_length : int | None = 512 ,
101+ max_length : int | None | _UnsetType = _UNSET ,
101102 batch_size : int = 1024 ,
102103 use_multiprocessing : bool = True ,
103104 multiprocessing_threshold : int = 10_000 ,
@@ -107,7 +108,8 @@ def predict(
107108
108109 :param X: The input data to predict. Can be a list of strings or a single string.
109110 :param show_progress_bar: Whether to display a progress bar during prediction. Defaults to False.
110- :param max_length: The maximum length of the input sequences. Defaults to 512.
111+ :param max_length: The maximum length of the input sequences. If not passed, the encoder model's
112+ `max_length` is used. Pass `max_length=None` to disable truncation.
111113 :param batch_size: The batch size for prediction. Defaults to 1024.
112114 :param use_multiprocessing: Whether to use multiprocessing for encoding. Defaults to True.
113115 :param multiprocessing_threshold: The threshold for the number of samples to use multiprocessing. Defaults to 10,000.
@@ -139,7 +141,7 @@ def predict_proba(
139141 self ,
140142 X : Sequence [str ],
141143 show_progress_bar : bool = False ,
142- max_length : int | None = 512 ,
144+ max_length : int | None | _UnsetType = _UNSET ,
143145 batch_size : int = 1024 ,
144146 use_multiprocessing : bool = True ,
145147 multiprocessing_threshold : int = 10_000 ,
@@ -148,7 +150,8 @@ def predict_proba(
148150
149151 :param X: The input data to predict. Can be a list of strings or a single string.
150152 :param show_progress_bar: Whether to display a progress bar during prediction. Defaults to False.
151- :param max_length: The maximum length of the input sequences. Defaults to 512.
153+ :param max_length: The maximum length of the input sequences. If not passed, the encoder model's
154+ `max_length` is used. Pass `max_length=None` to disable truncation.
152155 :param batch_size: The batch size for prediction. Defaults to 1024.
153156 :param use_multiprocessing: Whether to use multiprocessing for encoding. Defaults to True.
154157 :param multiprocessing_threshold: The threshold for the number of samples to use multiprocessing. Defaults to 10,000.
@@ -220,7 +223,7 @@ def _load_pipeline(folder_or_repo_path: PathLike, token: str | None = None) -> t
220223
221224 model = StaticModel .from_pretrained (folder_or_repo_path )
222225
223- head_config = cast ( dict [ str , Any ], model .config .get ("head_config" , {}) )
226+ head_config = model .config .get ("head_config" , {})
224227 activation = Activation (head_config .get ("activation" , Activation .IDENTITY .value ))
225228 n_layers = head_config .get ("n_layers" , 0 )
226229 classes = head_config .get ("classes" )
@@ -337,7 +340,7 @@ def _save_pipeline(pipeline: StaticModelPipeline, folder_path: str | Path) -> No
337340 save_file (tensors , folder_path / _DEFAULT_HEAD_FILENAME )
338341
339342 model = pipeline .model
340- config = dict ( model .config )
343+ config : StaticModelConfig = { ** model .config }
341344 config ["head_config" ] = {
342345 "n_layers" : len (head .layers ),
343346 "activation" : head .activation .value ,
0 commit comments