ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

huggingface_hub 集合(Collection)管理指南:基于 HfApi 的创建、更新与删除全流程实战

huggingface_hub 集合(Collection)管理指南:基于 HfApi 的创建、更新与删除全流程实战 开发工具CLI机器学习【免费下载链接】huggingface_hubThe official CLI and Python client for the Hugging Face Hub.项目地址https://gitcode.com/gh_mirrors/hu/huggingface_hub点击查看免费下载Hugging Face Hub 上的集合Collection是一种将模型、数据集、Space、论文乃至其他集合聚合在同一个页面的组织方式适合用来搭建模型榜单、论文清单、团队资源导航等场景。本文以huggingface_hub官方文档中“컬렉션 관리管理集合”一节为骨架结合src/huggingface_hub/hf_api.py的源码实现系统讲解get_collection、create_collection、update_collection_metadata、delete_collection以及条目级增删改方法的参数语义与底层 REST 调用读完你可以直接用纯 Python 代码完成集合的完整生命周期管理。集合Collection是什么集合是 Hugging Face Hub 上的一种可排序、可协作编辑的资源清单。一个集合可以包含六类条目对应源码中定义的CollectionItemType_T字面量类型见 hf_api.pymodel模型仓库dataset数据集仓库spaceSpace 应用paper论文使用论文 ID例如 arXiv 编号collection嵌套引用其他集合使用其 slugbucket存储桶使用namespace/bucket-name形式每个集合都有一个唯一slug形如TheBloke/recent-models-64f9a55bb3115b4f513ec026owner 标题词元 尾部随机 ID所有管理接口都以这个 slug 作为操作标识。huggingface_hub 把集合管理能力封装在HfApi类中并在模块顶层导出为可直接导入的函数见 hf_api.py 附近的绑定逻辑因此你可以直接写from huggingface_hub import get_collection。两个核心数据模型Collection 与 CollectionItemCollectionItemCollectionItem描述集合中的单个条目见 hf_api.py核心字段如下字段类型含义item_object_idstr条目在集合数据库中的唯一 ID注意它不是Hub 上仓库的 ID更新/删除条目时必须使用它item_idstr底层对象在 Hub 上的 ID可以是 repo_id、论文 ID、集合 slug 或 bucket iditem_typestr条目类型即上文六种字面量之一positionint条目在集合中的位置notestr \| None与该条目关联的纯文本备注从构造函数可以看出__init__中slug参数的优先级处理当条目本身是另一个集合时item_id会被 slug 覆盖note来自服务端返回的{text: ...}结构。CollectionCollection描述集合本体见 hf_api.py字段包括字段类型含义slugstr集合的唯一 slugtitlestr集合标题ownerstr集合所有者用户名或组织名itemslist[CollectionItem]集合内条目列表last_updateddatetime最后更新时间源码中用parse_datetime解析positionint集合在所有者集合列表中的排序位置privatebool是否私有集合themestr集合主题配色如green、pinkupvotesint点赞数descriptionstr \| None描述文本Collection.url是一个 property指向该集合在 Hub 上的页面地址。读取集合get_collection 与 list_collections获取单个集合内容get_collection(collection_slug, *, tokenNone)见 hf_api.py通过GET {endpoint}/api/collections/{collection_slug}拉取集合完整信息并返回Collection对象from huggingface_hub import get_collection collection get_collection(TheBloke/recent-models-64f9a55bb3115b4f513ec026) print(collection.title) # Recent models print(len(collection.items)) # 37 print(collection.items[0]) # CollectionItem(item_object_id651446103cd773a050bf64c2, # item_idTheBloke/U-Amethyst-20B-AWQ, # item_typemodel, position88, noteNone)要点get_collection返回的是完整条目列表适合拿来做后续的条目更新或删除。token参数默认为本地已保存的 token推荐方式传入False可显式禁用鉴权。按条件列出集合list_collections(*, ownerNone, itemNone, sortNone, limitNone, tokenNone)见 hf_api.py返回一个Iterable[Collection]支持按所有者、包含的条目过滤并按lastModified、trending、upvotes排序from huggingface_hub import list_collections # 列出某个用户公开的、包含指定模型的集合 for collection in list_collections(ownerTheBloke, itemmodels/teknium/OpenHermes-2.5-Mistral-7B, limit5): print(collection.slug, collection.upvotes)源码显示list_collections通过paginate()对/api/collections做分页请求并在limit指定时用islice提前截断迭代。需要注意源码中的明确警告列出集合时每个集合的条目列表最多只返回 4 条要拿全量条目必须改用get_collection。创建集合create_collectioncreate_collection(title, *, namespaceNone, descriptionNone, privateFalse, resource_group_idNone, exists_okFalse, tokenNone)见 hf_api.py向POST {endpoint}/api/collections发送请求并返回新创建的Collectionfrom huggingface_hub import create_collection collection create_collection( titleICCV 2023, descriptionPortfolio of models, papers and demos I presented at ICCV 2023, ) print(collection.slug) # username/iccv-2023-64f9a55bb3115b4f513ec026参数语义与限制namespace集合所属命名空间用户名或组织名不传时源码会调用whoami(token)取当前登录用户的名称description描述文本最大长度为 150 字符private默认False即创建公开集合resource_group_id仅对组织拥有的集合生效将集合分配到组织的资源组ID 为 24 位十六进制字符串源码只在传入时将其以resourceGroupId键加入 payloadexists_ok为True时若服务端返回 409集合已存在源码不会抛错而是直接get_collection返回已有集合见 hf_api.py。更新集合元数据update_collection_metadataupdate_collection_metadata(collection_slug, *, titleNone, descriptionNone, positionNone, privateNone, themeNone, tokenNone)见 hf_api.py通过PATCH {endpoint}/api/collections/{collection_slug}更新集合的元信息。所有参数都是可选的只有传入非None的字段才会被发送到服务端源码用{key: value for key, value in payload.items() if value is not None}过滤因此可以只改单个字段from huggingface_hub import update_collection_metadata collection update_collection_metadata( collection_slugusername/iccv-2023-64f9a55bb3115b4f513ec026, titleICCV Oct. 2023, descriptionPortfolio of models, datasets, papers and demos I presented at ICCV Oct. 2023, privateFalse, themepink, ) print(collection.slug) # username/iccv-oct-2023-64f9a55bb3115b4f513ec026注意示例中注释提示的细节修改 title 会更新 slug 的前半部分但尾部的随机 ID 保持不变。position用于调整集合在用户集合列表中的排序theme是 Hub 上的主题配色。关联组织资源组update_collection_resource_group英文版参考文档中与元数据更新并列的还有update_collection_resource_group(collection_slug, resource_group_id, *, tokenNone)见 hf_api.py它通过POST {endpoint}/api/collections/{collection_slug}/resource-group把集合分配到某个组织资源组或传入None将其从资源组移除。该方法仅对组织拥有的集合有效from huggingface_hub import update_collection_resource_group update_collection_resource_group( collection_slugmy-org/iccv-2023-64f9a55bb3115b4f513ec026, resource_group_id66980ecfc1e12a49c8f0e42d, # 24 位十六进制资源组 ID )删除集合delete_collectiondelete_collection(collection_slug, *, missing_okFalse, tokenNone)见 hf_api.py通过DELETE {endpoint}/api/collections/{collection_slug}删除整个集合from huggingface_hub import delete_collection delete_collection(username/useless-collection-64f9a55bb3115b4f513ec026, missing_okTrue)missing_ok为True时服务端返回 404集合不存在不会抛错静默返回见 hf_api.py源码 docstring 中有明确的警告框该操作不可撤销被删除的集合无法恢复。条目级操作添加、更新、删除集合条目添加条目add_collection_itemadd_collection_item(collection_slug, item_id, item_type, *, noteNone, exists_okFalse, tokenNone)见 hf_api.py通过POST {endpoint}/api/collections/{collection_slug}/items向集合追加条目返回更新后的完整Collectionfrom huggingface_hub import add_collection_item collection add_collection_item( collection_slugdavanstrien/climate-64f99dc2a5067f6b65531bab, item_idpierre-loic/climate-news-articles, item_typedataset, ) print(collection.items[-1].item_id) # pierre-loic/climate-news-articles # 新条目被追加到末尾位置 # 也可以给条目附带备注 add_collection_item( collection_slugdavanstrien/climate-64f99dc2a5067f6b65531bab, item_iddatasets/climate_fever, item_typedataset, noteThis dataset adopts the FEVER methodology that consists of 1,535 real-world claims regarding climate-change collected on the internet., )item_idrepo 用repo_id论文用论文 ID集合用 slugbucket 用namespace/bucket-nameitem_type取六种字面量之一note最大 500 字符源码仅在非None时加入 payload{item: {id: ..., type: ...}}exists_ok为True时若返回 409条目已存在则改为返回当前集合源码 docstring 列出的典型错误403表示对仓库只有只读权限没有 write/admin 角色或传了只读 token404表示要添加的条目在 Hub 上不存在409表示条目已在集合中且exists_okFalse。更新条目update_collection_itemupdate_collection_item(collection_slug, item_object_id, *, noteNone, positionNone, tokenNone)见 hf_api.py通过PATCH {endpoint}/api/collections/{collection_slug}/items/{item_object_id}修改条目的备注或位置。关键点参数是item_object_id条目在集合数据库中的 ID而不是 Hub 上的 repo_id需要先从CollectionItem对象上取得from huggingface_hub import get_collection, update_collection_item collection get_collection(TheBloke/recent-models-64f9a55bb3115b4f513ec026) update_collection_item( collection_slugTheBloke/recent-models-64f9a55bb3115b4f513ec026, item_object_idcollection.items[-1].item_object_id, # 必须从 CollectionItem 取得 noteNewly updated model!, position0, # 调整到集合首位 )与元数据更新一致源码同样只发送非None字段note上限 500 字符。删除条目delete_collection_itemdelete_collection_item(collection_slug, item_object_id, *, missing_okFalse, tokenNone)见 hf_api.py通过DELETE {endpoint}/api/collections/{collection_slug}/items/{item_object_id}移除条目同样以item_object_id为标识from huggingface_hub import get_collection, delete_collection_item collection get_collection(TheBloke/recent-models-64f9a55bb3115b4f513ec026) delete_collection_item( collection_slugTheBloke/recent-models-64f9a55bb3115b4f513ec026, item_object_idcollection.items[-1].item_object_id, )missing_okTrue时条目不存在404不会抛错。底层实现与测试验证上述方法全部位于HfApi类的 Collection Endpoints 段落见 hf_api.py统一遵循以下模式使用get_session()获取全局会话调用对应 HTTP 动词访问{self.endpoint}/api/collections...REST 端点通过self._build_hf_headers(tokentoken)注入鉴权头用hf_raise_for_status(r)统一校验响应并抛出自定义的HfHubHTTPError返回对象统一用Collection(**{**r.json(), endpoint: self.endpoint})构造其中endpoint被塞进 kwargs 以支持urlproperty 的解析。项目的测试套件在 tests/test_hf_api.py 中对这些方法做了端到端覆盖例如test_list_collections、test_create_collection_with_description、test_create_collection_exists_ok、test_create_private_collection、test_update_collection、test_delete_collection、test_collection_items以及test_collection_items_with_collections见 tests/test_hf_api.py 附近验证了创建时exists_ok的幂等行为、私有集合、条目的增删改以及集合嵌套集合等场景可作为你编写集成测试时的参照。快速参考方法一览方法HTTP 端点作用关键参数get_collectionGET /api/collections/{slug}获取完整集合collection_sluglist_collectionsGET /api/collections按条件列出集合owner/item/sort/limitcreate_collectionPOST /api/collections创建集合title/description(≤150) /private/exists_okupdate_collection_metadataPATCH /api/collections/{slug}更新集合元信息title/description/position/themeupdate_collection_resource_groupPOST /api/collections/{slug}/resource-group分配/移除组织资源组resource_group_iddelete_collectionDELETE /api/collections/{slug}删除集合不可恢复missing_okadd_collection_itemPOST /api/collections/{slug}/items追加条目item_id/item_type/note(≤500) /exists_okupdate_collection_itemPATCH /api/collections/{slug}/items/{obj_id}更新条目备注/位置item_object_id/note/positiondelete_collection_itemDELETE /api/collections/{slug}/items/{obj_id}删除条目item_object_id/missing_ok使用建议与注意事项鉴权创建、更新、删除类操作需要对应账号的写权限token默认读取本地已保存的凭据显式传False可以走公开只读请求。ID 语义区分条目的更新和删除必须使用CollectionItem.item_object_id集合内数据库 ID误用 Hub 上的repo_id会导致 404。长度限制集合描述上限 150 字符、条目备注上限 500 字符超出会被服务端拒绝。列表截断list_collections返回的每个集合最多包含 4 个条目需要完整条目时务必调用get_collection。删除不可逆delete_collection是永久操作脚本化批量删除前建议先通过get_collection确认 slug 无误。赞分享开发工具CLI机器学习【免费下载链接】huggingface_hubThe official CLI and Python client for the Hugging Face Hub.项目地址https://gitcode.com/gh_mirrors/hu/huggingface_hub点击查看免费下载相关推荐KubeSphere FrontendIntegrationfi生命周期管理实战基于 frontend-forge 的创建、更新、启停与删除全流程KubeSphere FrontendIntegrationfi生命周期管理实战基于 frontend forge 的创建、更新、启停与删除全流程 Fro云原生容器编排后端微服务多集群DevOps可观测性AI 技能ZenML 定时管道管理实战创建、更新与删除 Pipeline Schedule 的完整流程ZenML 定时管道管理实战创建、更新与删除 Pipeline Schedule 的完整流程 本文基于 ZenML 官方教程文档系统讲解如何在 ZenMLMLOps机器学习后端工作流自动化AI Agenthuggingface_hub 仓库管理实战指南创建、删除、复制与配置 Hugging Face Hub 仓库huggingface_hub 仓库管理实战指南创建、删除、复制与配置 Hugging Face Hub 仓库 导读 本指南以 huggingface_hub开发工具CLI机器学习上一篇iPhone的HEIC照片在Windows打不开一款免费HEIF图片转换工具的完整实测下一篇agency-agents 中的 LLM Post-Training Engineer基于证据门禁的 SFT/DPO/GRPO/RLVR 实验管理与发布决策指南创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表