__init__.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 typing import Any, Dict
  15. from fastapi import FastAPI
  16. from ...attribute_recognition import PedestrianAttributeRecPipeline
  17. from ...base import BasePipeline
  18. from ...formula_recognition import FormulaRecognitionPipeline
  19. from ...layout_parsing import LayoutParsingPipeline
  20. from ...ocr import OCRPipeline
  21. from ...ppchatocrv3 import PPChatOCRPipeline
  22. from ...seal_recognition import SealOCRPipeline
  23. from ...single_model_pipeline import (
  24. AnomalyDetection,
  25. ImageClassification,
  26. InstanceSegmentation,
  27. MultiLableImageClas,
  28. ObjectDetection,
  29. SemanticSegmentation,
  30. SmallObjDet,
  31. TSAd,
  32. TSCls,
  33. TSFc,
  34. )
  35. from ...table_recognition import TableRecPipeline
  36. from ..app import create_app_config
  37. from .anomaly_detection import create_pipeline_app as create_anomaly_detection_app
  38. from .formula_recognition import create_pipeline_app as create_formula_recognition_app
  39. from .layout_parsing import create_pipeline_app as create_layout_parsing_app
  40. from .image_classification import create_pipeline_app as create_image_classification_app
  41. from .instance_segmentation import (
  42. create_pipeline_app as create_instance_segmentation_app,
  43. )
  44. from .multi_label_image_classification import (
  45. create_pipeline_app as create_multi_label_image_classification_app,
  46. )
  47. from .object_detection import create_pipeline_app as create_object_detection_app
  48. from .ocr import create_pipeline_app as create_ocr_app
  49. from .pedestrian_attribute_recognition import (
  50. create_pipeline_app as create_pedestrian_attribute_recognition_app,
  51. )
  52. from .ppchatocrv3 import create_pipeline_app as create_ppchatocrv3_app
  53. from .seal_recognition import create_pipeline_app as create_seal_recognition_app
  54. from .semantic_segmentation import (
  55. create_pipeline_app as create_semantic_segmentation_app,
  56. )
  57. from .small_object_detection import (
  58. create_pipeline_app as create_small_object_detection_app,
  59. )
  60. from .table_recognition import create_pipeline_app as create_table_recognition_app
  61. from .ts_ad import create_pipeline_app as create_ts_ad_app
  62. from .ts_cls import create_pipeline_app as create_ts_cls_app
  63. from .ts_fc import create_pipeline_app as create_ts_fc_app
  64. # XXX (Bobholamovic): This is tightly coupled to the name-pipeline mapping,
  65. # which is dirty but necessary. I want to keep the pipeline definition code
  66. # untouched while adding the pipeline serving feature. Each pipeline app depends
  67. # on a specific pipeline class, and a pipeline name must be provided (in the
  68. # pipeline config) to specify the type of the pipeline.
  69. def create_pipeline_app(
  70. pipeline: BasePipeline, pipeline_config: Dict[str, Any]
  71. ) -> FastAPI:
  72. pipeline_name = pipeline_config["Global"]["pipeline_name"]
  73. app_config = create_app_config(pipeline_config)
  74. if pipeline_name == "image_classification":
  75. if not isinstance(pipeline, ImageClassification):
  76. raise TypeError(
  77. "Expected `pipeline` to be an instance of `ImageClassification`."
  78. )
  79. return create_image_classification_app(pipeline, app_config)
  80. elif pipeline_name == "instance_segmentation":
  81. if not isinstance(pipeline, InstanceSegmentation):
  82. raise TypeError(
  83. "Expected `pipeline` to be an instance of `InstanceSegmentation`."
  84. )
  85. return create_instance_segmentation_app(pipeline, app_config)
  86. elif pipeline_name == "object_detection":
  87. if not isinstance(pipeline, ObjectDetection):
  88. raise TypeError(
  89. "Expected `pipeline` to be an instance of `ObjectDetection`."
  90. )
  91. return create_object_detection_app(pipeline, app_config)
  92. elif pipeline_name == "OCR":
  93. if not isinstance(pipeline, OCRPipeline):
  94. raise TypeError("Expected `pipeline` to be an instance of `OCRPipeline`.")
  95. return create_ocr_app(pipeline, app_config)
  96. elif pipeline_name == "semantic_segmentation":
  97. if not isinstance(pipeline, SemanticSegmentation):
  98. raise TypeError(
  99. "Expected `pipeline` to be an instance of `SemanticSegmentation`."
  100. )
  101. return create_semantic_segmentation_app(pipeline, app_config)
  102. elif pipeline_name == "table_recognition":
  103. if not isinstance(pipeline, TableRecPipeline):
  104. raise TypeError(
  105. "Expected `pipeline` to be an instance of `TableRecPipeline`."
  106. )
  107. return create_table_recognition_app(pipeline, app_config)
  108. elif pipeline_name == "ts_ad":
  109. if not isinstance(pipeline, TSAd):
  110. raise TypeError("Expected `pipeline` to be an instance of `TSAd`.")
  111. return create_ts_ad_app(pipeline, app_config)
  112. elif pipeline_name == "ts_cls":
  113. if not isinstance(pipeline, TSCls):
  114. raise TypeError("Expected `pipeline` to be an instance of `TSCls`.")
  115. return create_ts_cls_app(pipeline, app_config)
  116. elif pipeline_name == "ts_fc":
  117. if not isinstance(pipeline, TSFc):
  118. raise TypeError("Expected `pipeline` to be an instance of `TSFc`.")
  119. return create_ts_fc_app(pipeline, app_config)
  120. elif pipeline_name == "multi_label_image_classification":
  121. if not isinstance(pipeline, MultiLableImageClas):
  122. raise TypeError(
  123. "Expected `pipeline` to be an instance of `MultiLableImageClas`."
  124. )
  125. return create_multi_label_image_classification_app(pipeline, app_config)
  126. elif pipeline_name == "small_object_detection":
  127. if not isinstance(pipeline, SmallObjDet):
  128. raise TypeError("Expected `pipeline` to be an instance of `SmallObjDet`.")
  129. return create_small_object_detection_app(pipeline, app_config)
  130. elif pipeline_name == "anomaly_detection":
  131. if not isinstance(pipeline, AnomalyDetection):
  132. raise TypeError(
  133. "Expected `pipeline` to be an instance of `AnomalyDetection`."
  134. )
  135. return create_anomaly_detection_app(pipeline, app_config)
  136. elif pipeline_name == "PP-ChatOCRv3-doc":
  137. if not isinstance(pipeline, PPChatOCRPipeline):
  138. raise TypeError(
  139. "Expected `pipeline` to be an instance of `PPChatOCRPipeline`."
  140. )
  141. return create_ppchatocrv3_app(pipeline, app_config)
  142. elif pipeline_name == "seal_recognition":
  143. if not isinstance(pipeline, SealOCRPipeline):
  144. raise TypeError(
  145. "Expected `pipeline` to be an instance of `SealOCRPipeline`."
  146. )
  147. return create_seal_recognition_app(pipeline, app_config)
  148. elif pipeline_name == "formula_recognition":
  149. if not isinstance(pipeline, FormulaRecognitionPipeline):
  150. raise TypeError(
  151. "Expected `pipeline` to be an instance of `FormulaRecognitionPipeline`."
  152. )
  153. return create_formula_recognition_app(pipeline, app_config)
  154. elif pipeline_name == "layout_parsing":
  155. if not isinstance(pipeline, LayoutParsingPipeline):
  156. raise TypeError(
  157. "Expected `pipeline` to be an instance of `LayoutParsingPipeline`."
  158. )
  159. return create_layout_parsing_app(pipeline, app_config)
  160. elif pipeline_name == "pedestrian_attribute_recognition":
  161. if not isinstance(pipeline, PedestrianAttributeRecPipeline):
  162. raise TypeError(
  163. "Expected `pipeline` to be an instance of `PedestrianAttributeRecPipeline`."
  164. )
  165. return create_pedestrian_attribute_recognition_app(pipeline, app_config)
  166. else:
  167. if BasePipeline.get(pipeline_name):
  168. raise ValueError(
  169. f"The {pipeline_name} pipeline does not support pipeline serving."
  170. )
  171. else:
  172. raise ValueError("Unknown pipeline name")