Skip to content

Commit 75fbd6d

Browse files
tengomuchoclaude
andauthored
fix(kernels): allow uploads and commits to kernel repos (#5233)
`hf upload --type kernel` failed with "Invalid repo type" because the commit path validated against `REPO_TYPES`, which excludes kernels. Switch `create_commit`, `preupload_lfs_files`, `_duplicate_lfs_files`, `upload_file` and `upload_folder` to `REPO_TYPES_WITH_KERNEL`, following the per-method opt-in from #4068. Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
1 parent e1f595d commit 75fbd6d

2 files changed

Lines changed: 32 additions & 17 deletions

File tree

‎src/huggingface_hub/hf_api.py‎

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5032,8 +5032,8 @@ def create_commit(
50325032
To disable authentication, pass `False`.
50335033

50345034
repo_type (`str`, *optional*):
5035-
Set to `"dataset"` or `"space"` if uploading to a dataset or
5036-
space, `None` or `"model"` if uploading to a model. Default is
5035+
Set to `"dataset"`, `"space"` or `"kernel"` if uploading to a dataset,
5036+
space or kernel repo, `None` or `"model"` if uploading to a model. Default is
50375037
`None`.
50385038

50395039
revision (`str`, *optional*):
@@ -5102,8 +5102,8 @@ def create_commit(
51025102

51035103
commit_description = commit_description if commit_description is not None else ""
51045104
repo_type = repo_type if repo_type is not None else constants.REPO_TYPE_MODEL
5105-
if repo_type not in constants.REPO_TYPES:
5106-
raise ValueError(f"Invalid repo type, must be one of {constants.REPO_TYPES}")
5105+
if repo_type not in constants.REPO_TYPES_WITH_KERNEL:
5106+
raise ValueError(f"Invalid repo type, must be one of {constants.REPO_TYPES_WITH_KERNEL}")
51075107
unquoted_revision = revision or constants.DEFAULT_REVISION
51085108
revision = quote(unquoted_revision, safe="")
51095109
create_pr = create_pr if create_pr is not None else False
@@ -5308,7 +5308,7 @@ def preupload_lfs_files(
53085308
To disable authentication, pass `False`.
53095309

53105310
repo_type (`str`, *optional*):
5311-
The type of repository to upload to (e.g. `"model"` -default-, `"dataset"` or `"space"`).
5311+
The type of repository to upload to (e.g. `"model"` -default-, `"dataset"`, `"space"` or `"kernel"`).
53125312

53135313
revision (`str`, *optional*):
53145314
The git revision to commit from. Defaults to the head of the `"main"` branch.
@@ -5353,8 +5353,8 @@ def preupload_lfs_files(
53535353
from ._commit_api import _fetch_upload_modes, _upload_files
53545354

53555355
repo_type = repo_type if repo_type is not None else constants.REPO_TYPE_MODEL
5356-
if repo_type not in constants.REPO_TYPES:
5357-
raise ValueError(f"Invalid repo type, must be one of {constants.REPO_TYPES}")
5356+
if repo_type not in constants.REPO_TYPES_WITH_KERNEL:
5357+
raise ValueError(f"Invalid repo type, must be one of {constants.REPO_TYPES_WITH_KERNEL}")
53585358
revision = quote(revision, safe="") if revision is not None else constants.DEFAULT_REVISION
53595359
create_pr = create_pr if create_pr is not None else False
53605360
headers = self._build_hf_headers(token=token)
@@ -5476,8 +5476,8 @@ def _duplicate_lfs_files(
54765476
from ._commit_api import DUPLICATE_LFS_BATCH_SIZE, _CopySource
54775477

54785478
repo_type = repo_type if repo_type is not None else constants.REPO_TYPE_MODEL
5479-
if repo_type not in constants.REPO_TYPES:
5480-
raise ValueError(f"Invalid repo type, must be one of {constants.REPO_TYPES}")
5479+
if repo_type not in constants.REPO_TYPES_WITH_KERNEL:
5480+
raise ValueError(f"Invalid repo type, must be one of {constants.REPO_TYPES_WITH_KERNEL}")
54815481
headers = self._build_hf_headers(token=token)
54825482

54835483
copies = list(copies)
@@ -5614,8 +5614,8 @@ def upload_file(
56145614
https://huggingface.co/docs/huggingface_hub/quick-start#authentication).
56155615
To disable authentication, pass `False`.
56165616
repo_type (`str`, *optional*):
5617-
Set to `"dataset"` or `"space"` if uploading to a dataset or
5618-
space, `None` or `"model"` if uploading to a model. Default is
5617+
Set to `"dataset"`, `"space"` or `"kernel"` if uploading to a dataset,
5618+
space or kernel repo, `None` or `"model"` if uploading to a model. Default is
56195619
`None`.
56205620
revision (`str`, *optional*):
56215621
The git revision to commit from. Defaults to the head of the `"main"` branch.
@@ -5698,8 +5698,8 @@ def upload_file(
56985698

56995699
from ._commit_api import CommitOperationAdd
57005700

5701-
if repo_type not in constants.REPO_TYPES:
5702-
raise ValueError(f"Invalid repo type, must be one of {constants.REPO_TYPES}")
5701+
if repo_type not in constants.REPO_TYPES_WITH_KERNEL:
5702+
raise ValueError(f"Invalid repo type, must be one of {constants.REPO_TYPES_WITH_KERNEL}")
57035703

57045704
commit_message = (
57055705
commit_message if commit_message is not None else f"Upload {path_in_repo} with huggingface_hub"
@@ -5825,8 +5825,8 @@ def upload_folder(
58255825
https://huggingface.co/docs/huggingface_hub/quick-start#authentication).
58265826
To disable authentication, pass `False`.
58275827
repo_type (`str`, *optional*):
5828-
Set to `"dataset"` or `"space"` if uploading to a dataset or
5829-
space, `None` or `"model"` if uploading to a model. Default is
5828+
Set to `"dataset"`, `"space"` or `"kernel"` if uploading to a dataset,
5829+
space or kernel repo, `None` or `"model"` if uploading to a model. Default is
58305830
`None`.
58315831
revision (`str`, *optional*):
58325832
The git revision to commit from. Defaults to the head of the `"main"` branch.
@@ -5913,8 +5913,8 @@ def upload_folder(
59135913
... )
59145914
```
59155915
"""
5916-
if repo_type not in constants.REPO_TYPES:
5917-
raise ValueError(f"Invalid repo type, must be one of {constants.REPO_TYPES}")
5916+
if repo_type not in constants.REPO_TYPES_WITH_KERNEL:
5917+
raise ValueError(f"Invalid repo type, must be one of {constants.REPO_TYPES_WITH_KERNEL}")
59185918
if create_pr and revision is not None and revision != constants.DEFAULT_REVISION:
59195919
raise ValueError(
59205920
f"Cannot use `create_pr=True` with `revision='{revision}'`: pull requests created by"

‎tests/test_kernels.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,21 @@ def test_create_kernel(staging_api: HfApi, new_kernel_name: str) -> None:
6767
assert repo_url.endpoint == ENDPOINT_STAGING
6868

6969

70+
def test_upload_to_kernel(staging_api: HfApi, new_kernel_name: str, tmp_path) -> None:
71+
repo_id = staging_api.create_repo(new_kernel_name, repo_type="kernel").repo_id
72+
73+
commit = staging_api.upload_file(
74+
path_or_fileobj=b"# My kernel", path_in_repo="README.md", repo_id=repo_id, repo_type="kernel"
75+
)
76+
assert commit.commit_url.startswith(f"{ENDPOINT_STAGING}/kernels/{repo_id}/commit/")
77+
78+
(tmp_path / "build").mkdir()
79+
(tmp_path / "build" / "kernel.py").write_text("print('hello')")
80+
staging_api.upload_folder(folder_path=tmp_path, repo_id=repo_id, repo_type="kernel")
81+
82+
assert set(staging_api.list_repo_files(repo_id, repo_type="kernel")) >= {"README.md", "build/kernel.py"}
83+
84+
7085
def test_kernel_info(api: HfApi) -> None:
7186
kernel_info = api.kernel_info(KERNEL_TEST_REPO_ID)
7287
assert isinstance(kernel_info, KernelInfo)

0 commit comments

Comments
 (0)