Downloading Login-Required Civitai Models in Google Colab
Google Colab provides a fantastic environment for experimenting with AI models like Stable Diffusion. Civitai is a popular hub for sharing and discovering these models. This post explains how to download models from Civitai that require login, directly within your Colab notebook, using an API token managed by Colab's Secrets feature.
Download Example: Using Colab Secrets
To manage your API token properly, use Google Colab's built-in Secrets manager.
Add Your Token to Colab Secrets:
- In your Colab notebook, click the key icon (🔑) in the left sidebar.
- Click "+ Add a new secret".
- Enter a Name for the secret, for example,
CIVITAI_API_TOKEN
. - Paste your actual Civitai API token into the Value field.
- Make sure the "Notebook access" toggle is enabled.
- Close the Secrets panel.
Access the Secret and Download: Run the following code block in Colab. It will access your stored token and use it for the download.
python# 1. Import the secrets module and access your stored token from google.colab import userdata try: CIVITAI_TOKEN = userdata.get('CIVITAI_API_TOKEN') if not CIVITAI_TOKEN: raise ValueError("Civitai token not found or empty in Colab Secrets. Please add it.") except userdata.SecretNotFoundError: print("Secret 'CIVITAI_API_TOKEN' not found. Please add it to Colab Secrets (key icon 🔑).") raise # Stop execution if the secret isn't found except ValueError as e: print(e) raise # Stop execution if the secret is empty # 2. Construct and run the wget command using the retrieved token # Example URL - replace with the actual model URL and desired filename # Ensure the URL includes the necessary parameters (type, format, etc.) for your desired file # Note: The URL is enclosed in double quotes for the shell command. !wget "https://civitai.com/api/download/models/351306?type=Model&format=SafeTensor&size=full&fp=fp16&token=$CIVITAI_TOKEN" -O dreamshaperXL_v21TurboDPMSDE.safetensors print("Download command executed.")
(See "Getting Your Civitai API Token" below if you don't have a token yet, and "Important Considerations" for details on finding the correct URL and parameters).
The Problem (Why Direct Download Fails)
When you try to download certain models using tools like wget
directly in Colab without authentication, the download might fail if the model requires a user account for access. Civitai restricts some downloads to registered users.
The Solution: Civitai API Token via Colab Secrets
The solution is to authenticate your download request using a personal API token generated from your Civitai account, managed via Colab Secrets.
Getting Your Civitai API Token
If you don't have an API token yet, follow these steps to obtain one:
- Access Account Settings:
- Go to the Civitai website.
- Click on your profile icon (usually in the top right corner).
- Select "Account Settings" from the dropdown menu.
- Generate API Key:
- Within "Account Settings," look for a section named "API Keys" (or similar).
- Click on "Add API Key".
- Give your key a descriptive name (e.g., "Colab Downloads") and click "Save".
- Save Your Key:
- Civitai will generate and display your API key. Copy this key immediately. You will paste this key into the Colab Secrets manager (as described in the "Download Example" section). Treat it like a password – don't share it publicly.
Important Considerations
- Token Management (Use Colab Secrets):
- It's recommended to use the Colab Secrets manager (key icon 🔑) to store sensitive information like API keys. This keeps your token separate from your code. The example code shows how to access it.
- Finding Download URLs/Parameters: The most challenging part can still be finding the exact API download URL and the correct parameters (
type
,format
,size
,fp
, etc.) for the specific model file you need. You might need to:- Start a download in your browser while logged in and use the browser's developer tools (Network tab) to inspect the actual URL being requested.
- Look for API information or download links provided by the model creator on the Civitai page. The parameters in the example (
?type=Model&format=SafeTensor&size=full&fp=fp16
) are specific to that model file and will likely be different for others.
- Rate Limits: Be mindful of potential API rate limits Civitai might enforce. Avoid excessively frequent downloads.
By using your Civitai API token via Colab Secrets, you can seamlessly download login-required models directly into your Google Colab environment.