How one can Install and Use the Hugging Face Unity API

-


Dylan Ebert's avatar

The Hugging Face Unity API is an easy-to-use integration of the Hugging Face Inference API, allowing developers to access and use Hugging Face AI models of their Unity projects. On this blog post, we’ll walk through the steps to put in and use the Hugging Face Unity API.



Installation

  1. Open your Unity project
  2. Go to Window -> Package Manager
  3. Click + and choose Add Package from git URL
  4. Enter https://github.com/huggingface/unity-api.git
  5. Once installed, the Unity API wizard should pop up. If not, go to Window -> Hugging Face API Wizard

  1. Enter your API key. Your API key could be created in your Hugging Face account settings.
  2. Test the API key by clicking Test API key within the API Wizard.
  3. Optionally, change the model endpoints to alter which model to make use of. The model endpoint for any model that supports the inference API could be found by going to the model on the Hugging Face website, clicking Deploy -> Inference API, and copying the url from the API_URL field.
  4. Configure advanced settings if desired. For up-to-date information, visit the project repository at https://github.com/huggingface/unity-api
  5. To see examples of the way to use the API, click Install Examples. You’ll be able to now close the API Wizard.

Now that the API is about up, you possibly can make calls out of your scripts to the API. Let us take a look at an example of performing a Sentence Similarity task:

using HuggingFace.API;

/* other code */

// Make a call to the API
void Query() {
    string inputText = "I'm on my strategy to the forest.";
    string[] candidates = {
        "The player goes to the town",
        "The player goes 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 */



Supported Tasks and Custom Models

The Hugging Face Unity API also currently supports the next tasks:

Use the corresponding methods provided by the HuggingFaceAPI class to perform these tasks.

To make use of your personal custom model hosted on Hugging Face, change the model endpoint within the API Wizard.



Usage Suggestions

  1. Remember that the API makes calls asynchronously, and returns a response or error via callbacks.
  2. Address slow response times or performance issues by changing model endpoints to lower resource models.



Conclusion

The Hugging Face Unity API offers a straightforward strategy to integrate AI models into your Unity projects. We hope you found this tutorial helpful. If you’ve gotten any questions or would really like to get more involved in using Hugging Face for Games, join the Hugging Face Discord!



Source link

ASK ANA

What are your thoughts on this topic?
Let us know in the comments below.

0 0 votes
Article Rating
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Share this article

Recent posts

0
Would love your thoughts, please comment.x
()
x