__init__.py 6.2 KB

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