Hub 文件

在 Hugging Face Hub 上使用 AllenNLP

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

在 Hugging Face 上使用 AllenNLP

allennlp 是一個 NLP 庫,用於在不同語言任務上開發最先進的模型。它為現代 NLP 中的常見元件和模型提供高階抽象和 API。它還提供了一個可擴充套件的框架,使執行和管理 NLP 實驗變得容易。

探索 Hub 中的 allennlp

您可以透過在模型頁面左側進行篩選來查詢 Hub 中的 allennlp 模型。

Hub 上的所有模型都附帶了有用的功能

  1. 一個包含自動託管的 TensorBoard 軌跡的訓練指標選項卡。
  2. 有助於發現的元資料標籤。
  3. 一個互動式小部件,您可以使用它直接在瀏覽器中試用模型。
  4. 一個允許進行推理請求的推理 API。

使用現有模型

您可以使用 Predictor 類來載入 Hub 中現有的模型。為此,請使用 from_path 方法,並使用 "hf://" 字首和倉庫 ID。這是一個端到端的示例。

import allennlp_models
from allennlp.predictors.predictor import Predictor

predictor = Predictor.from_path("hf://allenai/bidaf-elmo")
predictor_input = {
    "passage": "My name is Wolfgang and I live in Berlin", 
    "question": "Where do I live?"
}
predictions = predictor.predict_json(predictor_input)

要獲取此類程式碼片段,您可以單擊右上角的 Use in AllenNLP

分享您的模型

第一步是將模型本地儲存。例如,您可以使用 archive_model 方法將模型儲存為 model.tar.gz 檔案。然後您可以將壓縮模型推送到 Hub。當您使用 allennlp 訓練模型時,模型會自動序列化,因此您可以將其作為首選選項。

使用 AllenNLP CLI

要使用 CLI 推送,您可以使用 allennlp push_to_hf 命令,如下所示。

allennlp push_to_hf --repo_name test_allennlp --archive_path model 
引數 型別 描述
--repo_name, -n str / Path Hub 上的倉庫名稱。
--organization, -o 字串 要上傳管道到的組織的可選名稱。
--serialization-dir, -s str / Path 包含序列化模型的目錄路徑。
--archive-path, -a str / Path 如果您使用的是壓縮模型(例如 model/model.tar.gz),而不是序列化路徑,則可以使用此標誌。
--local-repo-path, -l str / Path 模型倉庫的本地路徑(如果不存在則會建立)。預設為當前工作目錄中的 hub
--commit-message, -c 字串 用於更新的提交訊息。預設為 "update repository"

從 Python 指令碼

push_to_hf 函式與 bash 指令碼具有相同的引數。

from allennlp.common.push_to_hf import push_to_hf

serialization_dir = "path/to/serialization/directory"
push_to_hf(
    repo_name="my_repo_name",
    serialization_dir=serialization_dir,
    local_repo_path=self.local_repo_path
)

只需一分鐘,您就可以將模型上傳到 Hub,直接在瀏覽器中試用,並與社群其他成員分享。所有必需的元資料都將為您上傳!

其他資源

< > 在 GitHub 上更新

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