# 在 Hugging Face 上使用 `Transformers.js`

Transformers.js 是一个 JavaScript 库，用于直接在浏览器中运行 🤗 Transformers，无需服务器！它旨在与原始的 [Python 库](https://github.com/huggingface/transformers) 在功能上保持等效，这意味着你可以使用非常相似的 API 来运行相同的预训练模型。

## 在 Hub 中探索 `transformers.js`

您可以在[模型页面](https://huggingface.co/models?library=transformers.js)中通过按库筛选来查找`transformers.js`模型。

## 快速浏览

从现有代码迁移非常简单！与 Python 库一样，我们支持 `pipeline` API。Pipeline 将预训练模型与输入预处理和输出后处理组合在一起，是使用该库运行模型的最简单方式。

Python （原始）JavaScript（我们的）

```python
from transformers import pipeline

# Allocate a pipeline for sentiment-analysis
pipe = pipeline('sentiment-analysis')

out = pipe('I love transformers!')
# [{'label': 'POSITIVE', 'score': 0.999806941}]
```

```javascript
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` 函数的第二个参数来使用其他模型。例如：
```javascript
// Use a different model for sentiment-analysis
let pipe = await pipeline('sentiment-analysis', 'nlptown/bert-base-multilingual-uncased-sentiment');
```

有关支持的任务和模型的完整列表，请参阅[文档](https://huggingface.co/docs/transformers.js)。

## 安装

要通过 [NPM](https://www.npmjs.com/package/@huggingface/transformers) 安装，请运行：
```bash
npm i @huggingface/transformers
```

有关更多信息，包括如何通过 CDN 或静态托管在原生 JS（不使用任何打包器）中使用它，请参阅 [README](https://github.com/huggingface/transformers.js/blob/main/README.md#installation)。

## 其他资源

* Transformers.js [仓库](https://github.com/huggingface/transformers.js)
* Transformers.js [文档](https://huggingface.co/docs/transformers.js)
* Transformers.js [演示](https://huggingface.github.io/transformers.js/)

