Collections
Last updated
Last updated
A collection is a group of related items on the Hub (models, datasets, Spaces, papers) that are organized together on the same page. Collections are useful for creating your own portfolio, bookmarking content in categories, or presenting a curated list of items you want to share. Check out this to understand in more detail what collections are and how they look on the Hub.
You can directly manage collections in the browser, but in this guide, we will focus on how to manage it programmatically.
Use to fetch your collections or any public ones. You must have the collection’s slug to retrieve a collection. A slug is an identifier for a collection based on the title and a unique ID. You can find the slug in the URL of the collection page.
Let’s fetch the collection with, "TheBloke/recent-models-64f9a55bb3115b4f513ec026"
:
Copied
high-level metadata: slug
, owner
, title
, description
, etc.
All collection items are guaranteed to have:
a unique item_object_id
: this is the id of the collection item in the database
an item_id
: this is the id on the Hub of the underlying item (model, dataset, Space, paper); it is not necessarily unique, and only the item_id
/item_type
pair is unique
an item_type
: model, dataset, Space, paper
A note
can also be attached to the item. This is useful to add additional information about the item (a comment, a link to a blog post, etc.). The attribute still has a None
value if an item doesn’t have a note.
In addition to these base attributes, returned items can have additional attributes depending on their type: author
, private
, lastModified
, gated
, title
, likes
, upvotes
, etc. None of these attributes are guaranteed to be returned.
Copied
Copied
Copied
If an item already exists in a collection (same item_id
/item_type
pair), an HTTP 409 error will be raised. You can choose to ignore this error by setting exists_ok=True
.
Copied
Let’s reuse our example above:
Copied
Copied
This is a non-revertible action. A deleted collection cannot be restored.
Copied
The object returned by contains:
a list of objects; each item represents a model, a dataset, a Space, or a paper.
the position
of the item in the collection, which can be updated to reorganize your collection (see below)
Now that we know how to get a , let’s create our own! Use with a title and description. To create a collection on an organization page, pass namespace="my-cool-org"
when creating the collection. Finally, you can also create private collections by passing private=True
.
It will return a object with the high-level metadata (title, description, owner, etc.) and an empty list of items. You will now be able to refer to this collection using it’s slug
.
Now that we have a , we want to add items to it and organize them.
Items have to be added one by one using . You only need to know the collection_slug
, item_id
and item_type
. Optionally, you can also add a note
to the item (500 characters maximum).
You can modify an existing item to add or modify the note attached to it using . Let’s reuse the example above:
Items in a collection are ordered. The order is determined by the position
attribute of each item. By default, items are ordered by appending new items at the end of the collection. You can update the order using the same way you would add a note.
Finally, you can also remove an item using .
A collection can be deleted using .