paddlex_cli.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. 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("--pipeline", type=str, help="")
  56. parser.add_argument("--input", type=str, help="")
  57. parser.add_argument("--save_path", type=str, default=None, help="")
  58. parser.add_argument("--device", type=str, default=None, help="")
  59. return parser.parse_args()
  60. def install(args):
  61. """install paddlex"""
  62. # Enable debug info
  63. os.environ["PADDLE_PDX_DEBUG"] = "True"
  64. # Disable eager initialization
  65. os.environ["PADDLE_PDX_EAGER_INIT"] = "False"
  66. repo_names = args.devkits
  67. if len(repo_names) == 0:
  68. repo_names = get_all_supported_repo_names()
  69. setup(
  70. repo_names=repo_names,
  71. no_deps=args.no_deps,
  72. platform=args.platform,
  73. update_repos=args.update_repos,
  74. use_local_repos=args.use_local_repos,
  75. )
  76. return
  77. def pipeline_predict(pipeline, input, device=None, save_path=None):
  78. """pipeline predict"""
  79. pipeline = create_pipeline(pipeline, device=device)
  80. result = pipeline(input)
  81. for res in result:
  82. res.print(json_format=False)
  83. if save_path:
  84. res.save_all(save_path=save_path)
  85. # for CLI
  86. def main():
  87. """API for commad line"""
  88. args = args_cfg()
  89. if args.install:
  90. install(args)
  91. else:
  92. return pipeline_predict(
  93. args.pipeline,
  94. args.input,
  95. args.device,
  96. args.save_path,
  97. )