Hugging Face Hub 搜索 API 升级
Hugging Face 更新了 `huggingface_hub` 库,简化了通过代码在 Hub 上搜索模型和数据集的流程。新特性旨在降低用户门槛,提供友好的 API 以替代复杂的底层查询逻辑。
报道正文
本文目录6 个章节
Hugging Face Hub 上的超级搜索功能
发布于 2022 年 1 月 25 日 在 GitHub 上更新 点赞 - Zachary Mueller muellerzr
huggingface_hub 库是一个轻量级接口,提供了以编程方式探索 Hugging Face 提供的托管端点(模型、数据集和 Spaces)的方法。
在此之前,通过该接口在 Hub 上进行搜索颇具挑战性,用户需要“自行摸索”并适应许多细节。
在本文中,我们将探讨 huggingface_hub 中新增的一些令人兴奋的功能,这些功能旨在降低使用门槛,为用户提供友好的 API,使其无需离开 Jupyter 或 Python 界面即可搜索所需的模型和数据集。
在开始之前,如果你的系统中尚未安装最新版本的
huggingface_hub库,请运行以下代码单元格:
!pip install huggingface_hub -U问题背景:
首先,让我们设想一下你面临的场景。你想找到 Hugging Face Hub 上所有用于文本分类(Text Classification)、在 GLUE 数据集上训练过且兼容 PyTorch 的模型。
你可以直接打开 https://huggingface.co/models 并使用页面上的筛选控件。但这需要你离开 IDE 并浏览搜索结果,而且获取所需信息还需要点击几次按钮。
如果有一种解决方案,让你无需离开 IDE 就能完成这一切呢?借助编程接口,这种功能也更容易集成到探索 Hub 的工作流中。
这正是 huggingface_hub 发挥作用的地方。
对于熟悉该库的用户来说,你可能已经知道我们可以搜索这类模型。然而,构建正确的查询语句往往是一个充满试错的痛苦过程。
我们能简化这个过程吗?让我们来一探究竟!
寻找我们需要的内容
首先,我们导入 HfApi,这是一个帮助我们与 Hugging Face 后端托管服务交互的类。我们可以通过它与模型、数据集等进行交互。此外,我们还将导入几个辅助类:ModelFilter 和 ModelSearchArguments。
from huggingface_hub import HfApi, ModelFilter, ModelSearchArguments
api = HfApi()这两个类可以帮助我们构建上述问题的解决方案。ModelSearchArguments 类类似于一个命名空间,其中包含了我们可以搜索的所有有效参数!
让我们来看一下:
>>> model_args = ModelSearchArguments()
>>> model_argsAvailable Attributes or Keys:
* author
* dataset
* language
* library
* license
* model_name
* pipeline_tag我们可以看到各种可用的属性(关于这种“魔法”是如何实现的,稍后会详细介绍)。如果我们要对所需内容进行分类,大致可以将其划分为:
pipeline_tag(或任务):文本分类(Text Classification)dataset:GLUElibrary:PyTorch
基于这种划分,我们自然会在已声明的 model_args 中找到它们:
>>> model_args.pipeline_tag.TextClassification'text-classification'>>> model_args.dataset.glue'dataset:glue'>>> model_args.library.PyTorch'pytorch'不过,我们会注意到这里进行了一些便捷的封装处理。ModelSearchArguments(以及配套的 DatasetSearchArguments)提供了一个人类可读的接口,并将输出格式化为 API 所期望的形式,例如 GLUE 数据集应使用 dataset:glue 进行搜索。
这一点至关重要,因为如果没有这份关于特定参数书写方式的“速查表”,在使用 API 搜索模型时,你很容易陷入挫败感之中!
既然我们已经知道了正确的参数,就可以轻松地搜索 API:
>>> models = api.list_models(filter = (
>>> model_args.pipeline_tag.TextClassification,
>>> model_args.dataset.glue,
>>> model_args.library.PyTorch)
>>> )
>>> print(len(models)) 140我们发现共有 140 个符合我们条件的匹配模型!(截至本文撰写时)。如果我们仔细查看其中一个,会发现它确实看起来是正确的:
>>> models[0] ModelInfo: {
modelId: Jiva/xlm-roberta-large-it-mnli
sha: c6e64469ec4aa17fedbd1b2522256f90a90b5b86
lastModified: 2021-12-10T14:56:38.000Z
tags: ['pytorch', 'xlm-roberta', 'text-classification', 'it', 'dataset:multi_nli', 'dataset:glue', 'arxiv:1911.02116', 'transformers', 'tensorflow', 'license:mit', 'zero-shot-classification']
pipeline_tag: zero-shot-classification
siblings: [ModelFile(rfilename='.gitattributes'), ModelFile(rfilename='README.md'), ModelFile(rfilename='config.json'), ModelFile(rfilename='pytorch_model.bin'), ModelFile(rfilename='sentencepiece.bpe.model'), ModelFile(rfilename='special_tokens_map.json'), ModelFile(rfilename='tokenizer.json'), ModelFile(rfilename='tokenizer_config.json')]
config: None
private: False
downloads: 680
library_name: transformers
likes: 1
}这样可读性更强,而且无需猜测“这个参数我写对了吗?”
你知道吗?你也可以通过模型 ID 以编程方式获取该模型的信息。具体做法如下:
PYTHON api.model_info('Jiva/xlm-roberta-large-it-mnli')
更进一步
我们已经看到如何利用 ModelSearchArguments 和 DatasetSearchArguments 来消除在 Hub 上搜索时的猜测成分,但如果我们有一个非常复杂、混乱的查询该怎么办呢?
例如:我想查找所有同时用于 text-classification(文本分类)和 zero-shot(零样本)分类任务的模型,这些模型是在 Multi NLI 和 GLUE 数据集上训练的,并且兼容 PyTorch 和 TensorFlow(这是一个更精确的查询,旨在找到上述模型)。
为了设置此查询,我们将使用 ModelFilter 类。它专为处理此类情况而设计,因此我们不必绞尽脑汁:
>>> filt = ModelFilter(
>>> task = ["text-classification", "zero-shot-classification"],
>>> trained_dataset = [model_args.dataset.multi_nli, model_args.dataset.glue],
>>> library = ['pytorch', 'tensorflow']
>>> )
>>> api.list_models(filt) [ModelInfo: {
modelId: Jiva/xlm-roberta-large-it-mnli
sha: c6e64469ec4aa17fedbd1b2522256f90a90b5b86
lastModified: 2021-12-10T14:56:38.000Z
tags: ['pytorch', 'xlm-roberta', 'text-classification', 'it', 'dataset:multi_nli', 'dataset:glue', 'arxiv:1911.02116', 'transformers', 'tensorflow', 'license:mit', 'zero-shot-classification']
pipeline_tag: zero-shot-classification
siblings: [ModelFile(rfilename='.gitattributes'), ModelFile(rfilename='README.md'), ModelFile(rfilename='config.json'), ModelFile(rfilename='pytorch_model.bin'), ModelFile(rfilename='sentencepiece.bpe.model'), ModelFile(rfilename='special_tokens_map.json'), ModelFile(rfilename='tokenizer.json'), ModelFile(rfilename='tokenizer_config.json')]
config: None
private: False
downloads: 680
library_name: transformers
likes: 1
}]我们可以很快发现,通过 API 进行搜索的方式变得更加协调统一,而且完全不会给你增添任何麻烦!
其中的“魔法”是什么?
我们将简要探讨赋予我们这种类似枚举字典数据类型的底层机制——AttributeDictionary。
该设计深受 fastcore 库中 AttrDict 类的启发。其核心思想是:将一个普通字典进行增强,以支持探索式编程,为字典中的每个键提供 Tab 自动补全功能。
正如我们之前所见,当处理嵌套字典时,这种能力会变得更加强大,例如我们可以通过 model_args.dataset.glue 这样的路径进行探索!
对于熟悉 JavaScript 的读者来说,我们模仿了
object类的工作方式。
这个简单的工具类在探索嵌套数据类型、理解数据结构内容(例如 API 请求的返回结果)时,能够为用户提供更加以用户为中心的体验。
如前所述,我们在 AttrDict 的基础上进行了几项关键扩展:
- 你可以使用
del model_args[key]或del model_args.key来删除键 - 我们之前看到的那个简洁清晰的
__repr__表示形式
不过,有一个非常重要的概念需要注意:如果某个键包含数字或特殊字符,则必须将其作为字典索引访问,而不能作为对象属性访问。
>>> from huggingface_hub.utils.endpoint_helpers import AttributeDictionary下面是一个简短的示例,展示了一个包含键 3_c 的 AttributeDictionary:
>>> d = {"a":2, "b":3, "3_c":4}
>>> ad = AttributeDictionary(d)[注:本文篇幅超长,以上为核心前篇精译,后续内容保留原文呈现]
>>>
>>> ad.3_c File "<ipython-input-6-c0fe109cf75d>", line 2
ad.3_c
^
SyntaxError: invalid token>>>
>>> ad["3_c"]4Concluding thoughts
Hopefully by now you have a brief understanding of how this new searching API can directly impact your workflow and exploration of the Hub! Along with this, perhaps you know of a place in your code where the AttributeDictionary might be useful for you to use.
From here, make sure to check out the official documentation on Searching the Hub Efficiently and don't forget to give us a star!