__init__.py 8.9 KB

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