Hub Python 庫文件

倉庫卡片

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

倉庫卡片

huggingface_hub 庫提供了一個 Python 介面來建立、共享和更新模型/資料集卡片。訪問 專門的文件頁面,更深入地瞭解 Hub 上的模型卡片是什麼以及它們在底層是如何工作的。你也可以檢視我們的 模型卡片指南,以瞭解如何在自己的專案中如何使用這些實用工具。

倉庫卡片

RepoCard 物件是 ModelCardDatasetCardSpaceCard 的父類。

class huggingface_hub.RepoCard

< >

( content: str ignore_metadata_errors: bool = False )

__init__

< >

( content: str ignore_metadata_errors: bool = False )

引數

  • content (str) — Markdown 檔案的內容。

從字串內容初始化 RepoCard。內容應為帶有開頭 YAML 塊和 Markdown 主體的 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'
> [!TIP] > 引發以下錯誤: > > - [`ValueError`](https://docs.python.club.tw/3/library/exceptions.html#ValueError) > 當儲存庫卡片元資料的內容不是字典時。

from_template

< >

( card_data: CardData template_path: typing.Optional[str] = None template_str: typing.Optional[str] = None **template_kwargs ) huggingface_hub.repocard.RepoCard

引數

  • card_data (huggingface_hub.CardData) — 一個 huggingface_hub.CardData 例項,包含您想包含在 Hugging Face Hub 上的倉庫卡片的 YAML 頭部中的元資料。
  • template_path (str, 可選) — 指向一個 markdown 檔案,其中包含可選的 Jinja 模板變數,可以透過 template_kwargs 填充。預設為預設模板。

返回

huggingface_hub.repocard.RepoCard

使用指定的卡片資料和模板內容初始化的 RepoCard 例項。

從模板初始化 RepoCard。預設情況下,它使用預設模板。

模板是 Jinja2 模板,可以透過傳遞關鍵字引數進行自定義。

load

< >

( repo_id_or_path: typing.Union[str, pathlib.Path] repo_type: typing.Optional[str] = None token: typing.Optional[str] = None ignore_metadata_errors: bool = False ) huggingface_hub.repocard.RepoCard

引數

  • repo_id_or_path (Union[str, Path]) — Hugging Face Hub 倉庫相關的倉庫 ID 或本地檔案路徑。
  • repo_type (str, 可選) — 推送到 Hugging Face Hub 的 Hugging Face 倉庫的型別。預設為 None,表示“model”。其他選項是“dataset”和“space”。從本地檔案路徑載入時未使用。如果這是從子類呼叫,則預設值將是子類的 repo_type
  • token (str, 可選) — 認證令牌,透過 huggingface_hub.HfApi.login 方法獲得。將預設為儲存的令牌。
  • ignore_metadata_errors (str) — 如果為 True,將忽略解析元資料部分時的錯誤。某些資訊在處理過程中可能會丟失。請自行承擔風險使用。

返回

huggingface_hub.repocard.RepoCard

從倉庫的 README.md 檔案或檔案路徑初始化的 RepoCard(或子類)。

從 Hugging Face Hub 倉庫的 README.md 或本地檔案路徑初始化 RepoCard。

示例

>>> from huggingface_hub.repocard import RepoCard
>>> card = RepoCard.load("nateraw/food")
>>> assert card.data.tags == ["generated_from_trainer", "image-classification", "pytorch"]

push_to_hub

< >

( repo_id: str token: typing.Optional[str] = None repo_type: typing.Optional[str] = None commit_message: typing.Optional[str] = None commit_description: typing.Optional[str] = None revision: typing.Optional[str] = None create_pr: typing.Optional[bool] = None parent_commit: typing.Optional[str] = None ) str

引數

  • repo_id (str) — 推送到 Hugging Face Hub 的 Hugging Face 倉庫的倉庫 ID。示例:“nateraw/food”。
  • token (str, 可選) — 認證令牌,透過 huggingface_hub.HfApi.login 方法獲得。將預設為儲存的令牌。
  • repo_type (str, 可選, 預設值為“model”) — 推送到 Hugging Face 倉庫的 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 個字元)。如果指定了 parent_commitcreate_prFalse,則如果 revision 未指向 parent_commit,提交將失敗。如果指定了 parent_commitcreate_prTrue,則拉取請求將從 parent_commit 建立。指定 parent_commit 可確保在提交更改之前倉庫沒有更改,並且在倉庫併發更新/提交時特別有用。

返回

字串

更新卡片元資料的提交的 URL。

將 RepoCard 推送到 Hugging Face Hub 倉庫。

save

< >

( filepath: typing.Union[pathlib.Path, str] )

引數

  • filepath (Union[Path, str]) — 要儲存的 markdown 檔案的檔案路徑。

將 RepoCard 儲存到檔案。

示例

>>> from huggingface_hub.repocard import RepoCard
>>> card = RepoCard("---\nlanguage: en\n---\n# This is a test repo card")
>>> card.save("/tmp/test.md")

validate

< >

( repo_type: typing.Optional[str] = None )

引數

  • repo_type (str, 可選, 預設值為“model”) — 推送到 Hugging Face 倉庫的 Hugging Face 倉庫型別。選項為“model”、“dataset”和“space”。如果此函式由子類呼叫,則預設值將是子類的 repo_type

針對 Hugging Face Hub 的卡片驗證邏輯驗證卡片。使用此函式需要網際網路訪問,因此它僅在內部由 huggingface_hub.repocard.RepoCard.push_to_hub() 呼叫。

引發以下錯誤

  • ValueError 當卡片未能透過驗證檢查時。
  • HTTPError 當 Hub API 請求因任何其他原因失敗時。

卡片資料

CardData 物件是 ModelCardDataDatasetCardData 的父類。

class huggingface_hub.CardData

< >

( ignore_metadata_errors: bool = False **kwargs )

包含來自 RepoCard 的元資料的結構。

CardDataModelCardDataDatasetCardData 的父類。

元資料可以匯出為字典或 YAML。匯出可以自定義以更改資料的表示(例如:展平評估結果)。CardData 的行為類似於字典(可以獲取、彈出、設定值),但它不繼承自 dict,以允許進行此匯出步驟。

get

< >

( key: str default: typing.Any = None )

獲取給定元資料鍵的值。

pop

< >

( key: str default: typing.Any = None )

彈出給定元資料鍵的值。

to_dict

< >

( ) dict

返回

字典

CardData 表示為字典,可用於轉儲到 YAML 塊以包含在 README.md 檔案中。

將 CardData 轉換為 dict。

to_yaml

< >

( line_break = None original_order: typing.Optional[list[str]] = None ) str

引數

  • line_break (str, 可選) — 轉儲到 yaml 時使用的換行符。

返回

字串

CardData 表示為 YAML 塊。

將 CardData 轉儲為 YAML 塊,以便包含在 README.md 檔案中。

模型卡

ModelCard

class huggingface_hub.ModelCard

< >

( content: str ignore_metadata_errors: bool = False )

from_template

< >

( card_data: ModelCardData template_path: typing.Optional[str] = None template_str: typing.Optional[str] = None **template_kwargs ) huggingface_hub.ModelCard

引數

  • card_data (huggingface_hub.ModelCardData) — 一個 huggingface_hub.ModelCardData 例項,包含您想包含在 Hugging Face Hub 上的模型卡片的 YAML 頭部中的元資料。
  • template_path (str, 可選) — 指向一個 markdown 檔案,其中包含可選的 Jinja 模板變數,可以透過 template_kwargs 填充。預設為預設模板。

返回

huggingface_hub.ModelCard

使用指定的卡片資料和模板內容初始化的 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

< >

( base_model: typing.Union[list[str], str, NoneType] = None datasets: typing.Union[list[str], str, NoneType] = None eval_results: typing.Optional[list[huggingface_hub.repocard_data.EvalResult]] = None language: typing.Union[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[list[str]] = None model_name: typing.Optional[str] = None pipeline_tag: typing.Optional[str] = None tags: typing.Optional[list[str]] = None ignore_metadata_errors: bool = False **kwargs )

引數

  • base_model (strlist[str], 可選) — 該模型派生的基礎模型的識別符號。例如,如果您的模型是現有模型的微調或介面卡,則適用。該值必須是 Hub 上的模型 ID(如果您的模型派生自多個模型,則為 ID 列表)。預設為 None。
  • datasets (Union[str, list[str]], 可選) — 用於訓練此模型的(或列表)資料集。應該是 Hub 上的資料集 ID (https://huggingface.co/datasets)。預設為 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, optional) — 使用此模型的庫的名稱。例如:keras 或來自 https://github.com/huggingface/huggingface.js/blob/main/packages/tasks/src/model-libraries.ts 的任何庫。預設為 None。
  • license (str, optional) — 此模型的許可證。例如:apache-2.0 或來自 https://huggingface.co/docs/hub/repositories-licenses 的任何許可證。預設為 None。
  • license_name (str, optional) — 此模型許可證的名稱。預設為 None。與 license_link 結合使用。常見許可證(Apache-2.0、MIT、CC-BY-SA-4.0)不需要名稱。在這種情況下,請改用 license
  • license_link (str, optional) — 此模型許可證的連結。預設為 None。與 license_name 結合使用。常見許可證(Apache-2.0、MIT、CC-BY-SA-4.0)不需要連結。在這種情況下,請改用 license
  • metrics (list[str], optional) — 用於評估此模型的指標列表。應為可以在 https://huggingface.co/metrics 找到的指標名稱。例如:“accuracy”。預設為 None。
  • model_name (str, optional) — 此模型的名稱。它與 eval_results 一起用於構建卡片元資料中的 model-index。您在此處提供的名稱將用於 PapersWithCode 的排行榜。如果未提供 None,則預設為倉庫名稱。預設為 None。
  • pipeline_tag (str, optional) — 與模型關聯的 pipeline 標籤。示例:“text-classification”。
  • tags (list[str], optional) — 要新增到您的模型的標籤列表,可在 Hugging Face Hub 上進行篩選時使用。預設為 None。
  • ignore_metadata_errors (str) — 如果為 True,則會忽略解析元資料部分時出現的錯誤。在此過程中可能會丟失一些資訊。請自行承擔風險使用。
  • kwargs (dict, optional) — 將新增到模型卡的其他元資料。預設為 None。

模型卡元資料,當包含在 README.md 的頂部時,Hugging Face Hub 會使用這些元資料

示例

>>> 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

class huggingface_hub.DatasetCard

< >

( content: str ignore_metadata_errors: bool = False )

from_template

< >

( card_data: DatasetCardData template_path: typing.Optional[str] = None template_str: typing.Optional[str] = None **template_kwargs ) huggingface_hub.DatasetCard

引數

  • card_data (huggingface_hub.DatasetCardData) — 一個 huggingface_hub.DatasetCardData 例項,包含您想包含在 Hugging Face Hub 的資料集卡片的 YAML 頭部中的元資料。
  • template_path (str, optional) — 指向一個 markdown 檔案,其中包含可選的 Jinja 模板變數,可以使用 template_kwargs 進行填充。預設為預設模板。

返回

huggingface_hub.DatasetCard

一個 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

< >

( language: typing.Union[list[str], str, NoneType] = None license: typing.Union[list[str], str, NoneType] = None annotations_creators: typing.Union[list[str], str, NoneType] = None language_creators: typing.Union[list[str], str, NoneType] = None multilinguality: typing.Union[list[str], str, NoneType] = None size_categories: typing.Union[list[str], str, NoneType] = None source_datasets: typing.Optional[list[str]] = None task_categories: typing.Union[list[str], str, NoneType] = None task_ids: typing.Union[list[str], str, NoneType] = None paperswithcode_id: typing.Optional[str] = None pretty_name: typing.Optional[str] = None train_eval_index: typing.Optional[dict] = None config_names: typing.Union[list[str], str, NoneType] = None ignore_metadata_errors: bool = False **kwargs )

引數

  • language (list[str], optional) — 資料集資料的或元資料的語言。它必須是 ISO 639-1、639-2 或 639-3 程式碼(兩個/三個字母),或特殊值,如“code”、“multilingual”。
  • license (Union[str, list[str]], optional) — 此資料集的許可證。示例:apache-2.0 或來自 https://huggingface.co/docs/hub/repositories-licenses 的任何許可證。
  • annotations_creators (Union[str, list[str]], optional) — 資料集的註釋是如何建立的。選項包括:“found”、“crowdsourced”、“expert-generated”、“machine-generated”、“no-annotation”、“other”。
  • language_creators (Union[str, list[str]], optional) — 資料集中的文字資料是如何建立的。選項包括:“found”、“crowdsourced”、“expert-generated”、“machine-generated”、“other”。
  • multilinguality (Union[str, list[str]], optional) — 該資料集是否是多語言的。選項包括:“monolingual”、“multilingual”、“translation”、“other”。
  • size_categories (Union[str, list[str]], optional) — 資料集中的示例數量。選項包括:“n<1K”、“1K<n<10k”、“10k<n<100k”、“100k<n<1m”、“1m<n<10m”、“10m<n<100m”、“100m<n<1b”、“1b<n<10b”、“10b<n<100b”、“100b<n<1t”、“n="">1T”,以及“other”。
  • source_datasets (list[str]], optional) — 指示資料集是原始資料集還是從另一個現有資料集擴充套件而來。選項包括:“original”和“extended”。
  • task_categories (Union[str, list[str]], optional) — 資料集支援哪些任務類別?
  • task_ids (Union[str, list[str]], optional) — 資料集支援哪些具體任務?
  • paperswithcode_id (str, optional) — 資料集在 PapersWithCode 上的 ID。
  • pretty_name (str, optional) — 資料集的一個更易讀的名稱。(例如:“Cats vs. Dogs”)
  • train_eval_index (dict, optional) — 一個描述在 Hub 上進行評估所需的規範的字典。如果未提供,它將從 kwargs 的 'train-eval-index' 鍵中收集。
  • config_names (Union[str, list[str]], optional) — 資料集中可用的資料集配置列表。

資料集卡片元資料,當包含在 README.md 的頂部時,Hugging Face Hub 會使用這些元資料

空間卡片

SpaceCard

class huggingface_hub.SpaceCard

< >

( content: str ignore_metadata_errors: bool = False )

SpaceCardData

class huggingface_hub.SpaceCardData

< >

( 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[list[str]] = None datasets: typing.Optional[list[str]] = None tags: typing.Optional[list[str]] = None ignore_metadata_errors: bool = False **kwargs )

引數

  • title (str, optional) — Space 的標題。
  • sdk (str, optional) — Space 的 SDK(gradiostreamlitdockerstatic 之一)。
  • sdk_version (str, optional) — 所使用的 SDK 版本(如果為 Gradio/Streamlit SDK)。
  • python_version (str, optional) — Space 中使用的 Python 版本(如果為 Gradio/Streamlit SDK)。
  • app_file (str, optional) — 您的主應用程式檔案的路徑(包含 gradio 或 streamlit Python 程式碼,或靜態 html 程式碼)。路徑相對於儲存庫的根目錄。
  • app_port (str, optional) — 應用程式執行的埠。僅在 sdk 為 docker 時使用。
  • license (str, optional) — 此模型的許可證。示例:apache-2.0 或來自 https://huggingface.co/docs/hub/repositories-licenses 的任何許可證。
  • duplicated_from (str, optional) — 如果這是複製的空間,則為原始空間 ID。
  • models (liststr, optional) — 與此 Space 相關的模型列表。應為在 https://huggingface.co/models 上找到的資料集 ID。
  • datasets (list[str], optional) — 與此 Space 相關的列表。應為在 https://huggingface.co/datasets 上找到的資料集 ID。
  • tags (list[str], optional) — 要新增到您的 Space 的標籤列表,可在 Hub 上進行篩選時使用。
  • ignore_metadata_errors (str) — 如果為 True,則會忽略解析元資料部分時出現的錯誤。在此過程中可能會丟失一些資訊。請自行承擔風險使用。
  • kwargs (dict, optional) — 將新增到 space 卡的其他元資料。

Space 卡片元資料,當包含在 README.md 的頂部時,Hugging Face Hub 會使用這些元資料

要獲取 Space 配置的詳盡參考,請訪問 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

< >

( 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[dict[str, typing.Any]] = None metric_name: typing.Optional[str] = None metric_config: typing.Optional[str] = None metric_args: typing.Optional[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, optional) — 任務的友好名稱。示例:“Speech Recognition”。
  • dataset_config (str, optional) — 在 load_dataset() 中使用的資料集配置名稱。示例:fr,在 load_dataset("common_voice", "fr") 中。有關更多資訊,請參閱 datasets 文件:https://huggingface.co/docs/datasets/package_reference/loading_methods#datasets.load_dataset.name
  • dataset_split (str, optional) — 在 load_dataset() 中使用的拆分。示例:“test”。
  • dataset_revision (str, optional) — 在 load_dataset() 中使用的資料集修訂(也稱為 Git Sha)。示例:5503434ddd753f426f4b38109466949a1217c2bb
  • dataset_args (dict[str, Any], optional) — 在 Metric.compute() 期間傳遞的引數。對於 bleu{"max_order": 4}
  • metric_name (str, optional) — 指標的友好名稱。示例:“Test WER”。
  • metric_config (str, optional) — 在 load_metric() 中使用的指標配置名稱。示例:bleurt-large-512,在 load_metric("bleurt", "bleurt-large-512") 中。有關更多資訊,請參閱 datasets 文件:https://huggingface.co/docs/datasets/v2.1.0/en/loading#load-configurations
  • metric_args (dict[str, Any], optional) — 在 Metric.compute() 期間傳遞的引數。示例:對於 bleu:max_order: 4
  • verified (bool, optional) — 指示指標是否源自 Hugging Face 的 evaluation service。由 Hugging Face 自動計算,請勿設定。
  • verify_token (str, optional) — 用於驗證指標是否源自 Hugging Face evaluation service 的 JSON Web Token。
  • source_name (str, optional) — 評估結果的來源名稱。示例:“Open LLM Leaderboard”。
  • source_url (str, optional) — 評估結果的來源 URL。示例:“https://huggingface.co/spaces/open-llm-leaderboard/open_llm_leaderboard”

模型卡中找到的各個評估結果的扁平化表示。

有關 model-index 規範的更多資訊,請參閱 https://github.com/huggingface/hub-docs/blob/main/modelcard.md?plain=1

is_equal_except_value

< >

( other: EvalResult )

如果 selfother 描述相同的指標但具有不同的值,則返回 True。

model_index_to_eval_results

huggingface_hub.repocard_data.model_index_to_eval_results

< >

( model_index: list ) model_name (str)

引數

  • model_index (list[dict[str, Any]]) — 模型索引資料結構,可能來自 Hugging Face Hub 上的 README.md 檔案。

返回

model_name (str)

在模型索引中找到的模型名稱。它用作 PapersWithCode 等排行榜上的模型識別符號。 eval_results (list[EvalResult]):一個 huggingface_hub.EvalResult 物件列表,其中包含提供的 model_index 中報告的指標。

接收一個模型索引並返回模型名稱以及一個 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

< >

( model_name: str eval_results: list ) model_index (list[dict[str, Any]])

引數

  • model_name (str) — 模型的名稱(例如,“my-cool-model”)。它用作 PapersWithCode 等排行榜上的模型識別符號。
  • eval_results (list[EvalResult]) — 要在 model-index 中報告的指標的 huggingface_hub.EvalResult 物件列表。

返回

model_index (list[dict[str, Any]])

將 eval_results 轉換為 model-index。

接收給定的模型名稱和 huggingface_hub.EvalResult 列表,並返回一個與 Hugging Face Hub 所需格式相容的有效 model-index。

示例

>>> 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

< >

( 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) — 指標的自然語言名稱。示例:Test 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, optional) — 在 load_metric() 中使用的指標配置名稱。示例:bleurt-large-512,在 load_metric("bleurt", "bleurt-large-512") 中。
  • metrics_verified (bool, optional, defaults to False) — 指示指標是否源自 Hugging Face 的 evaluation service。由 Hugging Face 自動計算,請勿設定。
  • dataset_config (str, optional) — 示例:fr。在 load_dataset() 中使用的與資料集配置同名的引數。
  • dataset_split (str, optional) — 示例:test。在 load_dataset() 中使用的與資料集拆分同名的引數。
  • dataset_revision (str, optional) — 示例:5503434ddd753f426f4b38109466949a1217c2bb。在 load_dataset() 中使用的資料集修訂名稱。
  • metrics_verification_token (bool, optional) — 用於驗證指標是否源自 Hugging Face evaluation service 的 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

< >

( repo_id: str metadata: 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, optional) — 如果更新到資料集或空間,則設定為 "dataset""space",如果更新到模型,則設定為 None"model"。預設為 None
  • overwrite (bool, optional, defaults to False) — 如果設定為 True,則可以覆蓋現有欄位,否則嘗試覆蓋現有欄位將導致錯誤。
  • token (str, optional) — Hugging Face 認證令牌。
  • commit_message (str, optional) — 生成的提交的摘要/標題/第一行。預設為 f"Update metadata with huggingface_hub"
  • commit_description (str optional) — 生成的提交的描述
  • revision (str, optional) — 要從中提交的 git 修訂版本。預設為 "main" 分支的頭部。
  • create_pr (boolean, optional) — 是否從此提交建立拉取請求。預設為 False
  • parent_commit (str, optional) — 父提交的 OID / SHA,以十六進位制字串形式表示。也支援簡寫(前 7 個字元)。如果指定了 parent_commit 並且 create_prFalse,則如果 revision 未指向 parent_commit,提交將失敗。如果指定了 parent_commit 並且 create_prTrue,則拉取請求將從 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)
在 GitHub 上更新

© . This site is unofficial and not affiliated with Hugging Face, Inc.