Noqta
  • Home
  • Services
  • About us
  • Writing
  • Sign in
writing/tutorial/2024/12
● TutorialDec 6, 2024·8 min read

Exploring Transformers.js

An in-depth look at Transformers.js, its capabilities, and how to use it for machine learning tasks directly in the browser.

Anis Marrouchi
Anis Marrouchi
Author
·EN · FR · AR

Transformers.js is a powerful library that brings state-of-the-art machine learning capabilities directly to your web browser. It is designed to be functionally equivalent to Hugging Face’s transformers Python library, allowing you to run the same pretrained models using a similar API. This article explores the key features, concepts, and dependencies of Transformers.js, providing a comprehensive guide to getting started with machine learning on the web.

Key Features of Transformers.js

Transformers.js supports a wide range of tasks across different modalities, including:

  • Natural Language Processing (NLP): Tasks such as text classification, named entity recognition, question answering, language modeling, summarization, translation, and text generation.
  • Computer Vision: Image classification, object detection, segmentation, and depth estimation.
  • Audio Processing: Automatic speech recognition, audio classification, and text-to-speech.
  • Multimodal Tasks: Embeddings, zero-shot audio classification, zero-shot image classification, and zero-shot object detection.

Transformers.js leverages the ONNX Runtime to execute models in the browser, making it possible to convert pretrained PyTorch, TensorFlow, or JAX models to ONNX using the 🤗 Optimum tool.

Getting Started with Transformers.js

To begin using Transformers.js, you need to install the library. The installation process is straightforward and can be done via npm:

npm install @huggingface/transformers

Once installed, you can start using the pipeline API, which simplifies the process of running models by grouping together a pretrained model with input preprocessing and output postprocessing.

import { pipeline } from '@huggingface/transformers';
 
// Allocate a pipeline for sentiment-analysis
const pipe = await pipeline('sentiment-analysis');
 
const out = await pipe('I love transformers!');
// Output: [{'label': 'POSITIVE', 'score': 0.999817686}]

Running Models on WebGPU

By default, models run on the CPU via WebAssembly (WASM). However, you can leverage WebGPU for enhanced performance by setting the device option:

const pipe = await pipeline('sentiment-analysis', 'Xenova/distilbert-base-uncased-finetuned-sst-2-english', {
  device: 'webgpu',
});

Note that WebGPU is still experimental in many browsers, so you may encounter issues.

Optimizing Performance with Quantization

In resource-constrained environments like web browsers, using a quantized version of the model can optimize performance. You can adjust the dtype option to select the appropriate data type for your model:

const pipe = await pipeline('sentiment-analysis', 'Xenova/distilbert-base-uncased-finetuned-sst-2-english', {
  dtype: 'q4',
});

Conclusion

Transformers.js is a versatile tool that brings advanced machine learning capabilities to the web, enabling developers to build powerful applications without the need for server-side infrastructure. By understanding its features and how to optimize performance, you can effectively integrate machine learning into your web projects.


Reference:

  • Author: Hugging Face
  • URL: Hugging Face Transformers.js Documentation
● Tags
#Transformers.js#Machine Learning#Web Development#JavaScript#ONNX#intermediate#8 min read
● Share
● A question?

Talk to a Noqta agent about this article.

Anis Marrouchi
Anis Marrouchi
Author · noqta
Follow ↗

● Read next

AI SDK 4.0: New Features and Use Cases
● Tutorial

AI SDK 4.0: New Features and Use Cases

Dec 9, 2024
Getting Started with ALLaM-7B-Instruct-preview
● Tutorial

Getting Started with ALLaM-7B-Instruct-preview

Apr 20, 2025
Automating Workflows with Zapier and Webhooks in a Next.js App
● Tutorial

Automating Workflows with Zapier and Webhooks in a Next.js App

May 19, 2024
Noqta
Terms and Conditions · Privacy Policy
Services
  • AI Automation
  • AI Agents
  • CX Automation
  • Vibe Coding
  • Project Management
  • Quality Assurance
  • Web Development
  • API Integration
  • Business Applications
  • Maintenance
  • Low-Code/No-Code
Links
  • About Us
  • How It Works?
  • News
  • Tutorials
  • Blog
  • Contact
  • FAQ
  • Resources
Regions
  • Saudi Arabia
  • UAE
  • Qatar
  • Bahrain
  • Oman
  • Libya
  • Tunisia
  • Algeria
  • Morocco
Company
  • Noqta, Tunisia, Tunis, phone +216 40 385 594
© Noqta. All rights reserved.