interactive_get_pipeline.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. from pathlib import Path
  15. import shutil
  16. from ..utils import logging
  17. from ..inference.utils.get_pipeline_path import get_pipeline_path
  18. def interactive_get_pipeline(pipeline):
  19. file_path = get_pipeline_path(pipeline)
  20. file_name = Path(file_path).name
  21. logging.info(
  22. "Please enter the path that you want to save the pipeline config file: (default `./`)"
  23. )
  24. target_path = input() or "."
  25. target_path = Path(target_path)
  26. if not target_path.suffix in (".yaml", ".yml"):
  27. if not target_path.exists():
  28. try:
  29. target_path.mkdir(parents=True, exist_ok=True)
  30. except Exception as e:
  31. logging.error(f"Failed to create directory: {e}")
  32. return
  33. target_path = target_path / file_name
  34. if target_path.exists():
  35. logging.info(f"The file({target_path}) already exists. Is it covered? (y/N):")
  36. overwrite = input().lower()
  37. if overwrite != "y":
  38. logging.warning("Exit!")
  39. return
  40. try:
  41. shutil.copy2(file_path, target_path)
  42. logging.info(f"The pipeline config has been saved to: {target_path}")
  43. except Exception as e:
  44. logging.error(f"File saving failed: {e}")