iT邦幫忙

2025 iThome 鐵人賽

DAY 20
0
DevOps

Mac 環境 n8n 30 天學習計畫系列 第 21

Day 21:AI 任務 →n8n自動翻譯

  • 分享至 

  • xImage
  •  

🎯 學習目標

  • 學會使用 Hugging Face 免費模型 API 做翻譯
  • 了解如何在 n8n 中串接 API → 輸入文字 → 輸出翻譯結果
  • 建立一個「翻譯小幫手」應用

🛠️ (1) 實作步驟

Step 1. 申請 Hugging Face API Token(免費)

  1. Hugging Face 註冊帳號

  2. Access Tokens 頁面建立一個 token

    • scope:選 read

    • copy token,格式像這樣:

      hf_xxxxxxxxxxxxxxxxxxxxx
      

https://ithelp.ithome.com.tw/upload/images/20251003/20169144RQODziml7t.png

Step 2. 選擇翻譯模型

我們用 Hugging Face 提供的免費翻譯模型:

  • Helsinki-NLP/opus-mt-zh-en (中文 → 英文)
  • Helsinki-NLP/opus-mt-en-zh (英文 → 中文)

這些是最常用的翻譯模型,速度快而且免費。
https://ithelp.ithome.com.tw/upload/images/20251003/20169144EuzxOiytpV.png

Step 3. 在 n8n 建立 Workflow

節點流程設計:

https://ithelp.ithome.com.tw/upload/images/20251003/20169144unucooESWy.png

  1. Manual Trigger → 手動觸發

  2. Set Node → 輸入要翻譯的文字

    • Key: text
    • Value: 例如 今天天氣很好,我想去公園散步

https://ithelp.ithome.com.tw/upload/images/20251003/20169144mVREpy82Sl.png
3. HTTP Request (Hugging Face) → 呼叫翻譯 API

  • Method: POST

  • URL:

    • 中文 → 英文: https://api-inference.huggingface.co/models/Helsinki-NLP/opus-mt-zh-en
    • 英文 → 中文: https://api-inference.huggingface.co/models/Helsinki-NLP/opus-mt-en-zh
  • Authentication: None

  • Headers:

    • Authorization: Bearer hf_xxxxx
    • Content-Type: application/json
  • Body Parameters → 選 JSON

    • Key: inputs
    • Value: ={{ $json.text }}

https://ithelp.ithome.com.tw/upload/images/20251003/201691446dReUfpHCz.png

  1. Function Node (格式化翻譯結果)

    return [{
      json: {
        original: $json.text,
        translation: $json[0].translation_text
      }
    }];
    

https://ithelp.ithome.com.tw/upload/images/20251003/20169144wrMH2eKwMC.png

  1. Console Log → 印出原文與翻譯結果

Step 4. 測試 Workflow

  • Execute Workflow
  • 你會看到輸出結果像這樣:
{
  "original": "今天天氣很好,我想去公園散步",
  "translation": "The weather is nice today, I want to take a walk in the park."
}

完成!
https://ithelp.ithome.com.tw/upload/images/20251003/20169144GCPPU00rQQ.png


上一篇
Day 20:AI 任務 1 — 自動摘要文章( Hugging Face Inference API)
下一篇
Day 22:AI + Email 自動回覆草稿HuggingFace API(免費模型)
系列文
Mac 環境 n8n 30 天學習計畫22
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言