##
Zero-Shot Classification

Zero-shot text classification is super useful to try out classification with zero code, you simply pass a sentence/paragraph and the possible labels for that sentence, and you get a result. The model has not been necessarily trained on the labels you provide, but it can still predict the correct label.

> For more details about the `zero-shot-classification` task, check out its dedicated page! You will find examples and related materials.

###
Recommended models

- facebook/bart-large-mnli: Powerful zero-shot text classification model.
- MoritzLaurer/ModernBERT-large-zeroshot-v2.0: Cutting-edge zero-shot multilingual text classification model.

Explore all available models and find the one that suits you best here.

###
Using the API

Language

Provider

Settings

    import os
    import requests

    API_URL = "https://router.huggingface.co/hf-inference/models/facebook/bart-large-mnli"
    headers = {
        "Authorization": f"Bearer {os.environ['HF_TOKEN']}",
    }

    def query(payload):
        response = requests.post(API_URL, headers=headers, json=payload)
        return response.json()

    output = query({
        "inputs": "Hi, I recently bought a device from your company but it is not working as advertised and I would like to get reimbursed!",
        "parameters": {"candidate_labels": ["refund", "legal", "faq"]},
    })

###
API specification

####
Request

Headers |  |
--- | --- | ---
authorization | string | Authentication header in the form `'Bearer: hf_****'` when `hf_****` is a personal user access token with “Inference Providers” permission. You can generate one from your settings page.

Payload |  |
--- | --- | ---
inputs* | string | The text to classify
parameters* | object |
candidate_labels* | string[] | The set of possible class labels to classify the text into.
hypothesis_template | string | The sentence used in conjunction with `candidate_labels` to attempt the text classification by replacing the placeholder with the candidate labels.
multi_label | boolean | Whether multiple candidate labels can be true. If false, the scores are normalized such that the sum of the label likelihoods for each sequence is 1. If true, the labels are considered independent and probabilities are normalized for each candidate.

####
Response

Body |  |
--- | --- | ---
(array) | object[] | Output is an array of objects.
label | string | The predicted class label.
score | number | The corresponding probability.

Update on GitHub