__init__.py 7.6 KB

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