googlecolab-colabtools
repository·main·Indexed 25 days ago
https://github.com/googlecolab/colabtoolsPython 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.
What's inside colabtools
- Google Colaboratory (Colab) is a Jupyter notebook environment designed to facilitate machine learning education and research. It requires no local setup to use and is accessible via colab.research.google.com. This repository contains the source code for the Python libraries that are available within the Colab environment.
Important usage notice for colabtools code
mainThe code published in this repository is intended for sharing resources with the Colab community and soliciting product feedback. The code published here is not intended for private reuse.Install dependencies and setup Stable Diffusion pipeline
mainInstall the necessary libraries for image generation and move the pipeline to the GPU (
cuda).Required packages:
diffusersacceleratemediapy
This example uses the
stabilityai/sdxl-turbomodel withtorch.float16for 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")Verify GPU availability for Stable Diffusion
mainStable 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.")Get support for Google Colaboratory
mainIf you need help using Colab, you can:
- General Questions: Submit questions on StackOverflow using the
google-colaboratorytag. - Product Issues: Submit an issue via GitHub Issues or use the "Help" -> "Send Feedback" menu within the Colab interface.
- General Questions: Submit questions on StackOverflow using the
Understand available model types
mainModels in
google.colab.aiare 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.
How slides are generated from notebook cells
mainColab 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
H2header will use its parentH1header as the title).
How Keras 3 distribution API works with DeviceMesh and LayoutMap
mainThe 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 theDeviceMeshandLayoutMapto shard model weights or activation tensors across devices.set_distribution(): Applies the configuredModelParallelstrategy 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)Configure the Gemini API in Google Colab
mainTo use Gemini in a Colab notebook, you must retrieve your API key from Google AI Studio and store it in the Colab Secrets manager.
- Create an API key at https://makersuite.google.com/app/apikey.
- In your Colab notebook, click the key icon (Secrets) on the left sidebar.
- Add a new secret with the name
GOOGLE_API_KEYand paste your key into the Value field. - Ensure the notebook has permission to access the secret by toggling the access switch.
Use
google.colab.userdata.get()to retrieve the secret andgoogle.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')Start a Colab slideshow
mainYou 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=trueto 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
Configure the Gemini API using Colab Secrets
mainTo 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.
- Create an API key at https://makersuite.google.com/app/apikey.
- In Colab, add a new secret named
GOOGLE_API_KEY(or your preferred name) and paste the key. - Ensure 'Notebook access' is toggled ON for that secret.
Use
google.colab.userdatato retrieve the key andgoogle.generativeaito 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')Configure the OpenAI API key in Colab
mainTo use OpenAI models in Colab, follow these steps:
- Create an API key at https://platform.openai.com/api-keys.
- In Colab, click the key icon (Secrets) on the left sidebar.
- Add a new secret named
OPENAI_API_KEYand paste your key. - Grant the notebook access to the secret.
Note: It is recommended to install
llmxbeforeopenaito avoid installation errors.Required packages:
llmxopenai
!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