Transformers.js brings Hugging Face models to JavaScript. Run NLP, vision, and audio models locally — no server, no API keys, no costs.
Text Generation
import { pipeline } from "@xenova/transformers";
const generator = await pipeline("text-generation", "Xenova/gpt2");
const result = await generator("The future of web scraping is", {
max_new_tokens: 100,
temperature: 0.7,
});
console.log(result[0].generated_text);
Sentiment Analysis
const classifier = await pipeline("sentiment-analysis");
const results = await classifier([
"This product is amazing, best purchase ever!",
"Terrible quality, broke after one day.",
"It's okay, nothing special.",
]);
// [{ label: 'POSITIVE', score: 0.99 }, { label: 'NEGATIVE', score: 0.98 }, ...]
Text Embeddings: Semantic Search
const embedder = await pipeline("feature-extraction", "Xenova/all-MiniLM-L6-v2");
const embedding1 = await embedder("web scraping tools", { pooling: "mean", normalize: true });
const embedding2 = await embedder("data extraction software", { pooling: "mean", normalize: true });
function cosineSimilarity(a, b) {
let dot = 0, normA = 0, normB = 0;
for (let i = 0; i < a.length; i++) {
dot += a[i] * b[i];
normA += a[i] * a[i];
normB += b[i] * b[i];
}
return dot / (Math.sqrt(normA) * Math.sqrt(normB));
}
const similarity = cosineSimilarity(embedding1.data, embedding2.data);
console.log(similarity); // ~0.85 — very similar!
Image Classification
const classifier = await pipeline("image-classification", "Xenova/vit-base-patch16-224");
const result = await classifier("https://example.com/product.jpg");
// [{ label: 'laptop', score: 0.92 }, { label: 'notebook', score: 0.05 }]
Object Detection
const detector = await pipeline("object-detection", "Xenova/detr-resnet-50");
const results = await detector(imageUrl);
// [{ label: 'person', score: 0.99, box: { xmin: 10, ymin: 20, xmax: 200, ymax: 400 } }]
Translation
const translator = await pipeline("translation", "Xenova/nllb-200-distilled-600M");
const result = await translator("Web scraping is the process of extracting data from websites.", {
src_lang: "eng_Latn",
tgt_lang: "fra_Latn",
});
Speech-to-Text (Whisper)
const transcriber = await pipeline("automatic-speech-recognition", "Xenova/whisper-small");
const result = await transcriber(audioUrl, {
chunk_length_s: 30,
stride_length_s: 5,
return_timestamps: true,
});
console.log(result.text);
AI-powered scraping analysis? My Apify tools + Transformers.js = intelligent data extraction.
Custom AI solution? Email spinov001@gmail.com
Top comments (0)