How to Log in to Hugging Face from Google Colab

Step 1. Create a Secret in Secrets

First, create a new secret. In this case, we will use HF_TOKEN, but you can name it whatever you want.

Colab Secrets

Step 2. Load the Secret and Log In to Hugging Face

Use the secret you created to log in to Hugging Face from Google Colab.

from huggingface_hub import login
from google.colab import userdata

# Retrieve your secret token
HF_TOKEN = userdata.get('HF_TOKEN')

if HF_TOKEN:
    login(HF_TOKEN)
    print("Successfully logged in to Hugging Face!")
else:
    print("Token is not set. Please save the token first.")