paddlex_cli.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 .pipelines import build_pipeline, BasePipeline
  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. """
  24. def str2bool(v):
  25. """convert str to bool type
  26. """
  27. return v.lower() in ("true", "t", "1")
  28. parser = argparse.ArgumentParser()
  29. ################# install pdx #################
  30. parser.add_argument(
  31. '--install', action='store_true', default=False, help="")
  32. parser.add_argument('devkits', nargs='*', default=[])
  33. parser.add_argument('--no_deps', action='store_true')
  34. parser.add_argument('--platform', type=str, default='github.com')
  35. parser.add_argument('--update_repos', action='store_true')
  36. parser.add_argument(
  37. '-y',
  38. '--yes',
  39. dest='reinstall',
  40. action='store_true',
  41. help="Whether to reinstall all packages.")
  42. ################# pipeline predict #################
  43. parser.add_argument('--predict', action='store_true', default=True, help="")
  44. parser.add_argument('--pipeline', type=str, help="")
  45. parser.add_argument('--model', nargs='+', help="")
  46. parser.add_argument('--input', type=str, help="")
  47. parser.add_argument('--output', type=str, default="./", help="")
  48. parser.add_argument('--device', type=str, default='gpu:0', help="")
  49. return parser.parse_args()
  50. def install(args):
  51. """install paddlex
  52. """
  53. # Enable debug info
  54. os.environ['PADDLE_PDX_DEBUG'] = 'True'
  55. # Disable eager initialization
  56. os.environ['PADDLE_PDX_EAGER_INIT'] = 'False'
  57. repo_names = args.devkits
  58. if len(repo_names) == 0:
  59. repo_names = get_all_supported_repo_names()
  60. setup(
  61. repo_names=repo_names,
  62. reinstall=args.reinstall or None,
  63. no_deps=args.no_deps,
  64. platform=args.platform,
  65. update_repos=args.update_repos)
  66. return
  67. def pipeline_predict(pipeline, model_name_list, input_path, output, device):
  68. """pipeline predict
  69. """
  70. pipeline = build_pipeline(pipeline, model_name_list, output, device)
  71. pipeline.predict({"input_path": input_path})
  72. # for CLI
  73. def main():
  74. """API for commad line
  75. """
  76. args = args_cfg()
  77. if args.install:
  78. install(args)
  79. else:
  80. return pipeline_predict(args.pipeline, args.model, args.input,
  81. args.output, args.device)