Interactive Development In HF Spaces

Authored by: Moritz Laurer

Services like Google Colab or Kaggle Notebooks have made it dramatically easier for people to access compute in easy-to-use Jupyter notebooks in the browser. Unfortunately, these services also have several limitations:

HF JupyterLab Spaces overcome these limitations. With a HF JupyterLab Space, you can:

This recipe guides you through the setup of your own JupyterLab Space.

Interactive Development in HF JupyterLab Spaces

Creating your JupyterLab Space

To create your own HF JupyterLab Space, navigate to the Space creation page and click on Docker > JupyterLab. A HF JupyterLab Space is essentially a Docker container with a pre-configured copy of JupyterLab that runs on Hugging Face’s cloud infrastructure. Here is some advice on configuring your JupyterLab Space:

Once you have configured the JupyterLab Space, you can click on Create Space. The Space will be built and after a few seconds you will see the JupyterLab login screen. You can now login with the password you defined before.

Using your JupyterLab Space

You can now work in your own JupyterLab Space in the browser! You can create your own directory structure with .ipynb notebooks or any other files and datasets in the File Browser on the left. If you have activated persistent storage, all files are permanently stored in the default /data directory of the Space.

Dynamically switching between CPUs and GPUs

Similar to services like Google Colab, you can change the hardware the Space is running on-the-fly. We recommend doing initial setup work on the upgraded or free CPU, for example data cleaning, setting up Endpoints, or testing APIs. Once your code is set up, you can simply click on Settings at the top right of the Space and change to a wide selection of hardware that might be required for more compute intensive inference or training jobs. When you change hardware, the Space will restart itself and all environment variables will be lost (like with Google Colab) and you will have a new clean environment on the new hardware after some seconds. Your stored and saved files (code, data etc.) will of course also be available on the new hardware. The image below shows the available hardware at the time of writing (June 2024) and this will be updated in the future.

In the bottom left of the image, you can also see the Sleep time settings where you can define how long you want the hardware to run in case of inactivity. This is a major advantage over Google Colab. If you want to save money, you can make the Space sleep after 15 minutes of inactivity, but if you need the hardware to be available for a 48 hour training run or longer, you can just prevent the Space from falling asleep and let it run for as long as you want. You can also manually Pause the Space and you will no longer be charged for the Space Hardware.

If you scroll down in the settings, you will see additional options, like expanding storage, resetting the Space, etc. In case you have not set a password during Space creation, you can also create a secret called JUPYTER_TOKEN here later, which will replace the default “huggingface” password.

When you actively work with the Space over several days or weeks, files can accumulate in the storage cache. When you get a warning that the persistent storage is full and you think that the storage quota should not be reached yet, it might be helpful to factory reset the Space to empty the cache.

Customizing your JupyterLab Space

Remember that your JupyterLab Space is just a pre-configured Docker container, so if you are familiar with Docker, you can also customize it to your needs. For example, you can go to the Files section of your Space and add new requirements to the requirements.txt file or you can change from the default container image to another image in the Dockerfile, e.g. if you need a specific CUDA and PyTorch version preinstalled.

Dev Mode: Develop on HF Spaces from your local VS Code

What if you don’t like working in JupyterLab in the browser? Enter Dev Mode. Dev Mode enables you to SSH into any Space’s hardware from a local IDE like VS Code. HF Pro/Enterprise subscribers can activate Dev Mode for any Space in the Space’s settings.

Once Dev Mode is activated, you will see a pop-up at the bottom left of your JupyterLab Space’s window. To SSH into your local VS Code, you first need to install the VS Code Remote - SSH extension locally and add your SSH key to your HF Profile. Clicking on Connect with VS Code should then open your local VS Code window and establish the remote connection to your Space. A similar process should be possible with any IDE that supports remote development with SSH.

When connecting to your Space with SSH, your default directory will be an empty /app directory. You then need to change to the /data directory, where all your persistent files (code, data, models etc.) are stored. The /data directory is the only directory with guaranteed file persistance across sessions. You can find the files of your Docker container in the HOME/user/app directory, in case you want to modify the underlying Docker container.

Persisted files in the /data directory are currently not automatically backed up. We therefore recommend making backups of your most important files on a regular basis to avoid accidental data loss.

Now write some code!

That’s it, you can now run a JupyterLab Space in your browser, switch between one or multiple powerful GPUs on-the-fly, and connect to the hardware from your local IDE.

This entire recipe was written in a JupyterLab Space on a free CPU and we invite you to follow all other recipes of the Enterprise Hub Cookbook in your own JupyterLab Space.

< > Update on GitHub