Using Hugging Face Models in Google Colab: A Beginner's Guide
Ajmal Hasan
Posted on November 13, 2024
This guide will walk you through using Hugging Face models in Google Colab. We’ll cover everything from setting up your Colab environment with GPU to running your first Hugging Face model. Hugging Face provides pre-trained models that make it easy to build powerful natural language processing (NLP) and machine learning applications without starting from scratch.
Prerequisite Knowledge
It’s helpful to have a basic understanding of:
- Python: Familiarity with Python syntax.
- Machine Learning: Knowledge of ML tasks like classification, text generation, and embeddings.
- Google Colab: Basic skills with Colab notebooks.
- Hugging Face Basics: An understanding of concepts like transformers, pipelines, and the Hugging Face Hub.
Step 1: Set Up Google Colab with GPU
Running Hugging Face models, especially transformer-based ones, can be resource-intensive, so setting up a GPU in Colab will help speed things up.
-
Open a New Colab Notebook:
- Go to Google Colab, create a new notebook, and name it as needed.
-
Change the Runtime to GPU:
- In the top menu, go to Runtime > Change runtime type.
- Set Hardware accelerator to GPU and click Save.
-
Verify the GPU:
- To confirm your GPU setup, run the following command:
!nvidia-smi
- This will display information about the GPU available for your session.
Step 2: Install Hugging Face Libraries
To access Hugging Face models in Colab, you need to install the Hugging Face transformers
library, which includes pre-trained models and pipelines.
-
Install the Hugging Face Transformers Library:
- In a new cell, run the following command:
!pip install transformers
-
Install Other Optional Libraries (if needed):
- If your task requires datasets or tokenizers, you can install additional libraries from Hugging Face:
!pip install datasets !pip install tokenizers
Step 3: Explore and Select a Model on the Hugging Face Hub
Hugging Face provides a large repository of models for various tasks. Here’s how to find and select one:
-
Visit the Hugging Face Hub:
- Go to the Hugging Face Hub and browse models by task, such as text classification, summarization, or image processing.
-
Filter by Task and Model:
- Use the Tasks tab to filter models based on your task requirements (e.g., sentiment analysis, text generation).
- You can also explore models by specific categories like NLP, computer vision, and audio.
3) Choose a Model and Copy Usage Code:
- Each model has a "Usage" section with example code to use the model. Select a model and copy this code to your Colab notebook for easy setup.
Step 4: Use the Model in Google Colab
After selecting a model, you can use the code snippet provided to load and run it directly in Colab. Here’s a step-by-step example of setting up a classifier model.
-
Import the Pipeline Function:
- The
pipeline
function in Hugging Face makes it easy to load a model by specifying the task type. Run the following code in your Colab notebook:
from transformers import pipeline
- The
-
Initialize a Model Pipeline:
- Here, we’ll initialize a sentiment analysis model using
pipeline
:
# Set up a sentiment-analysis pipeline classifier = pipeline("sentiment-analysis")
- Here, we’ll initialize a sentiment analysis model using
- This creates a
classifier
object you can use to classify text input. If you don’t specify a model name,pipeline
will load a default model for the task.
-
Run the Model on Sample Text:
- Now, let’s use the classifier on some text:
result = classifier("I love using Hugging Face models in Colab!") print(result)
- The output will display the classification label (e.g., POSITIVE or NEGATIVE) along with a confidence score.
-
Specify a Model Explicitly (Optional):
- To use a specific model from the Hugging Face Hub, pass the model name as a parameter:
classifier = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
- Specifying a model can be useful if you have a preferred architecture or language support.
Step 5: Try Other Tasks and Models
Hugging Face models aren’t limited to sentiment analysis. You can try other tasks by changing the task name in the pipeline
function:
- Text Generation:
generator = pipeline("text-generation", model="gpt2")
result = generator("Once upon a time,")
print(result)
- Translation:
translator = pipeline("translation_en_to_fr")
result = translator("I love coding in Python!")
print(result)
- Question Answering:
question_answerer = pipeline("question-answering")
result = question_answerer({
"question": "What is the capital of France?",
"context": "Paris is the capital of France."
})
print(result)
Additional Tips
- Explore Hugging Face Tutorials: For task-specific guides, check out the Hugging Face Task Page.
Experiment with Large Language Models (LLMs): A curated list of open-source LLMs can be found on GitHub, where you can discover more powerful models for your tasks.
For using as an api endpoint:
With either a Python FastAPI server or a Node.js Express server, you can create an API that uses Hugging Face models. Here’s a quick recap of each approach:-
Python FastAPI:
- Directly uses Hugging Face’s
transformers
library. - Provides model flexibility without relying on the Hugging Face Inference API.
- Ideal if you are comfortable with Python and need a custom setup.Example->
- Directly uses Hugging Face’s
-
Node.js Express:
- Uses the Hugging Face Inference API to perform inference.
- No need to install large model files; ideal for lightweight setups.
- Great if you prefer working in JavaScript and Node.js. Example->
Both approaches allow you to integrate Hugging Face’s powerful models into your applications, enabling tasks such as text generation, sentiment analysis, image classification, and more!
By following these steps, you can run a variety of Hugging Face models on Google Colab with minimal setup. The pipeline
function simplifies model usage for beginners, letting you focus on experimenting with NLP and ML models to achieve impressive results quickly.
Posted on November 13, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.