Created
August 12, 2022 16:00
-
-
Save severo/0e9a1d8fb91f5ecb9c123ac48fbe6ea4 to your computer and use it in GitHub Desktop.
A function to set the gated parameter on a HF repository
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from huggingface_hub.hf_api import ( # type: ignore | |
REPO_TYPES, | |
REPO_TYPES_URL_PREFIXES, | |
HfApi, | |
_raise_for_status, | |
) | |
def update_repo_settings( | |
hf_api: HfApi, | |
repo_id: str, | |
*, | |
private: Optional[bool] = None, | |
gated: Optional[bool] = None, | |
token: Optional[str] = None, | |
organization: Optional[str] = None, | |
repo_type: Optional[str] = None, | |
name: str = None, | |
) -> Dict[str, bool]: | |
"""Update the settings of a repository. | |
Args: | |
repo_id (`str`, *optional*): | |
A namespace (user or an organization) and a repo name separated | |
by a `/`. | |
<Tip> | |
Version added: 0.5 | |
</Tip> | |
private (`bool`, *optional*, defaults to `None`): | |
Whether the repo should be private. | |
gated (`bool`, *optional*, defaults to `None`): | |
Whether the repo should request user access. | |
token (`str`, *optional*): | |
An authentication token (See https://huggingface.co/settings/token) | |
repo_type (`str`, *optional*): | |
Set to `"dataset"` or `"space"` if uploading to a dataset or | |
space, `None` or `"model"` if uploading to a model. Default is | |
`None`. | |
Returns: | |
The HTTP response in json. | |
<Tip> | |
Raises the following errors: | |
- [`~huggingface_hub.utils.RepositoryNotFoundError`] | |
If the repository to download from cannot be found. This may be because it doesn't exist, | |
or because it is set to `private` and you do not have access. | |
</Tip> | |
""" | |
if repo_type not in REPO_TYPES: | |
raise ValueError("Invalid repo type") | |
organization, name = repo_id.split("/") if "/" in repo_id else (None, repo_id) | |
token, name = hf_api._validate_or_retrieve_token(token, name, function_name="update_repo_settings") | |
if organization is None: | |
namespace = hf_api.whoami(token)["name"] | |
else: | |
namespace = organization | |
path_prefix = f"{hf_api.endpoint}/api/" | |
if repo_type in REPO_TYPES_URL_PREFIXES: | |
path_prefix += REPO_TYPES_URL_PREFIXES[repo_type] | |
path = f"{path_prefix}{namespace}/{name}/settings" | |
json = {} | |
if private is not None: | |
json["private"] = private | |
if gated is not None: | |
json["gated"] = gated | |
r = requests.put( | |
path, | |
headers={"authorization": f"Bearer {token}"}, | |
json=json, | |
) | |
_raise_for_status(r) | |
return r.json() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment