paddlex_cli.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. import os
  15. import argparse
  16. import textwrap
  17. from types import SimpleNamespace
  18. from . import create_pipeline
  19. from .repo_manager import setup, get_all_supported_repo_names
  20. from .utils import logging
  21. from .utils.interactive_get_pipeline import interactive_get_pipeline
  22. def args_cfg():
  23. """parse cli arguments"""
  24. def parse_str(s):
  25. """convert str type value
  26. to None type if it is "None",
  27. to bool type if it means True or False.
  28. """
  29. if s in ("None"):
  30. return None
  31. elif s in ("TRUE", "True", "true", "T", "t"):
  32. return True
  33. elif s in ("FALSE", "False", "false", "F", "f"):
  34. return False
  35. return s
  36. parser = argparse.ArgumentParser()
  37. ################# install pdx #################
  38. parser.add_argument("--install", action="store_true", default=False, help="")
  39. parser.add_argument("devkits", nargs="*", default=[])
  40. parser.add_argument("--no_deps", action="store_true")
  41. parser.add_argument("--platform", type=str, default="github.com")
  42. parser.add_argument(
  43. "-y",
  44. "--yes",
  45. dest="update_repos",
  46. action="store_true",
  47. help="Whether to update_repos all packages.",
  48. )
  49. parser.add_argument(
  50. "--use_local_repos",
  51. action="store_true",
  52. default=False,
  53. help="Use local repos when existing.",
  54. )
  55. ################# pipeline predict #################
  56. parser.add_argument("--pipeline", type=str, help="")
  57. parser.add_argument("--input", type=str, default=None, help="")
  58. parser.add_argument("--save_path", type=str, default=None, help="")
  59. parser.add_argument("--device", type=str, default=None, help="")
  60. parser.add_argument("--get_pipeline_config", type=str, default=None, help="")
  61. return parser.parse_args()
  62. def install(args):
  63. """install paddlex"""
  64. # Enable debug info
  65. os.environ["PADDLE_PDX_DEBUG"] = "True"
  66. # Disable eager initialization
  67. os.environ["PADDLE_PDX_EAGER_INIT"] = "False"
  68. repo_names = args.devkits
  69. if len(repo_names) == 0:
  70. repo_names = get_all_supported_repo_names()
  71. setup(
  72. repo_names=repo_names,
  73. no_deps=args.no_deps,
  74. platform=args.platform,
  75. update_repos=args.update_repos,
  76. use_local_repos=args.use_local_repos,
  77. )
  78. return
  79. def pipeline_predict(pipeline, input, device=None, save_path=None):
  80. """pipeline predict"""
  81. predictor_kwargs = {"device": device} if device else {}
  82. pipeline = create_pipeline(pipeline)
  83. result = pipeline(input)
  84. for res in result:
  85. res.print(json_format=False)
  86. if save_path:
  87. res.save_all(save_path=save_path)
  88. # for CLI
  89. def main():
  90. """API for commad line"""
  91. args = args_cfg()
  92. if args.install:
  93. install(args)
  94. else:
  95. if args.get_pipeline_config is not None:
  96. interactive_get_pipeline(args.get_pipeline_config)
  97. else:
  98. return pipeline_predict(
  99. args.pipeline,
  100. args.input,
  101. args.device,
  102. args.save_path,
  103. )