predictor.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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 copy import deepcopy
  15. from ..inference.models import create_model
  16. from ..inference.utils.pp_option import PaddlePredictorOption
  17. from ..utils.config import AttrDict
  18. class Predictor(object):
  19. def __init__(self, config):
  20. model_name = config.Global.model
  21. self.predict_config = deepcopy(config.Predict)
  22. model_dir = self.predict_config.pop("model_dir", None)
  23. # if model_dir is None, using official
  24. model = model_name if model_dir is None else model_dir
  25. self.input_path = self.predict_config.pop("input_path")
  26. self.pp_option = PaddlePredictorOption(
  27. **self.predict_config.pop("kernel_option", {})
  28. )
  29. self.model = create_model(model)
  30. def predict(self):
  31. for res in self.model(
  32. input=self.input_path, pp_option=self.pp_option, **self.predict_config
  33. ):
  34. res.print(json_format=False)
  35. def build_predictor(config: AttrDict):
  36. """build predictor by config for dev"""
  37. return Predictor(config)