Skip to main content

Run Hugging Face Models in Google Colab

Overview

Google Colab and Hugging Face have added an “Open in Colab” button to all model cards on the Hub. With a single click, you can generate a pre-configured notebook to load, test, and even fine-tune any model—skipping boilerplate and accelerating experiments. (medium.com)

Prerequisites

  • Google account to access Colab
  • Hugging Face account (optional, but recommended for saving tokens and private models)
  • An up-to-date browser
GOOD TO KNOW

If you are a model author, simply include a notebook.ipynb file in the root of the repository, and it will be used instead of the automatically generated notebook. (medium.com)

Step-by-Step

1. Choose the model

Go to any model card on the Hub, for example:

https://huggingface.co/google/gemma-3-27b-it

2. Open in Colab

Click on Use this model → Open in Colab or simply add /colab to the end of the URL:

https://huggingface.co/google/gemma-3-27b-it/colab

3. Configure the environment

In the generated notebook, go to Runtime ▸ Change runtime type and select GPU (or TPU if available).

Free GPUs in Colab are sufficient for inference on most Transformer-based models.

4. Execute the cells

The first cell usually installs dependencies:

!pip install -q transformers accelerate

Next, try out the model:

from transformers import pipeline
pipe = pipeline("text-generation", model="google/gemma-3-27b-it")
print(pipe("Hello, how are you?")[0]['generated_text'])

5. Customize

  • Replace model= with another Hugging Face model ID
  • Adjust parameters like max_new_tokens, temperature, top_p
  • Perform fine-tuning by adding your own training step
pip install huggingface_hub
export MODEL_ID=google/gemma-3-27b-it
python - <<'PY'
from huggingface_hub import InferenceClient
client = InferenceClient(model="$MODEL_ID")
print(client.text_generation("Hello, world!"))
PY

For Model Authors

  1. Create or upload a notebook.ipynb file demonstrating advanced usage of your model.
  2. Commit it to the same repository; the Hub will prioritize this file over the auto-generated one.
  3. The “Open in Colab” button will now point to your custom notebook.

Next Steps

Resources


Tutorial created on June 8, 2025, based on the Google Colab article.