How to get an access token for Hugging Face

Hugging Face is an online machine learning platform that allows users to create, share, and collaborate on machine learning models, datasets, and applications. We’ll need the access token from your account settings to access and upload models and datasets with Hugging Face. In this Answer, we’ll explain how to get the Hugging Face access token.

Step 1

Open the browser and go to huggingface.co. We’ll see the following landing page of the platform. Click the “Sign Up” button in the top right corner:

The landing page of huggingface.co
The landing page of huggingface.co

Step 2

We’ll see a form that requires our email and password, as shown in the screenshot below. Provide the information and click the “Next” button.

The sign-up page
The sign-up page

Step 3

We’ll be redirected to a “Complete your profile” page. We have to choose our username and provide our full name. We can also link our X (formerly known as Twitter) and GitHub accounts here. Before proceeding, check the “Terms and Code of Conduct” check box.

Note: All the models and datasets you upload using Hugging Face will have your username. Be sure to choose a username to share with friends and family.

Adding your profile details
Adding your profile details

When we click the “Create Account” button, we’ll be asked to verify that we’re human. Select the correct options as the pop-up explains and click “Verify.”

Step 4

Once we’ve verified that we are human, we’ll receive an email to verify the email address we provided. As shown in the screenshot below, we’ll be directed to our profile’s “Welcome” page. Click the “Resend confirmation email” button if you did not receive the email.

The welcome page
The welcome page

Step 5

On the “Welcome” page, click your profile photo in the navigation bar and open the settings as shown below:

Open profile settings
Open profile settings

Step 6

When we open settings, the profile settings will open by default, as shown in the screenshot below. Click the “Access Tokens” option in the sidebar.

The settings page
The settings page

Step 7

On the “Access Tokens“ page, click the “New token” button as shown below:

Managing access tokens
Managing access tokens

Step 8

A pop-up will appear when we click the “New token” button, as shown in the screenshot below. Provide a name and role for the token. The following role can be assigned to a token:

  • read: The token will allow us to download models and datasets from Hugging Face.

  • write: The token will allow us to download and upload models and datasets from Hugging Face.

Note: You must first confirm your email address to generate access tokens on Hugging Face.

Creating a new access token
Creating a new access token

Provide a name, select an appropriate role, and click the “Generate a token” button.

Step 9

The token will now appear under “User Access Tokens” as shown below:

Getting the token string
Getting the token string

We can click the “Show” button to inspect the token value and press the copy button.

Keep your access token secure by removing it from your code before pushing it to GitHub.

We can now use this key to experience the awesomeness of Hugging Face!

Code

In the following code, we will attempt to log in to the Huggingface_hub using the access token you just generated.

Note: Replace <ADD-YOUR-HUGGINGFACE-TOKEN-HERE> with the value of your access token in the code.

from huggingface_hub import login
access_token = '<ADD-YOUR-HUGGINGFACE-TOKEN-HERE>'
print(login(token = access_token))

Explanation

  • Line 1: We import the login method from the huggingface_hub library.

  • Line 3: We set the access token.

  • Line 4: We print the output of the login() method when attempting to sign-in with the given access token.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved