File size: 693 Bytes
ddc8a59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from huggingface_hub import HfApi, create_repo

def push_to_hub(hf_token: str, push_dir: str, repo_prefix: None) -> str:
    try:
        if hf_token == "":
            return "No HF token provided. Model won't be pushed."
        else:
            hf_api = HfApi(token=hf_token)
            user = hf_api.whoami()["name"]
            repo_id = f"{user}/{push_dir}" if repo_prefix == "" else f"{user}/{repo_prefix}-{push_dir}"
            _ = create_repo(repo_id=repo_id, token=hf_token)
            url = hf_api.upload_folder(folder_path=push_dir, repo_id=repo_id, exist_ok=True)
            return f"Model successfully pushed: [{url}]({url})"
    except Exception as e:
        return f"{e}"