The Hugging Face Unity API provides a straightforward way to access and utilize Hugging Face's AI models within Unity projects. Here's a step-by-step guide to get you started.
Installation
- Open your Unity project.
- Navigate to
Window>Package Manager. - Click the
+icon and selectAdd Package from Git URL. - Enter the URL:
https://github.com/huggingface/unity-api.git. - After installation, the API wizard will appear. If not, find it under
Window>Hugging Face API Wizard.
In the wizard:
- Enter your API key, which you can generate in your Hugging Face account settings.
- Test the key by clicking
Test API key. - Optionally, modify the model endpoint to use a different model. The endpoint for any model supporting the Inference API can be found on the model's page under
Deploy>Inference API. - Configure advanced settings as needed, referring to the project repository for the latest details.
- To view usage examples, click
Install Examples. You can then close the wizard.
Making API Calls
Once set up, you can call the API from your scripts. Below is an example for sentence similarity:
using HuggingFace.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);
}
void OnSuccess(float[] result) {
foreach(float value in result) {
Debug.Log(value);
}
}
void OnError(string error) {
Debug.LogError(error);
}
Supported Tasks and Custom Models
The API supports several tasks, including Conversation, Text Generation, Text-to-Image, Text Classification, Question Answering, Translation, Summarization, and Speech Recognition. Use the corresponding methods in the HuggingFaceAPI class. To use a custom model hosted on Hugging Face, simply change the model endpoint in the API wizard.
Usage Tips
- API calls are asynchronous, so responses and errors are handled via callbacks.
- If you experience slow responses, try switching to a less resource-intensive model endpoint.
Conclusion
The Hugging Face Unity API simplifies integrating AI into Unity projects. For questions or to get involved with Hugging Face for Games, join the Hugging Face Discord.