interactive_get_pipeline.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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, save_path):
  19. file_path = get_pipeline_path(pipeline)
  20. file_name = Path(file_path).name
  21. if save_path is None:
  22. logging.info(
  23. "Please enter the path that you want to save the pipeline config file: (default `./`)"
  24. )
  25. target_path = input() or "."
  26. else:
  27. target_path = save_path
  28. target_path = Path(target_path)
  29. if target_path.suffix not in (".yaml", ".yml"):
  30. target_path /= file_name
  31. if not target_path.parent.exists():
  32. try:
  33. target_path.parent.mkdir(parents=True, exist_ok=True)
  34. except Exception as e:
  35. logging.error(f"Failed to create directory: {e}")
  36. return
  37. if target_path.exists():
  38. logging.info(f"The file({target_path}) already exists. Is it covered? (y/N):")
  39. overwrite = input().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}")