Hub Python 函式庫文件

儲存庫卡片

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

儲存庫卡片

huggingface_hub 程式庫提供了一個 Python 介面來建立、共享和更新模型/資料集卡片(Model/Dataset Cards)。請造訪專屬說明文件頁面,以深入了解 Hub 上的模型卡片是什麼,以及它們在底層是如何運作的。您也可以查看我們的模型卡片指南,以了解如何在自己的專案中使用這些公用程式。

儲存庫卡片 (Repo Card)

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: str | None = None template_str: str | None = 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: str | pathlib.Path repo_type: str | None = None token: str | None = None ignore_metadata_errors: bool = False ) huggingface_hub.RepoCard

參數

  • repo_id_or_path (Union[str, Path]) — 與 Hugging Face Hub 儲存庫相關聯的儲存庫 ID 或本機檔案路徑。
  • repo_type (str, 選填) — 要推送到的 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: str | None = None repo_type: str | None = None commit_message: str | None = None commit_description: str | None = None revision: str | None = None create_pr: bool | None = None parent_commit: str | None = 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, 選填) — 是否要建立包含此提交的 Pull Request。預設為 False
  • parent_commit (str, 選填) — 父提交的 OID / SHA,以十六進位字串表示。也支援縮寫(前 7 個字元)。如果指定了此項且 create_prFalse,若 revision 未指向 parent_commit,提交將會失敗。如果指定了此項且 create_prTrue,將會從 parent_commit 建立 Pull Request。指定 parent_commit 可確保在提交變更之前儲存庫未發生變更,如果儲存庫更新/提交太頻繁,這特別有用。

返回

str

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

將 RepoCard 推送到 Hugging Face Hub 儲存庫。

save

< >

( filepath: 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: str | None = None )

參數

  • repo_type (str, 選填, 預設為 “model”) — 要推送到的 Hugging Face 儲存庫類型。選項為 “model”、“dataset” 和 “space”。如果此函數從子類別呼叫,預設值將為子類別的 repo_type

根據 Hugging Face Hub 的卡片驗證邏輯驗證卡片。使用此函數需要網際網路存取,因此僅由 huggingface_hub.repocard.RepoCard.push_to_hub() 在內部呼叫。

引發以下錯誤

  • ValueError 如果卡片未通過驗證檢查。
  • HTTPError 如果對 Hub API 的請求因任何其他原因而失敗。

卡片資料 (Card Data)

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

返回

dict

將 CardData 表示為準備好傾印至 YAML 區塊以包含在 README.md 檔案中的字典。

將 CardData 轉換為字典。

to_yaml

< >

( line_break = None original_order: list[str] | None = None ) str

參數

  • line_break (str, 選填) — 傾印至 yaml 時要使用的換行符號。

返回

str

表示為 YAML 區塊的 CardData。

將 CardData 傾印至 YAML 區塊以包含在 README.md 檔案中。

模型卡

ModelCard

class huggingface_hub.ModelCard

< >

( content: str ignore_metadata_errors: bool = False )

from_template

< >

( card_data: ModelCardData template_path: str | None = None template_str: str | None = 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: str | list[str] | None = None datasets: str | list[str] | None = None eval_results: list[huggingface_hub.repocard_data.EvalResult] | None = None language: str | list[str] | None = None library_name: str | None = None license: str | None = None license_name: str | None = None license_link: str | None = None metrics: list[str] | None = None model_name: str | None = None pipeline_tag: str | None = None tags: list[str] | None = None ignore_metadata_errors: bool = False **kwargs )

參數

  • base_model (strlist[str], 選填) — 模型衍生的基礎模型識別碼。這適用於您的模型是現有模型的微調或轉接器(adapter)的情況。該值必須是 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], optional) — 用於評估此模型的指標列表。應為可以在 https://huggingface.co/metrics 找到的指標名稱。範例:‘accuracy’。預設值為 None。
  • model_name (str, optional) — 此模型的名稱。它與 eval_results 一起使用,以在卡片的元數據中建構 model-index。您在此處提供的名稱將用於 PapersWithCode 的排行榜。如果未提供,則預設使用儲存庫名稱。預設值為 None。
  • pipeline_tag (str, optional) — 與模型相關聯的流水線標籤(pipeline tag)。範例:“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']}

資料集說明卡 (Dataset Cards)

資料集卡片(Dataset cards)在機器學習社群中也稱為資料卡(Data Cards)。

DatasetCard

class huggingface_hub.DatasetCard

< >

( content: str ignore_metadata_errors: bool = False )

from_template

< >

( card_data: DatasetCardData template_path: str | None = None template_str: str | None = None **template_kwargs ) huggingface_hub.DatasetCard

參數

  • card_data (huggingface_hub.DatasetCardData) — 一個 huggingface_hub.DatasetCardData 實例,包含您想要包含在 Hugging Face Hub 上資料集卡片的 YAML 標頭中的元數據。
  • template_path (str, optional) — 指向包含可選 Jinja 模板變數的 Markdown 檔案路徑,這些變數可以使用 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: str | list[str] | None = None license: str | list[str] | None = None annotations_creators: str | list[str] | None = None language_creators: str | list[str] | None = None multilinguality: str | list[str] | None = None size_categories: str | list[str] | None = None source_datasets: list[str] | None = None task_categories: str | list[str] | None = None task_ids: str | list[str] | None = None paperswithcode_id: str | None = None pretty_name: str | None = None train_eval_index: dict | None = None config_names: str | list[str] | None = 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 會使用的資料集卡元數據。

Space 卡片

SpaceCard

class huggingface_hub.SpaceCard

< >

( content: str ignore_metadata_errors: bool = False )

SpaceCardData

class huggingface_hub.SpaceCardData

< >

( title: str | None = None sdk: str | None = None sdk_version: str | None = None python_version: str | None = None app_file: str | None = None app_port: int | None = None license: str | None = None duplicated_from: str | None = None models: list[str] | None = None datasets: list[str] | None = None tags: list[str] | None = None ignore_metadata_errors: bool = False **kwargs )

參數

  • title (str, optional) — Space 的標題。
  • sdk (str, optional) — Space 的 SDK(gradiostreamlitdockerstatic 之一)。
  • sdk_version (str, optional) — 所使用的 SDK 版本(如果 SDK 為 Gradio/Streamlit)。
  • python_version (str, optional) — Space 中使用的 Python 版本(如果 SDK 為 Gradio/Streamlit)。
  • 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) — 如果這是複製的 Space,則為原始 Space 的 ID。
  • models (list[str], 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 卡片的額外元數據。

當包含在您的 README.md 頂部時,Hugging Face Hub 會使用的 Space 卡元數據。

要獲得完整的 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'}

公用程式 (Utilities)

EvalResult

class huggingface_hub.EvalResult

< >

( task_type: str dataset_type: str dataset_name: str metric_type: str metric_value: typing.Any task_name: str | None = None dataset_config: str | None = None dataset_split: str | None = None dataset_revision: str | None = None dataset_args: dict[str, typing.Any] | None = None metric_name: str | None = None metric_config: str | None = None metric_args: dict[str, typing.Any] | None = None verified: bool | None = None verify_token: str | None = None source_name: str | None = None source_url: str | None = 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, 選用) — 任務的易讀名稱。範例:「Speech Recognition」。
  • 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 的 評測服務 (evaluation service)。由 Hugging Face 自動計算,請勿設定。
  • verify_token (str, 選用) — 用於驗證這些指標是否來自 Hugging Face 評測服務的 JSON Web Token。
  • source_name (str, 選用) — 評測結果來源的名稱。範例:「Open LLM Leaderboard」。
  • source_url (str, 選用) — 評測結果來源的 URL。範例:”https://huggingface.co/spaces/open-llm-leaderboard/open_llm_leaderboard”

模型卡 (Model Cards) 中 model-index 所包含之個別評測結果的扁平化表示形式。

關於 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]):包含在提供的 model_index 中報告的指標之 huggingface_hub.EvalResult 物件列表。

接收模型索引,並回傳模型名稱與一組 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]) — huggingface_hub.EvalResult 物件列表,包含要報告於 model-index 中的指標。

返回

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

已轉換為 model-index 的 eval_results。

接收給定的模型名稱與 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: str | None = None metrics_verified: bool = False dataset_config: str | None = None dataset_split: str | None = None dataset_revision: str | None = None metrics_verification_token: str | None = 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, 選用) — 在 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。

返回

dict

一個包含模型在特定資料集上評測結果的詮釋資料字典 (metadata dict)。

建立一個包含模型在特定資料集上評測結果的詮釋資料字典。

範例

>>> 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: str | None = None overwrite: bool = False token: str | None = None commit_message: str | None = None commit_description: str | None = None revision: str | None = None create_pr: bool = False parent_commit: str | None = None ) str

參數

  • repo_id (str) — 儲存庫名稱。
  • metadata (dict) — 包含要更新之詮釋資料的字典。
  • repo_type (str, 選用) — 若要更新至資料集或 space,請設為 "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" 分支的開頭。
  • create_pr (boolean, 選用) — 是否要建立一個從 revision 開始並包含該提交的提取請求 (Pull Request)。預設為 False
  • parent_commit (str, 選用) — 父提交的 OID / SHA,以十六進位字串表示。也支援縮寫(前 7 個字元)。若已指定且 create_prFalse,當 revision 未指向 parent_commit 時,提交將會失敗。若已指定且 create_prTrue,則提取請求將從 parent_commit 建立。指定 parent_commit 可確保在提交變更前儲存庫未被更改,當儲存庫被同時更新或提交時,這特別有用。

返回

str

更新卡片元資料之提交的 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.