Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MNT deprecate name and organization in favor of repo_id #733

Merged
merged 18 commits into from
Mar 11, 2022
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 97 additions & 22 deletions src/huggingface_hub/hf_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,28 @@
USERNAME_PLACEHOLDER = "hf_user"


# TODO: remove after deprecation period is over (v0.7)
def _validate_repo_id_deprecation(repo_id, name, organization):
"""Returns (name, organization) from the input."""
if repo_id and (name or organization):
raise ValueError(
"Only pass `repo_id` and leave deprecated `name` and "
"`organization` to be None."
)
elif name or organization:
warnings.warn(
"`name` and `organization` input arguments are deprecated and "
"will be removed in v0.7. Pass `repo_id` instead.",
FutureWarning,
)
else:
if "/" in repo_id:
name, organization = repo_id.split("/")
else:
name, organization = repo_id, None
adrinjalali marked this conversation as resolved.
Show resolved Hide resolved
return name, organization


def repo_type_and_id_from_hf_id(hf_id: str):
"""
Returns the repo type and ID from a huggingface.co URL linking to a repository
Expand Down Expand Up @@ -992,34 +1014,47 @@ def dataset_info(

def create_repo(
self,
name: str,
name: str = None,
token: Optional[str] = None,
organization: Optional[str] = None,
private: Optional[bool] = None,
repo_type: Optional[str] = None,
exist_ok=False,
lfsmultipartthresh: Optional[int] = None,
space_sdk: Optional[str] = None,
repo_id: str = None,
) -> str:
"""
HuggingFace git-based system, used for models, datasets, and spaces.
"""Create an empty repo on the HuggingFace Hub.

Args:
repo_id: A namespace (user or an organization) and a repo name
seperated by a ``/``.

Call HF API to create a whole repo.
.. versionadded: 0.4.0

Params:
private: Whether the model repo should be private (requires a paid huggingface.co account)
token: An authentication token [1]_.

repo_type: Set to :obj:`"dataset"` or :obj:`"space"` if uploading to a dataset or space, :obj:`None` or :obj:`"model"` if uploading to a model. Default is :obj:`None`.
private: Whether the model repo should be private.

exist_ok: Do not raise an error if repo already exists
repo_type: Set to :obj:`"dataset"` or :obj:`"space"` if uploading
to a dataset or space, :obj:`None` or :obj:`"model"` if
uploading to a model. Default is :obj:`None`.

lfsmultipartthresh: Optional: internal param for testing purposes.
exist_ok: If ``True``, do not raise an error if repo already
exists.

space_sdk: Choice of SDK to use if repo_type is "space". Can be "streamlit", "gradio", or "static".
space_sdk: Choice of SDK to use if repo_type is "space". Can be
"streamlit", "gradio", or "static".

Returns:
URL to the newly created repo.

References:
.. [1] https://huggingface.co/settings/tokens

Reference
"""
name, organization = _validate_repo_id_deprecation(repo_id, name, organization)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add a check to ensure that at least repo_id or name is passed, since they are now both optional arguments:

>>> from huggingface_hub import create_repo
>>> create_repo()
Traceback (most recent call last):
  File "/usr/lib/python3.6/code.py", line 91, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
  File "/home/lysandre/Workspaces/Python/huggingface_hub/src/huggingface_hub/hf_api.py", line 1056, in create_repo
    name, organization = _validate_repo_id_deprecation(repo_id, name, organization)
  File "/home/lysandre/Workspaces/Python/huggingface_hub/src/huggingface_hub/hf_api.py", line 68, in _validate_repo_id_deprecation
    if "/" in repo_id:
TypeError: argument of type 'NoneType' is not iterable

Something like the following

Suggested change
name, organization = _validate_repo_id_deprecation(repo_id, name, organization)
if name is None and repo_id is None:
raise ValueError("Please pass either `name` or `repo_id`")
name, organization = _validate_repo_id_deprecation(repo_id, name, organization)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, but since name is deprecated, I would just ask to specify repo_id.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, good call!


path = f"{self.endpoint}/api/repos/create"
if token is None:
token = self._validate_or_retrieve_token()
Expand Down Expand Up @@ -1082,8 +1117,8 @@ def create_repo(
"Ignoring provided space_sdk because repo_type is not 'space'."
)

if lfsmultipartthresh is not None:
json["lfsmultipartthresh"] = lfsmultipartthresh
if getattr(self, "_lfsmultipartthresh", None):
json["lfsmultipartthresh"] = self._lfsmultipartthresh
osanseviero marked this conversation as resolved.
Show resolved Hide resolved
r = requests.post(
path,
headers={"authorization": f"Bearer {token}"},
Expand All @@ -1109,18 +1144,36 @@ def create_repo(

def delete_repo(
self,
name: str,
name: str = None,
token: Optional[str] = None,
organization: Optional[str] = None,
repo_type: Optional[str] = None,
repo_id: str = None,
):
"""
HuggingFace git-based system, used for models, datasets, and spaces.

Call HF API to delete a whole repo.
"""Delete a repo from the HuggingFace Hub.

CAUTION(this is irreversible).

Args:
repo_id: A namespace (user or an organization) and a repo name
seperated by a ``/``.

.. versionadded: 0.4.0

token: An authentication token [1]_.

repo_type: Set to :obj:`"dataset"` or :obj:`"space"` if uploading
to a dataset or space, :obj:`None` or :obj:`"model"` if
uploading to a model. Default is :obj:`None`.

Returns:
None

References:
.. [1] https://huggingface.co/settings/tokens
"""
name, organization = _validate_repo_id_deprecation(repo_id, name, organization)

path = f"{self.endpoint}/api/repos/delete"
if token is None:
token = self._validate_or_retrieve_token()
Expand Down Expand Up @@ -1177,18 +1230,40 @@ def delete_repo(

def update_repo_visibility(
self,
name: str,
private: bool,
name: str = None,
private: bool = False,
token: Optional[str] = None,
organization: Optional[str] = None,
repo_type: Optional[str] = None,
repo_id: str = None,
) -> Dict[str, bool]:
"""
Update the visibility setting of a repository.
"""Update the visibility setting of a repository.

Args:
repo_id: A namespace (user or an organization) and a repo name
seperated by a ``/``.

.. versionadded: 0.4.0
adrinjalali marked this conversation as resolved.
Show resolved Hide resolved

private: Whether the model repo should be private.

token: An authentication token [1]_.

repo_type: Set to :obj:`"dataset"` or :obj:`"space"` if uploading
to a dataset or space, :obj:`None` or :obj:`"model"` if
uploading to a model. Default is :obj:`None`.

Returns:
The HTTP response in json.

References:
.. [1] https://huggingface.co/settings/tokens
"""
if repo_type not in REPO_TYPES:
raise ValueError("Invalid repo type")

name, organization = _validate_repo_id_deprecation(repo_id, name, organization)

if token is None:
token = self._validate_or_retrieve_token()
elif not self._is_valid_token(token):
Expand Down
2 changes: 1 addition & 1 deletion src/huggingface_hub/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ def __init__(

If specifying a `clone_from`:
will clone an existing remote repository, for instance one
that was previously created using ``HfApi().create_repo(name=repo_name)``.
that was previously created using ``HfApi().create_repo(repo_id=repo_name)``.
``Repository`` uses the local git credentials by default, but if required, the ``huggingface_token``
as well as the git ``user`` and the ``email`` can be explicitly specified.
If `clone_from` is used, and the repository is being instantiated into a non-empty directory,
Expand Down
Loading