Hub Python 庫文件
集合
並獲得增強的文件體驗
開始使用
集合
集合是 Hub 上相關專案(模型、資料集、空間、論文)的分組,它們被組織在同一頁面上。集合對於建立自己的作品集、分類書籤內容或呈現您想要分享的精選專案列表非常有用。檢視此指南,以更詳細地瞭解集合是什麼以及它們在 Hub 上的外觀。
您可以直接在瀏覽器中管理集合,但在本指南中,我們將重點介紹如何以程式設計方式管理它們。
獲取集合
使用 get_collection() 獲取您的集合或任何公共集合。您必須擁有集合的 _slug_ 才能檢索集合。slug 是一個基於標題和唯一 ID 的集合識別符號。您可以在集合頁面的 URL 中找到 slug。

讓我們獲取集合 "TheBloke/recent-models-64f9a55bb3115b4f513ec026"
>>> from huggingface_hub import get_collection
>>> collection = get_collection("TheBloke/recent-models-64f9a55bb3115b4f513ec026")
>>> collection
Collection(
slug='TheBloke/recent-models-64f9a55bb3115b4f513ec026',
title='Recent models',
owner='TheBloke',
items=[...],
last_updated=datetime.datetime(2023, 10, 2, 22, 56, 48, 632000, tzinfo=datetime.timezone.utc),
position=1,
private=False,
theme='green',
upvotes=90,
description="Models I've recently quantized. Please note that currently this list has to be updated manually, and therefore is not guaranteed to be up-to-date."
)
>>> collection.items[0]
CollectionItem(
item_object_id='651446103cd773a050bf64c2',
item_id='TheBloke/U-Amethyst-20B-AWQ',
item_type='model',
position=88,
note=None
)
由 get_collection() 返回的 Collection 物件包含
- 高階元資料:
slug
、owner
、title
、description
等。 - CollectionItem 物件的列表;每個專案代表一個模型、一個數據集、一個空間或一篇論文。
所有集合專案都保證具有
- 唯一的
item_object_id
:這是資料庫中集合專案的 ID - 一個
item_id
:這是 Hub 上底層專案(模型、資料集、空間、論文)的 ID;它不一定是唯一的,只有item_id
/item_type
對是唯一的 - 一個
item_type
:模型、資料集、空間、論文 - 專案中集合中的
position
,可以更新此位置以重新組織您的集合(參見下面的 update_collection_item())
還可以向專案附加 note
。這對於新增有關專案的附加資訊(評論、指向部落格文章的連結等)很有用。如果專案沒有備註,則該屬性仍為 None
。
除了這些基本屬性外,返回的專案還可以根據其型別具有附加屬性:`author`、`private`、`lastModified`、`gated`、`title`、`likes`、`upvotes` 等。這些屬性均不保證返回。
列出集合
我們還可以使用 list_collections() 檢索集合。可以使用一些引數篩選集合。讓我們列出使用者 teknium
的所有集合。
>>> from huggingface_hub import list_collections
>>> collections = list_collections(owner="teknium")
這將返回一個 Collection
物件的可迭代物件。我們可以遍歷它們來列印,例如,每個集合的點贊數。
>>> for collection in collections:
... print("Number of upvotes:", collection.upvotes)
Number of upvotes: 1
Number of upvotes: 5
列出集合時,每個集合的專案列表最多截斷為 4 個專案。要檢索集合中的所有專案,您必須使用 get_collection()。
可以進行更高階的篩選。讓我們獲取所有包含模型 TheBloke/OpenHermes-2.5-Mistral-7B-GGUF 的集合,按趨勢排序,並將數量限制為 5。
>>> collections = list_collections(item="models/TheBloke/OpenHermes-2.5-Mistral-7B-GGUF", sort="trending", limit=5):
>>> for collection in collections:
... print(collection.slug)
teknium/quantized-models-6544690bb978e0b0f7328748
AmeerH/function-calling-65560a2565d7a6ef568527af
PostArchitekt/7bz-65479bb8c194936469697d8c
gnomealone/need-to-test-652007226c6ce4cdacf9c233
Crataco/favorite-7b-models-651944072b4fffcb41f8b568
引數 sort
必須是 "last_modified"
、"trending"
或 "upvotes"
之一。引數 item
接受任何特定專案。例如
"models/teknium/OpenHermes-2.5-Mistral-7B"
"spaces/julien-c/open-gpt-rhyming-robot"
"datasets/squad"
"papers/2311.12983"
有關更多詳細資訊,請檢視 list_collections() 參考。
建立新集合
既然我們知道如何獲取 Collection,那麼就來建立我們自己的集合吧!使用 create_collection() 並提供標題和描述。要在組織頁面上建立集合,請在建立集合時傳入 namespace="my-cool-org"
。最後,您還可以透過傳入 private=True
來建立私有集合。
>>> from huggingface_hub import create_collection
>>> collection = create_collection(
... title="ICCV 2023",
... description="Portfolio of models, papers and demos I presented at ICCV 2023",
... )
它將返回一個帶有高階元資料(標題、描述、所有者等)和空專案列表的 Collection 物件。您現在可以使用其 slug
引用此集合。
>>> collection.slug
'owner/iccv-2023-15e23b46cb98efca45'
>>> collection.title
"ICCV 2023"
>>> collection.owner
"username"
>>> collection.url
'https://huggingface.co/collections/owner/iccv-2023-15e23b46cb98efca45'
管理集合中的專案
既然我們有了一個 Collection,我們想向其中新增專案並組織它們。
新增專案
專案必須使用 add_collection_item() 逐個新增。您只需要知道 collection_slug
、item_id
和 item_type
。可選地,您還可以為專案新增 note
(最多 500 個字元)。
>>> from huggingface_hub import create_collection, add_collection_item
>>> collection = create_collection(title="OS Week Highlights - Sept 18 - 24", namespace="osanseviero")
>>> collection.slug
"osanseviero/os-week-highlights-sept-18-24-650bfed7f795a59f491afb80"
>>> add_collection_item(collection.slug, item_id="coqui/xtts", item_type="space")
>>> add_collection_item(
... collection.slug,
... item_id="warp-ai/wuerstchen",
... item_type="model",
... note="Würstchen is a new fast and efficient high resolution text-to-image architecture and model"
... )
>>> add_collection_item(collection.slug, item_id="lmsys/lmsys-chat-1m", item_type="dataset")
>>> add_collection_item(collection.slug, item_id="warp-ai/wuerstchen", item_type="space") # same item_id, different item_type
如果集合中已存在某個專案(相同的 item_id
/item_type
對),則會引發 HTTP 409 錯誤。您可以透過設定 exists_ok=True
來選擇忽略此錯誤。
為現有專案新增備註
您可以使用 update_collection_item() 修改現有專案以新增或修改附加到該專案的備註。讓我們重用上面的示例
>>> from huggingface_hub import get_collection, update_collection_item
# Fetch collection with newly added items
>>> collection_slug = "osanseviero/os-week-highlights-sept-18-24-650bfed7f795a59f491afb80"
>>> collection = get_collection(collection_slug)
# Add note the `lmsys-chat-1m` dataset
>>> update_collection_item(
... collection_slug=collection_slug,
... item_object_id=collection.items[2].item_object_id,
... note="This dataset contains one million real-world conversations with 25 state-of-the-art LLMs.",
... )
重新排序專案
集合中的專案是有序的。順序由每個專案的 position
屬性決定。預設情況下,專案透過在集合末尾新增新專案來排序。您可以使用 update_collection_item() 更新順序,就像新增備註一樣。
讓我們重用上面的示例
>>> from huggingface_hub import get_collection, update_collection_item
# Fetch collection
>>> collection_slug = "osanseviero/os-week-highlights-sept-18-24-650bfed7f795a59f491afb80"
>>> collection = get_collection(collection_slug)
# Reorder to place the two `Wuerstchen` items together
>>> update_collection_item(
... collection_slug=collection_slug,
... item_object_id=collection.items[3].item_object_id,
... position=2,
... )
刪除專案
最後,您還可以使用 delete_collection_item() 刪除專案。
>>> from huggingface_hub import get_collection, update_collection_item
# Fetch collection
>>> collection_slug = "osanseviero/os-week-highlights-sept-18-24-650bfed7f795a59f491afb80"
>>> collection = get_collection(collection_slug)
# Remove `coqui/xtts` Space from the list
>>> delete_collection_item(collection_slug=collection_slug, item_object_id=collection.items[0].item_object_id)
刪除集合
可以使用 delete_collection() 刪除集合。
這是一項不可逆的操作。已刪除的集合無法恢復。
>>> from huggingface_hub import delete_collection
>>> collection = delete_collection("username/useless-collection-64f9a55bb3115b4f513ec026", missing_ok=True)