From a58dd84b7bab9826dcc8c46c673664c02f48ae0a Mon Sep 17 00:00:00 2001 From: Lucain Pouget Date: Mon, 25 Sep 2023 13:22:17 +0200 Subject: [PATCH 1/2] Add url attribute to Collection class --- docs/source/en/guides/collections.md | 4 +++- src/huggingface_hub/hf_api.py | 17 ++++++++++++----- tests/test_hf_api.py | 1 + 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/docs/source/en/guides/collections.md b/docs/source/en/guides/collections.md index 9421b7a50f..e054c38773 100644 --- a/docs/source/en/guides/collections.md +++ b/docs/source/en/guides/collections.md @@ -79,11 +79,13 @@ It will return a [`Collection`] object with the high-level metadata (title, desc ```py >>> collection.slug -'iccv-2023-15e23b46cb98efca45' +'owner/iccv-2023-15e23b46cb98efca45' >>> collection.title "ICCV 2023" >>> collection.owner "username" +>>> collection.url +'https://huggingface.co/collections/owner/iccv-2023-15e23b46cb98efca45' ``` ## Manage items in a collection diff --git a/src/huggingface_hub/hf_api.py b/src/huggingface_hub/hf_api.py index 55ed9383c2..de04b5c448 100644 --- a/src/huggingface_hub/hf_api.py +++ b/src/huggingface_hub/hf_api.py @@ -663,6 +663,8 @@ class Collection(ReprMixin): Whether the collection is private or not. theme (`str`): Theme of the collection. E.g. `"green"`. + url (`str`): + URL of the collection on the Hub. """ slug: str @@ -676,7 +678,7 @@ class Collection(ReprMixin): private: bool theme: str - def __init__(self, data: Dict) -> None: + def __init__(self, data: Dict, endpoint: Optional[str] = None) -> None: # Collection info self.slug = data["slug"] self.title = data["title"] @@ -690,6 +692,11 @@ def __init__(self, data: Dict) -> None: self.position = data["position"] self.theme = data["theme"] + # (internal) + if endpoint is None: + endpoint = ENDPOINT + self.url = f"{ENDPOINT}/collections/{self.slug}" + class ModelSearchArguments(AttributeDictionary): """ @@ -5840,7 +5847,7 @@ def get_collection(self, collection_slug: str, *, token: Optional[str] = None) - f"{self.endpoint}/api/collections/{collection_slug}", headers=self._build_hf_headers(token=token) ) hf_raise_for_status(r) - return Collection(r.json()) + return Collection(r.json(), endpoint=self.endpoint) def create_collection( self, @@ -5905,7 +5912,7 @@ def create_collection( return self.get_collection(slug, token=token) else: raise - return Collection(r.json()) + return Collection(r.json(), endpoint=self.endpoint) def update_collection_metadata( self, @@ -5970,7 +5977,7 @@ def update_collection_metadata( json={key: value for key, value in payload.items() if value is not None}, ) hf_raise_for_status(r) - return Collection(r.json()["data"]) + return Collection(r.json()["data"], endpoint=self.endpoint) def delete_collection( self, collection_slug: str, *, missing_ok: bool = False, token: Optional[str] = None @@ -6078,7 +6085,7 @@ def add_collection_item( return self.get_collection(collection_slug, token=token) else: raise - return Collection(r.json()) + return Collection(r.json(), endpoint=self.endpoint) def update_collection_item( self, diff --git a/tests/test_hf_api.py b/tests/test_hf_api.py index 6fb7110fd0..ca56eee240 100644 --- a/tests/test_hf_api.py +++ b/tests/test_hf_api.py @@ -3225,6 +3225,7 @@ def test_create_collection_with_description(self) -> None: self.assertEqual(collection.description, "Contains a lot of cool stuff") self.assertEqual(collection.items, []) self.assertTrue(collection.slug.startswith(self.slug_prefix)) + self.assertEqual(collection.url, f"{ENDPOINT_STAGING}/collections/{collection.slug}") def test_create_collection_exists_ok(self) -> None: # Create collection once without description From 9dfccc41aed2dd377a92a5b285c42d50d889c168 Mon Sep 17 00:00:00 2001 From: Lucain Date: Mon, 25 Sep 2023 13:46:11 +0200 Subject: [PATCH 2/2] Update src/huggingface_hub/hf_api.py Co-authored-by: Daniel van Strien --- src/huggingface_hub/hf_api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/huggingface_hub/hf_api.py b/src/huggingface_hub/hf_api.py index de04b5c448..5acaff3d17 100644 --- a/src/huggingface_hub/hf_api.py +++ b/src/huggingface_hub/hf_api.py @@ -664,7 +664,7 @@ class Collection(ReprMixin): theme (`str`): Theme of the collection. E.g. `"green"`. url (`str`): - URL of the collection on the Hub. + URL for the collection on the Hub. """ slug: str