base_predictor.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. import yaml
  15. import codecs
  16. from pathlib import Path
  17. from abc import abstractmethod
  18. from ...components.base import BaseComponent
  19. from ...utils.process_hook import generatorable_method
  20. class BasePredictor(BaseComponent):
  21. KEEP_INPUT = False
  22. YIELD_BATCH = False
  23. INPUT_KEYS = "input"
  24. DEAULT_INPUTS = {"input": "input"}
  25. OUTPUT_KEYS = "result"
  26. DEAULT_OUTPUTS = {"result": "result"}
  27. MODEL_FILE_PREFIX = "inference"
  28. def __init__(self, model_dir, config=None):
  29. super().__init__()
  30. self.model_dir = Path(model_dir)
  31. self.config = config if config else self.load_config(self.model_dir)
  32. # alias predict() to the __call__()
  33. self.predict = self.__call__
  34. self.benchmark = None
  35. def __call__(self, input, **kwargs):
  36. self.set_predictor(**kwargs)
  37. for res in super().__call__(input):
  38. yield res["result"]
  39. @property
  40. def config_path(self):
  41. return self.get_config_path(self.model_dir)
  42. @property
  43. def model_name(self) -> str:
  44. return self.config["Global"]["model_name"]
  45. @abstractmethod
  46. def apply(self, input):
  47. raise NotImplementedError
  48. @abstractmethod
  49. def set_predictor(self):
  50. raise NotImplementedError
  51. @classmethod
  52. def get_config_path(cls, model_dir):
  53. return model_dir / f"{cls.MODEL_FILE_PREFIX}.yml"
  54. @classmethod
  55. def load_config(cls, model_dir):
  56. config_path = cls.get_config_path(model_dir)
  57. with codecs.open(config_path, "r", "utf-8") as file:
  58. dic = yaml.load(file, Loader=yaml.FullLoader)
  59. return dic