googlecolab-colabtools

repository·main·Indexed 25 days ago

https://github.com/googlecolab/colabtools

Python library code used within the Google Colaboratory environment. This repository provides resources for the Colab community to understand underlying tools, including documentation on using the Gemini API, accessing Gemma models via Kaggle, integrating with GitHub, and utilizing the google.colab.ai module for text generation.

Tokens
8.3K
Snippets
31
Records
44
Agent score
82%

What's inside colabtools

  1. Install dependencies and setup Stable Diffusion pipeline

    main

    Install the necessary libraries for image generation and move the pipeline to the GPU (cuda).

    Required packages:

    • diffusers
    • accelerate
    • mediapy

    This example uses the stabilityai/sdxl-turbo model with torch.float16 for optimized performance.

    %pip install --quiet --upgrade diffusers accelerate mediapy
    
    import mediapy as media, random, sys, torch
    from diffusers import AutoPipelineForText2Image
    
    pipe = AutoPipelineForText2Image.from_pretrained(
        "stabilityai/sdxl-turbo",
        torch_dtype=torch.float16,
        use_safetensors=True,
        variant="fp16",
        )
    
    pipe = pipe.to("cuda")
  2. Verify GPU availability for Stable Diffusion

    main

    Stable Diffusion requires a GPU to run efficiently. Before starting, verify that a GPU is attached to your Colab runtime by checking the output of nvidia-smi. If it fails, navigate to Runtime > Change runtime type and select a GPU-enabled hardware accelerator.

    import os
    
    if os.system('nvidia-smi'):
      raise Exception("No GPU found. Access a GPU through Runtime > Change runtime type and try again.")
  3. Understand available model types

    main

    Models in google.colab.ai are categorized by their intended use case:

    • Pro: Most capable models, ideal for complex reasoning, creative tasks, and detailed analysis.
    • Flash: Optimized for high speed and efficiency; best for summarization, chat applications, and rapid responses.
    • Gemma: Lightweight, open-weight models suitable for various text generation tasks and experimentation.
  4. How slides are generated from notebook cells

    main

    Colab breaks your notebook into slides based on cell types and hierarchy:

    • Standard Cells: Generally, each individual cell (text or code) becomes its own slide.
    • Code Cells: Code cells are shown as slides and remain executable within slideshow mode.
    • Collapsible Sections (Special Case): If you use Colab's collapsible section feature to create a hierarchy, a section consisting of exactly one text cell (explaining a concept) and one code cell (demonstrating the concept) will be grouped together into a single slide.
    • Slide Titles:
      • By default, the notebook title is used.
      • If using collapsible sections, the slide title becomes the header of the parent section (e.g., an H2 header will use its parent H1 header as the title).
  5. How Keras 3 distribution API works with DeviceMesh and LayoutMap

    main

    The Keras 3 distribution API enables data and model parallelism by leveraging the underlying framework (like JAX) to distribute tensors according to sharding directives via Single Program, Multiple Data (SPMD) expansion.

    Core Components:

    • DeviceMesh: Represents a collection of hardware devices configured for distributed computation. You define its shape (e.g., (1, 8) for 8 TPU cores) and dimension names (e.g., ["batch", "model"]).
    • LayoutMap: Specifies how weights and tensors are sharded or replicated. It uses string keys (which can be regex) to match tensor paths. A value of (None, model_dim) indicates sharding across the specified dimension.
    • ModelParallel: An object that uses the DeviceMesh and LayoutMap to shard model weights or activation tensors across devices.
    • set_distribution(): Applies the configured ModelParallel strategy to the Keras environment.
    device_mesh = keras.distribution.DeviceMesh(
        (1, 8),
        ["batch", "model"],
        devices=keras.distribution.list_devices())
    
    layout_map = keras.distribution.LayoutMap(device_mesh)
    layout_map["token_embedding/embeddings"] = (None, model_dim)
    
    model_parallel = keras.distribution.ModelParallel(
        device_mesh, layout_map, batch_dim_name="batch")
    
    keras.distribution.set_distribution(model_parallel)
  6. Configure the Gemini API in Google Colab

    main

    To use Gemini in a Colab notebook, you must retrieve your API key from Google AI Studio and store it in the Colab Secrets manager.

    1. Create an API key at https://makersuite.google.com/app/apikey.
    2. In your Colab notebook, click the key icon (Secrets) on the left sidebar.
    3. Add a new secret with the name GOOGLE_API_KEY and paste your key into the Value field.
    4. Ensure the notebook has permission to access the secret by toggling the access switch.

    Use google.colab.userdata.get() to retrieve the secret and google.generativeai.configure() to initialize the library.

    import google.generativeai as genai
    from google.colab import userdata
    
    gemini_api_secret_name = 'GOOGLE_API_KEY'
    
    try:
      GOOGLE_API_KEY = userdata.get(gemini_api_secret_name)
      genai.configure(api_key=GOOGLE_API_KEY)
    except userdata.SecretNotFoundError as e:
       print(f'Secret not found\n\nThis expects you to create a secret named {gemini_api_secret_name} in Colab...')
       raise e
    except userdata.NotebookAccessError as e:
      print(f'You need to grant this notebook access to the {gemini_api_secret_name} secret...')
      raise e
    
    model = genai.GenerativeModel('gemini-pro')
  7. Start a Colab slideshow

    main

    You can enter slideshow mode to present your notebook as a series of slides. You can start a slideshow from the beginning or from the currently focused cell.

    Ways to start from the beginning:

    • Use the menu: View > Start slideshow from beginning
    • Use the command palette (Ctrl + Shift + P) and search for Start notebook slideshow from beginning
    • Use the keyboard shortcut: Alt + Shift + V
    • Append #slideshowMode=true to the end of any Colab notebook URL.

    Ways to start from the current cell:

    • Use the menu: View > Start slideshow
    • Use the command palette (Ctrl + Shift + P) and search for Start notebook slideshow
    • Use the keyboard shortcut: Alt + V
  8. Configure the Gemini API using Colab Secrets

    main

    To use Gemini, you should store your API key in the Colab 'Secrets' section (the key icon on the left sidebar) to avoid hardcoding sensitive information.

    1. Create an API key at https://makersuite.google.com/app/apikey.
    2. In Colab, add a new secret named GOOGLE_API_KEY (or your preferred name) and paste the key.
    3. Ensure 'Notebook access' is toggled ON for that secret.

    Use google.colab.userdata to retrieve the key and google.generativeai to configure the model.

    import google.generativeai as genai
    from google.colab import userdata
    
    gemini_api_secret_name = 'GOOGLE_API_KEY'
    
    try:
      GOOGLE_API_KEY = userdata.get(gemini_api_secret_name)
      genai.configure(api_key=GOOGLE_API_KEY)
    except userdata.SecretNotFoundError as e:
       print(f'Secret not found\n\nThis expects you to create a secret named {gemini_api_secret_name} in Colab...')
       raise e
    except userdata.NotebookAccessError as e:
      print(f'You need to grant this notebook access to the {gemini_api_secret_name} secret...')
      raise e
    
    model = genai.GenerativeModel('gemini-pro')
  9. Configure the OpenAI API key in Colab

    main

    To use OpenAI models in Colab, follow these steps:

    1. Create an API key at https://platform.openai.com/api-keys.
    2. In Colab, click the key icon (Secrets) on the left sidebar.
    3. Add a new secret named OPENAI_API_KEY and paste your key.
    4. Grant the notebook access to the secret.

    Note: It is recommended to install llmx before openai to avoid installation errors.

    Required packages:

    • llmx
    • openai
    !pip install -q llmx
    !pip install -q openai
    from openai import OpenAI
    from google.colab import userdata
    
    openai_api_secret_name = 'OPENAI_API_KEY'
    
    try:
      OPENAI_API_KEY=userdata.get(openai_api_secret_name)
      client = OpenAI(
        api_key=OPENAI_API_KEY
      )
    except userdata.SecretNotFoundError as e:
       # Handle missing secret
       raise e
    except userdata.NotebookAccessError as e:
      # Handle missing access permissions
      raise e