##
Text Classification

Text Classification is the task of assigning a label or class to a given text. Some use cases are sentiment analysis, natural language inference, and assessing grammatical correctness.

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

###
Recommended models

- distilbert/distilbert-base-uncased-finetuned-sst-2-english: A robust model trained for sentiment analysis.
- ProsusAI/finbert: A sentiment analysis model specialized in financial sentiment.
- cardiffnlp/twitter-roberta-base-sentiment-latest: A sentiment analysis model specialized in analyzing tweets.
- papluca/xlm-roberta-base-language-detection: A model that can classify languages.
- meta-llama/Prompt-Guard-86M: A model that can classify text generation attacks.

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

###
Using the API

Language

Client

Provider

Settings

    import os
    from huggingface_hub import InferenceClient

    client = InferenceClient(
        provider="hf-inference",
        api_key=os.environ["HF_TOKEN"],
    )

    result = client.text_classification(
        "I like you. I love you",
        model="ProsusAI/finbert",
    )

###
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 |
function_to_apply | enum | Possible values: sigmoid, softmax, none.
top_k | integer | When specified, limits the output to the top K most probable classes.

####
Response

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

Update on GitHub