Setting up Ubuntu for HuggingFace

Just wanted to document steps I took to successfully setup an Ubuntu machine for using models on HuggingFace. Refer to https://huggingface.co/docs/transformers/installation for installation instructions for using Transformers module.

Ubuntu comes installed with Python3. I started with creating a new environment for the project. I switched to the folder where I was going to create projects/files for using HuggingFace

Creating new Python Environment

  1. python3 -m venv .env
  2. source .env/bin/activate

Adding the new environment to Jupyter

Jupyter does not use the new environment created above. We need to add the environment to Jupyter. But before that we need to install ipykernel

  1. python3 -m pip install ipykernel
  2. python3 -m ipykernel install –user –name=myenv –display-name=“HuggingFace Env”
    Here name can be anything, you don’t have to crate the environment with that name.
  3. Launch “jupyter notebook”, create a notebook and switch kernel to “HuggingFace Env” as specified in the display-name above

Installing PyTorch

Go to https://pytorch.org/get-started/locally/ and select appropriate options in “Start Locally” section to see the command to be executed for installing PyTorch. I used the following command (for CPU only option on Linux –

python3 -m pip install torch torchvision torchaudio –index-url https://download.pytorch.org/whl/cpu

Install Transformers

python3 -m pip install transformers

Verify Setup

Run following code, either in Jupyter notebook or from a .py file –

import torch
from transformers import pipeline

sentiment_analyzer = pipeline(“sentiment-analysis”)
result = sentiment_analyzer(“this setup is working!”)
print(result)

-Ram Kulkarni

Leave a Reply

Social