Skip to content

Commit

Permalink
Bring consistency to download and upload APIs (#574)
Browse files Browse the repository at this point in the history
* Start merge

* Potentially finish

* revert testing

* Better errors

* create_repo -> delete_repo

* Refactor

* Refactor x2

* Try explicit else if

* Try second conditional for tests

* Try fix
  • Loading branch information
muellerzr authored Jan 5, 2022
1 parent 1f83ed2 commit bb6fec0
Showing 1 changed file with 56 additions and 1 deletion.
57 changes: 56 additions & 1 deletion src/huggingface_hub/hf_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,32 @@ def create_repo(
else:
raise ValueError("Invalid token passed!")

checked_name = repo_type_and_id_from_hf_id(name)

if (
repo_type is not None
and checked_name[0] is not None
and repo_type != checked_name[0]
):
raise ValueError(
f"""Passed `repo_type` and found `repo_type` are not the same ({repo_type}, {checked_name[0]}).
Please make sure you are expecting the right type of repository to exist."""
)

if (
organization is not None
and checked_name[1] is not None
and organization != checked_name[1]
):
raise ValueError(
f"""Passed `organization` and `name` organization are not the same ({organization}, {checked_name[1]}).
Please either include the organization in only `name` or the `organization` parameter, such as `api.create_repo({checked_name[0]}, organization={organization})` or `api.create_repo({checked_name[1]}/{checked_name[2]})`"""
)

repo_type = repo_type or checked_name[0]
organization = organization or checked_name[1]
name = checked_name[2]

if repo_type not in REPO_TYPES:
raise ValueError("Invalid repo type")

Expand Down Expand Up @@ -888,6 +914,32 @@ def delete_repo(
else:
raise ValueError("Invalid token passed!")

checked_name = repo_type_and_id_from_hf_id(name)

if (
repo_type is not None
and checked_name[0] is not None
and repo_type != checked_name[0]
):
raise ValueError(
f"""Passed `repo_type` and found `repo_type` are not the same ({repo_type}, {checked_name[0]}).
Please make sure you are expecting the right type of repository to exist."""
)

if (
organization is not None
and checked_name[1] is not None
and organization != checked_name[1]
):
raise ValueError(
f"""Passed `organization` and `name` organization are not the same ({organization}, {checked_name[1]}).
Please either include the organization in only `name` or the `organization` parameter, such as `api.create_repo({checked_name[0]}, organization={organization})` or `api.create_repo({checked_name[1]}/{checked_name[2]})`"""
)

repo_type = repo_type or checked_name[0]
organization = organization or checked_name[1]
name = checked_name[2]

if repo_type not in REPO_TYPES:
raise ValueError("Invalid repo type")

Expand Down Expand Up @@ -1185,7 +1237,10 @@ def get_full_repo_name(
otherwise.
"""
if organization is None:
username = self.whoami(token=token)["name"]
if "/" in model_id:
username = model_id.split("/")[0]
else:
username = self.whoami(token=token)["name"]
return f"{username}/{model_id}"
else:
return f"{organization}/{model_id}"
Expand Down

0 comments on commit bb6fec0

Please # to comment.