Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 7 additions & 2 deletions src/huggingface_hub/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,19 @@

REPO_TYPE_DATASET = "dataset"
REPO_TYPE_SPACE = "space"
REPO_TYPES = [None, REPO_TYPE_DATASET, REPO_TYPE_SPACE]
REPO_TYPE_MODEL = "model"
REPO_TYPES = [None, REPO_TYPE_MODEL, REPO_TYPE_DATASET, REPO_TYPE_SPACE]
SPACES_SDK_TYPES = ["gradio", "streamlit", "static"]

REPO_TYPES_URL_PREFIXES = {
REPO_TYPE_DATASET: "datasets/",
REPO_TYPE_SPACE: "spaces/",
}
REPO_TYPES_MAPPING = {"datasets": REPO_TYPE_DATASET, "spaces": REPO_TYPE_SPACE}
REPO_TYPES_MAPPING = {
"datasets": REPO_TYPE_DATASET,
"spaces": REPO_TYPE_SPACE,
"models": REPO_TYPE_MODEL,
}


# default cache
Expand Down
34 changes: 17 additions & 17 deletions src/huggingface_hub/hf_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ def list_repo_files(
"""
Get the list of files in a given repo.
"""
if repo_type is None:
if repo_type is None or repo_type == "model":
info = self.model_info(
repo_id, revision=revision, token=token, timeout=timeout
)
Expand Down Expand Up @@ -708,7 +708,7 @@ def create_repo(
Params:
private: Whether the model repo should be private (requires a paid huggingface.co account)

repo_type: Set to "dataset" or "space" if creating a dataset or space, default is model
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`.

exist_ok: Do not raise an error if repo already exists

Expand Down Expand Up @@ -742,19 +742,19 @@ def create_repo(
raise ValueError("Invalid repo type")

json = {"name": name, "organization": organization, "private": private}
if repo_type is not None:
if repo_type is not None and repo_type != "model":
json["type"] = repo_type
if repo_type == "space":
if space_sdk is None:
raise ValueError(
"No space_sdk provided. `create_repo` expects space_sdk to be one of "
f"{SPACES_SDK_TYPES} when repo_type is 'space'`"
)
if space_sdk not in SPACES_SDK_TYPES:
raise ValueError(
f"Invalid space_sdk. Please choose one of {SPACES_SDK_TYPES}."
)
json["sdk"] = space_sdk
elif repo_type == "space":
Copy link
Member

Choose a reason for hiding this comment

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

Actually shouldn't this be an if rather than elif given that if repo_type == "space" would have entered the previous branch of the if statement?

Would be nice to add a test for that specific behavior to ensure it's not broken.

Thanks @julien-c for raising!

if space_sdk is None:
raise ValueError(
"No space_sdk provided. `create_repo` expects space_sdk to be one of "
f"{SPACES_SDK_TYPES} when repo_type is 'space'`"
)
if space_sdk not in SPACES_SDK_TYPES:
raise ValueError(
f"Invalid space_sdk. Please choose one of {SPACES_SDK_TYPES}."
)
json["sdk"] = space_sdk
if space_sdk is not None and repo_type != "space":
warnings.warn(
"Ignoring provided space_sdk because repo_type is not 'space'."
Expand Down Expand Up @@ -822,7 +822,7 @@ def delete_repo(
raise ValueError("Invalid repo type")

json = {"name": name, "organization": organization}
if repo_type is not None:
if repo_type is not None and repo_type != "model":
Copy link
Member

Choose a reason for hiding this comment

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

same comment here, you can remove that and clause

json["type"] = repo_type

r = requests.delete(
Expand Down Expand Up @@ -913,7 +913,7 @@ def upload_file(
Authentication token, obtained with :function:`HfApi.login` method. Will default to the stored token.

repo_type (``str``, Optional):
Set to :obj:`"dataset"` or :obj:`"space"` if uploading to a dataset or space, :obj:`None` if uploading to a model. Default is :obj:`None`.
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`.

revision (``str``, Optional):
The git revision to commit from. Defaults to the :obj:`"main"` branch.
Expand Down Expand Up @@ -1049,7 +1049,7 @@ def delete_file(
Authentication token, obtained with :function:`HfApi.login` method. Will default to the stored token.

repo_type (``str``, Optional):
Set to :obj:`"dataset"` or :obj:`"space"` if the file is in a dataset or space repository, :obj:`None` if in a model. Default is :obj:`None`.
Set to :obj:`"dataset"` or :obj:`"space"` if the file is in a dataset or space, :obj:`None` or :obj:`"model"` if in a model. Default is :obj:`None`.

revision (``str``, Optional):
The git revision to commit from. Defaults to the :obj:`"main"` branch.
Expand Down
18 changes: 18 additions & 0 deletions tests/test_hf_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from huggingface_hub.commands.user import _login
from huggingface_hub.constants import (
REPO_TYPE_DATASET,
REPO_TYPE_MODEL,
REPO_TYPE_SPACE,
SPACES_SDK_TYPES,
)
Expand Down Expand Up @@ -161,6 +162,23 @@ def test_create_update_and_delete_repo(self):
self.assertFalse(res["private"])
self._api.delete_repo(name=REPO_NAME, token=self._token)

def test_create_update_and_delete_model_repo(self):
REPO_NAME = repo_name("crud")
self._api.create_repo(
name=REPO_NAME, token=self._token, repo_type=REPO_TYPE_MODEL
)
res = self._api.update_repo_visibility(
name=REPO_NAME, token=self._token, private=True, repo_type=REPO_TYPE_MODEL
)
self.assertTrue(res["private"])
res = self._api.update_repo_visibility(
name=REPO_NAME, token=self._token, private=False, repo_type=REPO_TYPE_MODEL
)
self.assertFalse(res["private"])
self._api.delete_repo(
name=REPO_NAME, token=self._token, repo_type=REPO_TYPE_MODEL
)

def test_create_update_and_delete_dataset_repo(self):
DATASET_REPO_NAME = dataset_repo_name("crud")
self._api.create_repo(
Expand Down