前言:為什麼要整合 Hugging Face?

當你想讓 AI 助理(如 OpenClaw 上的 Claude)具備圖片生成能力時,通常會遇到幾個問題:

  1. OpenAI DALL·E:需要付費 API,每張圖片約 $0.02-0.08
  2. 本地 Stable Diffusion:需要高階顯卡(至少 8GB VRAM),配置複雜
  3. 其他線上服務:註冊麻煩、額度限制、服務不穩

Hugging Face Inference API 提供了一個完美的解決方案:

完全免費(有合理 rate limit)
無需顯卡(雲端運算)
多模型支援(FLUX.1、SD3、SDXL 等)
簡單易用(一個 HTTP 請求即可)
適合整合(RESTful API,Python/CLI 友善)

這篇文章將教你如何建立一個 OpenClaw Skill,讓 AI 助理能隨時呼叫 Hugging Face 生成圖片。


Hugging Face Inference API 介紹

什麼是 Inference API?

Hugging Face 的 Inference API 是一個託管的機器學習模型推理服務。你只需提供模型名稱和輸入,HF 就會幫你執行模型並返回結果。

對於圖片生成(text-to-image):

  • 輸入:文字提示詞(prompt)
  • 輸出:PNG/JPEG 圖片檔案(二進位資料)
  • 延遲:首次呼叫 20-60 秒(冷啟動),後續 3-10 秒

支援的熱門模型

模型 特色 適用場景
FLUX.1-dev 最高品質,擬真度極高 寫實風格、人像、產品圖
FLUX.1-schnell 快速版本,品質仍佳 快速迭代、草圖生成
Stable Diffusion 3 平衡品質與速度 通用場景、藝術創作
SDXL 經典可靠 需要穩定輸出時
Playground v2.5 美學導向 藝術插圖、概念圖

免費額度與限制

免費使用者(無付費訂閱):

  • ✅ 每日約 1000 次請求(所有模型共享)
  • ✅ 無需信用卡
  • ⚠️ 並發限制(避免濫用)
  • ⚠️ 冷啟動時間較長(首次呼叫 20-60 秒)
  • ⚠️ 優先權低於付費用戶

付費用戶(Hugging Face Pro,$9/月):

  • ⏩ 更快的推理速度
  • ⏩ 更高的並發額度
  • ⏩ 免冷啟動(模型常駐記憶體)

對於個人使用或小型專案,免費額度完全夠用


申請 HF Token 步驟

1. 註冊 Hugging Face 帳號

前往 https://huggingface.co/join 完成註冊(可用 GitHub 快速登入)。

2. 生成 API Token

  1. 登入後前往 Settings → Access Tokens
  2. 點擊 “New token” 按鈕
  3. 設定如下:
    • Name: openclaw-inference(或任意名稱)
    • Type: 選擇 Read(讀取權限即可,無需 Write)
    • Repository access: 保持預設(All repositories)
  4. 點擊 “Generate token”
  5. 立即複製 token(格式為 hf_xxxxxxxxxxxxxxxxxxxxx

⚠️ 重要:Token 只會顯示一次,請妥善保存!

3. 設定環境變數

永久設定(推薦)

編輯 ~/.bashrc~/.zshrc

export HF_TOKEN="hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

套用設定:

source ~/.bashrc  # 或 source ~/.zshrc

臨時設定(單次使用)

export HF_TOKEN="hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

驗證設定:

echo $HF_TOKEN

Skill 安裝與設定

1. 建立 Skill 目錄

mkdir -p ~/.openclaw/workspace/skills/hf-image/scripts

2. 下載 Python 腳本

將以下腳本儲存為 ~/.openclaw/workspace/skills/hf-image/scripts/hf_image.py

#!/usr/bin/env python3
"""
Hugging Face Inference API - Text-to-Image Generator
"""

import argparse
import json
import os
import sys
import time
from pathlib import Path

import requests

DEFAULT_MODEL = "black-forest-labs/FLUX.1-dev"
API_BASE = "https://api-inference.huggingface.co/models"

POPULAR_MODELS = {
    "flux": "black-forest-labs/FLUX.1-dev",
    "flux-schnell": "black-forest-labs/FLUX.1-schnell",
    "sd3": "stabilityai/stable-diffusion-3-medium-diffusers",
    "sdxl": "stabilityai/stable-diffusion-xl-base-1.0",
    "playground": "playgroundai/playground-v2.5-1024px-aesthetic",
}


def generate_image(prompt, model=DEFAULT_MODEL, token=None, max_retries=3):
    """Generate image using HF Inference API with retry logic"""
    if not token:
        token = os.getenv("HF_TOKEN")
        if not token:
            raise Exception("HF_TOKEN not found")
    
    if model in POPULAR_MODELS:
        model = POPULAR_MODELS[model]
    
    api_url = f"{API_BASE}/{model}"
    headers = {"Authorization": f"Bearer {token}"}
    payload = {"inputs": prompt}
    
    for attempt in range(max_retries):
        response = requests.post(api_url, headers=headers, json=payload, timeout=60)
        
        if response.status_code == 200:
            return response.content
        
        # Handle model loading (503)
        if response.status_code == 503:
            error_data = response.json()
            if "loading" in error_data.get("error", "").lower():
                wait_time = error_data.get("estimated_time", 20)
                print(f"⏳ Model loading... Retry in {wait_time}s", file=sys.stderr)
                time.sleep(wait_time)
                continue
        
        # Rate limit (429)
        if response.status_code == 429:
            print("⚠️  Rate limit. Retry in 10s...", file=sys.stderr)
            time.sleep(10)
            continue
        
        raise Exception(f"API error ({response.status_code}): {response.text}")
    
    raise Exception(f"Failed after {max_retries} retries")


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="HF Inference API Image Generator")
    parser.add_argument("--prompt", required=True)
    parser.add_argument("--model", default=DEFAULT_MODEL)
    parser.add_argument("--output", "-o")
    parser.add_argument("--json", action="store_true")
    
    args = parser.parse_args()
    
    try:
        print(f"🎨 Generating with {args.model}...", file=sys.stderr)
        image_data = generate_image(args.prompt, args.model)
        
        # Determine output path
        if args.output:
            output_path = Path(args.output)
        else:
            safe_name = "".join(c if c.isalnum() else '_' for c in args.prompt[:50])
            output_path = Path(f"hf-{safe_name}-{int(time.time())}.png")
        
        output_path.parent.mkdir(parents=True, exist_ok=True)
        output_path.write_bytes(image_data)
        
        if args.json:
            print(json.dumps({"success": True, "path": str(output_path.absolute())}))
        else:
            print(f"✅ Saved: {output_path.absolute()}", file=sys.stderr)
            print(str(output_path.absolute()))
    
    except Exception as e:
        if args.json:
            print(json.dumps({"success": False, "error": str(e)}))
        else:
            print(f"❌ {e}", file=sys.stderr)
        sys.exit(1)

賦予執行權限:

chmod +x ~/.openclaw/workspace/skills/hf-image/scripts/hf_image.py

3. 安裝相依套件

/home/linuxbrew/.linuxbrew/bin/python3 -m pip install requests

4. 建立 SKILL.md

(完整文件請參考 GitHub 上的 Skill 範例


使用方式

CLI 直接呼叫

基本用法

~/.openclaw/workspace/skills/hf-image/scripts/hf_image.py \
  --prompt "A serene mountain landscape at sunset"

輸出:

🎨 Generating with black-forest-labs/FLUX.1-dev...
✅ Saved: /home/user/hf-A_serene_mountain_landscape_at_sunset-1712456789.png
/home/user/hf-A_serene_mountain_landscape_at_sunset-1712456789.png

指定模型

# 使用快速模型
~/.openclaw/workspace/skills/hf-image/scripts/hf_image.py \
  --prompt "Cyberpunk city street" \
  --model flux-schnell

# 使用 Stable Diffusion 3
~/.openclaw/workspace/skills/hf-image/scripts/hf_image.py \
  --prompt "Abstract digital art" \
  --model sd3

自訂輸出路徑

~/.openclaw/workspace/skills/hf-image/scripts/hf_image.py \
  --prompt "Majestic dragon in flight" \
  --output ~/Pictures/dragon.png

JSON 模式(適合腳本)

~/.openclaw/workspace/skills/hf-image/scripts/hf_image.py \
  --prompt "Futuristic robot" \
  --json

輸出:

{
  "success": true,
  "path": "/home/user/hf-Futuristic_robot-1712456890.png"
}

在 OpenClaw 中使用

當你在聊天中請求 AI 生成圖片時,OpenClaw 會自動呼叫此 Skill:

對話範例:

:幫我生成一張賽博龐克風格的城市夜景

Claude:正在使用 FLUX.1-dev 生成圖片…

✅ 圖片已生成:/home/user/hf-cyberpunk_city_night-1712456999.png

[圖片預覽]

背後執行的命令:

~/.openclaw/workspace/skills/hf-image/scripts/hf_image.py \
  --prompt "Cyberpunk city night scene with neon lights" \
  --model flux \
  --json

可用模型推薦與比較

實測比較(相同 prompt)

Prompt: “A wise elderly wizard with long white beard, holding a glowing staff, mystical atmosphere, photorealistic”

模型 生成時間 品質評分 特色
FLUX.1-dev 45s(首次)/ 8s ⭐⭐⭐⭐⭐ 細節豐富,光影逼真,最接近攝影作品
FLUX.1-schnell 30s(首次)/ 4s ⭐⭐⭐⭐ 速度快,品質仍佳,適合快速迭代
SD3 35s(首次)/ 6s ⭐⭐⭐⭐ 色彩飽和,藝術感強,偏插畫風
SDXL 25s(首次)/ 5s ⭐⭐⭐ 穩定可靠,但細節不如新模型
Playground v2.5 40s(首次)/ 7s ⭐⭐⭐⭐ 美學化處理,適合概念圖

選擇建議

  • 追求極致品質flux(FLUX.1-dev)
  • 快速原型/測試flux-schnell
  • 平衡品質與速度sd3
  • 需要穩定輸出sdxl
  • 藝術創作playground

Rate Limit 說明

觸發 Rate Limit 的情況

  1. 短時間內大量請求(如 1 分鐘內 10+ 請求)
  2. 多設備同時使用同一 token
  3. 使用熱門模型的尖峰時段

錯誤訊息

⚠️  Rate limit hit. Retrying in 10s...

或 HTTP 429:

{
  "error": "Rate limit reached for requests",
  "estimated_time": 15.5
}

應對策略

  1. 請求間加入延遲:每次請求後等待 2-3 秒
  2. 批次處理:將多個請求分散到不同時段
  3. 使用快取:相同 prompt 不重複生成
  4. 選擇冷門模型:非尖峰時段使用較少人用的模型

自動重試機制

腳本內建 exponential backoff 重試邏輯:

# 第一次失敗 → 等待 5 秒
# 第二次失敗 → 等待 10 秒
# 第三次失敗 → 拋出錯誤

完整範例:批次生成

假設你要生成一組概念圖:

#!/bin/bash

PROMPTS=(
  "A medieval castle on a hill"
  "A futuristic space station"
  "An underwater city with bioluminescent plants"
  "A steampunk airship in the clouds"
)

for prompt in "${PROMPTS[@]}"; do
  echo "生成:$prompt"
  ~/.openclaw/workspace/skills/hf-image/scripts/hf_image.py \
    --prompt "$prompt" \
    --model flux-schnell \
    --output "output/$(echo $prompt | tr ' ' '-').png"
  
  # 避免 rate limit
  sleep 3
done

echo "✅ 批次生成完成!"

執行結果:

生成:A medieval castle on a hill
🎨 Generating with flux-schnell...
✅ Saved: output/A-medieval-castle-on-a-hill.png

生成:A futuristic space station
🎨 Generating with flux-schnell...
✅ Saved: output/A-futuristic-space-station.png

...
✅ 批次生成完成!

進階技巧

1. Prompt Engineering

不佳的 prompt

a cat

優化後

A fluffy orange tabby cat sitting on a windowsill, 
soft morning sunlight, photorealistic, 8k, detailed fur texture

關鍵要素

  • 主體描述(fluffy orange tabby cat)
  • 場景/構圖(sitting on a windowsill)
  • 光線/氛圍(soft morning sunlight)
  • 風格指定(photorealistic)
  • 品質提示(8k, detailed)

2. 負面 Prompt(部分模型支援)

某些模型支援 negative_prompt 參數:

payload = {
    "inputs": "A portrait of a woman",
    "negative_prompt": "ugly, blurry, low quality, distorted"
}

3. 與其他工具整合

結合 ImageMagick 自動裁切

# 生成圖片
IMAGE=$(~/.openclaw/workspace/skills/hf-image/scripts/hf_image.py \
  --prompt "Landscape photo" --json | jq -r '.path')

# 自動裁切為正方形
convert "$IMAGE" -gravity center -crop 1:1 "${IMAGE%.png}-square.png"

上傳到 S3

IMAGE=$(~/.openclaw/workspace/skills/hf-image/scripts/hf_image.py \
  --prompt "Product photo" --json | jq -r '.path')

aws s3 cp "$IMAGE" s3://my-bucket/images/

疑難排解

❌ “HF_TOKEN not found”

原因:環境變數未設定

解決

export HF_TOKEN="hf_your_token_here"
echo $HF_TOKEN  # 驗證

❌ “Authentication failed. Check your HF_TOKEN”

原因:Token 無效或過期

解決

  1. 前往 https://huggingface.co/settings/tokens
  2. 刪除舊 token,重新生成
  3. 更新環境變數

⏳ 長時間卡在 “Model loading”

原因:模型首次載入(冷啟動)

正常行為

 Model loading... Retry in 20s (attempt 1/3)
 Model loading... Retry in 20s (attempt 2/3)
 Saved: ...

如果超過 3 分鐘

  1. 模型可能正在維護
  2. 嘗試其他模型:--model flux-schnell

❌ “Rate limit reached”

解決

  1. 等待 5-10 分鐘
  2. 減少請求頻率(請求間隔 > 3 秒)
  3. 考慮升級到 HF Pro($9/月)

🐍 “requests library not found”

解決

/home/linuxbrew/.linuxbrew/bin/python3 -m pip install requests

總結

透過 Hugging Face Inference API,你可以:

零成本讓 AI 助理具備圖片生成能力
無需顯卡,完全雲端運算
多模型選擇,靈活應對不同需求
易於整合,一個 Python 腳本搞定

這個 Skill 已經在 OpenClaw 中實戰驗證,主公可以直接對 AI 說「幫我生成一張圖」,Claude 就會自動呼叫此工具完成任務。

下一步建議

  1. 嘗試不同模型,找到最適合你需求的
  2. 收藏常用的 prompt 模板
  3. 結合其他工具(如圖片編輯、自動上傳)建立完整工作流
  4. 考慮貢獻你的優化到 ClawHub

相關資源

Model: github-copilot/claude-sonnet-4.5