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/mDeBERTa-v3-base-xnli-multilingual-nli-2mil7: Powerful zero-shot multilingual text classification model that can accomplish multiple tasks.
Explore all available models and find the one that suits you best here.
Using the API
import requests
API_URL = "https://api-inference.huggingface.co/models/facebook/bart-large-mnli"
headers = {"Authorization": "Bearer hf_***"}
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"]},
})
To use the Python client, see huggingface_hub
’s package reference.
API specification
Request
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. |
Some options can be configured by passing headers to the Inference API. Here are the available headers:
Headers | ||
---|---|---|
authorization | string | Authentication header in the form 'Bearer: hf_****' when hf_**** is a personal user access token with Inference API permission. You can generate one from your settings page. |
x-use-cache | boolean, default to true | There is a cache layer on the inference API to speed up requests we have already seen. Most models can use those results as they are deterministic (meaning the outputs will be the same anyway). However, if you use a nondeterministic model, you can set this parameter to prevent the caching mechanism from being used, resulting in a real new query. Read more about caching here. |
x-wait-for-model | boolean, default to false | If the model is not ready, wait for it instead of receiving 503. It limits the number of requests required to get your inference done. It is advised to only set this flag to true after receiving a 503 error, as it will limit hanging in your application to known places. Read more about model availability here. |
For more information about Inference API headers, check out the parameters guide.
Response
Body | ||
---|---|---|
(array) | object[] | Output is an array of objects. |
label | string | The predicted class label. |
score | number | The corresponding probability. |