Skip to content

Commit 0ef2d68

Browse files
[HfApi] Collections in collections (#3120)
* support collections in collection * nit
1 parent 52b4373 commit 0ef2d68

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

src/huggingface_hub/hf_api.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@
135135

136136

137137
R = TypeVar("R") # Return type
138-
CollectionItemType_T = Literal["model", "dataset", "space", "paper"]
138+
CollectionItemType_T = Literal["model", "dataset", "space", "paper", "collection"]
139139

140140
ExpandModelProperty_T = Literal[
141141
"author",
@@ -1169,16 +1169,16 @@ def __init__(self, **kwargs):
11691169
@dataclass
11701170
class CollectionItem:
11711171
"""
1172-
Contains information about an item of a Collection (model, dataset, Space or paper).
1172+
Contains information about an item of a Collection (model, dataset, Space, paper or collection).
11731173
11741174
Attributes:
11751175
item_object_id (`str`):
11761176
Unique ID of the item in the collection.
11771177
item_id (`str`):
1178-
ID of the underlying object on the Hub. Can be either a repo_id or a paper id
1179-
e.g. `"jbilcke-hf/ai-comic-factory"`, `"2307.09288"`.
1178+
ID of the underlying object on the Hub. Can be either a repo_id, a paper id or a collection slug.
1179+
e.g. `"jbilcke-hf/ai-comic-factory"`, `"2307.09288"`, `"celinah/cerebras-function-calling-682607169c35fbfa98b30b9a"`.
11801180
item_type (`str`):
1181-
Type of the underlying object. Can be one of `"model"`, `"dataset"`, `"space"` or `"paper"`.
1181+
Type of the underlying object. Can be one of `"model"`, `"dataset"`, `"space"`, `"paper"` or `"collection"`.
11821182
position (`int`):
11831183
Position of the item in the collection.
11841184
note (`str`, *optional*):
@@ -1192,10 +1192,20 @@ class CollectionItem:
11921192
note: Optional[str] = None
11931193

11941194
def __init__(
1195-
self, _id: str, id: str, type: CollectionItemType_T, position: int, note: Optional[Dict] = None, **kwargs
1195+
self,
1196+
_id: str,
1197+
id: str,
1198+
type: CollectionItemType_T,
1199+
position: int,
1200+
note: Optional[Dict] = None,
1201+
**kwargs,
11961202
) -> None:
11971203
self.item_object_id: str = _id # id in database
11981204
self.item_id: str = id # repo_id or paper id
1205+
# if the item is a collection, override item_id with the slug
1206+
slug = kwargs.get("slug")
1207+
if slug is not None:
1208+
self.item_id = slug # collection slug
11991209
self.item_type: CollectionItemType_T = type
12001210
self.position: int = position
12011211
self.note: str = note["text"] if note is not None else None

tests/test_hf_api.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4130,6 +4130,13 @@ def test_collection_items(self) -> None:
41304130
self._api.delete_repo(dataset_id, repo_type="dataset")
41314131
self._api.delete_collection(collection.slug)
41324132

4133+
@with_production_testing
4134+
def test_collection_items_with_collections(self) -> None:
4135+
collection = HfApi().get_collection("celinah/inference-providers-function-calling-6826023e8ae9b24b3039ee5f")
4136+
assert len(collection.items) > 1
4137+
assert collection.items[0].item_type == "collection"
4138+
assert collection.items[0].item_id.startswith("celinah/")
4139+
41334140

41344141
class AccessRequestAPITest(HfApiCommonTest):
41354142
def setUp(self) -> None:

0 commit comments

Comments
 (0)