Nodes/ComfyUI-Transformers/Zero-Shot Classification
ComfyUI Node

Zero-Shot Classification

Sort text into categories you invent — no training required

By kadirnar·Created 3 years ago·Updated 7 months ago· 25
Zero-Shot Classification
    • results_json
    text
    candidate_labelspositive, negative, neutral
    model_namefacebook/bart-large-mnli

    Zero-shot classification is the one NLP trick that feels like cheating: you don't fine-tune anything, you just list the categories you care about and a model that was never trained on your task scores the text against each one. ZeroShotClassificationPipeline brings that to ComfyUI - type a prompt, type "fantasy, sci-fi, horror" into the labels box, and get back a ranked JSON of how well each fits. It's a router, a tagger, and a decision node all in one, and it's usually the first node people reach for in this pack.

    It's from kadirnar/ComfyUI-Transformers, the HF-pipeline wrapper pack. Default model is facebook/bart-large-mnli - a BART model fine-tuned on the MNLI natural-language-inference dataset, which is the classic recipe for this task. The catch: it's ~1.5 GB. That's the price of the "it just works on anything" nature of NLI-based zero-shot.

    How it works

    The mechanism is entailment in disguise. pipeline("zero-shot-classification", model=model_name) reformulates each candidate label as a hypothesis ("This text is about fantasy") and asks the NLI model whether the text entails it. Scores are softmax-normalized over your labels, so they always sum to 1 - meaning a score of 0.5 for "fantasy" isn't an absolute truth, it's relative to the other labels you listed. Add more labels and the numbers reshuffle.

    The output results_json is a JSON array of {label, score} pairs (plus the original text), best first. Your candidate_labels string is split on commas and whitespace-trimmed, so "positive, negative, neutral" becomes three labels. The node only takes a single string of text at a time - no batch mode.

    Inputs and outputs

    • text - multiline STRING, the thing you're classifying.
    • candidate_labels - STRING, comma-separated. The entire game is here: pick labels that are mutually exclusive and semantically distinct.
    • model_name - default facebook/bart-large-mnli; free text. Lighter options exist (the typeform/distilbert-base-uncased-mnli family) if 1.5 GB is too much.
    • Output: results_json - STRING.

    Installing it

    Pack-standard install - ComfyUI Manager (search "ComfyUI-Transformers") or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/kadirnar/ComfyUI-Transformers
    cd ComfyUI-Transformers
    pip install -r requirements.txt
    

    then restart. The README's cd custom/nodes is a typo; requirements pull transformers>=4.42 plus the usual heavy stack. First run downloads BART-large from the Hub to ~/.cache/huggingface - that's the ~1.5 GB stall you'll see on the console, and it happens before anything else works.

    Common issues

    • Scores that "don't add up to 1" in your head. They do - they're normalized across your labels. Read them relatively.
    • First run is slow. Big model download, then model load on every execution (this pack's nodes ignore its own pipeline cache). Budget for it.
    • Poor discrimination between close labels. "sci-fi" vs. "science fiction" will tie and look broken. Keep labels distinct; the model needs the separation.
    • Heavy on RAM. BART-large isn't small. If your machine is tight, swap the model for a distilbert MNLI variant.

    The honest take: this is the pack's most-likely-to-stay-installed node. The image side of the pack has flashier tricks, but if your workflow ever routes on text - sorting captions, deciding which of several prompt branches to take - this is the cleanest "no training, no API key" way to do it in a graph. Just budget for the model size.

    CategoryTransformers/NLP/ZeroShotClassification

    Inputs (3)

    NameTypeDefaultDescription
    textSTRING
    candidate_labelsSTRINGpositive, negative, neutral
    model_nameSTRINGfacebook/bart-large-mnli

    Outputs (1)

    NameTypeDescription
    results_jsonSTRING