Skip to content

Classification

Bases: TextAPI

TextClassificationAPI leveraging Hugging Face's transformers for text classification tasks. This API provides an interface to classify text into various categories like sentiment, topic, intent, etc.

Attributes:

Name Type Description
model AutoModelForSequenceClassification

A Hugging Face model for sequence classification.

tokenizer AutoTokenizer

A tokenizer for preprocessing text.

hf_pipeline Pipeline

A Hugging Face pipeline for text classification.

Methods

classify(self): Classifies text using the model and tokenizer. classification_pipeline(self): Classifies text using the Hugging Face pipeline. initialize_pipeline(self): Lazy initialization of the classification pipeline.

Example CLI Usage:

genius TextClassificationAPI rise \
    batch \
        --input_folder ./input \
    batch \
        --output_folder ./output \
    none \
    --id cardiffnlp/twitter-roberta-base-hate-multiclass-latest-lol \
    listen \
        --args \
            model_name="cardiffnlp/twitter-roberta-base-hate-multiclass-latest" \
            model_class="AutoModelForSequenceClassification" \
            tokenizer_class="AutoTokenizer" \
            use_cuda=True \
            precision="float" \
            quantization=0 \
            device_map="cuda:0" \
            max_memory=None \
            torchscript=False \
            endpoint="*" \
            port=3000 \
            cors_domain="http://localhost:3000" \
            username="user" \
            password="password"

__init__(input, output, state, **kwargs)

Initializes the TextClassificationAPI with the necessary configurations for input, output, and state management.

Parameters:

Name Type Description Default
input BatchInput

Configuration for the input data.

required
output BatchOutput

Configuration for the output data.

required
state State

State management for the API.

required
**kwargs

Additional keyword arguments for extended functionality.

{}

classification_pipeline()

Accepts text input and returns classification results using the Hugging Face pipeline.

This method uses the Hugging Face pipeline for efficient and robust text classification. It's suitable for various classification tasks such as sentiment analysis, topic classification, and intent recognition.

Returns:

Type Description
Dict[str, Any]

Dict[str, Any]: A dictionary containing the original input text and the classification results.

Example CURL Request for text classification:

/usr/bin/curl -X POST localhost:3000/api/v1/classification_pipeline             -H "Content-Type: application/json"             -d '{"text": "The movie was fantastic, with great acting and plot."}' | jq

classify()

Accepts text input and returns classification results. The method uses the model and tokenizer to classify the text and provide the likelihood of each class label.

Returns:

Type Description
Dict[str, Any]

Dict[str, Any]: A dictionary containing the original input text and the classification scores for each label.

Example CURL Request for text classification:

/usr/bin/curl -X POST localhost:3000/api/v1/classify             -H "Content-Type: application/json"             -d '{
        "text": "tata sons lost a major contract to its rival mahindra motors"
    }' | jq

initialize_pipeline()

Lazy initialization of the Hugging Face pipeline for classification.