Skip to content
AI Productivity Insights

Tutorial · 3 min read

Running Google MedGemma 4B on Hugging Face with Google Colab

Learn how to set up and run Google’s medical AI model for analyzing medical images like X-rays using free GPU resources in Google Colab.

Time: 15–20 minutes · Level: Beginner · GPU: T4 (Google Colab free tier) · Updated: Dec 31, 2025

Open Hugging Face · Open the Google Colab notebook

For education and experimentation only. MedGemma’s answers are not a medical diagnosis. Never use them for clinical decisions; a qualified clinician must review any medical image and interpretation.

Prerequisites

  • A Hugging Face account with an access token
  • A Google account for Google Colab access
  • Basic Python knowledge (helpful but not required)
  • Accept the MedGemma model license on Hugging Face (on the google/medgemma-4b-it model page)

Part 1: Hugging Face Setup

Step 1: Create a Hugging Face Access Token

Generate an authentication token to access gated models. Navigate to your Hugging Face account settings to create a new access token:

  1. Click on your profile icon in the top-right corner.
  2. Select Access Tokens from the dropdown menu.
  3. Click the ”+ Create new token” button.

💡 Tip: Access tokens authenticate your identity and allow applications to perform actions on your behalf. Never share your tokens publicly.

Hugging Face profile menu open, with "Access Tokens" highlighted

Hugging Face Access Tokens page with the "+ Create new token" button highlighted

Step 2: Configure Token Permissions

Set up a Write token for model access. When creating your token, configure it with the following settings:

  1. Select Write as the token type.
  2. Enter a descriptive name (e.g., “MedGemma-Colab”).
  3. Click Create token.

A dialog will appear with your new token. Copy it immediately and store it securely — you won’t be able to see it again.

Create new Access Token form with token type "Write" and the name "MedGemma-Colab"

The "Save your Access Token" dialog showing the new token (partly hidden) and a Copy button

⚠️ Security best practices. Keep your token secure:

  • Never commit tokens to version control.
  • Use environment variables or secure vaults.
  • Rotate tokens periodically.
  • Revoke tokens you no longer need.

Part 2: Google Colab Setup

Step 3: Enable GPU Runtime

Configure Colab to use a T4 GPU for model inference. MedGemma 4B requires GPU acceleration. In Google Colab:

  1. Go to Runtime → Change runtime type.
  2. Select T4 GPU (or higher) as the hardware accelerator.
  3. Click Save.

Free tier: the T4 GPU is available on Google Colab’s free tier, making MedGemma accessible without any cost.

Step 4: Install the Transformers Library

Install the latest version of the Hugging Face Transformers library:

!pip install -U transformers

This command installs or upgrades the transformers library with all the dependencies needed to run MedGemma.

Colab cell output after running pip install -U transformers, showing the requirements are satisfied

Step 5: Authenticate with Hugging Face

Authenticate your session to access the gated MedGemma model:

from huggingface_hub import login
login(new_session=False)

A login prompt will appear. Paste your access token from Step 2 and click Login.

Colab login prompt for the Hugging Face Hub with a token field and Login button

Alternative: use Colab Secrets. For a more secure approach, store your token in Colab Secrets:

  1. Click the 🔑 key icon in Colab’s sidebar.
  2. Add a secret named HF_TOKEN.
  3. Paste your token as the value.
  4. Enable notebook access.

Step 6: Test with a Sample X-ray Image

Use the pipeline API to analyze a sample chest X-ray from Wikimedia Commons:

from transformers import pipeline
from PIL import Image
import requests
import torch

pipe = pipeline(
    "image-text-to-text",
    model="google/medgemma-4b-it",
    torch_dtype=torch.bfloat16,
    device="cuda",
)

# Image attribution: Stillwaterising, CC0, via Wikimedia Commons
image_url = "https://upload.wikimedia.org/wikipedia/commons/c/c8/Chest_Xray_PA_3-8-2010.png"
image = Image.open(requests.get(image_url, headers={"User-Agent": "example"}, stream=True).raw)

messages = [
    {
        "role": "system",
        "content": [{"type": "text", "text": "You are an expert radiologist."}]
    },
    {
        "role": "user",
        "content": [
            {"type": "text", "text": "Describe this X-ray"},
            {"type": "image", "image": image}
        ]
    }
]

output = pipe(text=messages, max_new_tokens=8192)
print(output[0]["generated_text"][-1]["content"])

Loading time: the first run downloads the model (~10 GB), which may take several minutes depending on your connection speed. If you see a warning that torch_dtype is deprecated, the code still works; newer versions of Transformers accept dtype= instead.

Colab output: the model files download, then MedGemma describes the chest X-ray with an overall impression and specific findings

Step 7: Use Your Own Images

To analyze your own X-ray images, upload them to Colab first:

  1. Click the Files icon 📁 in Colab’s left sidebar.
  2. Click Upload to session storage.
  3. Select your image file.

Colab Files panel with the Files icon and the "Upload to session storage" button marked

File picker selecting the image Chest_Xray_PA_3-8-2010.png

Colab Files panel showing the uploaded Chest_Xray_PA_3-8-2010.png

Then run this modified code, which reads a local file instead of a URL:

from transformers import pipeline
from PIL import Image
import torch
import os

# --- Configure the model/pipeline ---
pipe = pipeline(
    "image-text-to-text",
    model="google/medgemma-4b-it",
    torch_dtype=torch.bfloat16,
    device="cuda" if torch.cuda.is_available() else "cpu",
)

# --- Use a local image file instead of a URL ---
# Example: put your image in the same folder and set the filename below
image_path = "Chest_Xray_PA_3-8-2010.png"  # <- change this to your file name

if not os.path.exists(image_path):
    raise FileNotFoundError(f"Image file not found: {image_path}")

# Open the image with PIL
image = Image.open(image_path)

# Optional: Ensure RGB mode (some models expect RGB; X-rays may be 'L' or 'I')
if image.mode != "RGB":
    image = image.convert("RGB")

messages = [
    {
        "role": "system",
        "content": [{"type": "text", "text": "You are an expert radiologist."}]
    },
    {
        "role": "user",
        "content": [
            {"type": "text", "text": "Describe this X-ray"},
            {"type": "image", "image": image}
        ]
    }
]

# Run inference
output = pipe(text=messages, max_new_tokens=8192)
print(output[0]["generated_text"][-1]["content"])

💡 Customization tips

  • Adjust the prompt: change "Describe this X-ray" to ask specific questions, such as “Are there any signs of pneumonia?”, “Identify any abnormalities in this chest X-ray” or “Provide a structured radiology report”. The next two steps show two of these.
  • Adjust the output length: change max_new_tokens=8192 for shorter or longer responses.

Step 8: Ask a Specific Question (Pneumonia Check)

Reuse the loaded pipe and image, and ask a focused question instead of a general description:

messages = [
    {
        "role": "system",
        "content": [{"type": "text", "text": "You are an expert radiologist."}]
    },
    {
        "role": "user",
        "content": [
            {"type": "text", "text": "Are there any signs of pneumonia?"},
            {"type": "image", "image": image}
        ]
    }
]

output = pipe(text=messages, max_new_tokens=8192)
print(output[0]["generated_text"][-1]["content"])

Example output:

Based on the chest X-ray provided, there are no obvious signs of pneumonia. The lung fields appear clear, and there is no evidence of consolidation, pleural effusion, or pneumothorax. The heart size is within normal limits, and the mediastinum is unremarkable.

However, it is important to note that this is a single image and a comprehensive evaluation would require a full clinical history, physical examination, and potentially additional imaging studies.

Therefore, while I cannot definitively rule out pneumonia based on this single image, it is unlikely to be present.

Colab cell asking "Are there any signs of pneumonia?" with MedGemma's answer below

Step 9: Generate a Structured Radiology Report

Ask for a structured report to see how MedGemma organizes its findings:

messages = [
    {
        "role": "system",
        "content": [{"type": "text", "text": "You are an expert radiologist."}]
    },
    {
        "role": "user",
        "content": [
            {"type": "text", "text": "Provide a structured radiology report"},
            {"type": "image", "image": image}
        ]
    }
]

output = pipe(text=messages, max_new_tokens=8192)
print(output[0]["generated_text"][-1]["content"])

Example output (abridged; the model also explains each section and how to use the template):

Radiology Report

Patient: [Insert Patient Name Here]
Patient ID: [Insert Patient ID Here]
Exam: Chest X-ray (PA and Lateral)
Referring Physician: [Insert Referring Physician Name Here]
Indication: [Insert Indication Here - e.g., Routine screening, evaluation of cough, etc.]

Technique:
  PA (Posteroanterior) and Lateral chest radiographs were obtained.

Findings:
  Heart Size: The heart size is within normal limits.
  Mediastinum: The mediastinum is unremarkable in appearance.
  Lungs: The lungs are clear bilaterally without evidence of focal consolidation, pleural effusion, or pneumothorax.
  Bones: The visualized bony structures are unremarkable.
  Soft Tissues: No subcutaneous emphysema is identified.

Impression:
  Normal chest radiograph.

Recommendations:
  No further imaging is recommended at this time.

Important Considerations and Disclaimers:
  - This report is based solely on the provided image. A complete evaluation requires review of the
    full PA and lateral views, and correlation with the patient's clinical history and examination.
  - This report is for informational purposes only and should not be used for diagnostic or
    treatment decisions.
  - The report should be reviewed and modified by a qualified radiologist.

Colab cell requesting "Provide a structured radiology report" with the start of MedGemma's structured report below

Newsletter

Get AI productivity insights delivered

Practical AI guides, tutorials, and fresh insights in your inbox.