|
1 | 1 | #!/usr/bin/env python
|
2 |
| -# Copyright (c) 2024 Oracle and/or its affiliates. |
| 2 | +# Copyright (c) 2024, 2025 Oracle and/or its affiliates. |
3 | 3 | # Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
|
4 | 4 | """AQUA utils and constants."""
|
5 | 5 |
|
|
11 | 11 | import random
|
12 | 12 | import re
|
13 | 13 | import shlex
|
| 14 | +import shutil |
14 | 15 | import subprocess
|
15 | 16 | from datetime import datetime, timedelta
|
16 | 17 | from functools import wraps
|
|
21 | 22 | import fsspec
|
22 | 23 | import oci
|
23 | 24 | from cachetools import TTLCache, cached
|
| 25 | +from huggingface_hub.constants import HF_HUB_CACHE |
| 26 | +from huggingface_hub.file_download import repo_folder_name |
24 | 27 | from huggingface_hub.hf_api import HfApi, ModelInfo
|
25 | 28 | from huggingface_hub.utils import (
|
26 | 29 | GatedRepoError,
|
@@ -788,7 +791,9 @@ def get_ocid_substring(ocid: str, key_len: int) -> str:
|
788 | 791 | return ocid[-key_len:] if ocid and len(ocid) > key_len else ""
|
789 | 792 |
|
790 | 793 |
|
791 |
| -def upload_folder(os_path: str, local_dir: str, model_name: str, exclude_pattern: str = None) -> str: |
| 794 | +def upload_folder( |
| 795 | + os_path: str, local_dir: str, model_name: str, exclude_pattern: str = None |
| 796 | +) -> str: |
792 | 797 | """Upload the local folder to the object storage
|
793 | 798 |
|
794 | 799 | Args:
|
@@ -818,6 +823,38 @@ def upload_folder(os_path: str, local_dir: str, model_name: str, exclude_pattern
|
818 | 823 | return f"oci://{os_details.bucket}@{os_details.namespace}" + "/" + object_path
|
819 | 824 |
|
820 | 825 |
|
| 826 | +def cleanup_local_hf_model_artifact( |
| 827 | + model_name: str, |
| 828 | + local_dir: str = None, |
| 829 | +): |
| 830 | + """ |
| 831 | + Helper function that deletes local artifacts downloaded from Hugging Face to free up disk space. |
| 832 | + Parameters |
| 833 | + ---------- |
| 834 | + model_name (str): Name of the huggingface model |
| 835 | + local_dir (str): Local directory where the object is downloaded |
| 836 | +
|
| 837 | + """ |
| 838 | + if local_dir and os.path.exists(local_dir): |
| 839 | + model_dir = os.path.join(local_dir, model_name) |
| 840 | + if os.path.exists(model_dir): |
| 841 | + shutil.rmtree(model_dir) |
| 842 | + logger.debug(f"Deleted local model artifact directory: {model_dir}") |
| 843 | + |
| 844 | + if not os.listdir(local_dir): |
| 845 | + shutil.rmtree(local_dir) |
| 846 | + logger.debug(f"Deleted local directory {model_dir} as it is empty.") |
| 847 | + |
| 848 | + hf_local_path = os.path.join( |
| 849 | + HF_HUB_CACHE, repo_folder_name(repo_id=model_name, repo_type="model") |
| 850 | + ) |
| 851 | + if os.path.exists(hf_local_path): |
| 852 | + shutil.rmtree(hf_local_path) |
| 853 | + logger.debug( |
| 854 | + f"Deleted local Hugging Face cache directory {hf_local_path} for the model {model_name} " |
| 855 | + ) |
| 856 | + |
| 857 | + |
821 | 858 | def is_service_managed_container(container):
|
822 | 859 | return container and container.startswith(SERVICE_MANAGED_CONTAINER_URI_SCHEME)
|
823 | 860 |
|
|
0 commit comments