@@ -227,7 +227,9 @@ def __repr__(self) -> str:
227
227
return f"{ self .__class__ .__name__ } ({ nrows } x{ ncols } )"
228
228
229
229
230
- def _load_model (model : Any , trusted = False ) -> Any :
230
+ def _load_model (
231
+ model : Any , trusted : Optional [Sequence [str ]] = None , allow_pickle : bool = False
232
+ ) -> Any :
231
233
"""Return a model instance.
232
234
233
235
Loads the model if provided a file path, if already a model instance return
@@ -238,10 +240,14 @@ def _load_model(model: Any, trusted=False) -> Any:
238
240
model : pathlib.Path, str, or sklearn estimator
239
241
Path/str or the actual model instance. if a Path or str, loads the model.
240
242
241
- trusted : bool , default=False
243
+ trusted: list of str , default=None
242
244
Passed to :func:`skops.io.load` if the model is a file path and it's
243
245
a `skops` file.
244
246
247
+ allow_pickle : bool, default=False
248
+ If `True`, allows loading models using `joblib.load`. This may lead to
249
+ security issues if the model file is not trustworthy.
250
+
245
251
Returns
246
252
-------
247
253
model : object
@@ -255,13 +261,28 @@ def _load_model(model: Any, trusted=False) -> Any:
255
261
if not model_path .exists ():
256
262
raise FileNotFoundError (f"File is not present: { model_path } " )
257
263
264
+ if trusted and allow_pickle :
265
+ raise ValueError (
266
+ "`allow_pickle` cannot be `True` if `trusted` is not empty. "
267
+ "Pickles cannot be trusted or checked for security issues."
268
+ )
269
+
270
+ msg = ""
258
271
try :
259
272
if zipfile .is_zipfile (model_path ):
260
273
model = load (model_path , trusted = trusted )
261
- else :
274
+ elif allow_pickle :
262
275
model = joblib .load (model_path )
276
+ else :
277
+ msg = (
278
+ "Model file is not a skops file, and allow_pickle is set to False. "
279
+ "Please set allow_pickle=True to load the model."
280
+ "This may lead to security issues if the model file is not trustworthy."
281
+ )
282
+ raise RuntimeError (msg )
263
283
except Exception as ex :
264
- msg = f'An "{ type (ex ).__name__ } " occurred during model loading.'
284
+ if not msg :
285
+ msg = f'"{ type (ex ).__name__ } " occurred during model loading.'
265
286
raise RuntimeError (msg ) from ex
266
287
267
288
return model
@@ -310,10 +331,14 @@ class Card:
310
331
not work, e.g. :meth:`Card.add_metrics`, since it's not clear where to
311
332
put the metrics when there is no template or a custom template.
312
333
313
- trusted: bool , default=False
334
+ trusted: list of str , default=None
314
335
Passed to :func:`skops.io.load` if the model is a file path and it's
315
336
a `skops` file.
316
337
338
+ allow_pickle: bool, default=False
339
+ If `True`, allows loading models using `joblib.load`. This may lead to
340
+ security issues if the model file is not trustworthy.
341
+
317
342
Attributes
318
343
----------
319
344
model: estimator object
@@ -379,11 +404,13 @@ def __init__(
379
404
model_diagram : bool | Literal ["auto" ] | str = "auto" ,
380
405
template : Literal ["skops" ] | dict [str , str ] | None = "skops" ,
381
406
trusted : Optional [List [str ]] = None ,
407
+ allow_pickle : bool = False ,
382
408
) -> None :
383
409
self .model = model
384
410
self .model_format = model_format
385
411
self .template = template
386
412
self .trusted = trusted
413
+ self .allow_pickle = allow_pickle
387
414
388
415
self ._data : dict [str , Section ] = {}
389
416
self ._metrics : dict [str , str | float | int ] = {}
@@ -465,7 +492,7 @@ def get_model(self) -> Any:
465
492
466
493
@cached_property
467
494
def _model (self ):
468
- model = _load_model (self .model , self .trusted )
495
+ model = _load_model (self .model , self .trusted , self . allow_pickle )
469
496
return model
470
497
471
498
def add (self , folded : bool = False , ** kwargs : str ) -> Self :
0 commit comments