如何安裝及使用 Hugging Face Unity API

釋出於 2023 年 5 月 1 日
在 GitHub 上更新

Hugging Face Unity API 是一個易於使用的 Hugging Face 推理 API 整合,它允許開發者在他們的 Unity 專案中訪問和使用 Hugging Face 的 AI 模型。在這篇博文中,我們將詳細介紹安裝和使用 Hugging Face Unity API 的步驟。

安裝

  1. 開啟你的 Unity 專案
  2. 前往 Window -> Package Manager
  3. 點選 + 並選擇 Add Package from git URL
  4. 輸入 https://github.com/huggingface/unity-api.git
  5. 安裝完成後,Unity API 嚮導應該會彈出。如果沒有,請前往 Window -> Hugging Face API Wizard
  1. 輸入你的 API 金鑰。你可以在 Hugging Face 賬戶設定 中建立 API 金鑰。
  2. 在 API 嚮導中點選 Test API key 來測試 API 金鑰。
  3. (可選)更改模型端點以切換要使用的模型。任何支援推理 API 的模型的端點都可以在 Hugging Face 網站上找到,方法是轉到模型頁面,點選 Deploy -> Inference API,然後從 API_URL 欄位中複製 URL。
  4. 如果需要,配置高階設定。要獲取最新資訊,請訪問專案倉庫 https://github.com/huggingface/unity-api
  5. 要檢視如何使用 API 的示例,請點選 Install Examples。現在你可以關閉 API 嚮導了。

現在 API 已經設定好,你可以從你的指令碼中呼叫 API。讓我們看一個執行句子相似度任務的例子。

using HuggingFace.API;

/* other code */

// Make a call to the API
void Query() {
    string inputText = "I'm on my way to the forest.";
    string[] candidates = {
        "The player is going to the city",
        "The player is going to the wilderness",
        "The player is wandering aimlessly"
    };
    HuggingFaceAPI.SentenceSimilarity(inputText, OnSuccess, OnError, candidates);
}

// If successful, handle the result
void OnSuccess(float[] result) {
    foreach(float value in result) {
        Debug.Log(value);
    }
}

// Otherwise, handle the error
void OnError(string error) {
    Debug.LogError(error);
}

/* other code */

支援的任務和自定義模型

Hugging Face Unity API 目前還支援以下任務:

使用 HuggingFaceAPI 類提供的相應方法來執行這些任務。

要使用託管在 Hugging Face 上的你自己的自定義模型,請在 API 嚮導中更改模型端點。

使用技巧

  1. 請記住,API 呼叫是非同步的,並透過回撥函式返回響應或錯誤。
  2. 透過將模型端點更改為資源消耗較低的模型來解決響應時間慢或效能問題。

總結

Hugging Face Unity API 提供了一種將 AI 模型整合到 Unity 專案中的簡單方法。我們希望本教程對您有所幫助。如果您有任何問題或想更多地參與使用 Hugging Face 進行遊戲開發,請加入 Hugging Face Discord

社群

錯誤
image.png

我嘗試在 Unity 中更新官方 API 連結,但完全沒用,這個專案可能被建立者放棄了,並且在 2025 年沒有任何更新。基礎連結還是 2023 年的!太舊了 :(

註冊登入 以發表評論

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