Hub Python 库文档
仓库卡片
并获得增强的文档体验
开始使用
仓库卡片
huggingface_hub 库提供了用于创建、共享和更新模型/数据集卡片的 Python 接口。访问专用文档页面以深入了解 Hub 上的模型卡片是什么以及它们在底层如何工作。您还可以查看我们的模型卡片指南,了解如何在自己的项目中使用这些实用工具。
仓库卡片
RepoCard
对象是 ModelCard、DatasetCard 和 SpaceCard
的父类。
从字符串内容初始化 RepoCard。内容应为 Markdown 文件,开头包含 YAML 块,后跟 Markdown 主体。
示例
>>> from huggingface_hub.repocard import RepoCard
>>> text = '''
... ---
... language: en
... license: mit
... ---
...
... # My repo
... '''
>>> card = RepoCard(text)
>>> card.data.to_dict()
{'language': 'en', 'license': 'mit'}
>>> card.text
'\n# My repo\n'
- 当仓库卡片元数据内容不是字典时,引发
ValueError
。
from_template
< 来源 >( 卡片_数据: CardData 模板_路径: typing.Optional[str] = None 模板_字符串: typing.Optional[str] = None **template_kwargs ) → huggingface_hub.repocard.RepoCard
从模板初始化 RepoCard。默认情况下,它使用默认模板。
模板是 Jinja2 模板,可以通过传递关键字参数进行自定义。
加载
< 来源 >( 仓库_ID_或_路径: typing.Union[str, pathlib.Path] 仓库_类型: typing.Optional[str] = None 令牌: typing.Optional[str] = None 忽略_元数据_错误: bool = False ) → huggingface_hub.repocard.RepoCard
参数
- repo_id_or_path (
Union[str, Path]
) — 与 Hugging Face Hub 仓库或本地文件路径关联的仓库 ID。 - repo_type (
str
, 可选) — 要推送到的 Hugging Face 仓库类型。默认为 None,将使用“模型”。其他选项是“数据集”和“空间”。从本地文件路径加载时未使用。如果从子类调用此函数,默认值将为子类的repo_type
。 - token (
str
, 可选) — 身份验证令牌,通过huggingface_hub.HfApi.login
方法获取。将默认为存储的令牌。 - ignore_metadata_errors (
str
) — 如果为 True,解析元数据部分时发生的错误将被忽略。在此过程中可能会丢失一些信息。请自行承担风险。
从仓库的 README.md 文件或文件路径初始化的 RepoCard(或子类)。
从 Hugging Face Hub 仓库的 README.md 或本地文件路径初始化 RepoCard。
push_to_hub
< 来源 >( 仓库_ID: str 令牌: typing.Optional[str] = None 仓库_类型: typing.Optional[str] = None 提交_信息: typing.Optional[str] = None 提交_描述: typing.Optional[str] = None 版本: typing.Optional[str] = None 创建_PR: typing.Optional[bool] = None 父提交: typing.Optional[str] = None ) → str
参数
- repo_id (
str
) — 要推送到的 Hugging Face Hub 仓库的仓库 ID。示例:“nateraw/food”。 - token (
str
, 可选) — 身份验证令牌,通过huggingface_hub.HfApi.login
方法获取。将默认为存储的令牌。 - repo_type (
str
, 可选, 默认为“model”) — 要推送到的 Hugging Face 仓库类型。选项有“model”、“dataset”和“space”。如果此函数由子类调用,它将默认为子类的repo_type
。 - commit_message (
str
, 可选) — 生成的提交的摘要/标题/第一行。 - commit_description (
str
, 可选) — 生成的提交的描述。 - revision (
str
, 可选) — 要从其提交的 git 版本。默认为"main"
分支的头部。 - create_pr (
bool
, 可选) — 是否创建包含此提交的拉取请求。默认为False
。 - parent_commit (
str
, 可选) — 父提交的 OID / SHA,以十六进制字符串表示。也支持简写(前 7 个字符)。如果指定且create_pr
为False
,则如果revision
不指向parent_commit
,提交将失败。如果指定且create_pr
为True
,将从parent_commit
创建拉取请求。指定parent_commit
确保在提交更改之前仓库未更改,在仓库同时更新/提交时尤其有用。
返回
字符串
更新卡片元数据的提交 URL。
将 RepoCard 推送到 Hugging Face Hub 仓库。
保存
< 来源 >( 文件路径: typing.Union[pathlib.Path, str] )
将 RepoCard 保存到文件。
验证
< 来源 >( 仓库_类型: typing.Optional[str] = None )
根据 Hugging Face Hub 的卡片验证逻辑验证卡片。使用此函数需要访问互联网,因此它仅由 huggingface_hub.repocard.RepoCard.push_to_hub() 内部调用。
- 如果卡片未能通过验证检查,则引发
ValueError
。 - 如果 Hub API 请求因任何其他原因失败,则引发
HTTPError
。
卡片数据
CardData 对象是 ModelCardData 和 DatasetCardData 的父类。
包含 RepoCard 元数据的结构。
CardData 是 ModelCardData 和 DatasetCardData 的父类。
元数据可以导出为字典或 YAML。导出可以自定义以改变数据的表示(例如:扁平化评估结果)。CardData
的行为类似于字典(可以获取、弹出、设置值),但不继承自 dict
以允许此导出步骤。
获取给定元数据键的值。
弹出给定元数据键的值。
将 CardData 转换为字典。
to_yaml
< 来源 >( 换行符 = None 原始_顺序: typing.Optional[typing.List[str]] = None ) → str
将 CardData 转储为 YAML 块,以便包含在 README.md 文件中。
模型卡片
ModelCard
from_template
< 来源 >( 卡片_数据: ModelCardData 模板_路径: typing.Optional[str] = None 模板_字符串: typing.Optional[str] = None **template_kwargs ) → huggingface_hub.ModelCard
从模板初始化 ModelCard。默认情况下,它使用默认模板,该模板可在此处找到:https://github.com/huggingface/huggingface_hub/blob/main/src/huggingface_hub/templates/modelcard_template.md
模板是 Jinja2 模板,可以通过传递关键字参数进行自定义。
示例
>>> from huggingface_hub import ModelCard, ModelCardData, EvalResult
>>> # Using the Default Template
>>> card_data = ModelCardData(
... language='en',
... license='mit',
... library_name='timm',
... tags=['image-classification', 'resnet'],
... datasets=['beans'],
... metrics=['accuracy'],
... )
>>> card = ModelCard.from_template(
... card_data,
... model_description='This model does x + y...'
... )
>>> # Including Evaluation Results
>>> card_data = ModelCardData(
... language='en',
... tags=['image-classification', 'resnet'],
... eval_results=[
... EvalResult(
... task_type='image-classification',
... dataset_type='beans',
... dataset_name='Beans',
... metric_type='accuracy',
... metric_value=0.9,
... ),
... ],
... model_name='my-cool-model',
... )
>>> card = ModelCard.from_template(card_data)
>>> # Using a Custom Template
>>> card_data = ModelCardData(
... language='en',
... tags=['image-classification', 'resnet']
... )
>>> card = ModelCard.from_template(
... card_data=card_data,
... template_path='./src/huggingface_hub/templates/modelcard_template.md',
... custom_template_var='custom value', # will be replaced in template if it exists
... )
ModelCardData
class huggingface_hub.ModelCardData
< source >( base_model: typing.Union[typing.List[str], str, NoneType] = None datasets: typing.Union[typing.List[str], str, NoneType] = None eval_results: typing.Optional[typing.List[huggingface_hub.repocard_data.EvalResult]] = None language: typing.Union[typing.List[str], str, NoneType] = None library_name: typing.Optional[str] = None license: typing.Optional[str] = None license_name: typing.Optional[str] = None license_link: typing.Optional[str] = None metrics: typing.Optional[typing.List[str]] = None model_name: typing.Optional[str] = None pipeline_tag: typing.Optional[str] = None tags: typing.Optional[typing.List[str]] = None ignore_metadata_errors: bool = False **kwargs )
参数
- base_model (
str
或List[str]
, 可选) — 派生模型的基模型标识符。例如,如果您的模型是现有模型的微调或适配器,则适用。该值必须是 Hub 上模型的 ID(如果您的模型派生自多个模型,则为 ID 列表)。默认为 None。 - datasets (
Union[str, List[str]]
, 可选) — 用于训练此模型的数据集或数据集列表。应为 https://huggingface.co/datasets 上找到的数据集 ID。默认为 None。 - eval_results (
Union[List[EvalResult], EvalResult]
, 可选) — 定义模型评估结果的 `huggingface_hub.EvalResult` 列表。如果提供,`model_name` 将用作 PapersWithCode 排行榜上的名称。默认为 `None`。 - language (
Union[str, List[str]]
, 可选) — 模型训练数据或元数据的语言。必须是 ISO 639-1、639-2 或 639-3 代码(两个/三个字母),或“code”、“multilingual”等特殊值。默认为 `None`。 - library_name (
str
, 可选) — 模型使用的库名称。示例:keras 或 https://github.com/huggingface/huggingface.js/blob/main/packages/tasks/src/model-libraries.ts 中的任何库。默认为 None。 - license (
str
, 可选) — 此模型的许可证。示例:apache-2.0 或 https://huggingface.co/docs/hub/repositories-licenses 中的任何许可证。默认为 None。 - license_name (
str
, 可选) — 此模型的许可证名称。默认为 None。与 `license_link` 结合使用。常见许可证(Apache-2.0、MIT、CC-BY-SA-4.0)不需要名称。在这种情况下,请使用 `license`。 - license_link (
str
, 可选) — 此模型的许可证链接。默认为 None。与 `license_name` 结合使用。常见许可证(Apache-2.0、MIT、CC-BY-SA-4.0)不需要链接。在这种情况下,请使用 `license`。 - metrics (
List[str]
, 可选) — 用于评估此模型的指标列表。应为可在 https://huggingface.co/metrics 找到的指标名称。示例:“accuracy”。默认为 None。 - model_name (
str
, 可选) — 此模型的名称。它与 `eval_results` 一起用于在卡片元数据中构建 `model-index`。您在此处提供的名称将用于 PapersWithCode 的排行榜。如果未提供,则默认使用仓库名称。默认为 None。 - pipeline_tag (
str
, 可选) — 与模型关联的管道标签。示例:“text-classification”。 - tags (
List[str]
, 可选) — 用于在 Hugging Face Hub 上筛选时添加到模型的标签列表。默认为 None。 - ignore_metadata_errors (
str
) — 如果为 True,解析元数据部分时发生的错误将被忽略。在此过程中可能会丢失一些信息。使用风险自负。 - kwargs (
dict
, 可选) — 将添加到模型卡中的其他元数据。默认为 None。
Hugging Face Hub 在您的 README.md 顶部包含时使用的模型卡元数据
示例
>>> from huggingface_hub import ModelCardData
>>> card_data = ModelCardData(
... language="en",
... license="mit",
... library_name="timm",
... tags=['image-classification', 'resnet'],
... )
>>> card_data.to_dict()
{'language': 'en', 'license': 'mit', 'library_name': 'timm', 'tags': ['image-classification', 'resnet']}
数据集卡片
数据集卡片在机器学习社区中也称为数据卡片。
DatasetCard
from_template
< source >( card_data: DatasetCardData template_path: typing.Optional[str] = None template_str: typing.Optional[str] = None **template_kwargs ) → huggingface_hub.DatasetCard
从模板初始化 DatasetCard。默认情况下,它使用默认模板,该模板可在此处找到:https://github.com/huggingface/huggingface_hub/blob/main/src/huggingface_hub/templates/datasetcard_template.md
模板是 Jinja2 模板,可以通过传递关键字参数进行自定义。
示例
>>> from huggingface_hub import DatasetCard, DatasetCardData
>>> # Using the Default Template
>>> card_data = DatasetCardData(
... language='en',
... license='mit',
... annotations_creators='crowdsourced',
... task_categories=['text-classification'],
... task_ids=['sentiment-classification', 'text-scoring'],
... multilinguality='monolingual',
... pretty_name='My Text Classification Dataset',
... )
>>> card = DatasetCard.from_template(
... card_data,
... pretty_name=card_data.pretty_name,
... )
>>> # Using a Custom Template
>>> card_data = DatasetCardData(
... language='en',
... license='mit',
... )
>>> card = DatasetCard.from_template(
... card_data=card_data,
... template_path='./src/huggingface_hub/templates/datasetcard_template.md',
... custom_template_var='custom value', # will be replaced in template if it exists
... )
DatasetCardData
class huggingface_hub.DatasetCardData
< source >( language: typing.Union[typing.List[str], str, NoneType] = None license: typing.Union[typing.List[str], str, NoneType] = None annotations_creators: typing.Union[typing.List[str], str, NoneType] = None language_creators: typing.Union[typing.List[str], str, NoneType] = None multilinguality: typing.Union[typing.List[str], str, NoneType] = None size_categories: typing.Union[typing.List[str], str, NoneType] = None source_datasets: typing.Optional[typing.List[str]] = None task_categories: typing.Union[typing.List[str], str, NoneType] = None task_ids: typing.Union[typing.List[str], str, NoneType] = None paperswithcode_id: typing.Optional[str] = None pretty_name: typing.Optional[str] = None train_eval_index: typing.Optional[typing.Dict] = None config_names: typing.Union[typing.List[str], str, NoneType] = None ignore_metadata_errors: bool = False **kwargs )
参数
- language (
List[str]
, 可选) — 数据集的语言数据或元数据。必须是 ISO 639-1、639-2 或 639-3 代码(两个/三个字母),或“code”、“multilingual”等特殊值。 - license (
Union[str, List[str]]
, 可选) — 此数据集的许可证。示例:apache-2.0 或 https://huggingface.co/docs/hub/repositories-licenses 中的任何许可证。 - annotations_creators (
Union[str, List[str]]
, 可选) — 数据集的注释创建方式。选项包括:“found”、“crowdsourced”、“expert-generated”、“machine-generated”、“no-annotation”、“other”。 - language_creators (
Union[str, List[str]]
, 可选) — 数据集中基于文本的数据的创建方式。选项包括:“found”、“crowdsourced”、“expert-generated”、“machine-generated”、“other” - multilinguality (
Union[str, List[str]]
, 可选) — 数据集是否为多语言。选项包括:“monolingual”、“multilingual”、“translation”、“other”。 - size_categories (
Union[str, List[str]]
, 可选) — 数据集中的示例数量。选项包括:“n<1K”、“1K1T”和“other”。 - source_datasets (
List[str]]
, 可选) — 指示数据集是原始数据集还是从现有数据集扩展而来。选项包括:“original”和“extended”。 - task_categories (
Union[str, List[str]]
, 可选) — 数据集支持的任务类别? - task_ids (
Union[str, List[str]]
, 可选) — 数据集支持的特定任务? - paperswithcode_id (
str
, 可选) — PapersWithCode 上数据集的 ID。 - pretty_name (
str
, 可选) — 数据集更易于阅读的名称。(例如:“猫狗大战”) - train_eval_index (
Dict
, 可选) — 描述在 Hub 上进行评估所需规范的字典。如果未提供,将从 kwargs 的“train-eval-index”键中收集。 - config_names (
Union[str, List[str]]
, 可选) — 数据集的可用数据集配置列表。
Hugging Face Hub 在您的 README.md 顶部包含时使用的数据集卡片元数据
空间卡片
SpaceCard
SpaceCardData
class huggingface_hub.SpaceCardData
< source >( title: typing.Optional[str] = None sdk: typing.Optional[str] = None sdk_version: typing.Optional[str] = None python_version: typing.Optional[str] = None app_file: typing.Optional[str] = None app_port: typing.Optional[int] = None license: typing.Optional[str] = None duplicated_from: typing.Optional[str] = None models: typing.Optional[typing.List[str]] = None datasets: typing.Optional[typing.List[str]] = None tags: typing.Optional[typing.List[str]] = None ignore_metadata_errors: bool = False **kwargs )
参数
- title (
str
, 可选) — Space 的标题。 - sdk (
str
, 可选) — Space 的 SDK(`gradio`、`streamlit`、`docker` 或 `static` 之一)。 - sdk_version (
str
, 可选) — 所使用 SDK 的版本(如果是 Gradio/Streamlit SDK)。 - python_version (
str
, 可选) — Space 中使用的 Python 版本(如果为 Gradio/Streamlit sdk)。 - app_file (
str
, 可选) — 主应用程序文件的路径(包含 gradio 或 streamlit Python 代码,或静态 html 代码)。路径是相对于仓库根目录的。 - app_port (
str
, 可选) — 应用程序运行的端口。仅当 sdk 为docker
时使用。 - license (
str
, 可选) — 此模型的许可证。示例:apache-2.0 或 https://huggingface.co/docs/hub/repositories-licenses 中的任何许可证。 - duplicated_from (
str
, 可选) — 如果此 Space 是复制的 Space,则为原始 Space 的 ID。 - models (List
str
, 可选) — 与此 Space 相关的模型列表。应为 https://huggingface.co/models 上找到的数据集 ID。 - datasets (
List[str]
, 可选) — 与此 Space 相关的列表。应为 https://huggingface.co/datasets 上找到的数据集 ID。 - tags (
List[str]
, 可选) — 要添加到 Space 的标签列表,可在 Hub 上过滤时使用。 - ignore_metadata_errors (
str
) — 如果为 True,则在解析元数据部分时将忽略错误。在此过程中可能会丢失某些信息。请自行承担风险。 - kwargs (
dict
, 可选) — 将添加到 Space 卡片中的其他元数据。
Hugging Face Hub 在 README.md 文件顶部包含的 Space Card 元数据
有关 Spaces 配置的详尽参考,请访问 https://huggingface.co/docs/hub/spaces-config-reference#spaces-configuration-reference。
示例
>>> from huggingface_hub import SpaceCardData
>>> card_data = SpaceCardData(
... title="Dreambooth Training",
... license="mit",
... sdk="gradio",
... duplicated_from="multimodalart/dreambooth-training"
... )
>>> card_data.to_dict()
{'title': 'Dreambooth Training', 'sdk': 'gradio', 'license': 'mit', 'duplicated_from': 'multimodalart/dreambooth-training'}
实用工具
EvalResult
class huggingface_hub.EvalResult
< source >( task_type: str dataset_type: str dataset_name: str metric_type: str metric_value: typing.Any task_name: typing.Optional[str] = None dataset_config: typing.Optional[str] = None dataset_split: typing.Optional[str] = None dataset_revision: typing.Optional[str] = None dataset_args: typing.Optional[typing.Dict[str, typing.Any]] = None metric_name: typing.Optional[str] = None metric_config: typing.Optional[str] = None metric_args: typing.Optional[typing.Dict[str, typing.Any]] = None verified: typing.Optional[bool] = None verify_token: typing.Optional[str] = None source_name: typing.Optional[str] = None source_url: typing.Optional[str] = None )
参数
- task_type (
str
) — 任务标识符。示例:“image-classification”。 - dataset_type (
str
) — 数据集标识符。示例:“common_voice”。使用 https://huggingface.co/datasets 中的数据集 ID。 - dataset_name (
str
) — 数据集的漂亮名称。示例:“Common Voice (French)”。 - metric_type (
str
) — 评估指标标识符。示例:“wer”。使用 https://huggingface.co/metrics 中的评估指标 ID。 - metric_value (
Any
) — 评估指标值。示例:0.9 或 “20.0 ± 1.2”。 - task_name (
str
, 可选) — 任务的漂亮名称。示例:“语音识别”。 - dataset_config (
str
, 可选) —load_dataset()
中使用的数据集配置名称。示例:在load_dataset("common_voice", "fr")
中使用 fr。有关更多信息,请参阅datasets
文档:https://huggingface.co/docs/datasets/package_reference/loading_methods#datasets.load_dataset.name - dataset_split (
str
, 可选) —load_dataset()
中使用的拆分。示例:“test”。 - dataset_revision (
str
, 可选) —load_dataset()
中使用的数据集修订版本(即 Git Sha)。示例:5503434ddd753f426f4b38109466949a1217c2bb - dataset_args (
Dict[str, Any]
, 可选) — 在Metric.compute()
期间传递的参数。bleu 的示例:{"max_order": 4}
- metric_name (
str
, 可选) — 评估指标的漂亮名称。示例:“Test WER”。 - metric_config (
str
, 可选) —load_metric()
中使用的评估指标配置名称。示例:在load_metric("bleurt", "bleurt-large-512")
中使用 bleurt-large-512。有关更多信息,请参阅datasets
文档:https://huggingface.co/docs/datasets/v2.1.0/en/loading#load-configurations - metric_args (
Dict[str, Any]
, 可选) — 在Metric.compute()
期间传递的参数。bleu 的示例:max_order: 4 - verified (
bool
, 可选) — 指示评估指标是否来自 Hugging Face 的评估服务。由 Hugging Face 自动计算,请勿设置。 - verify_token (
str
, 可选) — 用于验证评估指标是否来自 Hugging Face 的评估服务的 JSON Web Token。 - source_name (
str
, 可选) — 评估结果来源的名称。示例:“Open LLM 排行榜”。 - source_url (
str
, 可选) — 评估结果来源的 URL。示例:”https://huggingface.co/spaces/open-llm-leaderboard/open_llm_leaderboard”。
模型卡中模型索引中找到的单个评估结果的扁平化表示。
有关模型索引规范的更多信息,请参阅 https://github.com/huggingface/hub-docs/blob/main/modelcard.md?plain=1。
如果 self
和 other
描述的是完全相同的评估指标,但值不同,则返回 True。
model_index_to_eval_results
huggingface_hub.repocard_data.model_index_to_eval_results
< source >( model_index: typing.List[typing.Dict[str, typing.Any]] ) → model_name (str
)
接受模型索引并返回模型名称和 huggingface_hub.EvalResult
对象列表。
有关模型索引的详细规范,请参阅此处:https://github.com/huggingface/hub-docs/blob/main/modelcard.md?plain=1
示例
>>> from huggingface_hub.repocard_data import model_index_to_eval_results
>>> # Define a minimal model index
>>> model_index = [
... {
... "name": "my-cool-model",
... "results": [
... {
... "task": {
... "type": "image-classification"
... },
... "dataset": {
... "type": "beans",
... "name": "Beans"
... },
... "metrics": [
... {
... "type": "accuracy",
... "value": 0.9
... }
... ]
... }
... ]
... }
... ]
>>> model_name, eval_results = model_index_to_eval_results(model_index)
>>> model_name
'my-cool-model'
>>> eval_results[0].task_type
'image-classification'
>>> eval_results[0].metric_type
'accuracy'
eval_results_to_model_index
huggingface_hub.repocard_data.eval_results_to_model_index
< source >( model_name: str eval_results: typing.List[huggingface_hub.repocard_data.EvalResult] ) → model_index (List[Dict[str, Any]]
)
接受给定的模型名称和 huggingface_hub.EvalResult
列表,并返回一个与 Hugging Face Hub 期望的格式兼容的有效模型索引。
示例
>>> from huggingface_hub.repocard_data import eval_results_to_model_index, EvalResult
>>> # Define minimal eval_results
>>> eval_results = [
... EvalResult(
... task_type="image-classification", # Required
... dataset_type="beans", # Required
... dataset_name="Beans", # Required
... metric_type="accuracy", # Required
... metric_value=0.9, # Required
... )
... ]
>>> eval_results_to_model_index("my-cool-model", eval_results)
[{'name': 'my-cool-model', 'results': [{'task': {'type': 'image-classification'}, 'dataset': {'name': 'Beans', 'type': 'beans'}, 'metrics': [{'type': 'accuracy', 'value': 0.9}]}]}]
metadata_eval_result
huggingface_hub.metadata_eval_result
< source >( model_pretty_name: str task_pretty_name: str task_id: str metrics_pretty_name: str metrics_id: str metrics_value: typing.Any dataset_pretty_name: str dataset_id: str metrics_config: typing.Optional[str] = None metrics_verified: bool = False dataset_config: typing.Optional[str] = None dataset_split: typing.Optional[str] = None dataset_revision: typing.Optional[str] = None metrics_verification_token: typing.Optional[str] = None ) → dict
参数
- model_pretty_name (
str
) — 模型名称,采用自然语言表示。 - task_pretty_name (
str
) — 任务名称,采用自然语言表示。 - task_id (
str
) — 示例:automatic-speech-recognition。任务 ID。 - metrics_pretty_name (
str
) — 指标名称,采用自然语言表示。示例:测试 WER。 - metrics_id (
str
) — 示例:wer。来自 https://huggingface.co/metrics 的评估指标 ID。 - metrics_value (
Any
) — 指标的值。示例:20.0 或 “20.0 ± 1.2”。 - dataset_pretty_name (
str
) — 数据集名称,采用自然语言表示。 - dataset_id (
str
) — 示例:common_voice。来自 https://huggingface.co/datasets 的数据集 ID。 - metrics_config (
str
, 可选) —load_metric()
中使用的评估指标配置名称。示例:在load_metric("bleurt", "bleurt-large-512")
中使用 bleurt-large-512。 - metrics_verified (
bool
, 可选,默认为False
) — 指示评估指标是否来自 Hugging Face 的评估服务。由 Hugging Face 自动计算,请勿设置。 - dataset_config (
str
, 可选) — 示例:fr。load_dataset()
中使用的数据集配置名称。 - dataset_split (
str
, 可选) — 示例:test。load_dataset()
中使用的数据集拆分名称。 - dataset_revision (
str
, 可选) — 示例:5503434ddd753f426f4b38109466949a1217c2bb。load_dataset()
中使用的数据集修订版本。 - metrics_verification_token (
bool
, 可选) — 用于验证评估指标是否来自 Hugging Face 的评估服务的 JSON Web Token。
返回
字典
一个元数据字典,包含模型在数据集上评估的结果。
创建包含模型在数据集上评估结果的元数据字典。
示例
>>> from huggingface_hub import metadata_eval_result
>>> results = metadata_eval_result(
... model_pretty_name="RoBERTa fine-tuned on ReactionGIF",
... task_pretty_name="Text Classification",
... task_id="text-classification",
... metrics_pretty_name="Accuracy",
... metrics_id="accuracy",
... metrics_value=0.2662102282047272,
... dataset_pretty_name="ReactionJPEG",
... dataset_id="julien-c/reactionjpeg",
... dataset_config="default",
... dataset_split="test",
... )
>>> results == {
... 'model-index': [
... {
... 'name': 'RoBERTa fine-tuned on ReactionGIF',
... 'results': [
... {
... 'task': {
... 'type': 'text-classification',
... 'name': 'Text Classification'
... },
... 'dataset': {
... 'name': 'ReactionJPEG',
... 'type': 'julien-c/reactionjpeg',
... 'config': 'default',
... 'split': 'test'
... },
... 'metrics': [
... {
... 'type': 'accuracy',
... 'value': 0.2662102282047272,
... 'name': 'Accuracy',
... 'verified': False
... }
... ]
... }
... ]
... }
... ]
... }
True
metadata_update
huggingface_hub.metadata_update
< source >( repo_id: str metadata: typing.Dict repo_type: typing.Optional[str] = None overwrite: bool = False token: typing.Optional[str] = None commit_message: typing.Optional[str] = None commit_description: typing.Optional[str] = None revision: typing.Optional[str] = None create_pr: bool = False parent_commit: typing.Optional[str] = None ) → str
参数
- repo_id (
str
) — 仓库的名称。 - metadata (
dict
) — 包含要更新的元数据字典。 - repo_type (
str
, 可选) — 如果更新到数据集或空间,则设置为"dataset"
或"space"
;如果更新到模型,则设置为None
或"model"
。默认为None
。 - overwrite (
bool
, 可选, 默认为False
) — 如果设置为True
,则可以覆盖现有字段,否则尝试覆盖现有字段将导致错误。 - token (
str
, 可选) — Hugging Face 认证令牌。 - commit_message (
str
, 可选) — 生成提交的摘要/标题/第一行。默认为f"Update metadata with huggingface_hub"
- commit_description (
str
可选) — 生成提交的描述。 - revision (
str
, 可选) — 提交的 Git 修订版本。默认为"main"
分支的 HEAD。 - create_pr (
boolean
, 可选) — 是否从revision
创建带此提交的拉取请求。默认为False
。 - parent_commit (
str
, 可选) — 父提交的 OID / SHA,以十六进制字符串表示。也支持简写(前 7 个字符)。如果指定且create_pr
为False
,则如果revision
不指向parent_commit
,则提交将失败。如果指定且create_pr
为True
,则将从parent_commit
创建拉取请求。指定parent_commit
可确保仓库在提交更改之前没有更改,在仓库并发更新/提交时特别有用。
返回
字符串
更新卡片元数据的提交 URL。
更新 Hugging Face Hub 上仓库的 README.md 中的元数据。如果 README.md 文件尚不存在,则会创建一个新文件,其中包含元数据和默认的 ModelCard 或 DatasetCard 模板。对于 `space` 仓库,如果 Space 不存在 `README.md` 文件,则会抛出错误。
示例
>>> from huggingface_hub import metadata_update
>>> metadata = {'model-index': [{'name': 'RoBERTa fine-tuned on ReactionGIF',
... 'results': [{'dataset': {'name': 'ReactionGIF',
... 'type': 'julien-c/reactiongif'},
... 'metrics': [{'name': 'Recall',
... 'type': 'recall',
... 'value': 0.7762102282047272}],
... 'task': {'name': 'Text Classification',
... 'type': 'text-classification'}}]}]}
>>> url = metadata_update("hf-internal-testing/reactiongif-roberta-card", metadata)