__init__.py 8.6 KB

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