const result = await classifier('This product totally blew me away.');
// [{ label: 'POSITIVE', score: 0.9997 }]
The output is a single-element array. Inside that element are two values: label (the predicted category as text) and score (a decimal from 0 to 1 indicating how sure the model is). A value like 0.9997 means the model is extremely confident. A value around 0.52 means it is barely above the cutoff — treat that as a guess and add appropriate handling in your code.
The result is always wrapped in an array, even when you submit just one piece of text, because the same pipeline function also accepts batches:
const results = await classifier([
'This is great!',
'Totally broken, not worth the money.'
]);
// [
// { label: 'POSITIVE', score: 0.9998 },
// { label: 'NEGATIVE', score: 0.9991 }
// ]
// Complete Working Example from Scratch
The code below is a standalone HTML file. Open it directly in any modern web browser. The model is fetched the first time you run it and cached afterward; every subsequent load is nearly instant.
Text Classification using Transformers.js
Everything runs inside your browser -- no backend, no API calls.
Fetching the model on the first run (this may take a moment)...
The loadModel function calls pipeline() with the task name, the model identifier, and a set of options. The progress_callback triggers repeatedly during the download and updates the onscreen status so the user is not left looking at a frozen page. After the model is fully loaded, the button becomes clickable. When the user presses Classify, classifier(text) executes inference directly from the cache, typically completing in under 200ms on a modern laptop. The code pulls the label and score from the first entry in the array, formats the confidence as a percentage, and assigns a CSS class to color-code the result.
# Task 2: Zero-Shot Classification
Zero-shot classification does something that standard text classification cannot: it sorts text into categories you supply at runtime, with no training data needed. You provide the text along with a list of candidate labels written in plain English. The model then picks the label that best matches based on its grasp of language meaning.
This comes in handy whenever you are unable or unwilling to train a model on labeled examples — which describes the majority of real-world projects.
// What Happens Behind the Scenes
The model turns each candidate label into a natural language inference (NLI) hypothesis. For the label “billing issue“, it constructs the statement “This text is about a billing issue” and calculates how likely that statement is to be supported by the input text. The label with the highest entailment probability wins. This NLI-driven method is the reason you can use any descriptive English phrase as a label and still get a sensible result. The model grasps the meaning behind your labels, not just their literal wording.
Here is what the output looks like:
const classifier = await pipeline('zero-shot-classification',
'Xenova/bart-large-mnli');
const result = await classifier(
'My invoice is wrong and I was charged twice.',
['billing', 'technical support', 'shipping', 'returns', 'account access']
);
// {
// sequence: 'My invoice is wrong and I was charged twice.',
// labels: ['billing', 'returns', 'account access', 'technical support', 'shipping'],
// scores: [0.871, 0.063, 0.031, 0.022, 0.013]
// }
The output is an object containing three fields. sequence is the original text you submitted. labels is an array of your candidate labels, arranged from highest to lowest score. scores is an array of confidence values in the same order. The first entry in both arrays is always the top prediction. When multi_label is set to false (the default), the scores across all labels add up to roughly 1.
Switching to multi_label: true changes the behavior: each label is scored on its own rather than competing against the others, so several labels can all receive high scores at the same time. Use this setting when a piece of text could reasonably belong to more than one category.
// Complete Working Example
Below is the updated script block with all HTML brackets fully escaped. You can paste it straight into a Custom HTML block in WordPress and it will display correctly as a code snippet.
Paste a support ticket below. The model will automatically direct it to the appropriate department without requiring any prior training data.
Downloading model on first run…



