pipeline.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
  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 Any, Dict, Optional, Union
  15. from ....utils.deps import pipeline_requires_extra
  16. from ...models.doc_vlm.result import DocVLMResult
  17. from ...utils.benchmark import benchmark
  18. from ...utils.hpi import HPIConfig
  19. from ...utils.pp_option import PaddlePredictorOption
  20. from ..base import BasePipeline
  21. @benchmark.time_methods
  22. @pipeline_requires_extra("multimodal")
  23. class DocUnderstandingPipeline(BasePipeline):
  24. """Doc Understanding Pipeline"""
  25. entities = "doc_understanding"
  26. def __init__(
  27. self,
  28. config: Dict,
  29. device: str = None,
  30. pp_option: PaddlePredictorOption = None,
  31. use_hpip: bool = False,
  32. hpi_config: Optional[Union[Dict[str, Any], HPIConfig]] = None,
  33. ) -> None:
  34. """
  35. Initializes the class with given configurations and options.
  36. Args:
  37. config (Dict): Configuration dictionary containing model and other parameters.
  38. device (str): The device to run the prediction on. Default is None.
  39. pp_option (PaddlePredictorOption): Options for PaddlePaddle predictor. Default is None.
  40. use_hpip (bool, optional): Whether to use the high-performance
  41. inference plugin (HPIP) by default. Defaults to False.
  42. hpi_config (Optional[Union[Dict[str, Any], HPIConfig]], optional):
  43. The default high-performance inference configuration dictionary.
  44. Defaults to None.
  45. """
  46. super().__init__(
  47. device=device, pp_option=pp_option, use_hpip=use_hpip, hpi_config=hpi_config
  48. )
  49. doc_understanding_model_config = config.get("SubModules", {}).get(
  50. "DocUnderstanding",
  51. {"model_config_error": "config error for doc_understanding_model!"},
  52. )
  53. self.doc_understanding_model = self.create_model(doc_understanding_model_config)
  54. def predict(self, input: Dict, **kwargs) -> DocVLMResult:
  55. """Predicts doc understanding results for the given input.
  56. Args:
  57. input (dict): The input image and query.
  58. **kwargs: Additional keyword arguments that can be passed to the function.
  59. Returns:
  60. DocVLMResult: The predicted doc understanding results.
  61. """
  62. yield from self.doc_understanding_model(input, **kwargs)
  63. def close(self):
  64. self.doc_understanding_model.close()