Initial commit from comfyui-PD_comfy api node
This commit is contained in:
@@ -0,0 +1,312 @@
|
||||
"""
|
||||
PD: OpenAI GPT Image Node (ComfyUI API Key)
|
||||
使用 ComfyUI API Key 调用 ComfyUI 代理 API
|
||||
参数和官方节点完全一样
|
||||
"""
|
||||
|
||||
import torch
|
||||
import requests
|
||||
import time
|
||||
import io
|
||||
import base64
|
||||
import numpy as np
|
||||
from PIL import Image
|
||||
|
||||
|
||||
def tensor_to_bytes(tensor, max_pixels=2048*2048):
|
||||
"""将 Tensor 转换为 PNG BytesIO,并进行下采样"""
|
||||
if tensor.ndim == 4:
|
||||
tensor = tensor[0]
|
||||
|
||||
# 下采样大图片
|
||||
height, width = tensor.shape[:2]
|
||||
current_pixels = height * width
|
||||
if current_pixels > max_pixels:
|
||||
scale = (max_pixels / current_pixels) ** 0.5
|
||||
new_height = int(height * scale)
|
||||
new_width = int(width * scale)
|
||||
|
||||
# 使用 PIL 进行高质量缩放
|
||||
array = np.clip(255. * tensor.cpu().numpy(), 0, 255).astype(np.uint8)
|
||||
image = Image.fromarray(array)
|
||||
image = image.resize((new_width, new_height), Image.Resampling.LANCZOS)
|
||||
else:
|
||||
array = np.clip(255. * tensor.cpu().numpy(), 0, 255).astype(np.uint8)
|
||||
image = Image.fromarray(array)
|
||||
|
||||
buffered = io.BytesIO()
|
||||
image.save(buffered, format="PNG")
|
||||
buffered.seek(0)
|
||||
return buffered
|
||||
|
||||
|
||||
def load_image_to_tensor(image_data):
|
||||
"""将图片数据(URL 或 bytes)转换为 Tensor"""
|
||||
try:
|
||||
if isinstance(image_data, str) and image_data.startswith("http"):
|
||||
response = requests.get(image_data, timeout=30)
|
||||
response.raise_for_status()
|
||||
img = Image.open(io.BytesIO(response.content))
|
||||
else:
|
||||
img = Image.open(io.BytesIO(image_data))
|
||||
|
||||
img = img.convert("RGB")
|
||||
image_np = np.array(img).astype(np.float32) / 255.0
|
||||
return torch.from_numpy(image_np).unsqueeze(0)
|
||||
except Exception as e:
|
||||
print(f"[IMAGE_GPT_APIKEY] Error loading image: {e}")
|
||||
return torch.zeros((1, 512, 512, 3))
|
||||
|
||||
|
||||
class PDOpenAIGPTImageAPIKey:
|
||||
"""
|
||||
PD: OpenAI GPT Image (API Key)
|
||||
使用 ComfyUI API Key 调用 ComfyUI 代理 API
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
"required": {
|
||||
"api_key": ("STRING", {
|
||||
"default": "",
|
||||
"multiline": False,
|
||||
"placeholder": "Your ComfyUI API Key"
|
||||
}),
|
||||
"prompt": ("STRING", {
|
||||
"multiline": True,
|
||||
"default": "",
|
||||
"tooltip": "Text prompt for GPT Image"
|
||||
}),
|
||||
"seed": ("INT", {
|
||||
"default": 0,
|
||||
"min": 0,
|
||||
"max": 2**31 - 1,
|
||||
"step": 1,
|
||||
"tooltip": "not implemented yet in backend"
|
||||
}),
|
||||
"quality": (["low", "medium", "high"], {
|
||||
"default": "low",
|
||||
"tooltip": "Image quality, affects cost and generation time"
|
||||
}),
|
||||
"background": (["auto", "opaque", "transparent"], {
|
||||
"default": "auto",
|
||||
"tooltip": "Return image with or without background"
|
||||
}),
|
||||
"size": (["auto", "1024x1024", "1024x1536", "1536x1024"], {
|
||||
"default": "auto",
|
||||
"tooltip": "Image size"
|
||||
}),
|
||||
"n": ("INT", {
|
||||
"default": 1,
|
||||
"min": 1,
|
||||
"max": 8,
|
||||
"step": 1,
|
||||
"tooltip": "How many images to generate"
|
||||
}),
|
||||
},
|
||||
"optional": {
|
||||
"image": ("IMAGE", {
|
||||
"tooltip": "Optional reference image for image editing."
|
||||
}),
|
||||
"mask": ("MASK", {
|
||||
"tooltip": "Optional mask for inpainting (white areas will be replaced)"
|
||||
}),
|
||||
"model": (["gpt-image-1", "gpt-image-1.5"], {
|
||||
"default": "gpt-image-1"
|
||||
}),
|
||||
},
|
||||
"hidden": {
|
||||
"unique_id": "UNIQUE_ID",
|
||||
}
|
||||
}
|
||||
|
||||
RETURN_TYPES = ("IMAGE", "STRING")
|
||||
RETURN_NAMES = ("image", "info")
|
||||
FUNCTION = "generate_image"
|
||||
CATEGORY = "PD_Tools/Image_Generation"
|
||||
|
||||
def generate_image(self, api_key, prompt, seed, quality, background, size, n, unique_id, image=None, mask=None, model="gpt-image-1"):
|
||||
"""生成或编辑图片"""
|
||||
|
||||
# 验证 API Key
|
||||
if not api_key or not api_key.strip():
|
||||
error_msg = "Error: ComfyUI API Key is required"
|
||||
print(f"[IMAGE_GPT_APIKEY] {error_msg}")
|
||||
return (torch.zeros((1, 512, 512, 3)), error_msg)
|
||||
|
||||
# 清理 API Key
|
||||
api_key = api_key.strip()
|
||||
|
||||
# 判断模式:编辑 vs 生成
|
||||
is_edit_mode = image is not None
|
||||
|
||||
# ComfyUI 代理 API URL
|
||||
if is_edit_mode:
|
||||
base_url = "https://api.comfy.org/proxy/openai/images/edits"
|
||||
else:
|
||||
base_url = "https://api.comfy.org/proxy/openai/images/generations"
|
||||
|
||||
start_time = time.time()
|
||||
|
||||
try:
|
||||
if is_edit_mode:
|
||||
# 图片编辑模式 - 使用 multipart/form-data
|
||||
# 准备文件
|
||||
files = {}
|
||||
img_bytes = tensor_to_bytes(image)
|
||||
files['image'] = ('image.png', img_bytes, 'image/png')
|
||||
|
||||
# 处理 mask
|
||||
if mask is not None:
|
||||
m_tensor = mask
|
||||
if m_tensor.ndim == 3:
|
||||
m_tensor = m_tensor[0]
|
||||
|
||||
mask_h, mask_w = m_tensor.shape
|
||||
rgba = np.zeros((mask_h, mask_w, 4), dtype=np.uint8)
|
||||
|
||||
mask_np = m_tensor.cpu().numpy()
|
||||
alpha = ((1.0 - mask_np) * 255).astype(np.uint8)
|
||||
rgba[..., 3] = alpha
|
||||
|
||||
mask_pil = Image.fromarray(rgba)
|
||||
mask_buffer = io.BytesIO()
|
||||
mask_pil.save(mask_buffer, format="PNG")
|
||||
mask_buffer.seek(0)
|
||||
files['mask'] = ('mask.png', mask_buffer, 'image/png')
|
||||
|
||||
# 表单数据 - multipart/form-data 模式下所有值都应该是字符串
|
||||
data = {
|
||||
"model": model,
|
||||
"prompt": prompt,
|
||||
"n": str(n),
|
||||
"size": size,
|
||||
"quality": quality,
|
||||
"background": background,
|
||||
"moderation": "low"
|
||||
}
|
||||
|
||||
# 请求头
|
||||
headers = {
|
||||
"X-API-KEY": api_key,
|
||||
"Accept": "application/json",
|
||||
"User-Agent": "ComfyUI-PD-Node/2.0"
|
||||
}
|
||||
|
||||
print(f"[IMAGE_GPT_APIKEY] Sending edit request")
|
||||
print(f"[IMAGE_GPT_APIKEY] Model: {model}, Size: {size}, Quality: {quality}")
|
||||
print(f"[IMAGE_GPT_APIKEY] Image size: {image.shape}, Mask: {'Yes' if mask is not None else 'No'}")
|
||||
|
||||
response = requests.post(base_url, headers=headers, files=files, data=data, timeout=300)
|
||||
|
||||
else:
|
||||
# 文生图模式 - 使用 JSON
|
||||
payload = {
|
||||
"model": model,
|
||||
"prompt": prompt,
|
||||
"n": n,
|
||||
"size": size,
|
||||
"quality": quality,
|
||||
"background": background,
|
||||
"response_format": "url"
|
||||
}
|
||||
|
||||
headers = {
|
||||
"X-API-KEY": api_key,
|
||||
"Accept": "application/json",
|
||||
"Content-Type": "application/json",
|
||||
"User-Agent": "ComfyUI-PD-Node/2.0"
|
||||
}
|
||||
|
||||
print(f"[IMAGE_GPT_APIKEY] Sending generation request")
|
||||
print(f"[IMAGE_GPT_APIKEY] Model: {model}, Size: {size}, Quality: {quality}")
|
||||
print(f"[IMAGE_GPT_APIKEY] Prompt length: {len(prompt)} chars")
|
||||
|
||||
response = requests.post(base_url, headers=headers, json=payload, timeout=300)
|
||||
|
||||
# 检查响应
|
||||
if response.status_code != 200:
|
||||
error_msg = f"API Error {response.status_code}: {response.text[:200]}"
|
||||
print(f"[IMAGE_GPT_APIKEY] {error_msg}")
|
||||
return (torch.zeros((1, 512, 512, 3)), error_msg)
|
||||
|
||||
result = response.json()
|
||||
data_list = result.get("data", [])
|
||||
|
||||
if not data_list:
|
||||
error_msg = "Error: No image data in response"
|
||||
print(f"[IMAGE_GPT_APIKEY] {error_msg}")
|
||||
return (torch.zeros((1, 512, 512, 3)), error_msg)
|
||||
|
||||
# 提取图片
|
||||
image_tensors = []
|
||||
for item in data_list:
|
||||
if "url" in item:
|
||||
img_tensor = load_image_to_tensor(item["url"])
|
||||
image_tensors.append(img_tensor)
|
||||
elif "b64_json" in item:
|
||||
img_data = base64.b64decode(item["b64_json"])
|
||||
img_tensor = load_image_to_tensor(img_data)
|
||||
image_tensors.append(img_tensor)
|
||||
|
||||
if not image_tensors:
|
||||
error_msg = "Error: Failed to process output images"
|
||||
print(f"[IMAGE_GPT_APIKEY] {error_msg}")
|
||||
return (torch.zeros((1, 512, 512, 3)), error_msg)
|
||||
|
||||
final_image = torch.cat(image_tensors, dim=0)
|
||||
|
||||
# 计算耗时和成本
|
||||
end_time = time.time()
|
||||
duration = end_time - start_time
|
||||
|
||||
# 估算成本
|
||||
cost_map = {
|
||||
"gpt-image-1": {"low": 0.02, "medium": 0.04, "high": 0.08},
|
||||
"gpt-image-1.5": {"low": 0.03, "medium": 0.06, "high": 0.12}
|
||||
}
|
||||
cost = cost_map.get(model, {}).get(quality, 0.04)
|
||||
total_cost = cost * n
|
||||
|
||||
info_str = (
|
||||
f"Model: {model}\n"
|
||||
f"Mode: {'Edit' if is_edit_mode else 'Generate'}\n"
|
||||
f"Status: Success\n"
|
||||
f"Time: {duration:.2f}s\n"
|
||||
f"Images: {len(image_tensors)}\n"
|
||||
f"Size: {size}\n"
|
||||
f"Quality: {quality}\n"
|
||||
f"Background: {background}\n"
|
||||
f"Est. Cost: ${total_cost:.3f}"
|
||||
)
|
||||
|
||||
print(f"[IMAGE_GPT_APIKEY] Success! Generated {len(image_tensors)} images in {duration:.2f}s")
|
||||
return (final_image, info_str)
|
||||
|
||||
except requests.exceptions.Timeout:
|
||||
error_msg = "Error: Request timeout (300s)"
|
||||
print(f"[IMAGE_GPT_APIKEY] {error_msg}")
|
||||
return (torch.zeros((1, 512, 512, 3)), error_msg)
|
||||
|
||||
except requests.exceptions.ConnectionError as e:
|
||||
error_msg = f"Error: Connection failed - {str(e)}"
|
||||
print(f"[IMAGE_GPT_APIKEY] {error_msg}")
|
||||
return (torch.zeros((1, 512, 512, 3)), error_msg)
|
||||
|
||||
except Exception as e:
|
||||
error_msg = f"Error: {str(e)}"
|
||||
print(f"[IMAGE_GPT_APIKEY] {error_msg}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return (torch.zeros((1, 512, 512, 3)), error_msg)
|
||||
|
||||
|
||||
# 节点注册
|
||||
NODE_CLASS_MAPPINGS = {
|
||||
"PDOpenAIGPTImageAPIKey": PDOpenAIGPTImageAPIKey
|
||||
}
|
||||
|
||||
NODE_DISPLAY_NAME_MAPPINGS = {
|
||||
"PDOpenAIGPTImageAPIKey": "PD: GPT Image (apikey)"
|
||||
}
|
||||
@@ -0,0 +1,313 @@
|
||||
"""
|
||||
PD: OpenAI GPT Image Node (ComfyUI Auth Token)
|
||||
基于 gemini_image_api 的 Auth Token 认证方式
|
||||
支持文生图和图片编辑
|
||||
"""
|
||||
|
||||
import torch
|
||||
import requests
|
||||
import time
|
||||
import io
|
||||
import base64
|
||||
import numpy as np
|
||||
from PIL import Image
|
||||
|
||||
|
||||
def tensor_to_bytes(tensor, max_pixels=2048*2048):
|
||||
"""将 Tensor 转换为 PNG BytesIO,并进行下采样"""
|
||||
if tensor.ndim == 4:
|
||||
tensor = tensor[0]
|
||||
|
||||
# 下采样大图片
|
||||
height, width = tensor.shape[:2]
|
||||
current_pixels = height * width
|
||||
if current_pixels > max_pixels:
|
||||
scale = (max_pixels / current_pixels) ** 0.5
|
||||
new_height = int(height * scale)
|
||||
new_width = int(width * scale)
|
||||
|
||||
# 使用 PIL 进行高质量缩放
|
||||
array = np.clip(255. * tensor.cpu().numpy(), 0, 255).astype(np.uint8)
|
||||
image = Image.fromarray(array)
|
||||
image = image.resize((new_width, new_height), Image.Resampling.LANCZOS)
|
||||
else:
|
||||
array = np.clip(255. * tensor.cpu().numpy(), 0, 255).astype(np.uint8)
|
||||
image = Image.fromarray(array)
|
||||
|
||||
buffered = io.BytesIO()
|
||||
image.save(buffered, format="PNG")
|
||||
buffered.seek(0)
|
||||
return buffered
|
||||
|
||||
|
||||
def load_image_to_tensor(image_data):
|
||||
"""将图片数据(URL 或 bytes)转换为 Tensor"""
|
||||
try:
|
||||
if isinstance(image_data, str) and image_data.startswith("http"):
|
||||
response = requests.get(image_data, timeout=30)
|
||||
response.raise_for_status()
|
||||
img = Image.open(io.BytesIO(response.content))
|
||||
else:
|
||||
img = Image.open(io.BytesIO(image_data))
|
||||
|
||||
img = img.convert("RGB")
|
||||
image_np = np.array(img).astype(np.float32) / 255.0
|
||||
return torch.from_numpy(image_np).unsqueeze(0)
|
||||
except Exception as e:
|
||||
print(f"[IMAGE_GPT_AUTHTOKEN] Error loading image: {e}")
|
||||
return torch.zeros((1, 512, 512, 3))
|
||||
|
||||
|
||||
class PDOpenAIGPTImageAuthToken:
|
||||
"""
|
||||
PD: OpenAI GPT Image (Auth Token)
|
||||
使用 ComfyUI Auth Token 调用 GPT Image API
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
"required": {
|
||||
"auth_token": ("STRING", {
|
||||
"default": "",
|
||||
"multiline": False,
|
||||
"placeholder": "Paste your Auth Token here"
|
||||
}),
|
||||
"prompt": ("STRING", {
|
||||
"multiline": True,
|
||||
"default": "",
|
||||
"tooltip": "Describe what you want to generate or edit"
|
||||
}),
|
||||
"model": (["gpt-image-1", "gpt-image-1.5"], {
|
||||
"default": "gpt-image-1",
|
||||
"tooltip": "GPT Image model version"
|
||||
}),
|
||||
"quality": (["low", "medium", "high"], {
|
||||
"default": "low",
|
||||
"tooltip": "Image quality, affects cost and generation time"
|
||||
}),
|
||||
"background": (["auto", "opaque", "transparent"], {
|
||||
"default": "auto",
|
||||
"tooltip": "Background transparency (GPT Image only)"
|
||||
}),
|
||||
"size": (["auto", "1024x1024", "1024x1536", "1536x1024"], {
|
||||
"default": "auto",
|
||||
"tooltip": "Image size"
|
||||
}),
|
||||
"n": ("INT", {
|
||||
"default": 1,
|
||||
"min": 1,
|
||||
"max": 8,
|
||||
"step": 1,
|
||||
"tooltip": "Number of images to generate"
|
||||
}),
|
||||
"seed": ("INT", {
|
||||
"default": 0,
|
||||
"min": 0,
|
||||
"max": 0xFFFFFFFFFFFFFFFF
|
||||
}),
|
||||
},
|
||||
"optional": {
|
||||
"image": ("IMAGE", {"default": None, "tooltip": "Input image for editing"}),
|
||||
"mask": ("MASK", {"default": None, "tooltip": "Mask for inpainting"}),
|
||||
},
|
||||
"hidden": {
|
||||
"unique_id": "UNIQUE_ID",
|
||||
}
|
||||
}
|
||||
|
||||
RETURN_TYPES = ("IMAGE", "STRING")
|
||||
RETURN_NAMES = ("image", "info")
|
||||
FUNCTION = "generate_image"
|
||||
CATEGORY = "PD_Tools/Image_Generation"
|
||||
|
||||
def generate_image(self, auth_token, prompt, model, quality, background, size, n, seed, unique_id, image=None, mask=None):
|
||||
"""生成或编辑图片"""
|
||||
|
||||
# 验证 Token
|
||||
if not auth_token or not auth_token.strip():
|
||||
error_msg = "Error: Auth Token is required"
|
||||
print(f"[IMAGE_GPT_AUTHTOKEN] {error_msg}")
|
||||
return (torch.zeros((1, 512, 512, 3)), error_msg)
|
||||
|
||||
# 清理 Token
|
||||
auth_token = auth_token.strip()
|
||||
if auth_token.lower().startswith("bearer "):
|
||||
auth_token = auth_token[7:].strip()
|
||||
|
||||
# API 基础地址
|
||||
base_url = "https://api.comfy.org"
|
||||
|
||||
# 判断模式:编辑 vs 生成
|
||||
is_edit_mode = image is not None
|
||||
|
||||
start_time = time.time()
|
||||
|
||||
try:
|
||||
if is_edit_mode:
|
||||
# 图片编辑模式 - 使用 multipart/form-data
|
||||
url = f"{base_url}/proxy/openai/images/edits"
|
||||
|
||||
# 准备文件
|
||||
files = {}
|
||||
img_bytes = tensor_to_bytes(image)
|
||||
files['image'] = ('image.png', img_bytes, 'image/png')
|
||||
|
||||
# 处理 mask
|
||||
if mask is not None:
|
||||
m_tensor = mask
|
||||
if m_tensor.ndim == 3:
|
||||
m_tensor = m_tensor[0]
|
||||
|
||||
mask_h, mask_w = m_tensor.shape
|
||||
rgba = np.zeros((mask_h, mask_w, 4), dtype=np.uint8)
|
||||
|
||||
mask_np = m_tensor.cpu().numpy()
|
||||
alpha = ((1.0 - mask_np) * 255).astype(np.uint8)
|
||||
rgba[..., 3] = alpha
|
||||
|
||||
mask_pil = Image.fromarray(rgba)
|
||||
mask_buffer = io.BytesIO()
|
||||
mask_pil.save(mask_buffer, format="PNG")
|
||||
mask_buffer.seek(0)
|
||||
files['mask'] = ('mask.png', mask_buffer, 'image/png')
|
||||
|
||||
# 表单数据 - multipart/form-data 模式下所有值都应该是字符串
|
||||
data = {
|
||||
"model": model,
|
||||
"prompt": prompt,
|
||||
"n": str(n),
|
||||
"size": size,
|
||||
"quality": quality,
|
||||
"background": background,
|
||||
"moderation": "low"
|
||||
}
|
||||
|
||||
# 请求头
|
||||
headers = {
|
||||
"Authorization": f"Bearer {auth_token}",
|
||||
"User-Agent": "ComfyUI-PD-Node/2.0"
|
||||
}
|
||||
|
||||
print(f"[IMAGE_GPT_AUTHTOKEN] Sending edit request to {url}")
|
||||
print(f"[IMAGE_GPT_AUTHTOKEN] Model: {model}, Size: {size}, Quality: {quality}, Background: {background}")
|
||||
print(f"[IMAGE_GPT_AUTHTOKEN] Image size: {image.shape}, Mask: {'Yes' if mask is not None else 'No'}")
|
||||
|
||||
response = requests.post(url, headers=headers, files=files, data=data, timeout=300)
|
||||
|
||||
else:
|
||||
# 文生图模式 - 使用 JSON
|
||||
url = f"{base_url}/proxy/openai/images/generations"
|
||||
|
||||
payload = {
|
||||
"model": model,
|
||||
"prompt": prompt,
|
||||
"n": n,
|
||||
"size": size,
|
||||
"quality": quality,
|
||||
"background": background,
|
||||
"moderation": "low",
|
||||
"response_format": "url"
|
||||
}
|
||||
|
||||
headers = {
|
||||
"Authorization": f"Bearer {auth_token}",
|
||||
"Content-Type": "application/json",
|
||||
"User-Agent": "ComfyUI-PD-Node/2.0"
|
||||
}
|
||||
|
||||
print(f"[IMAGE_GPT_AUTHTOKEN] Sending generation request to {url}")
|
||||
print(f"[IMAGE_GPT_AUTHTOKEN] Model: {model}, Size: {size}, Quality: {quality}, Background: {background}")
|
||||
print(f"[IMAGE_GPT_AUTHTOKEN] Prompt length: {len(prompt)} chars")
|
||||
|
||||
response = requests.post(url, headers=headers, json=payload, timeout=300)
|
||||
|
||||
# 检查响应
|
||||
if response.status_code != 200:
|
||||
error_msg = f"API Error {response.status_code}: {response.text[:200]}"
|
||||
print(f"[IMAGE_GPT_AUTHTOKEN] {error_msg}")
|
||||
return (torch.zeros((1, 512, 512, 3)), error_msg)
|
||||
|
||||
result = response.json()
|
||||
data_list = result.get("data", [])
|
||||
|
||||
if not data_list:
|
||||
error_msg = "Error: No image data in response"
|
||||
print(f"[IMAGE_GPT_AUTHTOKEN] {error_msg}")
|
||||
return (torch.zeros((1, 512, 512, 3)), error_msg)
|
||||
|
||||
# 提取图片
|
||||
image_tensors = []
|
||||
for item in data_list:
|
||||
if "url" in item:
|
||||
img_tensor = load_image_to_tensor(item["url"])
|
||||
image_tensors.append(img_tensor)
|
||||
elif "b64_json" in item:
|
||||
img_data = base64.b64decode(item["b64_json"])
|
||||
img_tensor = load_image_to_tensor(img_data)
|
||||
image_tensors.append(img_tensor)
|
||||
|
||||
if not image_tensors:
|
||||
error_msg = "Error: Failed to process output images"
|
||||
print(f"[IMAGE_GPT_AUTHTOKEN] {error_msg}")
|
||||
return (torch.zeros((1, 512, 512, 3)), error_msg)
|
||||
|
||||
final_image = torch.cat(image_tensors, dim=0)
|
||||
|
||||
# 计算耗时和成本
|
||||
end_time = time.time()
|
||||
duration = end_time - start_time
|
||||
|
||||
# 估算成本
|
||||
if model == "gpt-image-1":
|
||||
# GPT Image 1: $10/1M input tokens, $40/1M output tokens
|
||||
# 估算:约 1000 tokens input, 2000 tokens output per image
|
||||
cost = (1000 * 10 + 2000 * 40) / 1_000_000
|
||||
elif model == "gpt-image-1.5":
|
||||
# GPT Image 1.5: $8/1M input tokens, $32/1M output tokens
|
||||
cost = (1000 * 8 + 2000 * 32) / 1_000_000
|
||||
else:
|
||||
cost = 0.10 # 默认估算
|
||||
total_cost = cost * n
|
||||
|
||||
info_str = (
|
||||
f"Model: {model}\n"
|
||||
f"Mode: {'Edit' if is_edit_mode else 'Generate'}\n"
|
||||
f"Status: Success\n"
|
||||
f"Time: {duration:.2f}s\n"
|
||||
f"Images: {len(image_tensors)}\n"
|
||||
f"Size: {size}\n"
|
||||
f"Quality: {quality}\n"
|
||||
f"Background: {background}\n"
|
||||
f"Est. Cost: ${total_cost:.4f}"
|
||||
)
|
||||
|
||||
print(f"[IMAGE_GPT_AUTHTOKEN] Success! Generated {len(image_tensors)} images in {duration:.2f}s")
|
||||
return (final_image, info_str)
|
||||
|
||||
except requests.exceptions.Timeout:
|
||||
error_msg = "Error: Request timeout (300s)"
|
||||
print(f"[IMAGE_GPT_AUTHTOKEN] {error_msg}")
|
||||
return (torch.zeros((1, 512, 512, 3)), error_msg)
|
||||
|
||||
except requests.exceptions.ConnectionError as e:
|
||||
error_msg = f"Error: Connection failed - {str(e)}"
|
||||
print(f"[IMAGE_GPT_AUTHTOKEN] {error_msg}")
|
||||
return (torch.zeros((1, 512, 512, 3)), error_msg)
|
||||
|
||||
except Exception as e:
|
||||
error_msg = f"Error: {str(e)}"
|
||||
print(f"[IMAGE_GPT_AUTHTOKEN] {error_msg}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return (torch.zeros((1, 512, 512, 3)), error_msg)
|
||||
|
||||
|
||||
# 节点注册
|
||||
NODE_CLASS_MAPPINGS = {
|
||||
"PDOpenAIGPTImageAuthToken": PDOpenAIGPTImageAuthToken
|
||||
}
|
||||
|
||||
NODE_DISPLAY_NAME_MAPPINGS = {
|
||||
"PDOpenAIGPTImageAuthToken": "PD: GPT Image (comfyui_AuthToken)"
|
||||
}
|
||||
@@ -0,0 +1,279 @@
|
||||
"""
|
||||
PD: Nano Banana 2 (ComfyUI Key)
|
||||
基于 Google Gemini 3.1 Flash Image 模型的图像生成/编辑节点
|
||||
支持进度反馈、Token 成本计算、及其独立输出接口
|
||||
"""
|
||||
|
||||
import torch
|
||||
import base64
|
||||
import json
|
||||
import time
|
||||
import uuid
|
||||
import requests
|
||||
import numpy as np
|
||||
import io
|
||||
from PIL import Image
|
||||
from typing import Optional, List
|
||||
from server import PromptServer
|
||||
import comfy.utils
|
||||
|
||||
# 默认系统提示词
|
||||
GEMINI_IMAGE_SYS_PROMPT = (
|
||||
"You are an expert image-generation engine. You must ALWAYS produce an image.\n"
|
||||
"Interpret all user input—regardless of "
|
||||
"format, intent, or abstraction—as literal visual directives for image composition.\n"
|
||||
"If a prompt is conversational or lacks specific visual details, "
|
||||
"you must creatively invent a concrete visual scenario that depicts the concept.\n"
|
||||
"Prioritize generating the visual representation above any text, formatting, or conversational requests."
|
||||
)
|
||||
|
||||
def tensor_to_base64(tensor):
|
||||
"""将 Tensor 转换为 base64 字符串"""
|
||||
if tensor.ndim == 4:
|
||||
tensor = tensor[0]
|
||||
|
||||
array = np.clip(255. * tensor.cpu().numpy(), 0, 255).astype(np.uint8)
|
||||
image = Image.fromarray(array)
|
||||
buffered = io.BytesIO()
|
||||
image.save(buffered, format="PNG")
|
||||
return base64.b64encode(buffered.getvalue()).decode("utf-8")
|
||||
|
||||
def bytes_to_tensor(data):
|
||||
"""将图片字节数据转换为 Tensor"""
|
||||
try:
|
||||
img = Image.open(io.BytesIO(data)).convert("RGB")
|
||||
image_np = np.array(img).astype(np.float32) / 255.0
|
||||
return torch.from_numpy(image_np).unsqueeze(0)
|
||||
except Exception as e:
|
||||
print(f"[PD_NB2] Image decode error: {e}")
|
||||
return torch.zeros((1, 512, 512, 3))
|
||||
|
||||
class PDGeminiNanoBanana2:
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
"required": {
|
||||
"comfy_api_key": ("STRING", {
|
||||
"default": "",
|
||||
"multiline": False,
|
||||
"placeholder": "Paste your ComfyUI API Key here"
|
||||
}),
|
||||
"prompt": ("STRING", {
|
||||
"multiline": True,
|
||||
"default": "A futuristic cityscape with flying cars and neon lights",
|
||||
}),
|
||||
"model": (["Nano Banana 2 (Gemini 3.1 Flash Image)"], {
|
||||
"default": "Nano Banana 2 (Gemini 3.1 Flash Image)"
|
||||
}),
|
||||
"seed": ("INT", {
|
||||
"default": 42,
|
||||
"min": 0,
|
||||
"max": 0x7FFFFFFF,
|
||||
}),
|
||||
"aspect_ratio": (["auto", "1:1", "2:3", "3:2", "3:4", "4:3", "4:5", "5:4", "9:16", "16:9", "21:9"], {
|
||||
"default": "auto",
|
||||
}),
|
||||
"resolution": (["1K", "2K", "4K"], {
|
||||
"default": "1K",
|
||||
}),
|
||||
"response_modalities": (["IMAGE", "IMAGE+TEXT"], {
|
||||
"default": "IMAGE",
|
||||
}),
|
||||
"thinking_level": (["MINIMAL", "HIGH"], {
|
||||
"default": "MINIMAL"
|
||||
}),
|
||||
},
|
||||
"optional": {
|
||||
"images": ("IMAGE", ),
|
||||
"files": ("GEMINI_INPUT_FILES", ),
|
||||
"system_prompt": ("STRING", {
|
||||
"multiline": True,
|
||||
"default": GEMINI_IMAGE_SYS_PROMPT,
|
||||
}),
|
||||
},
|
||||
"hidden": {
|
||||
"unique_id": "UNIQUE_ID",
|
||||
}
|
||||
}
|
||||
|
||||
RETURN_TYPES = ("IMAGE", "STRING", "STRING")
|
||||
RETURN_NAMES = ("image", "info", "price")
|
||||
FUNCTION = "generate_image"
|
||||
CATEGORY = "PD_Tools/Image_Generation"
|
||||
|
||||
def generate_image(self, comfy_api_key, prompt, model, seed, aspect_ratio, resolution, response_modalities, thinking_level, unique_id, images=None, files=None, system_prompt=""):
|
||||
pbar = comfy.utils.ProgressBar(100)
|
||||
pbar.update(10)
|
||||
|
||||
if not comfy_api_key or not comfy_api_key.strip():
|
||||
return (torch.zeros((1, 512, 512, 3)), "Error: Missing ComfyUI API Key", "$0.00")
|
||||
|
||||
api_key = comfy_api_key.strip()
|
||||
api_model = "gemini-3.1-flash-image-preview"
|
||||
|
||||
# 发送进度文字到前端
|
||||
self._send_status(unique_id, "Preparing tokens...")
|
||||
|
||||
# 构建请求内容
|
||||
parts = [{"text": prompt}]
|
||||
|
||||
if images is not None:
|
||||
for i in range(min(images.shape[0], 14)):
|
||||
b64_str = tensor_to_base64(images[i])
|
||||
parts.append({
|
||||
"inlineData": {
|
||||
"mimeType": "image/png",
|
||||
"data": b64_str
|
||||
}
|
||||
})
|
||||
|
||||
# Files 输入处理
|
||||
if files:
|
||||
for file_part in files:
|
||||
if hasattr(file_part, "model_dump"):
|
||||
parts.append(file_part.model_dump(exclude_none=True))
|
||||
else:
|
||||
parts.append(file_part)
|
||||
|
||||
# 核心参数修正:匹配官方 Pydantic 模型的 camelCase 结构
|
||||
# 且不发送 seed (官方 GeminiNanoBanana2.execute 并没有发送 seed 到 API)
|
||||
image_config = {
|
||||
"imageSize": resolution,
|
||||
"imageOutputOptions": {"mimeType": "image/png"}
|
||||
}
|
||||
if aspect_ratio != "auto":
|
||||
image_config["aspectRatio"] = aspect_ratio
|
||||
|
||||
generation_config = {
|
||||
"responseModalities": (["IMAGE"] if response_modalities == "IMAGE" else ["TEXT", "IMAGE"]),
|
||||
"imageConfig": image_config,
|
||||
"thinkingConfig": {"thinkingLevel": thinking_level}
|
||||
}
|
||||
|
||||
payload = {
|
||||
"contents": [{
|
||||
"role": "user",
|
||||
"parts": parts
|
||||
}],
|
||||
"generationConfig": generation_config
|
||||
}
|
||||
|
||||
if system_prompt and system_prompt.strip():
|
||||
payload["systemInstruction"] = {
|
||||
"parts": [{"text": system_prompt.strip()}]
|
||||
}
|
||||
|
||||
headers = {
|
||||
"X-API-KEY": api_key,
|
||||
"Accept": "application/json",
|
||||
"Content-Type": "application/json",
|
||||
"User-Agent": "ComfyUI-PD-Node/2.0"
|
||||
}
|
||||
|
||||
base_url = f"https://api.comfy.org/proxy/vertexai/gemini/{api_model}"
|
||||
|
||||
self._send_status(unique_id, "Sending request to Google...")
|
||||
pbar.update(30)
|
||||
|
||||
start_time = time.time()
|
||||
try:
|
||||
# 打印发送的 Payload 调试 (可选)
|
||||
# print(f"[PD_NB2] Payload: {json.dumps(payload)}")
|
||||
|
||||
response = requests.post(base_url, headers=headers, json=payload, timeout=120)
|
||||
pbar.update(80)
|
||||
|
||||
if response.status_code != 200:
|
||||
error_body = response.text
|
||||
error_msg = f"API Error {response.status_code}: {error_body}"
|
||||
self._send_status(unique_id, "Error occurred")
|
||||
return (torch.zeros((1, 512, 512, 3)), error_msg, "N/A")
|
||||
|
||||
result = response.json()
|
||||
image_tensors = []
|
||||
text_parts = []
|
||||
|
||||
self._send_status(unique_id, "Processing response...")
|
||||
|
||||
candidates = result.get("candidates", [])
|
||||
for cand in candidates:
|
||||
content = cand.get("content", {})
|
||||
parts_out = content.get("parts", [])
|
||||
for part in parts_out:
|
||||
inline_data = part.get("inline_data") or part.get("inlineData")
|
||||
if inline_data:
|
||||
data_b64 = inline_data.get("data")
|
||||
if data_b64:
|
||||
raw_data = base64.b64decode(data_b64)
|
||||
image_tensors.append(bytes_to_tensor(raw_data))
|
||||
|
||||
if "text" in part and part["text"]:
|
||||
text_parts.append(part["text"])
|
||||
|
||||
if not image_tensors:
|
||||
return (torch.zeros((1, 512, 512, 3)), "Error: No images found in response", "N/A")
|
||||
|
||||
final_image = torch.cat(image_tensors, dim=0)
|
||||
final_text = "\n".join(text_parts) if text_parts else "No description"
|
||||
|
||||
# 计算成本
|
||||
cost_info = self._calculate_cost(result, api_model, time.time() - start_time)
|
||||
|
||||
pbar.update(100)
|
||||
self._send_status(unique_id, "Success")
|
||||
|
||||
return (final_image, final_text, cost_info)
|
||||
|
||||
except Exception as e:
|
||||
self._send_status(unique_id, "Exception caught")
|
||||
return (torch.zeros((1, 512, 512, 3)), f"Error: {str(e)}", "N/A")
|
||||
|
||||
def _send_status(self, node_id, text):
|
||||
"""发送状态文字到节点下方显示"""
|
||||
try:
|
||||
PromptServer.instance.send_sync("progress_text", {"text": text, "node": node_id})
|
||||
except:
|
||||
pass
|
||||
|
||||
def _calculate_cost(self, result, model, duration):
|
||||
"""基于 API 响应计算成本"""
|
||||
usage = result.get("usageMetadata", {})
|
||||
prompt_tokens = usage.get("promptTokenCount", 0)
|
||||
candidates_tokens = usage.get("candidatesTokenCount", 0)
|
||||
details = usage.get("candidatesTokensDetails", [])
|
||||
thoughts_tokens = usage.get("thoughtsTokenCount", 0)
|
||||
|
||||
# 价格配置 (3.1 Flash Image Preview)
|
||||
prices = {"input": 0.5, "output_text": 3.0, "output_image": 60.0}
|
||||
|
||||
text_tokens = thoughts_tokens
|
||||
img_tokens = 0
|
||||
|
||||
for d in details:
|
||||
modality = d.get("modality")
|
||||
count = d.get("tokenCount", 0)
|
||||
if modality == "IMAGE":
|
||||
img_tokens += count
|
||||
else:
|
||||
text_tokens += count
|
||||
|
||||
if not details and candidates_tokens > 0:
|
||||
text_tokens += candidates_tokens
|
||||
|
||||
input_cost = (prompt_tokens / 1_000_000) * prices["input"]
|
||||
text_cost = (text_tokens / 1_000_000) * prices["output_text"]
|
||||
img_cost = (img_tokens / 1_000_000) * prices["output_image"]
|
||||
total_cost = input_cost + text_cost + img_cost
|
||||
|
||||
info = [
|
||||
f"Model: {model}",
|
||||
f"Status: Success",
|
||||
f"Time: {duration:.2f}s",
|
||||
f"Tokens: In:{prompt_tokens:,} | Out Text:{text_tokens:,} | Out Img:{img_tokens:,}",
|
||||
f"Est. Cost: ${total_cost:.6f}"
|
||||
]
|
||||
return "\n".join(info)
|
||||
|
||||
# 注册
|
||||
NODE_CLASS_MAPPINGS = {"PDGeminiNanoBanana2": PDGeminiNanoBanana2}
|
||||
NODE_DISPLAY_NAME_MAPPINGS = {"PDGeminiNanoBanana2": "PD: Nano Banana 2 (ComfyUI Key)"}
|
||||
@@ -0,0 +1,284 @@
|
||||
"""
|
||||
PD: Gemini Image Generation Node (ComfyUI API Key)
|
||||
使用 ComfyUI API Key 调用 ComfyUI 代理 API
|
||||
参数和官方节点完全一样
|
||||
"""
|
||||
|
||||
import torch
|
||||
import requests
|
||||
import time
|
||||
import io
|
||||
import base64
|
||||
import numpy as np
|
||||
from PIL import Image
|
||||
|
||||
# 默认系统提示词(来自 ComfyUI 官方 API 节点)
|
||||
GEMINI_IMAGE_SYS_PROMPT = (
|
||||
"You are an expert image-generation engine. You must ALWAYS produce an image.\n"
|
||||
"Interpret all user input—regardless of "
|
||||
"format, intent, or abstraction—as literal visual directives for image composition.\n"
|
||||
"If a prompt is conversational or lacks specific visual details, "
|
||||
"you must creatively invent a concrete visual scenario that depicts the concept.\n"
|
||||
"Prioritize generating the visual representation above any text, formatting, or conversational requests."
|
||||
)
|
||||
|
||||
|
||||
def tensor_to_base64(tensor):
|
||||
"""将 Tensor 转换为 base64 字符串"""
|
||||
if tensor.ndim == 4:
|
||||
tensor = tensor[0]
|
||||
|
||||
array = np.clip(255. * tensor.cpu().numpy(), 0, 255).astype(np.uint8)
|
||||
image = Image.fromarray(array)
|
||||
buffered = io.BytesIO()
|
||||
image.save(buffered, format="PNG")
|
||||
return base64.b64encode(buffered.getvalue()).decode("utf-8")
|
||||
|
||||
|
||||
def bytes_to_tensor(data):
|
||||
"""将图片字节数据转换为 Tensor"""
|
||||
try:
|
||||
img = Image.open(io.BytesIO(data)).convert("RGB")
|
||||
image_np = np.array(img).astype(np.float32) / 255.0
|
||||
return torch.from_numpy(image_np).unsqueeze(0)
|
||||
except Exception as e:
|
||||
print(f"[GEMINI_APIKEY] Image decode error: {e}")
|
||||
return torch.zeros((1, 512, 512, 3))
|
||||
|
||||
|
||||
class PDGeminiImageGenAPIKey:
|
||||
"""
|
||||
PD: Gemini Image Generation (API Key)
|
||||
使用 ComfyUI API Key 调用 ComfyUI 代理 API
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
"required": {
|
||||
"api_key": ("STRING", {
|
||||
"default": "",
|
||||
"multiline": False,
|
||||
"placeholder": "Your ComfyUI API Key"
|
||||
}),
|
||||
"prompt": ("STRING", {
|
||||
"multiline": True,
|
||||
"default": "A futuristic city with flying cars",
|
||||
"tooltip": "Describe what you want to generate"
|
||||
}),
|
||||
"model": ([
|
||||
"gemini-2.5-flash-image",
|
||||
"gemini-2.5-flash-image-preview"
|
||||
], {
|
||||
"default": "gemini-2.5-flash-image"
|
||||
}),
|
||||
"aspect_ratio": ([
|
||||
"auto", "1:1", "2:3", "3:2", "3:4", "4:3", "4:5", "5:4", "9:16", "16:9", "21:9"
|
||||
], {
|
||||
"default": "auto"
|
||||
}),
|
||||
"response_modalities": (["IMAGE+TEXT", "IMAGE"], {
|
||||
"default": "IMAGE+TEXT",
|
||||
"tooltip": "Choose 'IMAGE' for image-only output, or 'IMAGE+TEXT' for both"
|
||||
}),
|
||||
"seed": ("INT", {
|
||||
"default": 42,
|
||||
"min": 0,
|
||||
"max": 0xFFFFFFFFFFFFFFFF
|
||||
}),
|
||||
},
|
||||
"optional": {
|
||||
"image_ref": ("IMAGE", {
|
||||
"tooltip": "Reference image(s) for image-to-image generation"
|
||||
}),
|
||||
"system_prompt": ("STRING", {
|
||||
"multiline": True,
|
||||
"default": GEMINI_IMAGE_SYS_PROMPT,
|
||||
"tooltip": "Optional system instructions"
|
||||
}),
|
||||
},
|
||||
"hidden": {
|
||||
"unique_id": "UNIQUE_ID",
|
||||
}
|
||||
}
|
||||
|
||||
RETURN_TYPES = ("IMAGE", "STRING")
|
||||
RETURN_NAMES = ("image", "info")
|
||||
FUNCTION = "generate_image"
|
||||
CATEGORY = "PD_Tools/Image_Generation"
|
||||
|
||||
def generate_image(self, api_key, prompt, model, aspect_ratio, response_modalities, seed, unique_id, image_ref=None, system_prompt=""):
|
||||
"""生成图片"""
|
||||
|
||||
# 验证 API Key
|
||||
if not api_key or not api_key.strip():
|
||||
error_msg = "Error: ComfyUI API Key is required"
|
||||
print(f"[GEMINI_APIKEY] {error_msg}")
|
||||
return (torch.zeros((1, 512, 512, 3)), error_msg)
|
||||
|
||||
# 清理 API Key
|
||||
api_key = api_key.strip()
|
||||
|
||||
# ComfyUI 代理 API URL
|
||||
base_url = f"https://api.comfy.org/proxy/vertexai/gemini/{model}"
|
||||
|
||||
# 构建请求内容
|
||||
parts = []
|
||||
|
||||
# 添加用户提示词
|
||||
parts.append({"text": prompt})
|
||||
|
||||
# 添加参考图片(如果有)
|
||||
if image_ref is not None:
|
||||
for i in range(image_ref.shape[0]):
|
||||
b64_str = tensor_to_base64(image_ref[i])
|
||||
parts.append({
|
||||
"inlineData": {
|
||||
"mimeType": "image/png",
|
||||
"data": b64_str
|
||||
}
|
||||
})
|
||||
print(f"[GEMINI_APIKEY] Added reference image {i+1}")
|
||||
|
||||
# 生成配置
|
||||
generation_config = {
|
||||
"responseModalities": ["IMAGE"] if response_modalities == "IMAGE" else ["TEXT", "IMAGE"]
|
||||
}
|
||||
|
||||
# 图片配置
|
||||
if aspect_ratio != "auto":
|
||||
generation_config["imageConfig"] = {
|
||||
"aspectRatio": aspect_ratio
|
||||
}
|
||||
|
||||
# 系统指令
|
||||
system_instruction = None
|
||||
if system_prompt and system_prompt.strip():
|
||||
system_instruction = {
|
||||
"parts": [{"text": system_prompt.strip()}]
|
||||
}
|
||||
|
||||
# 完整请求体
|
||||
payload = {
|
||||
"contents": [{
|
||||
"role": "user",
|
||||
"parts": parts
|
||||
}],
|
||||
"generationConfig": generation_config
|
||||
}
|
||||
|
||||
if system_instruction:
|
||||
payload["systemInstruction"] = system_instruction
|
||||
|
||||
# 请求头
|
||||
headers = {
|
||||
"X-API-KEY": api_key,
|
||||
"Accept": "application/json",
|
||||
"Content-Type": "application/json",
|
||||
"User-Agent": "ComfyUI-PD-Node/2.0"
|
||||
}
|
||||
|
||||
start_time = time.time()
|
||||
|
||||
try:
|
||||
print(f"[GEMINI_APIKEY] Sending request to {model}")
|
||||
print(f"[GEMINI_APIKEY] Aspect Ratio: {aspect_ratio}")
|
||||
|
||||
response = requests.post(base_url, headers=headers, json=payload, timeout=120)
|
||||
|
||||
# 检查响应
|
||||
if response.status_code != 200:
|
||||
error_msg = f"API Error {response.status_code}: {response.text[:200]}"
|
||||
print(f"[GEMINI_APIKEY] {error_msg}")
|
||||
return (torch.zeros((1, 512, 512, 3)), error_msg)
|
||||
|
||||
result = response.json()
|
||||
|
||||
# 提取图片和文本
|
||||
image_tensors = []
|
||||
text_parts = []
|
||||
candidates = result.get("candidates", [])
|
||||
|
||||
if not candidates:
|
||||
error_msg = "Error: No candidates in response"
|
||||
print(f"[GEMINI_APIKEY] {error_msg}")
|
||||
return (torch.zeros((1, 512, 512, 3)), error_msg)
|
||||
|
||||
for cand in candidates:
|
||||
content = cand.get("content", {})
|
||||
parts = content.get("parts", [])
|
||||
|
||||
for part in parts:
|
||||
# 提取图片
|
||||
inline_data = part.get("inline_data") or part.get("inlineData")
|
||||
|
||||
if inline_data:
|
||||
mime = inline_data.get("mime_type") or inline_data.get("mimeType")
|
||||
data_b64 = inline_data.get("data")
|
||||
|
||||
if mime in ["image/png", "image/jpeg"] and data_b64:
|
||||
raw_data = base64.b64decode(data_b64)
|
||||
image_tensors.append(bytes_to_tensor(raw_data))
|
||||
print(f"[GEMINI_APIKEY] Extracted image ({mime})")
|
||||
|
||||
# 提取文本
|
||||
if "text" in part and part["text"]:
|
||||
text_parts.append(part["text"])
|
||||
|
||||
if not image_tensors:
|
||||
error_msg = f"Error: No images found in response"
|
||||
print(f"[GEMINI_APIKEY] {error_msg}")
|
||||
print(f"[GEMINI_APIKEY] Response: {str(result)[:500]}")
|
||||
return (torch.zeros((1, 512, 512, 3)), error_msg)
|
||||
|
||||
final_image = torch.cat(image_tensors, dim=0)
|
||||
final_text = "\n".join(text_parts) if text_parts else "No text output"
|
||||
|
||||
# 计算耗时和成本
|
||||
end_time = time.time()
|
||||
duration = end_time - start_time
|
||||
|
||||
# 估算成本
|
||||
cost_per_img = 0.04
|
||||
total_cost = cost_per_img * len(image_tensors)
|
||||
|
||||
info_str = (
|
||||
f"Model: {model}\n"
|
||||
f"Status: Success\n"
|
||||
f"Time: {duration:.2f}s\n"
|
||||
f"Images: {len(image_tensors)}\n"
|
||||
f"Aspect Ratio: {aspect_ratio}\n"
|
||||
f"Est. Cost: ${total_cost:.4f}\n"
|
||||
f"---\n"
|
||||
f"{final_text}"
|
||||
)
|
||||
|
||||
print(f"[GEMINI_APIKEY] Success! Generated {len(image_tensors)} images in {duration:.2f}s")
|
||||
return (final_image, info_str)
|
||||
|
||||
except requests.exceptions.Timeout:
|
||||
error_msg = "Error: Request timeout (120s)"
|
||||
print(f"[GEMINI_APIKEY] {error_msg}")
|
||||
return (torch.zeros((1, 512, 512, 3)), error_msg)
|
||||
|
||||
except requests.exceptions.ConnectionError as e:
|
||||
error_msg = f"Error: Connection failed - {str(e)}"
|
||||
print(f"[GEMINI_APIKEY] {error_msg}")
|
||||
return (torch.zeros((1, 512, 512, 3)), error_msg)
|
||||
|
||||
except Exception as e:
|
||||
error_msg = f"Error: {str(e)}"
|
||||
print(f"[GEMINI_APIKEY] {error_msg}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return (torch.zeros((1, 512, 512, 3)), error_msg)
|
||||
|
||||
|
||||
# 节点注册
|
||||
NODE_CLASS_MAPPINGS = {
|
||||
"PDGeminiImageGenAPIKey": PDGeminiImageGenAPIKey
|
||||
}
|
||||
|
||||
NODE_DISPLAY_NAME_MAPPINGS = {
|
||||
"PDGeminiImageGenAPIKey": "PD: Gemini Image (apikey)"
|
||||
}
|
||||
@@ -0,0 +1,281 @@
|
||||
"""
|
||||
PD: Gemini Image Generation Node (ComfyUI Auth Token)
|
||||
基于 gemini_image_api 的 Auth Token 认证方式
|
||||
支持文生图和图生图
|
||||
"""
|
||||
|
||||
import torch
|
||||
import requests
|
||||
import time
|
||||
import io
|
||||
import base64
|
||||
import numpy as np
|
||||
from PIL import Image
|
||||
|
||||
# 默认系统提示词(来自 ComfyUI 官方 API 节点)
|
||||
GEMINI_IMAGE_SYS_PROMPT = (
|
||||
"You are an expert image-generation engine. You must ALWAYS produce an image.\n"
|
||||
"Interpret all user input—regardless of "
|
||||
"format, intent, or abstraction—as literal visual directives for image composition.\n"
|
||||
"If a prompt is conversational or lacks specific visual details, "
|
||||
"you must creatively invent a concrete visual scenario that depicts the concept.\n"
|
||||
"Prioritize generating the visual representation above any text, formatting, or conversational requests."
|
||||
)
|
||||
|
||||
|
||||
def tensor_to_base64(tensor):
|
||||
"""将 Tensor 转换为 base64 字符串"""
|
||||
if tensor.ndim == 4:
|
||||
tensor = tensor[0]
|
||||
|
||||
array = np.clip(255. * tensor.cpu().numpy(), 0, 255).astype(np.uint8)
|
||||
image = Image.fromarray(array)
|
||||
buffered = io.BytesIO()
|
||||
image.save(buffered, format="PNG")
|
||||
return base64.b64encode(buffered.getvalue()).decode("utf-8")
|
||||
|
||||
|
||||
def bytes_to_tensor(data):
|
||||
"""将图片字节数据转换为 Tensor"""
|
||||
try:
|
||||
img = Image.open(io.BytesIO(data)).convert("RGB")
|
||||
image_np = np.array(img).astype(np.float32) / 255.0
|
||||
return torch.from_numpy(image_np).unsqueeze(0)
|
||||
except Exception as e:
|
||||
print(f"[GEMINI_AUTHTOKEN] Image decode error: {e}")
|
||||
return torch.zeros((1, 512, 512, 3))
|
||||
|
||||
|
||||
class PDGeminiImageGenAuthToken:
|
||||
"""
|
||||
PD: Gemini Image Generation (Auth Token)
|
||||
使用 ComfyUI Auth Token 调用 Gemini Image API
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
"required": {
|
||||
"auth_token": ("STRING", {
|
||||
"default": "",
|
||||
"multiline": False,
|
||||
"placeholder": "Paste your Auth Token here"
|
||||
}),
|
||||
"prompt": ("STRING", {
|
||||
"multiline": True,
|
||||
"default": "A futuristic city with flying cars",
|
||||
"tooltip": "Describe what you want to generate"
|
||||
}),
|
||||
"model": ([
|
||||
"gemini-2.5-flash-image",
|
||||
"gemini-2.5-flash-image-preview"
|
||||
], {
|
||||
"default": "gemini-2.5-flash-image"
|
||||
}),
|
||||
"aspect_ratio": ([
|
||||
"auto", "1:1", "16:9", "9:16", "4:3", "3:4", "21:9", "9:21"
|
||||
], {
|
||||
"default": "auto"
|
||||
}),
|
||||
"resolution": (["auto", "1K", "2K", "4K"], {
|
||||
"default": "1K"
|
||||
}),
|
||||
"seed": ("INT", {
|
||||
"default": 0,
|
||||
"min": 0,
|
||||
"max": 0xFFFFFFFFFFFFFFFF
|
||||
}),
|
||||
},
|
||||
"optional": {
|
||||
"image_ref": ("IMAGE", {
|
||||
"tooltip": "Reference image(s) for image-to-image generation"
|
||||
}),
|
||||
"system_prompt": ("STRING", {
|
||||
"multiline": True,
|
||||
"default": GEMINI_IMAGE_SYS_PROMPT,
|
||||
"tooltip": "Optional system instructions"
|
||||
}),
|
||||
},
|
||||
"hidden": {
|
||||
"unique_id": "UNIQUE_ID",
|
||||
}
|
||||
}
|
||||
|
||||
RETURN_TYPES = ("IMAGE", "STRING")
|
||||
RETURN_NAMES = ("image", "info")
|
||||
FUNCTION = "generate_image"
|
||||
CATEGORY = "PD_Tools/Image_Generation"
|
||||
|
||||
def generate_image(self, auth_token, prompt, model, aspect_ratio, resolution, seed, unique_id, image_ref=None, system_prompt=""):
|
||||
"""生成图片"""
|
||||
|
||||
print(f"[GEMINI_AUTHTOKEN] ========== 开始执行 ==========")
|
||||
print(f"[GEMINI_AUTHTOKEN] Prompt: {prompt[:50]}...")
|
||||
print(f"[GEMINI_AUTHTOKEN] Model: {model}")
|
||||
|
||||
# 验证 Token
|
||||
if not auth_token or not auth_token.strip():
|
||||
error_msg = "Error: Auth Token is required"
|
||||
print(f"[GEMINI_AUTHTOKEN] {error_msg}")
|
||||
return (torch.zeros((1, 512, 512, 3)), error_msg)
|
||||
|
||||
# 清理 Token
|
||||
auth_token = auth_token.strip()
|
||||
if auth_token.lower().startswith("bearer "):
|
||||
auth_token = auth_token[7:].strip()
|
||||
|
||||
# 构建 API URL (不需要添加 :generateContent 后缀,API 会自动处理)
|
||||
base_url = f"https://api.comfy.org/proxy/vertexai/gemini/{model}"
|
||||
|
||||
# 构建请求内容
|
||||
parts = []
|
||||
|
||||
# 添加系统提示词(如果有)
|
||||
if system_prompt and system_prompt.strip():
|
||||
parts.append({"text": system_prompt.strip()})
|
||||
|
||||
# 添加用户提示词
|
||||
parts.append({"text": prompt})
|
||||
|
||||
# 添加参考图片(如果有)
|
||||
if image_ref is not None:
|
||||
for i in range(image_ref.shape[0]):
|
||||
b64_str = tensor_to_base64(image_ref[i])
|
||||
parts.append({
|
||||
"inlineData": {
|
||||
"mimeType": "image/png",
|
||||
"data": b64_str
|
||||
}
|
||||
})
|
||||
print(f"[GEMINI_AUTHTOKEN] Added reference image {i+1}")
|
||||
|
||||
# 生成配置
|
||||
generation_config = {
|
||||
"responseModalities": ["IMAGE"]
|
||||
}
|
||||
|
||||
# 图片配置
|
||||
image_config = {}
|
||||
if aspect_ratio != "auto":
|
||||
image_config["aspectRatio"] = aspect_ratio
|
||||
if resolution != "auto":
|
||||
image_config["resolution"] = resolution
|
||||
|
||||
if image_config:
|
||||
generation_config["imageConfig"] = image_config
|
||||
|
||||
# 完整请求体
|
||||
payload = {
|
||||
"contents": [{
|
||||
"role": "user",
|
||||
"parts": parts
|
||||
}],
|
||||
"generationConfig": generation_config
|
||||
}
|
||||
|
||||
# 请求头
|
||||
headers = {
|
||||
"Authorization": f"Bearer {auth_token}",
|
||||
"Content-Type": "application/json",
|
||||
"User-Agent": "ComfyUI-PD-Node/2.0"
|
||||
}
|
||||
|
||||
start_time = time.time()
|
||||
|
||||
try:
|
||||
print(f"[GEMINI_AUTHTOKEN] Sending request to {model}")
|
||||
print(f"[GEMINI_AUTHTOKEN] Aspect Ratio: {aspect_ratio}, Resolution: {resolution}")
|
||||
|
||||
response = requests.post(base_url, headers=headers, json=payload, timeout=120)
|
||||
|
||||
# 检查响应
|
||||
if response.status_code != 200:
|
||||
error_msg = f"API Error {response.status_code}: {response.text[:200]}"
|
||||
print(f"[GEMINI_AUTHTOKEN] {error_msg}")
|
||||
return (torch.zeros((1, 512, 512, 3)), error_msg)
|
||||
|
||||
result = response.json()
|
||||
|
||||
# 提取图片
|
||||
image_tensors = []
|
||||
candidates = result.get("candidates", [])
|
||||
|
||||
if not candidates:
|
||||
error_msg = "Error: No candidates in response"
|
||||
print(f"[GEMINI_AUTHTOKEN] {error_msg}")
|
||||
return (torch.zeros((1, 512, 512, 3)), error_msg)
|
||||
|
||||
for cand in candidates:
|
||||
content = cand.get("content", {})
|
||||
parts = content.get("parts", [])
|
||||
|
||||
for part in parts:
|
||||
# 支持两种字段名格式
|
||||
inline_data = part.get("inline_data") or part.get("inlineData")
|
||||
|
||||
if inline_data:
|
||||
mime = inline_data.get("mime_type") or inline_data.get("mimeType")
|
||||
data_b64 = inline_data.get("data")
|
||||
|
||||
if mime in ["image/png", "image/jpeg"] and data_b64:
|
||||
raw_data = base64.b64decode(data_b64)
|
||||
image_tensors.append(bytes_to_tensor(raw_data))
|
||||
print(f"[GEMINI_AUTHTOKEN] Extracted image ({mime})")
|
||||
|
||||
if not image_tensors:
|
||||
error_msg = f"Error: No images found in response"
|
||||
print(f"[GEMINI_AUTHTOKEN] {error_msg}")
|
||||
print(f"[GEMINI_AUTHTOKEN] Response: {str(result)[:500]}")
|
||||
return (torch.zeros((1, 512, 512, 3)), error_msg)
|
||||
|
||||
final_image = torch.cat(image_tensors, dim=0)
|
||||
|
||||
# 计算耗时和成本
|
||||
end_time = time.time()
|
||||
duration = end_time - start_time
|
||||
|
||||
# 估算成本(根据模型)
|
||||
cost_per_img = 0.04
|
||||
if "pro" in model.lower():
|
||||
cost_per_img = 0.08
|
||||
total_cost = cost_per_img * len(image_tensors)
|
||||
|
||||
info_str = (
|
||||
f"Model: {model}\n"
|
||||
f"Status: Success\n"
|
||||
f"Time: {duration:.2f}s\n"
|
||||
f"Images: {len(image_tensors)}\n"
|
||||
f"Aspect Ratio: {aspect_ratio}\n"
|
||||
f"Resolution: {resolution}\n"
|
||||
f"Est. Cost: ${total_cost:.4f}"
|
||||
)
|
||||
|
||||
print(f"[GEMINI_AUTHTOKEN] Success! Generated {len(image_tensors)} images in {duration:.2f}s")
|
||||
return (final_image, info_str)
|
||||
|
||||
except requests.exceptions.Timeout:
|
||||
error_msg = "Error: Request timeout (120s)"
|
||||
print(f"[GEMINI_AUTHTOKEN] {error_msg}")
|
||||
return (torch.zeros((1, 512, 512, 3)), error_msg)
|
||||
|
||||
except requests.exceptions.ConnectionError as e:
|
||||
error_msg = f"Error: Connection failed - {str(e)}"
|
||||
print(f"[GEMINI_AUTHTOKEN] {error_msg}")
|
||||
return (torch.zeros((1, 512, 512, 3)), error_msg)
|
||||
|
||||
except Exception as e:
|
||||
error_msg = f"Error: {str(e)}"
|
||||
print(f"[GEMINI_AUTHTOKEN] {error_msg}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return (torch.zeros((1, 512, 512, 3)), error_msg)
|
||||
|
||||
|
||||
# 节点注册
|
||||
NODE_CLASS_MAPPINGS = {
|
||||
"PDGeminiImageGenAuthToken": PDGeminiImageGenAuthToken
|
||||
}
|
||||
|
||||
NODE_DISPLAY_NAME_MAPPINGS = {
|
||||
"PDGeminiImageGenAuthToken": "PD: Gemini Image (comfyui_AuthToken)"
|
||||
}
|
||||
@@ -0,0 +1,294 @@
|
||||
"""
|
||||
PD: Gemini Pro Image Generation Node (ComfyUI API Key)
|
||||
Nano Banana Pro - 使用 ComfyUI API Key 调用 ComfyUI 代理 API
|
||||
支持高分辨率图像生成(1K/2K/4K)
|
||||
"""
|
||||
|
||||
import torch
|
||||
import requests
|
||||
import time
|
||||
import io
|
||||
import base64
|
||||
import numpy as np
|
||||
from PIL import Image
|
||||
|
||||
# 默认系统提示词(来自 ComfyUI 官方 API 节点)
|
||||
GEMINI_IMAGE_SYS_PROMPT = (
|
||||
"You are an expert image-generation engine. You must ALWAYS produce an image.\n"
|
||||
"Interpret all user input—regardless of "
|
||||
"format, intent, or abstraction—as literal visual directives for image composition.\n"
|
||||
"If a prompt is conversational or lacks specific visual details, "
|
||||
"you must creatively invent a concrete visual scenario that depicts the concept.\n"
|
||||
"Prioritize generating the visual representation above any text, formatting, or conversational requests."
|
||||
)
|
||||
|
||||
|
||||
def tensor_to_base64(tensor):
|
||||
"""将 Tensor 转换为 base64 字符串"""
|
||||
if tensor.ndim == 4:
|
||||
tensor = tensor[0]
|
||||
|
||||
array = np.clip(255. * tensor.cpu().numpy(), 0, 255).astype(np.uint8)
|
||||
image = Image.fromarray(array)
|
||||
buffered = io.BytesIO()
|
||||
image.save(buffered, format="PNG")
|
||||
return base64.b64encode(buffered.getvalue()).decode("utf-8")
|
||||
|
||||
|
||||
def bytes_to_tensor(data):
|
||||
"""将图片字节数据转换为 Tensor"""
|
||||
try:
|
||||
img = Image.open(io.BytesIO(data)).convert("RGB")
|
||||
image_np = np.array(img).astype(np.float32) / 255.0
|
||||
return torch.from_numpy(image_np).unsqueeze(0)
|
||||
except Exception as e:
|
||||
print(f"[GEMINI_PRO_APIKEY] Image decode error: {e}")
|
||||
return torch.zeros((1, 512, 512, 3))
|
||||
|
||||
|
||||
class PDGeminiProImageGenAPIKey:
|
||||
"""
|
||||
PD: Gemini Pro Image Generation (API Key)
|
||||
Nano Banana Pro - 使用 ComfyUI API Key 调用 ComfyUI 代理 API
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
"required": {
|
||||
"api_key": ("STRING", {
|
||||
"default": "",
|
||||
"multiline": False,
|
||||
"placeholder": "Your ComfyUI API Key"
|
||||
}),
|
||||
"prompt": ("STRING", {
|
||||
"multiline": True,
|
||||
"default": "A futuristic city with flying cars",
|
||||
"tooltip": "Text prompt describing the image to generate or the edits to apply"
|
||||
}),
|
||||
"model": (["gemini-3-pro-image-preview"], {
|
||||
"default": "gemini-3-pro-image-preview"
|
||||
}),
|
||||
"aspect_ratio": ([
|
||||
"auto", "1:1", "2:3", "3:2", "3:4", "4:3", "4:5", "5:4", "9:16", "16:9", "21:9"
|
||||
], {
|
||||
"default": "auto",
|
||||
"tooltip": "If set to 'auto', matches your input image's aspect ratio"
|
||||
}),
|
||||
"resolution": (["1K", "2K", "4K"], {
|
||||
"default": "1K",
|
||||
"tooltip": "Target output resolution. For 2K/4K the native Gemini upscaler is used"
|
||||
}),
|
||||
"response_modalities": (["IMAGE+TEXT", "IMAGE"], {
|
||||
"default": "IMAGE+TEXT",
|
||||
"tooltip": "Choose 'IMAGE' for image-only output, or 'IMAGE+TEXT' for both"
|
||||
}),
|
||||
"seed": ("INT", {
|
||||
"default": 42,
|
||||
"min": 0,
|
||||
"max": 0xFFFFFFFFFFFFFFFF
|
||||
}),
|
||||
},
|
||||
"optional": {
|
||||
"images": ("IMAGE", {
|
||||
"tooltip": "Optional reference image(s) (up to 14)"
|
||||
}),
|
||||
"system_prompt": ("STRING", {
|
||||
"multiline": True,
|
||||
"default": GEMINI_IMAGE_SYS_PROMPT,
|
||||
"tooltip": "Foundational instructions that dictate an AI's behavior"
|
||||
}),
|
||||
},
|
||||
"hidden": {
|
||||
"unique_id": "UNIQUE_ID",
|
||||
}
|
||||
}
|
||||
|
||||
RETURN_TYPES = ("IMAGE", "STRING")
|
||||
RETURN_NAMES = ("image", "text")
|
||||
FUNCTION = "generate_image"
|
||||
CATEGORY = "PD_Tools/Image_Generation"
|
||||
|
||||
def generate_image(self, api_key, prompt, model, aspect_ratio, resolution, response_modalities, seed, unique_id, images=None, system_prompt=""):
|
||||
"""生成图片"""
|
||||
|
||||
# 验证 API Key
|
||||
if not api_key or not api_key.strip():
|
||||
error_msg = "Error: ComfyUI API Key is required"
|
||||
print(f"[GEMINI_PRO_APIKEY] {error_msg}")
|
||||
return (torch.zeros((1, 512, 512, 3)), error_msg)
|
||||
|
||||
# 清理 API Key
|
||||
api_key = api_key.strip()
|
||||
|
||||
# ComfyUI 代理 API URL
|
||||
base_url = f"https://api.comfy.org/proxy/vertexai/gemini/{model}"
|
||||
|
||||
# 构建请求内容
|
||||
parts = []
|
||||
|
||||
# 添加用户提示词
|
||||
parts.append({"text": prompt})
|
||||
|
||||
# 添加参考图片(如果有,最多14张)
|
||||
if images is not None:
|
||||
num_images = min(images.shape[0], 14)
|
||||
for i in range(num_images):
|
||||
b64_str = tensor_to_base64(images[i])
|
||||
parts.append({
|
||||
"inlineData": {
|
||||
"mimeType": "image/png",
|
||||
"data": b64_str
|
||||
}
|
||||
})
|
||||
print(f"[GEMINI_PRO_APIKEY] Added reference image {i+1}/{num_images}")
|
||||
|
||||
if images.shape[0] > 14:
|
||||
print(f"[GEMINI_PRO_APIKEY] Warning: Only first 14 images will be used (provided {images.shape[0]})")
|
||||
|
||||
# 生成配置
|
||||
generation_config = {
|
||||
"responseModalities": ["IMAGE"] if response_modalities == "IMAGE" else ["TEXT", "IMAGE"]
|
||||
}
|
||||
|
||||
# 图片配置
|
||||
image_config = {
|
||||
"imageSize": resolution
|
||||
}
|
||||
if aspect_ratio != "auto":
|
||||
image_config["aspectRatio"] = aspect_ratio
|
||||
|
||||
generation_config["imageConfig"] = image_config
|
||||
|
||||
# 系统指令
|
||||
system_instruction = None
|
||||
if system_prompt and system_prompt.strip():
|
||||
system_instruction = {
|
||||
"parts": [{"text": system_prompt.strip()}]
|
||||
}
|
||||
|
||||
# 完整请求体
|
||||
payload = {
|
||||
"contents": [{
|
||||
"role": "user",
|
||||
"parts": parts
|
||||
}],
|
||||
"generationConfig": generation_config
|
||||
}
|
||||
|
||||
if system_instruction:
|
||||
payload["systemInstruction"] = system_instruction
|
||||
|
||||
# 请求头
|
||||
headers = {
|
||||
"X-API-KEY": api_key,
|
||||
"Accept": "application/json",
|
||||
"Content-Type": "application/json",
|
||||
"User-Agent": "ComfyUI-PD-Node/2.0"
|
||||
}
|
||||
|
||||
start_time = time.time()
|
||||
|
||||
try:
|
||||
print(f"[GEMINI_PRO_APIKEY] Sending request to {model}")
|
||||
print(f"[GEMINI_PRO_APIKEY] Aspect Ratio: {aspect_ratio}, Resolution: {resolution}")
|
||||
|
||||
response = requests.post(base_url, headers=headers, json=payload, timeout=180)
|
||||
|
||||
# 检查响应
|
||||
if response.status_code != 200:
|
||||
error_msg = f"API Error {response.status_code}: {response.text[:200]}"
|
||||
print(f"[GEMINI_PRO_APIKEY] {error_msg}")
|
||||
return (torch.zeros((1, 512, 512, 3)), error_msg)
|
||||
|
||||
result = response.json()
|
||||
|
||||
# 提取图片和文本
|
||||
image_tensors = []
|
||||
text_parts = []
|
||||
candidates = result.get("candidates", [])
|
||||
|
||||
if not candidates:
|
||||
error_msg = "Error: No candidates in response"
|
||||
print(f"[GEMINI_PRO_APIKEY] {error_msg}")
|
||||
return (torch.zeros((1, 512, 512, 3)), error_msg)
|
||||
|
||||
for cand in candidates:
|
||||
content = cand.get("content", {})
|
||||
parts = content.get("parts", [])
|
||||
|
||||
for part in parts:
|
||||
# 提取图片
|
||||
inline_data = part.get("inline_data") or part.get("inlineData")
|
||||
|
||||
if inline_data:
|
||||
mime = inline_data.get("mime_type") or inline_data.get("mimeType")
|
||||
data_b64 = inline_data.get("data")
|
||||
|
||||
if mime in ["image/png", "image/jpeg"] and data_b64:
|
||||
raw_data = base64.b64decode(data_b64)
|
||||
image_tensors.append(bytes_to_tensor(raw_data))
|
||||
print(f"[GEMINI_PRO_APIKEY] Extracted image ({mime})")
|
||||
|
||||
# 提取文本
|
||||
if "text" in part and part["text"]:
|
||||
text_parts.append(part["text"])
|
||||
|
||||
if not image_tensors:
|
||||
error_msg = f"Error: No images found in response"
|
||||
print(f"[GEMINI_PRO_APIKEY] {error_msg}")
|
||||
print(f"[GEMINI_PRO_APIKEY] Response: {str(result)[:500]}")
|
||||
return (torch.zeros((1, 512, 512, 3)), error_msg)
|
||||
|
||||
final_image = torch.cat(image_tensors, dim=0)
|
||||
final_text = "\n".join(text_parts) if text_parts else "No text output"
|
||||
|
||||
# 计算耗时和成本
|
||||
end_time = time.time()
|
||||
duration = end_time - start_time
|
||||
|
||||
# 估算成本(Gemini 3 Pro Image)
|
||||
cost_per_img = 0.12 # 估算
|
||||
total_cost = cost_per_img * len(image_tensors)
|
||||
|
||||
info_str = (
|
||||
f"Model: {model}\n"
|
||||
f"Status: Success\n"
|
||||
f"Time: {duration:.2f}s\n"
|
||||
f"Images: {len(image_tensors)}\n"
|
||||
f"Aspect Ratio: {aspect_ratio}\n"
|
||||
f"Resolution: {resolution}\n"
|
||||
f"Est. Cost: ${total_cost:.4f}\n"
|
||||
f"---\n"
|
||||
f"{final_text}"
|
||||
)
|
||||
|
||||
print(f"[GEMINI_PRO_APIKEY] Success! Generated {len(image_tensors)} images in {duration:.2f}s")
|
||||
return (final_image, info_str)
|
||||
|
||||
except requests.exceptions.Timeout:
|
||||
error_msg = "Error: Request timeout (180s)"
|
||||
print(f"[GEMINI_PRO_APIKEY] {error_msg}")
|
||||
return (torch.zeros((1, 512, 512, 3)), error_msg)
|
||||
|
||||
except requests.exceptions.ConnectionError as e:
|
||||
error_msg = f"Error: Connection failed - {str(e)}"
|
||||
print(f"[GEMINI_PRO_APIKEY] {error_msg}")
|
||||
return (torch.zeros((1, 512, 512, 3)), error_msg)
|
||||
|
||||
except Exception as e:
|
||||
error_msg = f"Error: {str(e)}"
|
||||
print(f"[GEMINI_PRO_APIKEY] {error_msg}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return (torch.zeros((1, 512, 512, 3)), error_msg)
|
||||
|
||||
|
||||
# 节点注册
|
||||
NODE_CLASS_MAPPINGS = {
|
||||
"PDGeminiProImageGenAPIKey": PDGeminiProImageGenAPIKey
|
||||
}
|
||||
|
||||
NODE_DISPLAY_NAME_MAPPINGS = {
|
||||
"PDGeminiProImageGenAPIKey": "PD: Gemini Pro Image (apikey)"
|
||||
}
|
||||
@@ -0,0 +1,300 @@
|
||||
"""
|
||||
PD: Gemini Pro Image Generation Node (ComfyUI Auth Token)
|
||||
Nano Banana Pro - 基于 Gemini 3 Pro Image
|
||||
支持高分辨率图像生成(1K/2K/4K)
|
||||
"""
|
||||
|
||||
import torch
|
||||
import requests
|
||||
import time
|
||||
import io
|
||||
import base64
|
||||
import numpy as np
|
||||
from PIL import Image
|
||||
|
||||
# 默认系统提示词(来自 ComfyUI 官方 API 节点)
|
||||
GEMINI_IMAGE_SYS_PROMPT = (
|
||||
"You are an expert image-generation engine. You must ALWAYS produce an image.\n"
|
||||
"Interpret all user input—regardless of "
|
||||
"format, intent, or abstraction—as literal visual directives for image composition.\n"
|
||||
"If a prompt is conversational or lacks specific visual details, "
|
||||
"you must creatively invent a concrete visual scenario that depicts the concept.\n"
|
||||
"Prioritize generating the visual representation above any text, formatting, or conversational requests."
|
||||
)
|
||||
|
||||
|
||||
def tensor_to_base64(tensor):
|
||||
"""将 Tensor 转换为 base64 字符串"""
|
||||
if tensor.ndim == 4:
|
||||
tensor = tensor[0]
|
||||
|
||||
array = np.clip(255. * tensor.cpu().numpy(), 0, 255).astype(np.uint8)
|
||||
image = Image.fromarray(array)
|
||||
buffered = io.BytesIO()
|
||||
image.save(buffered, format="PNG")
|
||||
return base64.b64encode(buffered.getvalue()).decode("utf-8")
|
||||
|
||||
|
||||
def bytes_to_tensor(data):
|
||||
"""将图片字节数据转换为 Tensor"""
|
||||
try:
|
||||
img = Image.open(io.BytesIO(data)).convert("RGB")
|
||||
image_np = np.array(img).astype(np.float32) / 255.0
|
||||
return torch.from_numpy(image_np).unsqueeze(0)
|
||||
except Exception as e:
|
||||
print(f"[GEMINI_PRO_AUTHTOKEN] Image decode error: {e}")
|
||||
return torch.zeros((1, 512, 512, 3))
|
||||
|
||||
|
||||
class PDGeminiProImageGenAuthToken:
|
||||
"""
|
||||
PD: Gemini Pro Image Generation (Auth Token)
|
||||
Nano Banana Pro - 使用 ComfyUI Auth Token 调用 Gemini 3 Pro Image API
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
"required": {
|
||||
"auth_token": ("STRING", {
|
||||
"default": "",
|
||||
"multiline": False,
|
||||
"placeholder": "Paste your Auth Token here"
|
||||
}),
|
||||
"prompt": ("STRING", {
|
||||
"multiline": True,
|
||||
"default": "A futuristic city with flying cars",
|
||||
"tooltip": "Text prompt describing the image to generate or the edits to apply"
|
||||
}),
|
||||
"model": (["gemini-3-pro-image-preview"], {
|
||||
"default": "gemini-3-pro-image-preview"
|
||||
}),
|
||||
"aspect_ratio": ([
|
||||
"auto", "1:1", "2:3", "3:2", "3:4", "4:3", "4:5", "5:4", "9:16", "16:9", "21:9"
|
||||
], {
|
||||
"default": "auto",
|
||||
"tooltip": "If set to 'auto', matches your input image's aspect ratio"
|
||||
}),
|
||||
"resolution": (["1K", "2K", "4K"], {
|
||||
"default": "1K",
|
||||
"tooltip": "Target output resolution. For 2K/4K the native Gemini upscaler is used"
|
||||
}),
|
||||
"response_modalities": (["IMAGE+TEXT", "IMAGE"], {
|
||||
"default": "IMAGE+TEXT",
|
||||
"tooltip": "Choose 'IMAGE' for image-only output, or 'IMAGE+TEXT' for both"
|
||||
}),
|
||||
"seed": ("INT", {
|
||||
"default": 42,
|
||||
"min": 0,
|
||||
"max": 0xFFFFFFFFFFFFFFFF
|
||||
}),
|
||||
},
|
||||
"optional": {
|
||||
"images": ("IMAGE", {
|
||||
"tooltip": "Optional reference image(s) (up to 14)"
|
||||
}),
|
||||
"system_prompt": ("STRING", {
|
||||
"multiline": True,
|
||||
"default": GEMINI_IMAGE_SYS_PROMPT,
|
||||
"tooltip": "Foundational instructions that dictate an AI's behavior"
|
||||
}),
|
||||
},
|
||||
"hidden": {
|
||||
"unique_id": "UNIQUE_ID",
|
||||
}
|
||||
}
|
||||
|
||||
RETURN_TYPES = ("IMAGE", "STRING")
|
||||
RETURN_NAMES = ("image", "text")
|
||||
FUNCTION = "generate_image"
|
||||
CATEGORY = "PD_Tools/Image_Generation"
|
||||
|
||||
def generate_image(self, auth_token, prompt, model, aspect_ratio, resolution, response_modalities, seed, unique_id, images=None, system_prompt=""):
|
||||
"""生成图片"""
|
||||
|
||||
print(f"[GEMINI_PRO_AUTHTOKEN] ========== 开始执行 ==========")
|
||||
print(f"[GEMINI_PRO_AUTHTOKEN] Prompt: {prompt[:50]}...")
|
||||
print(f"[GEMINI_PRO_AUTHTOKEN] Model: {model}")
|
||||
|
||||
# 验证 Token
|
||||
if not auth_token or not auth_token.strip():
|
||||
error_msg = "Error: Auth Token is required"
|
||||
print(f"[GEMINI_PRO_AUTHTOKEN] {error_msg}")
|
||||
return (torch.zeros((1, 512, 512, 3)), error_msg)
|
||||
|
||||
# 清理 Token
|
||||
auth_token = auth_token.strip()
|
||||
if auth_token.lower().startswith("bearer "):
|
||||
auth_token = auth_token[7:].strip()
|
||||
|
||||
# 构建 API URL
|
||||
base_url = f"https://api.comfy.org/proxy/vertexai/gemini/{model}"
|
||||
|
||||
# 构建请求内容
|
||||
parts = []
|
||||
|
||||
# 添加用户提示词
|
||||
parts.append({"text": prompt})
|
||||
|
||||
# 添加参考图片(如果有,最多14张)
|
||||
if images is not None:
|
||||
num_images = min(images.shape[0], 14)
|
||||
for i in range(num_images):
|
||||
b64_str = tensor_to_base64(images[i])
|
||||
parts.append({
|
||||
"inlineData": {
|
||||
"mimeType": "image/png",
|
||||
"data": b64_str
|
||||
}
|
||||
})
|
||||
print(f"[GEMINI_PRO_AUTHTOKEN] Added reference image {i+1}/{num_images}")
|
||||
|
||||
if images.shape[0] > 14:
|
||||
print(f"[GEMINI_PRO_AUTHTOKEN] Warning: Only first 14 images will be used (provided {images.shape[0]})")
|
||||
|
||||
# 生成配置
|
||||
generation_config = {
|
||||
"responseModalities": ["IMAGE"] if response_modalities == "IMAGE" else ["TEXT", "IMAGE"]
|
||||
}
|
||||
|
||||
# 图片配置
|
||||
image_config = {
|
||||
"imageSize": resolution
|
||||
}
|
||||
if aspect_ratio != "auto":
|
||||
image_config["aspectRatio"] = aspect_ratio
|
||||
|
||||
generation_config["imageConfig"] = image_config
|
||||
|
||||
# 系统指令
|
||||
system_instruction = None
|
||||
if system_prompt and system_prompt.strip():
|
||||
system_instruction = {
|
||||
"parts": [{"text": system_prompt.strip()}]
|
||||
}
|
||||
|
||||
# 完整请求体
|
||||
payload = {
|
||||
"contents": [{
|
||||
"role": "user",
|
||||
"parts": parts
|
||||
}],
|
||||
"generationConfig": generation_config
|
||||
}
|
||||
|
||||
if system_instruction:
|
||||
payload["systemInstruction"] = system_instruction
|
||||
|
||||
# 请求头
|
||||
headers = {
|
||||
"Authorization": f"Bearer {auth_token}",
|
||||
"Content-Type": "application/json",
|
||||
"User-Agent": "ComfyUI-PD-Node/2.0"
|
||||
}
|
||||
|
||||
start_time = time.time()
|
||||
|
||||
try:
|
||||
print(f"[GEMINI_PRO_AUTHTOKEN] Sending request to {model}")
|
||||
print(f"[GEMINI_PRO_AUTHTOKEN] Aspect Ratio: {aspect_ratio}, Resolution: {resolution}")
|
||||
|
||||
response = requests.post(base_url, headers=headers, json=payload, timeout=180)
|
||||
|
||||
# 检查响应
|
||||
if response.status_code != 200:
|
||||
error_msg = f"API Error {response.status_code}: {response.text[:200]}"
|
||||
print(f"[GEMINI_PRO_AUTHTOKEN] {error_msg}")
|
||||
return (torch.zeros((1, 512, 512, 3)), error_msg)
|
||||
|
||||
result = response.json()
|
||||
|
||||
# 提取图片和文本
|
||||
image_tensors = []
|
||||
text_parts = []
|
||||
candidates = result.get("candidates", [])
|
||||
|
||||
if not candidates:
|
||||
error_msg = "Error: No candidates in response"
|
||||
print(f"[GEMINI_PRO_AUTHTOKEN] {error_msg}")
|
||||
return (torch.zeros((1, 512, 512, 3)), error_msg)
|
||||
|
||||
for cand in candidates:
|
||||
content = cand.get("content", {})
|
||||
parts = content.get("parts", [])
|
||||
|
||||
for part in parts:
|
||||
# 提取图片
|
||||
inline_data = part.get("inline_data") or part.get("inlineData")
|
||||
|
||||
if inline_data:
|
||||
mime = inline_data.get("mime_type") or inline_data.get("mimeType")
|
||||
data_b64 = inline_data.get("data")
|
||||
|
||||
if mime in ["image/png", "image/jpeg"] and data_b64:
|
||||
raw_data = base64.b64decode(data_b64)
|
||||
image_tensors.append(bytes_to_tensor(raw_data))
|
||||
print(f"[GEMINI_PRO_AUTHTOKEN] Extracted image ({mime})")
|
||||
|
||||
# 提取文本
|
||||
if "text" in part and part["text"]:
|
||||
text_parts.append(part["text"])
|
||||
|
||||
if not image_tensors:
|
||||
error_msg = f"Error: No images found in response"
|
||||
print(f"[GEMINI_PRO_AUTHTOKEN] {error_msg}")
|
||||
print(f"[GEMINI_PRO_AUTHTOKEN] Response: {str(result)[:500]}")
|
||||
return (torch.zeros((1, 512, 512, 3)), error_msg)
|
||||
|
||||
final_image = torch.cat(image_tensors, dim=0)
|
||||
final_text = "\n".join(text_parts) if text_parts else "No text output"
|
||||
|
||||
# 计算耗时和成本
|
||||
end_time = time.time()
|
||||
duration = end_time - start_time
|
||||
|
||||
# 估算成本(Gemini 3 Pro Image)
|
||||
# $2/1M input tokens, $12/1M output text tokens, $120/1M output image tokens
|
||||
cost_per_img = 0.12 # 估算
|
||||
total_cost = cost_per_img * len(image_tensors)
|
||||
|
||||
info_str = (
|
||||
f"Model: {model}\n"
|
||||
f"Status: Success\n"
|
||||
f"Time: {duration:.2f}s\n"
|
||||
f"Images: {len(image_tensors)}\n"
|
||||
f"Aspect Ratio: {aspect_ratio}\n"
|
||||
f"Resolution: {resolution}\n"
|
||||
f"Est. Cost: ${total_cost:.4f}\n"
|
||||
f"---\n"
|
||||
f"{final_text}"
|
||||
)
|
||||
|
||||
print(f"[GEMINI_PRO_AUTHTOKEN] Success! Generated {len(image_tensors)} images in {duration:.2f}s")
|
||||
return (final_image, info_str)
|
||||
|
||||
except requests.exceptions.Timeout:
|
||||
error_msg = "Error: Request timeout (180s)"
|
||||
print(f"[GEMINI_PRO_AUTHTOKEN] {error_msg}")
|
||||
return (torch.zeros((1, 512, 512, 3)), error_msg)
|
||||
|
||||
except requests.exceptions.ConnectionError as e:
|
||||
error_msg = f"Error: Connection failed - {str(e)}"
|
||||
print(f"[GEMINI_PRO_AUTHTOKEN] {error_msg}")
|
||||
return (torch.zeros((1, 512, 512, 3)), error_msg)
|
||||
|
||||
except Exception as e:
|
||||
error_msg = f"Error: {str(e)}"
|
||||
print(f"[GEMINI_PRO_AUTHTOKEN] {error_msg}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return (torch.zeros((1, 512, 512, 3)), error_msg)
|
||||
|
||||
|
||||
# 节点注册
|
||||
NODE_CLASS_MAPPINGS = {
|
||||
"PDGeminiProImageGenAuthToken": PDGeminiProImageGenAuthToken
|
||||
}
|
||||
|
||||
NODE_DISPLAY_NAME_MAPPINGS = {
|
||||
"PDGeminiProImageGenAuthToken": "PD: Gemini Pro Image (comfyui_AuthToken)"
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
import torch
|
||||
from comfy_api_nodes.apis import GeminiPart, GeminiInlineData, GeminiMimeType
|
||||
from comfy_api_nodes.util.conversions import tensor_to_base64_string
|
||||
|
||||
class PD_ComfyPlusImage:
|
||||
"""
|
||||
将最多9个尺寸不一的图片按顺序整合打包为 GEMINI_INPUT_FILES,
|
||||
可以直接连接给支持 files 的大模型节点 (如 Nano Banana Pro 等),
|
||||
从而完美突破 ComfyUI Batch 必须相同尺寸的限制。
|
||||
"""
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
# 第1张图片强制必填
|
||||
"required": {
|
||||
"image1": ("IMAGE", {"tooltip": "必须输入的第1张图片"}),
|
||||
},
|
||||
# 第 2-9 张及 previous_files 作为可选填
|
||||
"optional": {
|
||||
"image2": ("IMAGE", {"tooltip": "可选输入的第2张图片"}),
|
||||
"image3": ("IMAGE", {"tooltip": "可选输入的第3张图片"}),
|
||||
"image4": ("IMAGE", {"tooltip": "可选输入的第4张图片"}),
|
||||
"image5": ("IMAGE", {"tooltip": "可选输入的第5张图片"}),
|
||||
"image6": ("IMAGE", {"tooltip": "可选输入的第6张图片"}),
|
||||
"image7": ("IMAGE", {"tooltip": "可选输入的第7张图片"}),
|
||||
"image8": ("IMAGE", {"tooltip": "可选输入的第8张图片"}),
|
||||
"image9": ("IMAGE", {"tooltip": "可选输入的第9张图片"}),
|
||||
"previous_files": ("GEMINI_INPUT_FILES", {"tooltip": "如超过9张图需继续打包,可连接上一个该节点输出的files"}),
|
||||
}
|
||||
}
|
||||
|
||||
RETURN_TYPES = ("GEMINI_INPUT_FILES",)
|
||||
RETURN_NAMES = ("files",)
|
||||
FUNCTION = "pack_images"
|
||||
CATEGORY = "PD_Tools"
|
||||
|
||||
def pack_images(self, image1, image2=None, image3=None, image4=None,
|
||||
image5=None, image6=None, image7=None, image8=None,
|
||||
image9=None, previous_files=None):
|
||||
|
||||
files_list = []
|
||||
|
||||
# 如果需要无缝串联别的节点输出的 files ,优先加进去
|
||||
if previous_files is not None:
|
||||
files_list.extend(previous_files)
|
||||
|
||||
images = [image1, image2, image3, image4, image5, image6, image7, image8, image9]
|
||||
|
||||
for img in images:
|
||||
if img is not None:
|
||||
# 兼容万一单条线里输入了 Batch(N) 图片的特殊情况,分别解开处理
|
||||
for i in range(img.shape[0]):
|
||||
try:
|
||||
# 转换成 Base64
|
||||
img_b64 = tensor_to_base64_string(img[i].unsqueeze(0))
|
||||
# 构造为 Gemini 认证的 Part
|
||||
new_part = GeminiPart(
|
||||
inlineData=GeminiInlineData(
|
||||
mimeType=GeminiMimeType.image_png,
|
||||
data=img_b64
|
||||
)
|
||||
)
|
||||
# 转成字典防止 Pydantic 跨模块传递对象时校验失败
|
||||
files_list.append(new_part.model_dump(exclude_none=True))
|
||||
except Exception as e:
|
||||
print(f"[Error] PD_comfyplus_image pack failed for an image: {e}")
|
||||
|
||||
return (files_list,)
|
||||
|
||||
|
||||
# 注册节点
|
||||
NODE_CLASS_MAPPINGS = {
|
||||
"PD_comfyplus_image": PD_ComfyPlusImage
|
||||
}
|
||||
|
||||
NODE_DISPLAY_NAME_MAPPINGS = {
|
||||
"PD_comfyplus_image": "PD_comfyplus_image (Multi-Size Pack)"
|
||||
}
|
||||
+352
@@ -0,0 +1,352 @@
|
||||
# PD ComfyUI API Nodes
|
||||
|
||||
ComfyUI 自定义节点集合,用于调用各种 AI 图像生成 API(GPT Image、Gemini Image、Flux Kontext)。
|
||||
|
||||
## 📦 节点列表
|
||||
|
||||
### ComfyUI AuthToken 版本(通过 ComfyUI 代理)
|
||||
ComfyUI AuthToken 版本(通过 ComfyUI 代理)
|
||||
- PD: GPT Image (comfyui_AuthToken)
|
||||
- PD: Gemini Image (comfyui_AuthToken)
|
||||
- PD: Flux Kontext (comfyui_AuthToken)
|
||||
|
||||
### 直接 API Key 版本直接调用官方comfyui的key
|
||||
|
||||
API Key 版本:
|
||||
- 只保留 ComfyUI API Key 输入
|
||||
- 参数和调用逻辑跟 ComfyUI 官方自带的 API 节点一样 - 只是在调用时可以手动填入 key
|
||||
|
||||
| 节点名称 | 文件 | 说明 |
|
||||
|---------|------|------|
|
||||
| **PD: GPT Image (apikey)** | `IMAGE_GPT_apikey.py` | OpenAI GPT Image 生成/编辑 |
|
||||
| **PD: Gemini Image (apikey)** | `IMGE_GeminiNode_apikey.py` | Google Gemini 图像生成 |
|
||||
| **PD: Gemini Pro Image (apikey)** | `IMGE_GeminiPro_apikey.py` | Google Gemini 3 Pro 高分辨率图像生成 |
|
||||
| **PD: Flux Kontext (apikey)** | `IMGE_kontext_apikey.py` | Flux Kontext 图像编辑 |
|
||||
|
||||
---
|
||||
|
||||
## 🔑 两种版本的区别
|
||||
|
||||
### ComfyUI AuthToken 版本 (comfyui_AuthToken)
|
||||
|
||||
**认证方式**:使用 ComfyUI Auth Token,pandy自制油猴插件获取。
|
||||
|
||||
**认证头格式**:`Authorization: Bearer {auth_token}`
|
||||
|
||||
**使用示例**:
|
||||
```
|
||||
auth_token: eyJhbGciOiJSUzI1NiIsImtpZCI6ImEzOGVhNmEw...
|
||||
```
|
||||
|
||||
### API Key 版本 (apikey)
|
||||
|
||||
**认证方式**:使用 ComfyUI 官方 API Key
|
||||
|
||||
**认证头格式**:`X-API-KEY: {api_key}` (与 ComfyUI 官方 API 节点完全一致)
|
||||
|
||||
**使用示例**:
|
||||
```
|
||||
api_key: comfy_xxxxxxxxxxxxxxxxxxxxx
|
||||
```
|
||||
|
||||
**特点**:
|
||||
- 参数和调用逻辑跟 ComfyUI 官方自带的 API 节点一样
|
||||
- 只是在调用时可以手动填入 key
|
||||
- 直接使用官方账号的 key 申请下来填入使用
|
||||
|
||||
|
||||
## 📖 节点详细说明
|
||||
|
||||
### 1. GPT Image 节点
|
||||
|
||||
**功能**:
|
||||
- 文生图(Text-to-Image)
|
||||
- 图片编辑(Image Editing)
|
||||
- 图片修复(Inpainting with Mask)
|
||||
|
||||
**支持模型**:
|
||||
|
||||
**AuthToken 版本**:
|
||||
- `gpt-image-1`:OpenAI GPT Image 1(推荐)
|
||||
- `gpt-image-1.5`:OpenAI GPT Image 1.5(更高质量)
|
||||
|
||||
|
||||
### 3. Gemini Pro Image 节点(Nano Banana Pro)
|
||||
|
||||
**功能**:
|
||||
- 高分辨率图像生成(1K/2K/4K)
|
||||
- 文生图(Text-to-Image)
|
||||
- 图生图(Image-to-Image)
|
||||
- 支持多张参考图片(最多14张)
|
||||
- 同时输出图像和文本说明
|
||||
|
||||
**支持模型**:
|
||||
- `gemini-3-pro-image-preview`:Gemini 3 Pro Image(最高质量)
|
||||
|
||||
**参数说明**:
|
||||
- `prompt`:文本提示词,描述要生成的图像或编辑指令
|
||||
- `model`:gemini-3-pro-image-preview
|
||||
- `aspect_ratio`:宽高比
|
||||
- auto:自动匹配输入图片比例,无图片时默认 16:9
|
||||
- 1:1, 2:3, 3:2, 3:4, 4:3, 4:5, 5:4, 9:16, 16:9, 21:9
|
||||
- `resolution`:目标输出分辨率
|
||||
- 1K:标准分辨率
|
||||
- 2K:使用原生 Gemini 放大器
|
||||
- 4K:使用原生 Gemini 放大器(最高质量)
|
||||
- `response_modalities`:输出模式
|
||||
- IMAGE+TEXT:同时返回图像和文本说明
|
||||
- IMAGE:仅返回图像
|
||||
- `images`(可选):参考图片(最多14张,使用 Batch Images 节点)
|
||||
- `system_prompt`(可选):系统提示词,默认使用官方优化的图像生成提示词
|
||||
|
||||
**输出**:
|
||||
- `image`:生成的图像
|
||||
- `text`:文本说明(包含模型的思考过程和描述)
|
||||
|
||||
**成本估算**:
|
||||
- Gemini 3 Pro Image: 约 $0.12/张
|
||||
- 基于 token 使用量:$2/1M input + $12/1M output text + $120/1M output image
|
||||
|
||||
**特殊说明(apikey 版本)**:
|
||||
- 需要提供 `project_id`(Google Cloud 项目 ID)
|
||||
- 需要提供 `location`(区域,默认 us-central1)
|
||||
- 超时时间:180秒(因为高分辨率生成需要更长时间)
|
||||
|
||||
**使用建议**:
|
||||
- 适合需要高质量、高分辨率图像的场景
|
||||
- 2K/4K 分辨率会使用原生放大器,质量更好但成本更高
|
||||
- IMAGE+TEXT 模式可以看到模型的思考过程,有助于理解生成结果
|
||||
- 可以提供多张参考图片来引导生成风格
|
||||
|
||||
---
|
||||
|
||||
### 4. Flux Kontext 节点
|
||||
|
||||
**功能**:
|
||||
- 图片编辑(Image Editing)
|
||||
- 基于上下文的智能修改
|
||||
|
||||
**支持模型**:
|
||||
- `flux-kontext-pro`:快速版本
|
||||
- `flux-kontext-max`:高质量版本
|
||||
|
||||
**参数说明**:
|
||||
- `prompt`:编辑指令
|
||||
- `aspect_ratio`:宽高比
|
||||
- `guidance`:提示词遵循度(0.1-99.0)
|
||||
- `steps`:生成步数(1-150)
|
||||
- `prompt_upsampling`:AI 增强提示词
|
||||
- `input_image`(可选):输入图片
|
||||
|
||||
**成本估算**:
|
||||
- Kontext Pro: $0.05/张
|
||||
- Kontext Max: $0.10/张
|
||||
|
||||
**特点**:
|
||||
- 异步生成,需要轮询结果
|
||||
- 最长等待时间:10 分钟
|
||||
- 轮询间隔:2 秒
|
||||
|
||||
---
|
||||
|
||||
## 🚀 使用建议
|
||||
|
||||
### 选择 ComfyUI AuthToken 版本,如果你:
|
||||
- ✅ 已经有 ComfyUI Auth Token
|
||||
- ✅ 想要统一管理多个 AI 模型
|
||||
- ✅ 喜欢通过 ComfyUI 生态系统
|
||||
- ✅ 不想配置多个平台的 API Key
|
||||
|
||||
### 选择直接 API Key 版本,如果你:
|
||||
- ✅ 已经有各个平台的 API Key
|
||||
- ✅ 想要直接控制 API 调用
|
||||
- ✅ 不想依赖 ComfyUI 代理服务
|
||||
- ✅ 需要更灵活的配置(如 Gemini 的 project_id)
|
||||
|
||||
---
|
||||
|
||||
## 📝 使用示例
|
||||
|
||||
### 示例 1:使用 ComfyUI AuthToken 生成图片(GPT Image)
|
||||
|
||||
```
|
||||
节点:PD: GPT Image (comfyui_AuthToken)
|
||||
|
||||
参数:
|
||||
- auth_token: eyJhbGciOiJSUzI1NiIsImtpZCI6ImEzOGVhNmEw...
|
||||
- prompt: A futuristic city with flying cars at sunset
|
||||
- model: gpt-image-1
|
||||
- quality: medium
|
||||
- background: auto
|
||||
- size: 1024x1024
|
||||
- n: 1
|
||||
```
|
||||
|
||||
### 示例 2:使用直接 API Key 生成图片(DALL-E)
|
||||
|
||||
```
|
||||
节点:PD: GPT Image (apikey)
|
||||
|
||||
参数:
|
||||
- api_key: sk-proj-abc123...
|
||||
- prompt: A futuristic city with flying cars at sunset
|
||||
- model: dall-e-3
|
||||
- quality: hd
|
||||
- size: 1024x1792
|
||||
```
|
||||
|
||||
### 示例 3:使用 Gemini 生成图片(apikey 版本)
|
||||
|
||||
```
|
||||
节点:PD: Gemini Image (apikey)
|
||||
|
||||
参数:
|
||||
- api_key: AIzaSyD...
|
||||
- project_id: my-project-123
|
||||
- location: us-central1
|
||||
- prompt: A beautiful landscape with mountains
|
||||
- model: gemini-2.5-flash-image
|
||||
- aspect_ratio: 16:9
|
||||
- resolution: 2K
|
||||
```
|
||||
|
||||
### 示例 4:使用 Flux Kontext 编辑图片
|
||||
|
||||
```
|
||||
节点:PD: Flux Kontext (comfyui_AuthToken)
|
||||
|
||||
参数:
|
||||
- auth_token: eyJhbGciOiJSUzI1NiIsImtpZCI6ImEzOGVhNmEw...
|
||||
- prompt: Change the sky to sunset colors
|
||||
- model: flux-kontext-pro
|
||||
- aspect_ratio: 16:9
|
||||
- guidance: 3.0
|
||||
- steps: 50
|
||||
- input_image: [连接输入图片]
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## ⚠️ 注意事项
|
||||
|
||||
1. **API Key 安全**:
|
||||
- 不要在公开的工作流中暴露 API Key
|
||||
- 建议使用环境变量或配置文件管理
|
||||
|
||||
2. **成本控制**:
|
||||
- 所有节点都会显示预估成本
|
||||
- 实际成本以各平台账单为准
|
||||
- 建议设置 API 使用限额
|
||||
|
||||
3. **超时设置**:
|
||||
- GPT/Gemini: 120 秒超时
|
||||
- Flux Kontext: 10 分钟超时(包含轮询)
|
||||
|
||||
4. **错误处理**:
|
||||
- 所有节点都有详细的错误日志
|
||||
- 检查 ComfyUI 控制台查看详细信息
|
||||
|
||||
5. **网络要求**:
|
||||
- 需要稳定的网络连接
|
||||
- 某些地区可能需要代理访问
|
||||
|
||||
---
|
||||
|
||||
## 🔧 故障排除
|
||||
|
||||
### 问题 1:认证失败(401 错误)
|
||||
|
||||
**ComfyUI AuthToken 版本**:
|
||||
- 检查 Auth Token 是否正确
|
||||
- Token 可能已过期,重新获取
|
||||
- 认证头格式:`Authorization: Bearer {auth_token}`
|
||||
|
||||
**直接 API Key 版本**:
|
||||
- 检查 API Key 格式是否正确
|
||||
- 确认 API Key 有效且有余额
|
||||
- 认证头格式:`X-API-KEY: {api_key}` (与 ComfyUI 官方 API 节点一致)
|
||||
- Gemini 需要确认 Project ID 和 Location 正确
|
||||
|
||||
### 问题 2:请求超时
|
||||
|
||||
- 检查网络连接
|
||||
- 尝试增加超时时间
|
||||
- Flux Kontext 可能需要更长时间,属于正常现象
|
||||
|
||||
### 问题 3:图片无法生成 / 返回黑图
|
||||
|
||||
**症状**:节点返回 512x512 的黑色图片
|
||||
|
||||
**可能原因**:
|
||||
1. **API 端点错误**(已修复):
|
||||
- 旧版本的 Gemini AuthToken 节点使用了错误的端点格式
|
||||
- 错误:`https://api.comfy.org/proxy/vertexai/gemini/{model}:generateContent`
|
||||
- 正确:`https://api.comfy.org/proxy/vertexai/gemini/{model}`
|
||||
- **解决方案**:更新到最新版本(v2.1+)
|
||||
|
||||
2. **模型名称不支持**:
|
||||
- 检查控制台是否有 "Unsupported model" 错误
|
||||
- 确认使用的模型在支持列表中
|
||||
- Gemini AuthToken 版本支持:`gemini-2.5-flash-image`, `gemini-2.5-flash-image-preview`
|
||||
|
||||
3. **提示词违规**:
|
||||
- 检查提示词是否符合内容政策
|
||||
- 查看控制台日志获取详细错误信息
|
||||
|
||||
4. **API 配额不足**:
|
||||
- 确认账户有足够的配额/余额
|
||||
- 检查 API 使用限制
|
||||
|
||||
**调试步骤**:
|
||||
1. 打开 ComfyUI 控制台
|
||||
2. 查找 `[GEMINI_AUTHTOKEN]` 或 `[GEMINI_APIKEY]` 开头的日志
|
||||
3. 检查是否有 API Error 信息
|
||||
4. 根据错误信息采取相应措施
|
||||
|
||||
### 问题 4:认证失败(Gemini 节点)
|
||||
|
||||
- 确认已启用 Vertex AI API
|
||||
- 确认 Project ID 正确
|
||||
- 确认 Location 支持 Gemini 模型
|
||||
- 检查 API Key 权限
|
||||
|
||||
---
|
||||
|
||||
## 📚 相关链接
|
||||
|
||||
- [ComfyUI 官网](https://www.comfy.org)
|
||||
- [OpenAI Platform](https://platform.openai.com)
|
||||
- [Google Cloud Console](https://console.cloud.google.com)
|
||||
- [Black Forest Labs](https://blackforestlabs.ai)
|
||||
- [ComfyUI GitHub](https://github.com/comfyanonymous/ComfyUI)
|
||||
|
||||
---
|
||||
|
||||
## 📄 许可证
|
||||
|
||||
本项目遵循 MIT 许可证。
|
||||
|
||||
---
|
||||
|
||||
## 🤝 贡献
|
||||
|
||||
欢迎提交 Issue 和 Pull Request!
|
||||
|
||||
---
|
||||
|
||||
## 📧 联系方式
|
||||
|
||||
如有问题或建议,请通过 GitHub Issues 联系。
|
||||
|
||||
---
|
||||
|
||||
**最后更新**:2025-01-12
|
||||
**版本**:2.5
|
||||
|
||||
**更新日志**:
|
||||
- 2025-01-12 v2.5: 修复 API Key 版本认证头问题(401 错误),使用 `X-API-KEY` 与官方一致
|
||||
- 2025-01-12 v2.4: 新增 Gemini Pro Image (Nano Banana Pro) 节点,支持高分辨率生成
|
||||
- 2025-01-12 v2.3: 更新 GPT Image AuthToken 节点参数,匹配官方 ComfyUI API 节点
|
||||
- 2025-01-12 v2.2: 添加官方 ComfyUI API 节点的默认 system prompt
|
||||
- 2025-01-12 v2.1: 修复 Gemini AuthToken 版本黑图问题,更新模型列表
|
||||
- 2025-01-11 v2.0: 初始版本,支持 AuthToken 和 API Key 两种认证方式
|
||||
Reference in New Issue
Block a user