transformers-js
Skill này làm gì
Skill này cho phép AI thêm các tính năng machine learning vào một ứng dụng JavaScript hoặc TypeScript mà không cần máy chủ Python. Skill dành cho lập trình viên web và ứng dụng muốn model chạy trên trình duyệt hoặc Node.js.
Khi nào nên dùng
- ✓Khi bạn muốn phân tích văn bản, dịch hoặc tóm tắt chạy bên trong một ứng dụng JavaScript.
- ✓Khi bạn cần phân loại hình ảnh hoặc phát hiện đối tượng trên trình duyệt.
- ✓Khi bạn muốn nhận dạng giọng nói hoặc phân loại âm thanh mà không cần backend.
Liên kết skill
https://github.com/huggingface/skills/blob/main/skills/transformers-js/SKILL.mdSao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.
Cách dùng
Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.
Tin nhắn có sẵn để dán
Dành cho Claude Code, Codex hoặc Cursor: chạy lệnh trong thư mục dự án của bạn và skill sẽ được cài đặt.
Tải tệp SKILL.md về và thả vào thư mục skill của agent.
Ví dụ cách nhờ AI
“Thêm kiểm tra cảm xúc vào ứng dụng React của tôi để model chạy trên trình duyệt, không cần máy chủ.”
5 skill mới mỗi tuần. Đọc chỉ 1 phút.
Cách hoạt động
AI dùng thư viện Transformers.js, chạy các model đã huấn luyện sẵn thông qua API pipeline. Thư viện xử lý các bước tiền xử lý và kết quả đầu ra cho tác vụ bạn chọn, và chạy trên WebGPU hoặc WebAssembly khi có sẵn.
Nội dung kỹ thuật (SKILL.md gốc)
Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.
Transformers.js - Machine Learning for JavaScript
Transformers.js enables running state-of-the-art machine learning models directly in JavaScript across browsers and server-side runtimes (Node.js, Bun, Deno), with no Python server required.
When to Use This Skill
Use this skill when you need to:
- Run ML models for text analysis, generation, or translation in JavaScript
- Perform image classification, object detection, or segmentation
- Implement speech recognition or audio processing
- Build multimodal AI applications (text-to-image, image-to-text, etc.)
- Run models client-side in the browser without a backend
Installation
NPM Installation
npm install @huggingface/transformers
Browser Usage (CDN)
<script type="module">
import { pipeline } from 'https://cdn.jsdelivr.net/npm/@huggingface/transformers';
</script>
Core Concepts
1. Pipeline API
The pipeline API is the easiest way to use models. It groups together preprocessing, model inference, and postprocessing:
import { pipeline } from '@huggingface/transformers';
// Create a pipeline for a specific task
const pipe = await pipeline('sentiment-analysis');
// Use the pipeline
const result = await pipe('I love transformers!');
// Output: [{ label: 'POSITIVE', score: 0.999817686 }]
// IMPORTANT: Always dispose when done to free memory
await pipe.dispose();
⚠️ Memory Management: All pipelines must be disposed with pipe.dispose() when finished to prevent memory leaks. See examples in Code Examples for cleanup patterns across different environments.
2. Model Selection
You can specify a custom model as the second argument:
const pipe = await pipeline(
'sentiment-analysis',
'Xenova/bert-base-multilingual-uncased-sentiment'
);
Finding Models:
Browse available Transformers.js models on Hugging Face Hub:
- All models: https://huggingface.co/models?library=transformers.js&sort=trending
- By task: Add
pipeline_tagparameter- Text generation: https://huggingface.co/models?pipeline_tag=text-generation&library=transformers.js&sort=trending
- Image classification: https://huggingface.co/models?pipeline_tag=image-classification&library=transformers.js&sort=trending
- Speech recognition: https://huggingface.co/models?pipeline_tag=automatic-speech-recognition&library=transformers.js&sort=trending
Tip: Filter by task type, sort by trending/downloads, and check model cards for performance metrics and usage examples.
3. Device Selection
Choose where to run the model:
// Run on CPU (default for WASM)
const pipe = await pipeline('sentiment-analysis', 'model-id');
// Run on GPU (WebGPU)
const pipe = await pipeline('sentiment-analysis', 'model-id', {
device: 'webgpu',
});
4. Quantization Options
Control model precision vs. performance:
// Use quantized model (faster, smaller)
const pipe = await pipeline('sentiment-analysis', 'model-id', {
dtype: 'q4', // Options: 'fp32', 'fp16', 'q8', 'q4'
});
Supported Tasks
Note: All examples below show basic usage.
Natural Language Processing
Text Classification
const classifier = await pipeline('text-classification');
const result = await classifier('This movie was amazing!');
Named Entity Recognition (NER)
const ner = await pipeline('token-classification');
const entities = await ner('My name is John and I live in New York.');
Question Answering
const qa = await pipeline('question-answering');
const answer = await qa({
question: 'What is the capital of France?',
context: 'Paris is the capital and largest city of France.'
});
Text Generation
const generator = await pipeline('text-generation', 'onnx-community/gemma-3-270m-it-ONNX');
const text = await generator('Once upon a time', {
max_new_tokens: 100,
temperature: 0.7
});
For streaming and chat: See Text Generation Guide for:
- Streaming token-by-token output with
TextStreamer - Chat/conversation format with system/user/assistant roles
- Generation parameters (temperature, top_k, top_p)
- Browser and Node.js examples
- React components and API endpoints
Translation
const translator = await pipeline('translation', 'Xenova/nllb-200-distilled-600M');
const output = await translator('Hello, how are you?'
Câu hỏi thường gặp
Có cần máy chủ backend không?
Không. Model có thể chạy trực tiếp trên trình duyệt hoặc trong các môi trường máy chủ như Node.js, Bun hay Deno.
API pipeline là gì?
Đó là cách đơn giản để tải một model cho một tác vụ, xử lý các bước trước và sau khi model chạy.
Cài đặt thế nào?
Cài bằng npm, hoặc tải từ CDN trong một trang trình duyệt.
Chi tiết
Mô tả gốc của tác giả
Use Transformers.js to run state-of-the-art machine learning models directly in JavaScript/TypeScript. Supports NLP (text classification, translation, summarization), computer vision (image classification, object detection), audio (speech recognition, audio classification), and multimodal tasks. Works in browsers and server-side runtimes (Node.js, Bun, Deno) with WebGPU/WASM using pre-trained models from Hugging Face Hub.
repo: huggingface/skills
Độ phổ biến
- Sao GitHub
- 11k
- Vị trí xếp hạng
- #11782
- Giấy phép
- Apache-2.0
- Cập nhật
- 30/10/2026
Bình luận
- …
Thêm skill từ kho mã này
hf-cli
11kGiúp agent của bạn dùng công cụ dòng lệnh Hugging Face để tải xuống, tải lên và quản lý model, dataset, Spaces, repo và bộ nhớ đệm cục bộ.
hf-cloud-aws-context-discovery
11kGiúp agent của bạn tìm AWS profile, region và thông tin tài khoản trước khi làm bất kỳ việc gì trên cloud, để không phải đoán tài khoản nào sẽ dùng.
hf-cloud-python-env-setup
11kGiúp agent của bạn thiết lập môi trường Python sạch với phiên bản Python phù hợp và thư viện mới nhất trước khi chạy SageMaker hoặc tự động hóa AWS.
hf-cloud-sagemaker-deployment-planner
11kGiúp agent của bạn lên kế hoạch triển khai model lên Amazon SageMaker, chọn lưu trữ thời gian thực, serverless, async hoặc batch với mặc định hợp lý.
Skill tương tự
angular-developer
280kGiúp agent của bạn viết code Angular và tư vấn cấu trúc dự án, hữu ích khi bắt đầu dự án mới hoặc cải thiện hiệu suất.
frontend-a11y
280kGiúp agent của bạn làm cho giao diện web dễ tiếp cận với mọi người. Đảm bảo website và ứng dụng hoạt động tốt với trình đọc màn hình và điều hướng bằng bàn phím.
frontend-patterns
280kGiúp agent của bạn áp dụng các mẫu hiện đại trong phát triển frontend. Tối ưu hiệu năng, quản lý trạng thái và tuân theo thực hành tốt nhất với React và Next.js.