__init__.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 pathlib import Path
  15. from typing import Any, Dict, Optional, Union
  16. from importlib import import_module
  17. from .base import BasePipeline
  18. from ..utils.pp_option import PaddlePredictorOption
  19. from ..utils.hpi import HPIConfig
  20. from .components import BaseChat, BaseRetriever, BaseGeneratePrompt
  21. from ...utils import logging
  22. from ...utils.config import parse_config
  23. from .ocr import OCRPipeline
  24. from .doc_preprocessor import DocPreprocessorPipeline
  25. from .layout_parsing import LayoutParsingPipeline
  26. from .pp_chatocr import PP_ChatOCRv3_Pipeline, PP_ChatOCRv4_Pipeline
  27. from .image_classification import ImageClassificationPipeline
  28. from .object_detection import ObjectDetectionPipeline
  29. from .seal_recognition import SealRecognitionPipeline
  30. from .table_recognition import TableRecognitionPipeline
  31. from .table_recognition import TableRecognitionPipelineV2
  32. from .multilingual_speech_recognition import MultilingualSpeechRecognitionPipeline
  33. from .formula_recognition import FormulaRecognitionPipeline
  34. from .image_multilabel_classification import ImageMultiLabelClassificationPipeline
  35. from .video_classification import VideoClassificationPipeline
  36. from .video_detection import VideoDetectionPipeline
  37. from .anomaly_detection import AnomalyDetectionPipeline
  38. from .ts_forecasting import TSFcPipeline
  39. from .ts_anomaly_detection import TSAnomalyDetPipeline
  40. from .ts_classification import TSClsPipeline
  41. from .pp_shitu_v2 import ShiTuV2Pipeline
  42. from .face_recognition import FaceRecPipeline
  43. from .attribute_recognition import (
  44. PedestrianAttributeRecPipeline,
  45. VehicleAttributeRecPipeline,
  46. )
  47. from .semantic_segmentation import SemanticSegmentationPipeline
  48. from .instance_segmentation import InstanceSegmentationPipeline
  49. from .small_object_detection import SmallObjectDetectionPipeline
  50. from .rotated_object_detection import RotatedObjectDetectionPipeline
  51. from .keypoint_detection import KeypointDetectionPipeline
  52. from .open_vocabulary_detection import OpenVocabularyDetectionPipeline
  53. from .open_vocabulary_segmentation import OpenVocabularySegmentationPipeline
  54. module_3d_bev_detection = import_module(
  55. ".3d_bev_detection", "paddlex.inference.pipelines"
  56. )
  57. BEVDet3DPipeline = getattr(module_3d_bev_detection, "BEVDet3DPipeline")
  58. def get_pipeline_path(pipeline_name: str) -> str:
  59. """
  60. Get the full path of the pipeline configuration file based on the provided pipeline name.
  61. Args:
  62. pipeline_name (str): The name of the pipeline.
  63. Returns:
  64. str: The full path to the pipeline configuration file or None if not found.
  65. """
  66. pipeline_path = (
  67. Path(__file__).parent.parent.parent
  68. / "configs/pipelines"
  69. / f"{pipeline_name}.yaml"
  70. ).resolve()
  71. if not Path(pipeline_path).exists():
  72. return None
  73. return pipeline_path
  74. def load_pipeline_config(pipeline: str) -> Dict[str, Any]:
  75. """
  76. Load the pipeline configuration.
  77. Args:
  78. pipeline (str): The name of the pipeline or the path to the config file.
  79. Returns:
  80. Dict[str, Any]: The parsed pipeline configuration.
  81. Raises:
  82. Exception: If the config file of pipeline does not exist.
  83. """
  84. if not (pipeline.endswith(".yml") or pipeline.endswith(".yaml")):
  85. pipeline_path = get_pipeline_path(pipeline)
  86. if pipeline_path is None:
  87. raise Exception(
  88. f"The pipeline ({pipeline}) does not exist! Please use a pipeline name or a config file path!"
  89. )
  90. else:
  91. pipeline_path = pipeline
  92. config = parse_config(pipeline_path)
  93. return config
  94. def create_pipeline(
  95. pipeline: Optional[str] = None,
  96. config: Optional[Dict[str, Any]] = None,
  97. device: Optional[str] = None,
  98. pp_option: Optional[PaddlePredictorOption] = None,
  99. use_hpip: Optional[bool] = None,
  100. hpi_config: Optional[Union[Dict[str, Any], HPIConfig]] = None,
  101. *args: Any,
  102. **kwargs: Any,
  103. ) -> BasePipeline:
  104. """
  105. Create a pipeline instance based on the provided parameters.
  106. If the input parameter config is not provided, it is obtained from the
  107. default config corresponding to the pipeline name.
  108. Args:
  109. pipeline (Optional[str], optional): The name of the pipeline to
  110. create, or the path to the config file. Defaults to None.
  111. config (Optional[Dict[str, Any]], optional): The pipeline configuration.
  112. Defaults to None.
  113. device (Optional[str], optional): The device to run the pipeline on.
  114. Defaults to None.
  115. pp_option (Optional[PaddlePredictorOption], optional): The options for
  116. the PaddlePredictor. Defaults to None.
  117. use_hpip (Optional[bool], optional): Whether to use the high-performance
  118. inference plugin (HPIP) for prediction by default.
  119. Defaults to None.
  120. hpi_config (Optional[Union[Dict[str, Any], HPIConfig]], optional): The
  121. default high-performance inference configuration dictionary.
  122. Defaults to None.
  123. *args: Additional positional arguments.
  124. **kwargs: Additional keyword arguments.
  125. Returns:
  126. BasePipeline: The created pipeline instance.
  127. """
  128. if pipeline is None and config is None:
  129. raise ValueError(
  130. "Both `pipeline` and `config` cannot be None at the same time."
  131. )
  132. if config is None:
  133. config = load_pipeline_config(pipeline)
  134. else:
  135. if pipeline is not None and config["pipeline_name"] != pipeline:
  136. logging.warning(
  137. "The pipeline name in the config (%r) is different from the specified pipeline name (%r). %r will be used.",
  138. config["pipeline_name"],
  139. pipeline,
  140. config["pipeline_name"],
  141. )
  142. pipeline_name = config["pipeline_name"]
  143. if device is None:
  144. device = config.get("device", None)
  145. if use_hpip is None:
  146. use_hpip = config.get("use_hpip", False)
  147. if hpi_config is None:
  148. hpi_config = config.get("hpi_config", None)
  149. pipeline = BasePipeline.get(pipeline_name)(
  150. config=config,
  151. device=device,
  152. pp_option=pp_option,
  153. use_hpip=use_hpip,
  154. hpi_config=hpi_config,
  155. *args,
  156. **kwargs,
  157. )
  158. return pipeline
  159. def create_chat_bot(config: Dict, *args, **kwargs) -> BaseChat:
  160. """Creates an instance of a chat bot based on the provided configuration.
  161. Args:
  162. config (Dict): Configuration settings, expected to be a dictionary with at least a 'model_name' key.
  163. *args: Additional positional arguments. Not used in this function but allowed for future compatibility.
  164. **kwargs: Additional keyword arguments. Not used in this function but allowed for future compatibility.
  165. Returns:
  166. BaseChat: An instance of the chat bot class corresponding to the 'model_name' in the config.
  167. """
  168. if "chat_bot_config_error" in config:
  169. raise ValueError(config["chat_bot_config_error"])
  170. api_type = config["api_type"]
  171. chat_bot = BaseChat.get(api_type)(config)
  172. return chat_bot
  173. def create_retriever(
  174. config: Dict,
  175. *args,
  176. **kwargs,
  177. ) -> BaseRetriever:
  178. """
  179. Creates a retriever instance based on the provided configuration.
  180. Args:
  181. config (Dict): Configuration settings, expected to be a dictionary with at least a 'model_name' key.
  182. *args: Additional positional arguments. Not used in this function but allowed for future compatibility.
  183. **kwargs: Additional keyword arguments. Not used in this function but allowed for future compatibility.
  184. Returns:
  185. BaseRetriever: An instance of a retriever class corresponding to the 'model_name' in the config.
  186. """
  187. if "retriever_config_error" in config:
  188. raise ValueError(config["retriever_config_error"])
  189. api_type = config["api_type"]
  190. retriever = BaseRetriever.get(api_type)(config)
  191. return retriever
  192. def create_prompt_engineering(
  193. config: Dict,
  194. *args,
  195. **kwargs,
  196. ) -> BaseGeneratePrompt:
  197. """
  198. Creates a prompt engineering instance based on the provided configuration.
  199. Args:
  200. config (Dict): Configuration settings, expected to be a dictionary with at least a 'task_type' key.
  201. *args: Variable length argument list for additional positional arguments.
  202. **kwargs: Arbitrary keyword arguments.
  203. Returns:
  204. BaseGeneratePrompt: An instance of a prompt engineering class corresponding to the 'task_type' in the config.
  205. """
  206. if "pe_config_error" in config:
  207. raise ValueError(config["pe_config_error"])
  208. task_type = config["task_type"]
  209. pe = BaseGeneratePrompt.get(task_type)(config)
  210. return pe