75 lines
3.0 KiB
Plaintext
75 lines
3.0 KiB
Plaintext
|
|
It's super simple to translate from existing code! Just like the python library, we support the `pipeline` API. Pipelines group together a pretrained model with preprocessing of inputs and postprocessing of outputs, making it the easiest way to run models with the library.
|
|
|
|
<table>
|
|
<tr>
|
|
<th width="440px" align="center"><b>Python (original)</b></th>
|
|
<th width="440px" align="center"><b>Javascript (ours)</b></th>
|
|
</tr>
|
|
<tr>
|
|
<td>
|
|
|
|
```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}]
|
|
```
|
|
|
|
</td>
|
|
<td>
|
|
|
|
```javascript
|
|
import { pipeline } from '@huggingface/transformers';
|
|
|
|
// Allocate a pipeline for sentiment-analysis
|
|
const pipe = await pipeline('sentiment-analysis');
|
|
|
|
const out = await pipe('I love transformers!');
|
|
// [{'label': 'POSITIVE', 'score': 0.999817686}]
|
|
```
|
|
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
|
|
|
|
You can also use a different model by specifying the model id or path as the second argument to the `pipeline` function. For example:
|
|
```javascript
|
|
// Use a different model for sentiment-analysis
|
|
const pipe = await pipeline('sentiment-analysis', 'Xenova/bert-base-multilingual-uncased-sentiment');
|
|
```
|
|
|
|
By default, when running in the browser, the model will be run on your CPU (via WASM). If you would like
|
|
to run the model on your GPU (via WebGPU), you can do this by setting `device: 'webgpu'`, for example:
|
|
```javascript
|
|
// Run the model on WebGPU
|
|
const pipe = await pipeline('sentiment-analysis', 'Xenova/distilbert-base-uncased-finetuned-sst-2-english', {
|
|
device: 'webgpu',
|
|
});
|
|
```
|
|
|
|
For more information, check out the [WebGPU guide](./guides/webgpu).
|
|
|
|
> [!WARNING]
|
|
> The WebGPU API is still experimental in many browsers, so if you run into any issues,
|
|
> please file a [bug report](https://github.com/huggingface/transformers.js/issues/new?title=%5BWebGPU%5D%20Error%20running%20MODEL_ID_GOES_HERE&assignees=&labels=bug,webgpu&projects=&template=1_bug-report.yml).
|
|
|
|
In resource-constrained environments, such as web browsers, it is advisable to use a quantized version of
|
|
the model to lower bandwidth and optimize performance. This can be achieved by adjusting the `dtype` option,
|
|
which allows you to select the appropriate data type for your model. While the available options may vary
|
|
depending on the specific model, typical choices include `"fp32"` (default for WebGPU), `"fp16"`, `"q8"`
|
|
(default for WASM), and `"q4"`. For more information, check out the [quantization guide](./guides/dtypes).
|
|
```javascript
|
|
// Run the model at 4-bit quantization
|
|
const pipe = await pipeline('sentiment-analysis', 'Xenova/distilbert-base-uncased-finetuned-sst-2-english', {
|
|
dtype: 'q4',
|
|
});
|
|
```
|
|
|
|
Ready to dive in? Explore our wide variety of demo applications and templates [here](https://github.com/huggingface/transformers.js-examples). You can also launch your own project instantly using the official Transformers.js [template](https://huggingface.co/new-space?template=static-templates%2Ftransformers.js) on Hugging Face!
|
|
|