paddlex_cli.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 prettytable import PrettyTable
  19. from .pipelines import build_pipeline, BasePipeline
  20. from .repo_manager import setup, get_all_supported_repo_names
  21. from .utils import logging
  22. def args_cfg():
  23. """parse cli arguments
  24. """
  25. def str2bool(v):
  26. """convert str to bool type
  27. """
  28. return v.lower() in ("true", "t", "1")
  29. parser = argparse.ArgumentParser()
  30. ################# install pdx #################
  31. parser.add_argument(
  32. '--install', action='store_true', default=False, help="")
  33. parser.add_argument('devkits', nargs='*', default=[])
  34. parser.add_argument('--no_deps', action='store_true')
  35. parser.add_argument('--platform', type=str, default='github.com')
  36. parser.add_argument('--update_repos', action='store_true')
  37. parser.add_argument(
  38. '-y',
  39. '--yes',
  40. dest='reinstall',
  41. action='store_true',
  42. help="Whether to reinstall all packages.")
  43. ################# pipeline predict #################
  44. parser.add_argument('--predict', action='store_true', default=True, help="")
  45. parser.add_argument('--pipeline', type=str, help="")
  46. parser.add_argument('--model', nargs='+', help="")
  47. parser.add_argument('--input', type=str, help="")
  48. parser.add_argument('--output', type=str, help="")
  49. parser.add_argument('--device', type=str, default='gpu:0', help="")
  50. return parser.parse_args()
  51. def print_info():
  52. """Print list of supported models in formatted.
  53. """
  54. try:
  55. sz = os.get_terminal_size()
  56. total_width = sz.columns
  57. first_width = 30
  58. second_width = total_width - first_width if total_width > 50 else 10
  59. except OSError:
  60. total_width = 100
  61. second_width = 100
  62. total_width -= 4
  63. pipeline_table = PrettyTable()
  64. pipeline_table.field_names = ["Pipelines"]
  65. pipeline_table.add_row(
  66. [textwrap.fill(
  67. ", ".join(BasePipeline.all()), width=total_width)])
  68. table_width = len(str(pipeline_table).split("\n")[0])
  69. logging.info("{}".format("-" * table_width))
  70. logging.info("PaddleX".center(table_width))
  71. logging.info(pipeline_table)
  72. logging.info("Powered by PaddlePaddle!".rjust(table_width))
  73. logging.info("{}".format("-" * table_width))
  74. def install(args):
  75. """install paddlex
  76. """
  77. # Enable debug info
  78. os.environ['PADDLE_PDX_DEBUG'] = 'True'
  79. # Disable eager initialization
  80. os.environ['PADDLE_PDX_EAGER_INIT'] = 'False'
  81. repo_names = args.devkits
  82. if len(repo_names) == 0:
  83. repo_names = get_all_supported_repo_names()
  84. setup(
  85. repo_names=repo_names,
  86. reinstall=args.reinstall or None,
  87. no_deps=args.no_deps,
  88. platform=args.platform,
  89. update_repos=args.update_repos)
  90. return
  91. def pipeline_predict(pipeline, model_name_list, input_path, output_dir, device):
  92. pipeline = build_pipeline(pipeline, model_name_list, output_dir, device)
  93. pipeline.predict(input_path)
  94. # for CLI
  95. def main():
  96. """API for commad line
  97. """
  98. args = args_cfg()
  99. if args.install:
  100. install(args)
  101. else:
  102. print_info()
  103. return pipeline_predict(args.pipeline, args.model, args.input,
  104. args.output, args.device)