Creating an Access Token and Logging into Hugging Face Hub from a Notebook

Yuan An, PhD
3 min readOct 2, 2023

--

This is a series of short tutorials about using Hugging Face. The table of contents is here.

In this lesson, we will learn how to access the Hugging Face hub

Sign up and Log in to the Hugging Face Hub

Sign up and log in at https://huggingface.co/ . Notice it is not: https://huggingface.com

Create an Access Token

  • After logging in, click your avatar icon in the upper-right corner, click “setting” on the drop-down list, and then click “Access Token” on the left-hand-side navigation panel.
  • Click the “New token” button to create a new access token by typing a name and selecting a role.

Log into Hugging Face Hub from a Notebook

  • On a notebook, first import notebook_login from huggingface_hub as:
from huggingface_hub import notebook_login
  • Run notebook_login
notebook_login()
  • Copy the access token from your Hugging Face hub account and paste it to the Token box as shown below:

Great! You have now successfully logged into your Hugging Face hub account from your notebook.

Save and Share a Model in the Hugging Face Hub

If you have a model, you can save the model to the Hugging Face hub by using push_to_hub() as:

model.push_to_hub("fine_tuned_10012023")

The string inside the push_to_hub() function is the identifier for your model in the hub.

You can check the information about the model in the hub:

Save a Model and Tokenizer in a Local Place

You can also save a model and tokenizer in a local place instead of the hugging face hub.

First, define a path to save the mode:

saved_path = "./my_model_saved"

Next, save the model and tokenizer:

model.save_pretrained(saved_path)
tokenizer.save_pretrained(saved_path)

Check the result:

Load a Saved Model and Tokenizer from a Local Place

To load a saved model and tokenizer from a local place, pass the path to AutoTokenizer and AutoClass:

tokenizer_loaded_from_local = AutoTokenizer.from_pretrained(saved_path)
model_loaded_from_local = AutoModelForSequenceClassification.from_pretrained(saved_path)

The colab notebook is available here:

--

--

Yuan An, PhD

Faculty member in the College of Computing and Informatics at Drexel University; Doing research in NLP, Machine Learning, Ontology, Knowledge Graph, Embeddings