__init__.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 pathlib import Path
  15. from typing import Any, Dict, Optional
  16. from ...utils.config import parse_config
  17. from .base import BasePipeline
  18. from .single_model_pipeline import (
  19. _SingleModelPipeline,
  20. ImageClassification,
  21. ObjectDetection,
  22. InstanceSegmentation,
  23. SemanticSegmentation,
  24. TSFc,
  25. TSAd,
  26. TSCls,
  27. MultiLableImageClas,
  28. SmallObjDet,
  29. AnomolyDetection,
  30. )
  31. from .ocr import OCRPipeline
  32. from .table_recognition import TableRecPipeline
  33. from .ppchatocrv3 import PPChatOCRPipeline
  34. def create_pipeline(
  35. pipeline: str,
  36. device=None,
  37. pp_option=None,
  38. use_hpip: bool = False,
  39. hpi_params: Optional[Dict[str, Any]] = None,
  40. *args,
  41. **kwargs,
  42. ) -> BasePipeline:
  43. """build model evaluater
  44. Args:
  45. pipeline (str): the pipeline name, that is name of pipeline class
  46. Returns:
  47. BasePipeline: the pipeline, which is subclass of BasePipeline.
  48. """
  49. if not Path(pipeline).exists():
  50. # XXX: using dict class to handle all pipeline configs
  51. build_in_pipeline = (
  52. Path(__file__).parent.parent.parent / "pipelines" / f"{pipeline}.yaml"
  53. ).resolve()
  54. if not Path(build_in_pipeline).exists():
  55. raise Exception(f"The pipeline don't exist! ({pipeline})")
  56. pipeline = build_in_pipeline
  57. config = parse_config(pipeline)
  58. pipeline_name = config["Global"]["pipeline_name"]
  59. pipeline_setting = config["Pipeline"]
  60. predictor_kwargs = {"use_hpip": use_hpip}
  61. if "use_hpip" in pipeline_setting:
  62. predictor_kwargs["use_hpip"] = use_hpip
  63. if hpi_params is not None:
  64. predictor_kwargs["hpi_params"] = hpi_params
  65. elif "hpi_params" in pipeline_setting:
  66. predictor_kwargs["hpi_params"] = pipeline_setting.pop("hpi_params")
  67. if device is not None:
  68. predictor_kwargs["device"] = device
  69. elif "device" in pipeline_setting:
  70. predictor_kwargs["device"] = pipeline_setting.pop("device")
  71. if pp_option is not None:
  72. predictor_kwargs["pp_option"] = pp_option
  73. elif "pp_option" in pipeline_setting:
  74. predictor_kwargs["pp_option"] = pipeline_setting.pop("pp_option")
  75. pipeline_setting.update(kwargs)
  76. pipeline = BasePipeline.get(pipeline_name)(
  77. predictor_kwargs=predictor_kwargs, *args, **pipeline_setting
  78. )
  79. return pipeline