interactive_get_pipeline.py 1.8 KB

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