paddlex_cli.py 6.2 KB

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