Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 127 additions & 14 deletions tools/comfyui_custom_nodes/README.md
Original file line number Diff line number Diff line change
@@ -1,40 +1,153 @@
# Google Cloud Vertex AI Custom node for ComfyUI

This repo is for developing a cloud-based visual AI workflow on Google Cloud Platform (GCP) by leveraging ComfyUI and Google Cloud's powerful foundation models.

## ComfyUI Vertex AI Nodes

This custom node pack for ComfyUI provides seamless integration with Google Cloud's Vertex AI services, allowing you to leverage powerful AI models directly within your ComfyUI workflows.
This custom node pack for ComfyUI provides seamless integration with Google Cloud's Vertex AI services, allowing you to leverage powerful AI models directly within your ComfyUI workflows. This enables you to build sophisticated generative AI applications with the scalability and performance of Google Cloud.

### Nodes Included

This pack currently includes the following nodes:

* **ImageGen 3:** Generates images using a Vertex AI image generation model
* **Gemini Flash:** Interacts with the Google Gemini Flash model for fast and efficient text generation.
* **Veo 2:** Generates video using a Vertex AI video model.
| Node Name | Description|
| :---------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **Gemini** | Interacts with Google's Gemini Flash, Pro model for fast and efficient text generation, ideal for real-time chat, summarization, and rapid content creation. |
| **Veo Video Generation** | Generates video content using the Google Veo 2,3 model, an advanced text-to-video diffusion model capable of producing high-quality, diverse video clips from text and image prompts. |
| **VideoPreviewNode** | Used to preview video stored in Google Cloud Storage (GCS) directly within your ComfyUI workflow, providing immediate visual feedback for video generation processes. |
| **Image to B64 Node** | Converts an input image into a Base64 encoded string, useful for embedding image data in text-based formats (e.g., JSON, HTML) or for API transmissions. |
| **Image Generation 3** | The primary node for generating high-quality images from text prompts using Google's Imagen 3 model, designed for diverse visual content creation. |
| **Inpaint Insert w Mask** | Inserts new content into a specified masked area of an image based on a text prompt, requiring an explicit mask to define the insertion region. |
| **Inpaint Insert w AutoMask** | Similar to "Inpaint Insert w Mask," but automatically generates the mask based on the prompt, streamlining the inpainting process. |
| **Inpaint Insert w SemanticMask** | Uses semantic understanding to intelligently generate a mask around specified objects (e.g., "the car") for precise content insertion based on a text prompt. |
| **Inpaint Remove w Mask** | Removes content from a specified masked area of an image and intelligently fills in the gap, requiring an explicit mask for removal. |
| **Inpaint Remove w AutoMask** | Similar to "Inpaint Remove w Mask," but automatically generates the mask for content removal based on the prompt. |
| **Inpaint Remove w SemanticMask** | Removes content based on semantic understanding, where the model automatically generates the mask around the specified object for removal (e.g., "remove the person"). |
| **Imagen Product Background Swap w Mask** | Replaces the background of a product image, requiring an explicit mask for the product to ensure clean separation from the original background. |
| **Imagen Product Background Swap w AutoMask** | Automatically detects the product and generates a mask for it, significantly speeding up the background swapping process for product images. |
| **Imagen Mask-Free Editing** | Enables image editing without the need for explicit masks. Users provide an image and a text prompt describing the desired change (e.g., "change the color"), and Imagen 3 intelligently applies the edits. |
| **Imagen Outpainting** | Extends the boundaries of an image beyond its original canvas, generating new content that seamlessly blends with the existing image based on a text prompt. |

# Pre-Requisite

### Installation
1. Navigate to your ComfyUI `custom_nodes` directory.
2. Add the contents of the `comfyui_custom_nodes` directory to the `custom_nodes` directory.
3. Set the values of PROJECT_ID and REGION in your environment variables or directly in the const.py file located at `src/vertexai_nodes/const.py`. These values are required for the nodes to function correctly.
---
### Step 1: Create a Google Cloud Project

Every Google Cloud resource belongs to a project. If you don't have one, create a new project:

1. Go to the [Google Cloud Console](https://console.cloud.google.com/).
2. In the top navigation bar, click the **project dropdown** (usually displaying "My First Project" or your current project name).
3. Click **"New Project"**.
4. Enter a **Project name** (e.g., `my-api-access-project`).
5. (Optional) Choose an **Organization** and **Location** if applicable.
6. Click **"Create"**.
7. Once the project is created, ensure it is selected in the project dropdown in the Cloud Console. Note down your **Project ID** (e.g., `my-api-access-project-123456`) as you'll need it later.

---
### Step 2: Enable Required Google Cloud APIs

For your application to use a specific Google Cloud service (e.g., Cloud Storage, Vertex AI, Gemini for Google Cloud API), you must enable its corresponding API in your project.

1. In the Google Cloud Console, navigate to the **Navigation menu (☰)** on the top left.
2. Go to **APIs & Services > Dashboard**.
3. Click **"+ ENABLE APIS AND SERVICES"** at the top.
4. In the API Library, search for the API you need (e.g., "Vertex AI API", "Gemini for Google Cloud API", "Cloud Storage API").
5. Click on the API you want to enable.
6. Click the **"Enable"** button.

---
### Step 3: Authenticate Your Local Environment

Google Cloud APIs require authentication to verify your application's identity and permissions. There are two primary methods for local development:

```python
PROJECT_ID = "your-project-id"
REGION = "your-region" # e.g., "us-central1"
#### Method 1: Google Cloud CLI (Recommended for Development)

This method uses your personal Google account credentials to authenticate your `gcloud` CLI, which then automatically provides **Application Default Credentials (ADC)** to your local applications. This is the simplest and most secure method for individual development.

1. **Ensure `gcloud` CLI is installed:** If not, follow the [installation guide](https://cloud.google.com/sdk/docs/install).
2. **Log in to your Google Account via `gcloud`:** Open your terminal or command prompt and run:

```bash
gcloud auth login
```
Alternatively, you can set these environment variables in your terminal before starting ComfyUI:

This command will open a browser window, prompting you to log in with your Google account. After successful login, you can close the browser.

3. **Set your Google Cloud Project:** Tell `gcloud` which project to use for subsequent commands and for ADC:

```bash
export PROJECT_ID="your-project-id"
export REGION="your-region" # e.g., "us-central1"
gcloud config set project YOUR_PROJECT_ID
```

Replace `YOUR_PROJECT_ID` with the actual ID of the project you created (e.g., `my-api-access-project-123456`).

4. **Verify Authentication:** You can verify your active account and project:

```bash
gcloud auth list
gcloud config list project
```

Now, any Google Cloud client library or application running in your local environment that uses Application Default Credentials will automatically use the authenticated `gcloud` credentials.

#### Method 2: Service Account Key File (Recommended for Production/CI/CD)

For production environments, CI/CD pipelines, or situations where you don't want to use a personal account, **service accounts** are the secure and recommended method. A service account is a special type of Google account that represents your application rather than an individual user.

**Caution:** Service account key files grant powerful access. Keep them secure and never commit them to version control.

**Create a Service Account:**

1. Go to the [Google Cloud Console](https://console.cloud.google.com/).
2. Navigate to the **Navigation menu (☰) > IAM & Admin > Service Accounts**.
3. Click **"+ CREATE SERVICE ACCOUNT"**.
4. Enter a **Service account name** (e.g., `my-local-api-sa`) and an optional description.
5. Click **"CREATE AND CONTINUE"**.

**Grant Permissions (Roles):**

1. In the "Grant this service account access to project" section, select the roles necessary for your application to interact with the required APIs. Grant the **principle of least privilege**: only give the permissions absolutely needed.
* For example, if you're using Cloud Storage, you might grant `Storage Object Admin` or `Storage Object Viewer`. If using Vertex AI, you might grant `Vertex AI User` or `Vertex AI Administrator`.
2. Click **"CONTINUE"**.

**Create a Key (JSON File):**

1. In the "Grant users access to this service account" section (optional, skip if not needed), click **"DONE"**.
2. On the Service Accounts page, find your newly created service account.
3. Click on the **three dots (⋮)** under the "Actions" column for your service account.
4. Select **"Manage keys"**.
5. Click **"ADD KEY" > "Create new key"**.
6. Choose **"JSON"** as the key type.
7. Click **"Create"**.
8. Your browser will download a JSON key file (e.g., `your-service-account-name-xxxx.json`). **Save this file in a secure location on your local machine.**

**Set the `GOOGLE_APPLICATION_CREDENTIALS` Environment Variable:**

Most Google Cloud client libraries automatically detect credentials if the `GOOGLE_APPLICATION_CREDENTIALS` environment variable is set to the path of your service account JSON key file.

* **Linux/macOS:**

```bash
export GOOGLE_APPLICATION_CREDENTIALS="/path/to/your-service-account-key.json"
```
**Important:** This environment variable is usually session-specific. For persistent access, you might add this line to your shell's profile file (e.g., `.bashrc`, `.zshrc`, `~/.profile` for Linux/macOS, or configure it in your IDE/system environment variables). However, for development, setting it per-session or through your IDE's run configurations is often safer.


### Installation
1. Navigate to your ComfyUI `custom_nodes` directory.
2. Add the contents of this repository to the `custom_nodes` directory.
3. Set configurations in `config.yaml` file. You can find these values in your Google Cloud Console.
4. Install the dependencies present in the `requirements.txt` file. You can do this by running the following command in your terminal:

```bash
pip install -r requirements.txt
```
5. **Restart the ComfyUI server.**

### Installation from git
1. Navigate to your ComfyUI `custom_nodes` directory.
2. Clone the git repo under `custom_nodes` directory.
3. **Restart the ComfyUI server.**

### Usage
After installing and restarting ComfyUI, you will find the "VertexAI" category added to the node menu.
10 changes: 5 additions & 5 deletions tools/comfyui_custom_nodes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

"""Init nodes"""

from .src.vertexai_nodes.nodes.flash import (
GeminiFlashNode
from .src.vertexai_nodes.nodes.gemini import (
GeminiNode
)

from .src.vertexai_nodes.nodes.imagen import (
Expand Down Expand Up @@ -49,7 +49,7 @@
)

from .src.vertexai_nodes.nodes.veo import (
Veo2Node,
VeoNode,
VideoPreviewNode,
ImageToBase64Node
)
Expand All @@ -60,8 +60,8 @@
# that will be displayed in the UI.
NODE_CLASS_MAPPINGS = {

"Gemini Flash 2.0": GeminiFlashNode,
"Veo 2 Video Generation": Veo2Node,
"Gemini": GeminiNode,
"Veo Video Generation": VeoNode,
"VideoPreviewNode": VideoPreviewNode,
"Image to B64 Node": ImageToBase64Node,

Expand Down
78 changes: 0 additions & 78 deletions tools/comfyui_custom_nodes/src/vertexai_nodes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,81 +11,3 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


from .nodes.flash import (
GeminiFlashNode
)

from .nodes.imagen_bg_swap import (
ProductBGSwapMaskNode,
ProductBGSwapAutoMaskNode
)

from .nodes.imagen_custom_endpoint import (
ImagenCustomEndpointNode
)

from .nodes.imagen_inpaint_insert import (
InpaintInsertMaskNode,
InpaintInsertAutoMaskNode,
InpaintInsertSemanticMaskNode
)

from .nodes.imagen_inpaint_remove import (
InpaintRemoveMaskNode,
InpaintRemoveAutoMaskNode,
InpaintRemoveSemanticMaskNode
)

from .nodes.imagen_maskfree_editing import (
MaskFreeEditNode
)

from .nodes.imagen_outpaint import (
OutpaintingNode
)

from .nodes.imagen import (
Imagen3Node
)

from .nodes.veo import (
Veo2Node,
VideoPreviewNode,
ImageToBase64Node
)


WEB_DIRECTORY = "./web"


NODE_CLASS_MAPPINGS = {

"Gemini Flash 2.0": GeminiFlashNode,
"Veo 2 Video Generation": Veo2Node,
"VideoPreviewNode": VideoPreviewNode,
"Image to B64 Node": ImageToBase64Node,

# Imagen3 nodes
"Image Generation 3": Imagen3Node,

"Inpaint Insert w Mask": InpaintInsertMaskNode,
"Inpaint Insert w AutoMask": InpaintInsertAutoMaskNode,
"Inpaint Insert w SemanticMask": InpaintInsertSemanticMaskNode,

"Inpaint Remove w Mask": InpaintRemoveMaskNode,
"Inpaint Remove w AutoMask": InpaintRemoveAutoMaskNode,
"Inpaint Remove w SemanticMask": InpaintRemoveSemanticMaskNode,

"Imagen Product Background Swap w Mask": ProductBGSwapMaskNode,
"Imagen Product Background Swap w AutoMask": ProductBGSwapAutoMaskNode,

"Imagen Mask-Free Editing": MaskFreeEditNode,

"Imagen Outpainting": OutpaintingNode,

"Imagen Custom Endpoint": ImagenCustomEndpointNode,
}

__all__ = ['NODE_CLASS_MAPPINGS', 'WEB_DIRECTORY']
48 changes: 48 additions & 0 deletions tools/comfyui_custom_nodes/src/vertexai_nodes/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Copyright 2025 Google LLC

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


PROJECT_ID: kr-ais-demo
REGION: us-central1
FLASH_MODELS:
- gemini-2.0-flash-001
- gemini-2.5-pro-preview-05-06
- gemini-2.5-flash-preview-04-17
IMAGEN3_EDIT_MODELS:
- imagen-3.0-capability-001
SAFETY_FILTER_LEVELS:
- BLOCK_LOW_AND_ABOVE
- BLOCK_MEDIUM_AND_ABOVE
- BLOCK_ONLY_HIGH
- BLOCK_NONE
PERSON_GENERATION_MODES:
- ALLOW_ADULT
- ALLOW_ALL
- DONT_ALLOW
AUTO_MASK_MODES:
- MASK_MODE_FOREGROUND
- MASK_MODE_BACKGROUND
IMAGEN3_GENERATION_MODELS:
- imagen-3.0-generate-002
VEO_MODELS:
- veo-2.0-generate-001
- veo-3.0-generate-preview
VEO_PERSON_GENERATION_MODES:
- allow_adult
- disallow
VEO_ASPECT_RATIOS:
- "16:9"
- "9:16"
IMAGEN_MODELS:
- imagen-3.0-generate-002
51 changes: 34 additions & 17 deletions tools/comfyui_custom_nodes/src/vertexai_nodes/modules/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,41 @@
"Consts used in custom nodes"

import os
from ast import literal_eval

# This will be replaced by Cloud Run service.yaml
from ..modules.utils import load_config_and_set_env_vars

PROJECT_ID = os.environ.get("PROJECT_ID")
REGION = os.environ.get("REGION", "us-central1")

FLASH_MODELS = ['gemini-2.0-flash-001']
IMAGEN_MODELS = ['imagen-3.0-generate-002']
IMAGEN3_EDIT_MODELS = ['imagen-3.0-capability-001']
IMAGEN3_GENERATION_MODELS = ['imagen-3.0-generate-002']
VEO_MODELS = ['veo-2.0-generate-001']

AUTO_MASK_MODES = ['MASK_MODE_FOREGROUND', 'MASK_MODE_BACKGROUND']
VEO_ASPECT_RATIOS = ["16:9", "9:16"]

PERSON_GENERATION_MODES = ['ALLOW_ADULT', 'ALLOW_ALL', 'DONT_ALLOW']
VEO_PERSON_GENERATION_MODES = ['allow_adult', 'disallow']

SAFETY_FILTER_LEVELS = ['BLOCK_LOW_AND_ABOVE', 'BLOCK_MEDIUM_AND_ABOVE',
'BLOCK_ONLY_HIGH', 'BLOCK_NONE']
# Check if PROJECT_ID or REGION are set as environment variables.
# If not, load the config.yaml file.
if (
not os.environ.get("PROJECT_ID")
or not os.environ.get("REGION")
or not os.environ.get("FLASH_MODELS")
):
config_path = os.path.join(os.path.dirname(__file__), '../config.yaml')
load_config_and_set_env_vars(config_path=config_path)

PROJECT_ID = os.environ.get("PROJECT_ID")
REGION = os.environ.get("REGION")
FLASH_MODELS = literal_eval(os.environ.get("FLASH_MODELS"))
IMAGEN3_EDIT_MODELS = literal_eval(os.environ.get("IMAGEN3_EDIT_MODELS"))
SAFETY_FILTER_LEVELS = literal_eval(os.environ.get("SAFETY_FILTER_LEVELS"))
PERSON_GENERATION_MODES = literal_eval(
os.environ.get("PERSON_GENERATION_MODES")
)
AUTO_MASK_MODES = literal_eval(os.environ.get("AUTO_MASK_MODES"))
IMAGEN3_GENERATION_MODELS = literal_eval(
os.environ.get("IMAGEN3_GENERATION_MODELS")
)
VEO_MODELS = literal_eval(os.environ.get("VEO_MODELS"))
VEO_PERSON_GENERATION_MODES = literal_eval(
os.environ.get("VEO_PERSON_GENERATION_MODES")
)
VEO_ASPECT_RATIOS = literal_eval(
os.environ.get("VEO_ASPECT_RATIOS")
)
IMAGEN_MODELS = literal_eval(
os.environ.get("IMAGEN_MODELS")
)
INPAINT_CATEGORY = "VertexAI/Imagen3/Inpainting"
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.


"Utils used in custom nodes"

import logging
Expand Down
Loading