paddlex_cli.py 3.5 KB

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