Simple wrapper that delays loading the model, since the model isn't needed if the training data is already cached.
| 449 | |
| 450 | |
| 451 | class ModelWrapper: |
| 452 | '''Simple wrapper that delays loading the model, since the model isn't needed if the training data is already cached.''' |
| 453 | def __init__(self, load_fn): |
| 454 | self._load_fn = load_fn |
| 455 | self._model = None |
| 456 | |
| 457 | def __getattr__(self, name): |
| 458 | if self._model is None: |
| 459 | raise RuntimeError("Model wasn't loaded, this shouldn't ever happen.") |
| 460 | return getattr(self._model, name) |
| 461 | |
| 462 | def load_model_if_needed(self): |
| 463 | if self._model is None: |
| 464 | self._model = self._load_fn() |
| 465 | |
| 466 | |
| 467 | def tokenize_with_weights(self, text:str, return_word_ids=False, **kwargs): |