Spaces:
Sleeping

Rthur2003 commited on
Commit
ef35e36
·
1 Parent(s): 026a187

feat: implement custom unpickler for TorchSklearnWrapper to support model deserialization

Browse files
Files changed (1) hide show
  1. local_demo.py +13 -2
local_demo.py CHANGED
@@ -48,7 +48,18 @@ except ModuleNotFoundError as exc:
48
  import numpy as np
49
 
50
  from app.training.extract_features_batch import extract_sample_features
51
- from app.training.train_deep_classifiers import TorchSklearnWrapper # noqa: F401 — required for pickle deserialization of DL models
 
 
 
 
 
 
 
 
 
 
 
52
 
53
  BASE_DIR = Path(__file__).resolve().parent
54
  PROJECT_ROOT = BASE_DIR.parent
@@ -86,7 +97,7 @@ def _safe_model_name(name: str) -> str:
86
 
87
  def _load_pickle(path: Path) -> Any:
88
  with open(path, "rb") as f:
89
- return pickle.load(f)
90
 
91
 
92
  def _load_json(path: Path) -> dict[str, Any]:
 
48
  import numpy as np
49
 
50
  from app.training.extract_features_batch import extract_sample_features
51
+ from app.training.train_deep_classifiers import TorchSklearnWrapper
52
+
53
+
54
+ class _AurisUnpickler(pickle.Unpickler):
55
+ """Resolves TorchSklearnWrapper pickled under __main__ to the real module."""
56
+ _REMAP = {("__main__", "TorchSklearnWrapper"): TorchSklearnWrapper}
57
+
58
+ def find_class(self, module: str, name: str) -> Any:
59
+ cls = self._REMAP.get((module, name))
60
+ if cls is not None:
61
+ return cls
62
+ return super().find_class(module, name)
63
 
64
  BASE_DIR = Path(__file__).resolve().parent
65
  PROJECT_ROOT = BASE_DIR.parent
 
97
 
98
  def _load_pickle(path: Path) -> Any:
99
  with open(path, "rb") as f:
100
+ return _AurisUnpickler(f).load()
101
 
102
 
103
  def _load_json(path: Path) -> dict[str, Any]: