Hub documentation

Hugging FaceでのTransformers.jsの活用

Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

Hugging FaceでのTransformers.jsの活用

Transformers.jsは、サーバーなしでブラウザ上で🤗 Transformersを直接実行するためのJavaScriptライブラリです。元のPythonライブラリと機能的に同等になるよう設計されており、非常に類似したAPIを使用して同じ事前学習済みモデルを実行できます。

Hubでのtransformers.jsの探索

モデルページでライブラリをフィルタリングすることで、transformers.jsのモデルを見つけることができます。

クイックツアー

既存のコードからの変換は非常に簡単です。Pythonライブラリと同様に、pipeline APIをサポートしています。パイプラインは事前学習済みモデルと、入力の前処理および出力の後処理をまとめて提供するため、このライブラリでモデルを実行する最も簡単な方法です。

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}]

pipeline関数の第2引数としてモデルIDまたはパスを指定することで、別のモデルを使用することもできます。例:

// 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を参照してください。

追加のリソース

Update on GitHub