paddlex_cli.py 6.0 KB

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