# Python 3.10+: pip install comfy-sdk==0.4.0
import os
from comfy_sdk import Comfy
api_key = os.environ.get("COMFY_API_KEY", "").strip()
if not api_key: raise RuntimeError("Set COMFY_API_KEY")
os.environ.setdefault("COMFY_BASE_URL", "https://cloud.comfy.org")
client = Comfy(api_key=api_key)
workflow = client.workflows.from_json({
"132": {
"class_type": "LoadImage",
"inputs": {
"image": "dragon_fruit_cream.png"
},
"_meta": {
"title": "Load Image"
}
},
"143": {
"class_type": "SaveImage",
"inputs": {
"filename_prefix": "ComfyUI",
"images": [
"162:65",
0
]
},
"_meta": {
"title": "Save Image"
}
},
"149": {
"class_type": "Florence2Run",
"inputs": {
"do_sample": True,
"fill_mask": True,
"florence2_model": [
"150",
0
],
"image": [
"132",
0
],
"keep_model_loaded": False,
"max_new_tokens": 1024,
"num_beams": 3,
"output_mask_select": "",
"seed": 1093475924089169,
"task": "more_detailed_caption",
"text_input": ""
},
"_meta": {
"title": "Florence2Run"
}
},
"150": {
"class_type": "DownloadAndLoadFlorence2Model",
"inputs": {
"attention": "sdpa",
"convert_to_safetensors": False,
"model": "MiaoshouAI/Florence-2-large-PromptGen-v2.0",
"precision": "fp16"
},
"_meta": {
"title": "DownloadAndLoadFlorence2Model"
}
},
"157": {
"class_type": "PreviewAny",
"inputs": {
"source": [
"149",
2
]
},
"_meta": {
"title": "Preview as Text"
}
},
"158": {
"class_type": "ImageCompare",
"inputs": {
"compare_view": {
"__value__": [
"",
""
]
},
"image_a": [
"162:65",
0
],
"image_b": [
"132",
0
]
},
"_meta": {
"title": "Compare Images"
}
},
"162:164": {
"class_type": "ImageScaleToMaxDimension",
"inputs": {
"image": [
"132",
0
],
"largest_size": 2048,
"upscale_method": "lanczos"
},
"_meta": {
"title": "Scale Image to Max Dimension"
}
},
"162:62": {
"class_type": "CLIPLoader",
"inputs": {
"clip_name": "qwen_3_4b.safetensors",
"device": "default",
"type": "lumina2"
},
"_meta": {
"title": "Load CLIP"
}
},
"162:63": {
"class_type": "VAELoader",
"inputs": {
"vae_name": "ae.safetensors"
},
"_meta": {
"title": "Load VAE"
}
},
"162:65": {
"class_type": "VAEDecode",
"inputs": {
"samples": [
"162:69",
0
],
"vae": [
"162:63",
0
]
},
"_meta": {
"title": "VAE Decode"
}
},
"162:66": {
"class_type": "UNETLoader",
"inputs": {
"unet_name": "z_image_turbo_bf16.safetensors",
"weight_dtype": "default"
},
"_meta": {
"title": "Load Diffusion Model"
}
},
"162:67": {
"class_type": "CLIPTextEncode",
"inputs": {
"clip": [
"162:62",
0
],
"text": "masterpiece, 8k"
},
"_meta": {
"title": "CLIP Text Encode (Positive Prompt)"
}
},
"162:69": {
"class_type": "KSampler",
"inputs": {
"cfg": 1,
"denoise": 0.33,
"latent_image": [
"162:80",
0
],
"model": [
"162:70",
0
],
"negative": [
"162:71",
0
],
"positive": [
"162:67",
0
],
"sampler_name": "dpmpp_2m_sde",
"scheduler": "beta",
"seed": 344777149081245,
"steps": 5
},
"_meta": {
"title": "KSampler"
}
},
"162:70": {
"class_type": "ModelSamplingAuraFlow",
"inputs": {
"model": [
"162:66",
0
],
"sampling": "flow",
"shift": 3
},
"_meta": {
"title": "ModelSamplingAuraFlow"
}
},
"162:71": {
"class_type": "CLIPTextEncode",
"inputs": {
"clip": [
"162:62",
0
],
"text": ""
},
"_meta": {
"title": "CLIP Text Encode (Negative Prompt)"
}
},
"162:76": {
"class_type": "UpscaleModelLoader",
"inputs": {
"model_name": "RealESRGAN_x4plus.safetensors"
},
"_meta": {
"title": "Load Upscale Model"
}
},
"162:79": {
"class_type": "ImageUpscaleWithModel",
"inputs": {
"image": [
"162:164",
0
],
"upscale_model": [
"162:76",
0
]
},
"_meta": {
"title": "Upscale Image (using Model)"
}
},
"162:80": {
"class_type": "VAEEncode",
"inputs": {
"pixels": [
"162:81",
0
],
"vae": [
"162:63",
0
]
},
"_meta": {
"title": "VAE Encode"
}
},
"162:81": {
"class_type": "ImageScaleBy",
"inputs": {
"image": [
"162:79",
0
],
"scale_by": 0.5,
"upscale_method": "lanczos"
},
"_meta": {
"title": "Upscale Image By"
}
}
})
# run() also passes api_key to the partner nodes in this workflow.
job = client.run(workflow, api_key=api_key)