Logging in to HuggingFace from Jupyter notebook without interactive prompt

yash khandelwal
2 min readFeb 26, 2022

In a recent project, I came across a troubling setup problem. Being a student who wants to learn and contribute, but who is short of funds, salvaging time on powerful hardware is a logistical and frustrating issue. For instance, each time you try to log in to hugging-face via a notebook, you are greeted with an interactive prompt asking for your token. This creates two issues. First, you cannot just do a “run all”, and you have to be present there to manually log in each and every time. Secondly, the shell prompt does not seem to work on Kaggle, which has the competitions in which you may want to use your private model. Kaggle also gives a pretty powerful hardware quota for free, with support for relatively longer sessions.

Note: HF uses access_tokens to log in to an account, and therefore should be kept private, and never be published or saved in notebooks. However, considering that you have a pretty straightforward and private use-case like mine, I will have to ask you to bend the rules a little and add your token to the code. If and when you decide to publish or share the notebook, make sure to delete your token. Alternatively, you can set up yet another pipeline that integrates with your token securely saved elsewhere and fetches it for login. However, getting this done is not the focus of this article.

So the point is, how do you log in to hugging face in such cases to save or fetch your painstakingly trained models? In a few short easy steps, let me show you how:

Hope this helps!

--

--