paddlex_cli.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 subprocess
  17. import sys
  18. from importlib_resources import files, as_file
  19. from . import create_pipeline
  20. from .inference.pipelines import create_pipeline_from_config, load_pipeline_config
  21. from .repo_manager import setup, get_all_supported_repo_names
  22. from .utils import logging
  23. from .utils.interactive_get_pipeline import interactive_get_pipeline
  24. def _install_serving_deps():
  25. with as_file(files("paddlex").joinpath("serving_requirements.txt")) as req_file:
  26. return subprocess.check_call(
  27. [sys.executable, "-m", "pip", "install", "-r", str(req_file)]
  28. )
  29. def args_cfg():
  30. """parse cli arguments"""
  31. def parse_str(s):
  32. """convert str type value
  33. to None type if it is "None",
  34. to bool type if it means True or False.
  35. """
  36. if s in ("None"):
  37. return None
  38. elif s in ("TRUE", "True", "true", "T", "t"):
  39. return True
  40. elif s in ("FALSE", "False", "false", "F", "f"):
  41. return False
  42. return s
  43. parser = argparse.ArgumentParser()
  44. ################# install pdx #################
  45. parser.add_argument("--install", action="store_true", default=False, help="")
  46. parser.add_argument("plugins", nargs="*", default=[])
  47. parser.add_argument("--no_deps", action="store_true")
  48. parser.add_argument("--platform", type=str, default="github.com")
  49. parser.add_argument(
  50. "-y",
  51. "--yes",
  52. dest="update_repos",
  53. action="store_true",
  54. help="Whether to update_repos all packages.",
  55. )
  56. parser.add_argument(
  57. "--use_local_repos",
  58. action="store_true",
  59. default=False,
  60. help="Use local repos when existing.",
  61. )
  62. ################# pipeline predict #################
  63. parser.add_argument("--pipeline", type=str, help="")
  64. parser.add_argument("--input", type=str, default=None, help="")
  65. parser.add_argument("--save_path", type=str, default=None, help="")
  66. parser.add_argument("--device", type=str, default=None, help="")
  67. parser.add_argument("--use_hpip", action="store_true")
  68. parser.add_argument("--serial_number", type=str)
  69. parser.add_argument("--update_license", action="store_true")
  70. parser.add_argument("--get_pipeline_config", type=str, default=None, help="")
  71. ################# serving #################
  72. parser.add_argument("--serve", action="store_true")
  73. parser.add_argument("--host", type=str, default="0.0.0.0")
  74. parser.add_argument("--port", type=int, default=8080)
  75. return parser
  76. def install(args):
  77. """install paddlex"""
  78. # Enable debug info
  79. os.environ["PADDLE_PDX_DEBUG"] = "True"
  80. # Disable eager initialization
  81. os.environ["PADDLE_PDX_EAGER_INIT"] = "False"
  82. plugins = args.plugins[:]
  83. if "serving" in plugins:
  84. plugins.remove("serving")
  85. _install_serving_deps()
  86. return
  87. if plugins:
  88. repo_names = plugins
  89. elif len(plugins) == 0:
  90. repo_names = get_all_supported_repo_names()
  91. setup(
  92. repo_names=repo_names,
  93. no_deps=args.no_deps,
  94. platform=args.platform,
  95. update_repos=args.update_repos,
  96. use_local_repos=args.use_local_repos,
  97. )
  98. return
  99. def _get_hpi_params(serial_number, update_license):
  100. return {"serial_number": serial_number, "update_license": update_license}
  101. def pipeline_predict(
  102. pipeline, input, device, save_path, use_hpip, serial_number, update_license
  103. ):
  104. """pipeline predict"""
  105. hpi_params = _get_hpi_params(serial_number, update_license)
  106. pipeline = create_pipeline(
  107. pipeline, device=device, use_hpip=use_hpip, hpi_params=hpi_params
  108. )
  109. result = pipeline(input)
  110. for res in result:
  111. res.print(json_format=False)
  112. if save_path:
  113. res.save_all(save_path=save_path)
  114. def serve(pipeline, *, device, use_hpip, serial_number, update_license, host, port):
  115. from .inference.pipelines.serving import create_pipeline_app, run_server
  116. hpi_params = _get_hpi_params(serial_number, update_license)
  117. pipeline_config = load_pipeline_config(pipeline)
  118. pipeline = create_pipeline_from_config(
  119. pipeline_config, device=device, use_hpip=use_hpip, hpi_params=hpi_params
  120. )
  121. app = create_pipeline_app(pipeline, pipeline_config)
  122. run_server(app, host=host, port=port, debug=False)
  123. # for CLI
  124. def main():
  125. """API for commad line"""
  126. args = args_cfg().parse_args()
  127. if len(sys.argv) == 1:
  128. logging.warning("No arguments provided. Displaying help information:")
  129. args_cfg().print_help()
  130. return
  131. if args.install:
  132. install(args)
  133. elif args.serve:
  134. serve(
  135. args.pipeline,
  136. device=args.device,
  137. use_hpip=args.use_hpip,
  138. serial_number=args.serial_number,
  139. update_license=args.update_license,
  140. host=args.host,
  141. port=args.port,
  142. )
  143. else:
  144. if args.get_pipeline_config is not None:
  145. interactive_get_pipeline(args.get_pipeline_config, args.save_path)
  146. else:
  147. return pipeline_predict(
  148. args.pipeline,
  149. args.input,
  150. args.device,
  151. args.save_path,
  152. use_hpip=args.use_hpip,
  153. serial_number=args.serial_number,
  154. update_license=args.update_license,
  155. )