Run Z-Image-Turbo locally on your Mac with just two commands.
Z-Image-Turbo is an efficient single-stream diffusion transformer model from Tongyi Lab. This project provides a plug-and-play solution to run it natively on Apple Silicon (M1/M2/M3/M4) with an intuitive web interface.
┌─────────────────────────────────────────────────────────────┐
│ │
│ Your Mac (M1/M2/M3/M4) │
│ │
│ ┌─────────────┐ ┌─────────────────────────────────┐ │
│ │ Gradio │ HTTP │ FastAPI Model Server │ │
│ │ Web UI │─────▶│ Z-Image-Turbo (MPS) │ │
│ │ (main.py) │ │ (model_server.py) │ │
│ └─────────────┘ └─────────────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────┐ │
│ │ Grok API │ Optional: AI-powered prompt enhancement │
│ │ (xAI) │ │
│ └─────────────┘ │
│ │
└─────────────────────────────────────────────────────────────┘
- Native Apple Silicon Support - Optimized for MPS (Metal Performance Shaders)
- Fast Inference - ~8 steps for high-quality images with bfloat16 acceleration
- Two Input Modes
- Chat Mode: Describe in natural language, AI enhances your prompt
- Direct Mode: Full control with your own detailed prompts
- Flexible Resolutions - 512px to 1280px with various aspect ratios
- History & Persistence - All generated images saved to local database
- Bilingual UI - English and Chinese interface
- macOS with Apple Silicon (M1/M2/M3/M4)
- Python 3.12+
- uv package manager
# Clone the repository
git clone https://github.com/OrdinarySF/z-image-inference.git
cd z-image-inference
# Install dependencies (uv will handle everything)
uv syncStep 1: Start the model server (first run will download ~6GB model)
uv run python model_server.pyStep 2: In a new terminal, launch the web UI
uv run python main.pyOpen http://127.0.0.1:7860 in your browser. That's it!
Perfect for users who want full control over the prompt:
- Go to "Direct Input Prompt" tab
- Enter your detailed prompt
- Adjust resolution and steps
- Click "Generate Image"
Requires xAI API key for AI-powered prompt enhancement:
- Create
.envfile with your API key:XAI_API_KEY=your_api_key_here - Go to "Chat Mode" tab
- Describe your image in natural language
- The AI will transform it into an optimized prompt and generate the image
| Variable | Required | Description |
|---|---|---|
XAI_API_KEY |
No | xAI API key for Chat Mode prompt enhancement |
| Category | Available Ratios |
|---|---|
| 512px | 1:1, 5:3, 3:5, 4:3, 3:4, 16:9, 9:16 |
| 768px | 1:1, 5:3, 3:5, 4:3, 3:4, 16:9, 9:16 |
| 1024px | 1:1, 5:3, 3:5, 4:3, 3:4, 16:9, 9:16 |
| 1280px | 1:1, 3:2, 2:3, 16:9, 9:16 |
- Default: 8 - Good balance of speed and quality
- 4-6 - Faster, slightly lower quality
- 10-20 - Higher quality, slower generation
| Component | Minimum | Recommended |
|---|---|---|
| macOS | Ventura 13.0+ | Sonoma 14.0+ |
| Chip | Apple M1 | Apple M2/M3/M4 |
| Memory | 16GB | 32GB+ |
| Storage | 10GB free | 20GB+ free |
This project includes several optimizations for Apple MPS:
- bfloat16 precision for transformer blocks
- float32 VAE to prevent NaN artifacts
- CPU-based generator for reproducible seeds
- Attention slicing to reduce memory usage
- Memory cache clearing after each generation
z-image-inference/
├── main.py # Gradio web interface
├── model_server.py # FastAPI inference server
├── grok_client.py # xAI Grok API client
├── i18n/ # Internationalization
│ ├── __init__.py
│ └── translations.yaml
├── pyproject.toml # Project dependencies
└── history.db # Generated images database
Model server won't start?
- Ensure you have enough free memory (close other apps)
- First run downloads ~6GB model, be patient
Black or corrupted images?
- This is usually a MPS memory issue
- Try lowering resolution or closing other apps
- Restart the model server
Chat mode not working?
- Check if
XAI_API_KEYis set in.env - Verify your API key is valid
- Tongyi Lab for Z-Image-Turbo model
- Hugging Face for diffusers library
- Gradio for the web interface framework
- z-image-mps for MPS optimization inspiration
Apache License 2.0 - See LICENSE for details.
两条命令,在 Mac 上本地运行 Z-Image-Turbo。
- 原生 Apple Silicon 支持 - 针对 MPS (Metal) 深度优化
- 快速推理 - 约 8 步即可生成高质量图像,bfloat16 加速
- 双输入模式
- Chat 模式:用自然语言描述,AI 自动优化 prompt
- 直接输入模式:完全控制,直接输入详细 prompt
- 灵活分辨率 - 512px 到 1280px,多种宽高比
- 历史记录 - 所有生成的图片自动保存到本地数据库
- 中英双语界面
- 搭载 Apple Silicon 芯片的 Mac(M1/M2/M3/M4)
- Python 3.12+
- uv 包管理器
# 克隆仓库
git clone https://github.com/OrdinarySF/z-image-inference.git
cd z-image-inference
# 安装依赖(uv 会自动处理一切)
uv sync第一步:启动模型服务(首次运行会下载约 6GB 模型)
uv run python model_server.py第二步:新开一个终端,启动 Web 界面
uv run python main.py浏览器打开 http://127.0.0.1:7860 即可使用!
适合想要完全控制 prompt 的用户:
- 切换到 "直接输入 Prompt" 标签页
- 输入详细的 prompt
- 调整分辨率和步数
- 点击 "生成图片"
需要 xAI API 密钥 来启用 AI prompt 增强:
- 在项目根目录创建
.env文件:XAI_API_KEY=your_api_key_here - 切换到 "Chat 模式" 标签页
- 用自然语言描述你想要的图片
- AI 会将其转化为优化的 prompt 并生成图片
| 组件 | 最低配置 | 推荐配置 |
|---|---|---|
| macOS | Ventura 13.0+ | Sonoma 14.0+ |
| 芯片 | Apple M1 | Apple M2/M3/M4 |
| 内存 | 16GB | 32GB+ |
| 存储 | 10GB 可用空间 | 20GB+ 可用空间 |
- 通义实验室 的 Z-Image-Turbo 模型
- Hugging Face 的 diffusers 库
- Gradio Web 界面框架
- z-image-mps 提供 MPS 性能优化灵感
Apache License 2.0