base.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. from __future__ import annotations
  2. from abc import ABC, abstractmethod
  3. from pathlib import Path
  4. from shutil import rmtree
  5. from typing import Any
  6. from onnxruntime.capi.onnxruntime_pybind11_state import InvalidProtobuf # type: ignore
  7. from ..config import get_cache_dir
  8. from ..schemas import ModelType
  9. class InferenceModel(ABC):
  10. _model_type: ModelType
  11. def __init__(self, model_name: str, cache_dir: Path | str | None = None, **model_kwargs: Any) -> None:
  12. self.model_name = model_name
  13. self._cache_dir = Path(cache_dir) if cache_dir is not None else get_cache_dir(model_name, self.model_type)
  14. try:
  15. self.load(**model_kwargs)
  16. except (OSError, InvalidProtobuf):
  17. self.clear_cache()
  18. self.load(**model_kwargs)
  19. @abstractmethod
  20. def load(self, **model_kwargs: Any) -> None:
  21. ...
  22. @abstractmethod
  23. def predict(self, inputs: Any) -> Any:
  24. ...
  25. @property
  26. def model_type(self) -> ModelType:
  27. return self._model_type
  28. @property
  29. def cache_dir(self) -> Path:
  30. return self._cache_dir
  31. @cache_dir.setter
  32. def cache_dir(self, cache_dir: Path) -> None:
  33. self._cache_dir = cache_dir
  34. @classmethod
  35. def from_model_type(cls, model_type: ModelType, model_name: str, **model_kwargs: Any) -> InferenceModel:
  36. subclasses = {subclass._model_type: subclass for subclass in cls.__subclasses__()}
  37. if model_type not in subclasses:
  38. raise ValueError(f"Unsupported model type: {model_type}")
  39. return subclasses[model_type](model_name, **model_kwargs)
  40. def clear_cache(self) -> None:
  41. if not self.cache_dir.exists():
  42. return
  43. elif not rmtree.avoids_symlink_attacks:
  44. raise RuntimeError("Attempted to clear cache, but rmtree is not safe on this platform.")
  45. rmtree(self.cache_dir)