Hub 文件
在 Hugging Face 使用 Transformers.js
加入 Hugging Face 社群
並獲得增強的文件體驗
開始使用
在 Hugging Face 使用 Transformers.js
Transformers.js 是一個 JavaScript 庫,用於直接在瀏覽器中執行 🤗 Transformers,無需伺服器!它的設計旨在功能上與原始的 Python 庫等效,這意味著您可以使用非常相似的 API 執行相同的預訓練模型。
在 Hub 中探索 transformers.js
您可以透過在模型頁面中按庫篩選來找到 transformers.js
模型。
快速導覽
從現有程式碼進行轉換非常簡單!就像 Python 庫一樣,我們支援 pipeline
API。Pipelines 將預訓練模型與輸入預處理和輸出後處理結合在一起,使其成為使用該庫執行模型最簡單的方式。
Python(原版) | JavaScript(我們的版本) |
---|---|
from transformers import pipeline
# Allocate a pipeline for sentiment-analysis
pipe = pipeline('sentiment-analysis')
out = pipe('I love transformers!')
# [{'label': 'POSITIVE', 'score': 0.999806941}] | import { pipeline } from '@huggingface/transformers';
// Allocate a pipeline for sentiment-analysis
let pipe = await pipeline('sentiment-analysis');
let out = await pipe('I love transformers!');
// [{'label': 'POSITIVE', 'score': 0.999817686}] |
您還可以透過將模型 ID 或路徑作為第二個引數傳遞給 pipeline
函式來使用不同的模型。例如:
// Use a different model for sentiment-analysis
let pipe = await pipeline('sentiment-analysis', 'nlptown/bert-base-multilingual-uncased-sentiment');
有關支援的任務和模型的完整列表,請參閱文件。
安裝
要透過 NPM 安裝,請執行
npm i @huggingface/transformers
有關更多資訊,包括如何在不使用任何打包器的情況下透過 CDN 或靜態託管在純 JS 中使用它,請參閱 README。