Spaces:
Runtime error
Runtime error
File size: 2,216 Bytes
da3eeba |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
import os
from datetime import datetime
from huggingface_hub import snapshot_download
from ia_logging import ia_logging
class IAFileManager:
DOWNLOAD_COMPLETE = "Download complete"
def __init__(self) -> None:
self._ia_outputs_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)),
"outputs",
datetime.now().strftime("%Y-%m-%d"))
self._ia_models_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "models")
@property
def outputs_dir(self) -> str:
"""Get inpaint-anything outputs directory.
Returns:
str: inpaint-anything outputs directory
"""
if not os.path.isdir(self._ia_outputs_dir):
os.makedirs(self._ia_outputs_dir, exist_ok=True)
return self._ia_outputs_dir
@property
def models_dir(self) -> str:
"""Get inpaint-anything models directory.
Returns:
str: inpaint-anything models directory
"""
if not os.path.isdir(self._ia_models_dir):
os.makedirs(self._ia_models_dir, exist_ok=True)
return self._ia_models_dir
@property
def savename_prefix(self) -> str:
"""Get inpaint-anything savename prefix.
Returns:
str: inpaint-anything savename prefix
"""
return datetime.now().strftime("%Y%m%d-%H%M%S")
ia_file_manager = IAFileManager()
def download_model_from_hf(hf_model_id, local_files_only=False):
"""Download model from HuggingFace Hub.
Args:
sam_model_id (str): HuggingFace model id
local_files_only (bool, optional): If True, use only local files. Defaults to False.
Returns:
str: download status
"""
if not local_files_only:
ia_logging.info(f"Downloading {hf_model_id}")
try:
snapshot_download(repo_id=hf_model_id, local_files_only=local_files_only)
except FileNotFoundError:
return f"{hf_model_id} not found, please download"
except Exception as e:
return str(e)
return IAFileManager.DOWNLOAD_COMPLETE
|