55import tempfile
66import threading
77import time
8+ import warnings
89from contextlib import contextmanager
910from pathlib import Path
1011from typing import Callable , Dict , Iterator , List , Optional , Tuple , Union
1516
1617from .hf_api import HfApi , repo_type_and_id_from_hf_id
1718from .lfs import LFS_MULTIPART_UPLOAD_COMMAND
18- from .utils import HfFolder , logging , run_subprocess , tqdm , validate_hf_hub_args
19+ from .utils import (
20+ HfFolder ,
21+ RepositoryNotFoundError ,
22+ logging ,
23+ run_subprocess ,
24+ tqdm ,
25+ validate_hf_hub_args ,
26+ )
1927from .utils ._deprecation import _deprecate_arguments , _deprecate_method
2028from .utils ._typing import TypedDict
2129
@@ -684,9 +692,10 @@ def clone_from(self, repo_url: str, token: Union[bool, str, None] = None):
684692 if hub_url in repo_url or (
685693 "http" not in repo_url and len (repo_url .split ("/" )) <= 2
686694 ):
687- repo_type , namespace , repo_id = repo_type_and_id_from_hf_id (
695+ repo_type , namespace , repo_name = repo_type_and_id_from_hf_id (
688696 repo_url , hub_url = hub_url
689697 )
698+ repo_id = f"{ namespace } /{ repo_name } " if namespace is not None else repo_name
690699
691700 if repo_type is not None :
692701 self ._repo_type = repo_type
@@ -701,10 +710,32 @@ def clone_from(self, repo_url: str, token: Union[bool, str, None] = None):
701710 scheme = urlparse (repo_url ).scheme
702711 repo_url = repo_url .replace (f"{ scheme } ://" , f"{ scheme } ://user:{ token } @" )
703712
704- if namespace is not None :
705- repo_url += f"{ namespace } /"
706713 repo_url += repo_id
707714
715+ # To be removed: check if repo exists. If not, create it first.
716+ try :
717+ HfApi ().repo_info (f"{ repo_id } " , repo_type = self ._repo_type , token = token )
718+ except RepositoryNotFoundError :
719+ if self ._repo_type == "space" :
720+ raise ValueError (
721+ "Creating a Space through passing Space link to clone_from is"
722+ " not allowed. Make sure the Space exists on Hugging Face Hub."
723+ )
724+ else :
725+ warnings .warn (
726+ "Creating a repository through 'clone_from' is deprecated and"
727+ " will be removed in v0.12. Please create the repository first"
728+ " using `create_repo(..., exists_ok=True)`." ,
729+ FutureWarning ,
730+ )
731+ self .client .create_repo (
732+ repo_id = repo_id ,
733+ token = token ,
734+ repo_type = self ._repo_type ,
735+ exist_ok = True ,
736+ private = self ._private ,
737+ )
738+
708739 # For error messages, it's cleaner to show the repo url without the token.
709740 clean_repo_url = re .sub (r"(https?)://.*@" , r"\1://" , repo_url )
710741 try :
0 commit comments