base_predictor.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from typing import Union, Tuple, List, Dict, Any, Iterator
  15. from pathlib import Path
  16. from abc import abstractmethod, ABC
  17. from ....utils.io import YAMLReader
  18. class BasePredictor(ABC):
  19. """BasePredictor."""
  20. MODEL_FILE_PREFIX = "inference"
  21. def __init__(self, model_dir: str, config: dict = None) -> None:
  22. """Initializes the BasePredictor.
  23. Args:
  24. model_dir (str): The directory where the static model files is stored.
  25. config (dict, optional): The configuration of model to infer. Defaults to None.
  26. """
  27. super().__init__()
  28. self.model_dir = Path(model_dir)
  29. self.config = config if config else self.load_config(self.model_dir)
  30. # alias predict() to the __call__()
  31. self.predict = self.__call__
  32. self.benchmark = None
  33. @property
  34. def config_path(self) -> str:
  35. """
  36. Get the path to the configuration file.
  37. Returns:
  38. str: The path to the configuration file.
  39. """
  40. return self.get_config_path(self.model_dir)
  41. @property
  42. def model_name(self) -> str:
  43. """
  44. Get the model name.
  45. Returns:
  46. str: The model name.
  47. """
  48. return self.config["Global"]["model_name"]
  49. @classmethod
  50. def get_config_path(cls, model_dir) -> str:
  51. """Get the path to the configuration file for the given model directory.
  52. Args:
  53. model_dir (Path): The directory where the static model files is stored.
  54. Returns:
  55. Path: The path to the configuration file.
  56. """
  57. return model_dir / f"{cls.MODEL_FILE_PREFIX}.yml"
  58. @classmethod
  59. def load_config(cls, model_dir) -> dict:
  60. """Load the configuration from the specified model directory.
  61. Args:
  62. model_dir (Path): The where the static model files is stored.
  63. Returns:
  64. dict: The loaded configuration dictionary.
  65. """
  66. yaml_reader = YAMLReader()
  67. return yaml_reader.read(cls.get_config_path(model_dir))
  68. @abstractmethod
  69. def __call__(self, input: Any, **kwargs: dict[str, Any]) -> Iterator[Any]:
  70. """Predict with the given input and additional keyword arguments."""
  71. raise NotImplementedError
  72. @abstractmethod
  73. def apply(self, input: Any) -> Iterator[Any]:
  74. """Predict the given input."""
  75. raise NotImplementedError
  76. @abstractmethod
  77. def set_predictor(self) -> None:
  78. """Sets up the predictor."""
  79. raise NotImplementedError