這一篇主要是做一個開源工具的附屬產品,之前提到過 Modal 這個平台,它時不時有活動可以薅羊毛,並且帳號每個月有 $30 的額度,是一個不錯的體驗平台。
然後由於贈送的額度一直沒用,所以就簡單寫了一個工具,用來做一鍵部署。所以順帶把才發布的 Z-Image 體驗了一把。
需要用到的材料
Comfy-Org : Z-Image ComfyUI 版本,主要用於 ComfyUI 部署



幾張圖片的效果:
簡單體驗了一下,生產力應該是足夠了,出圖很快,效果也很好。
開源的模型,你想生成什麼都可以。
# =============================================================================
# Z-Image-Turbo ComfyUI 一鍵部署服務
# =============================================================================
# 啟動命令: modal deploy z_image_turbo_deploy.py
# =============================================================================
import json
import os
import subprocess
from pathlib import Path
import modal
# =============================================================================
# S1: 環境準備 - 構建基礎鏡像
# =============================================================================
image = (
modal.Image.debian_slim(python_version="3.11")
.apt_install("git", "wget", "curl")
.pip_install(
"fastapi[standard]==0.115.4",
"comfy-cli==1.5.3",
"requests==2.32.3",
)
.run_commands("comfy --skip-prompt install --fast-deps --nvidia")
)
# HuggingFace Secret
try:
hf_secret = modal.Secret.from_name("huggingface-secret")
except modal.exception.NotFoundError:
hf_secret = None
# =============================================================================
# S2: 模型下載 - 從 Tongyi-MAI/Z-Image-Turbo 下載 3 個核心模型
# =============================================================================
def hf_download():
"""
下載 Z-Image-Turbo 模型:
- z_image_turbo_bf16.safetensors (主擴散模型)
- qwen_3_4b.safetensors (CLIP 文本編碼器)
- ae.safetensors (VAE 解碼器)
"""
from huggingface_hub import hf_hub_download
hf_token = os.getenv("HF_TOKEN")
repo_id = "Comfy-Org/z_image_turbo"
print(f"📦 從 {repo_id} 下載模型...")
# 模型配置列表 (文件路徑包含 split_files/ 前綴)
models = [
{
"filename": "split_files/diffusion_models/z_image_turbo_bf16.safetensors",
"target_dir": "/root/comfy/ComfyUI/models/diffusion_models",
"target_name": "z_image_turbo_bf16.safetensors",
"desc": "主擴散模型"
},
{
"filename": "split_files/text_encoders/qwen_3_4b.safetensors",
"target_dir": "/root/comfy/ComfyUI/models/clip",
"target_name": "qwen_3_4b.safetensors",
"desc": "CLIP 文本編碼器"
},
{
"filename": "split_files/vae/ae.safetensors",
"target_dir": "/root/comfy/ComfyUI/models/vae",
"target_name": "ae.safetensors",
"desc": "VAE 解碼器"
}
]
for model in models:
print(f"📥 下載 {model['desc']} : {model['target_name']}...")
cached_path = hf_hub_download(
repo_id=repo_id,
filename=model["filename"],
cache_dir="/cache",
token=hf_token
)
Path(model["target_dir"]).mkdir(parents=True, exist_ok=True)
target_path = f"{model['target_dir']}/{model['target_name']}"
subprocess.run(f"ln -sf {cached_path} {target_path}", shell=True, check=True)
print(f" ✅ {model['desc']} 完成")
print("🎉 所有模型下載完成!")
def create_workflow_file():
"""創建工作流 JSON 文件"""
workflow = {
"1": {
"class_type": "UNETLoader",
"inputs": {
"unet_name": "z_image_turbo_bf16.safetensors",
"weight_dtype": "default"
}
},
"2": {
"class_type": "DualCLIPLoader",
"inputs": {
"clip_name1": "qwen_3_4b.safetensors",
"clip_name2": "qwen_3_4b.safetensors",
"type": "z_image"
}
},
"3": {
"class_type": "VAELoader",
"inputs": {"vae_name": "ae.safetensors"}
},
"4": {
"class_type": "CLIPTextEncode",
"inputs": {
"text": "一位美麗的亞洲女性,照片級真實,自然光線,高清細節",
"clip": ["2", 0]
}
},
"5": {
"class_type": "CLIPTextEncode",
"inputs": {
"text": "低質量,模糊,畸形,醜陋,文字,水印",
"clip": ["2", 0]
}
},
"6": {
"class_type": "EmptyLatentImage",
"inputs": {"width": 1024, "height": 1024, "batch_size": 1}
},
"7": {
"class_type": "KSampler",
"inputs": {
"model": ["1", 0],
"positive": ["4", 0],
"negative": ["5", 0],
"latent_image": ["6", 0],
"seed": 42,
"steps": 4,
"cfg": 1.0,
"sampler_name": "euler",
"scheduler": "simple",
"denoise": 1.0
}
},
"8": {
"class_type": "VAEDecode",
"inputs": {"samples": ["7", 0], "vae": ["3", 0]}
},
"9": {
"class_type": "SaveImage",
"inputs": {"filename_prefix": "z_image_turbo", "images": ["8", 0]}
}
}
Path("/root/workflow_api.json").write_text(json.dumps(workflow, ensure_ascii=False, indent=2))
print("📝 工作流文件已創建")
# =============================================================================
# S3: 服務配置
# =============================================================================
vol = modal.Volume.from_name("z-image-turbo-test-cache", create_if_missing=True)
image = (
image.pip_install("huggingface_hub[hf_transfer]==0.34.4")
.env({"HF_HUB_ENABLE_HF_TRANSFER": "1"})
.run_function(
hf_download,
volumes={"/cache": vol},
secrets=[hf_secret] if hf_secret else []
)
.run_function(create_workflow_file)
)
app = modal.App(name="z-image-turbo-test", image=image)
# =============================================================================
# S4: UI 服務
# =============================================================================
@app.function(
max_containers=1,
gpu="L40S",
volumes={"/cache": vol},
timeout=86400
)
@modal.concurrent(max_inputs=10)
@modal.web_server(8000, startup_timeout=60)
def ui():
"""ComfyUI Web 界面"""
print("🌐 啟動 Z-Image-Turbo Web 界面...")
subprocess.Popen("comfy launch -- --listen 0.0.0.0 --port 8000", shell=True)
# =============================================================================
# S5: 本地入口點
# =============================================================================
@app.local_entrypoint()
def main():
print("=" * 60)
print("Z-Image-Turbo ComfyUI 一鍵部署")
print("=" * 60)
print("\n📦 模型來源: Comfy-Org/z_image_turbo")
print("\n📋 已下載模型:")
print(" - z_image_turbo_bf16.safetensors (主擴散模型)")
print(" - qwen_3_4b.safetensors (CLIP 文本編碼器)")
print(" - ae.safetensors (VAE 解碼器)")
print("\n📌 部署命令: modal deploy z_image_turbo_deploy.py")
print("=" * 60)
部署成功後:
PS D:\code\Modal安裝\modal-manager> modal deploy z_image_turbo_deploy.py Building image im-RuODNKSpmlSj8WWyALvgT8
=> Step 0: running function 'hf_download'
📦 從 Comfy-Org/z_image_turbo 下載模型...
📥 下載 主擴散模型: z_image_turbo_bf16.safetensors...
✅ 主擴散模型 完成
📥 下載 CLIP 文本編碼器: qwen_3_4b.safetensors...
✅ CLIP 文本編碼器 完成
📥 下載 VAE 解碼器: ae.safetensors...
✅ VAE 解碼器 完成
🎉 所有模型下載完成!
Saving image...
Image saved, took 1.50s
Finished image build for im-RuODNKSpmlSj8WWyALvgT8
Building image im-KblbHI0Rx9z0J54HpxvUH3
=> Step 0: running function 'create_workflow_file'
📝 工作流文件已創建
Saving image...
Image saved, took 1.21s
Finished image build for im-KblbHI0Rx9z111HpxvUH3
✓ Created objects.
├── 🔨 Created mount D:\code\學習資料\Modal安裝\modal-manager\z_image_turbo_deploy.py
├── 🔨 Created function hf_download.
├── 🔨 Created function create_workflow_file.
└── 🔨 Created web function ui => https://aubreyce5ju45p2le3t--z-image-turbo-test-ui.modal.run
✓ App deployed in 106.760s! 🎉
View Deployment: https://modal.com/apps/aubreyce5ju45p2le3t/main/deployed/z-image-turbo-test