Skip to content

Summarization

Bases: TextAPI

A class for serving a Hugging Face-based summarization model. This API provides an interface to submit text and receive a summarized version, utilizing state-of-the-art machine learning models for text summarization.

Attributes:

Name Type Description
model AutoModelForSeq2SeqLM

The loaded Hugging Face model for summarization.

tokenizer AutoTokenizer

The tokenizer for preprocessing text.

Methods

summarize(self, **kwargs: Any) -> Dict[str, Any]: Summarizes the input text based on the given parameters.

CLI Usage:

genius SummarizationAPI rise \
    batch \
        --input_folder ./input \
    batch \
        --output_folder ./output \
    none \
    --id facebook/bart-large-cnn-lol \
    listen \
        --args \
            model_name="facebook/bart-large-cnn" \
            model_class="AutoModelForSeq2SeqLM" \
            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 SummarizationAPI class with input, output, and state configurations.

Parameters:

Name Type Description Default
input BatchInput

Configuration for input data.

required
output BatchOutput

Configuration for output data.

required
state State

State management for API.

required
**kwargs Any

Additional keyword arguments for extended functionality.

{}

initialize_pipeline()

Lazy initialization of the summarization Hugging Face pipeline.

summarize(**kwargs)

Summarizes the input text based on the given parameters using a machine learning model. The method accepts parameters via a POST request and returns the summarized text.

Parameters:

Name Type Description Default
**kwargs Any

Arbitrary keyword arguments. Expected to receive these from the POST request's JSON body.

{}

Returns:

Type Description
Dict[str, Any]

Dict[str, Any]: A dictionary containing the input text and its summary.

Example CURL Requests:

/usr/bin/curl -X POST localhost:3000/api/v1/summarize \
    -H "Content-Type: application/json" \
    -d '{
        "text": "Theres something magical about Recurrent Neural Networks (RNNs). I still remember when I trained my first recurrent network for Image Captioning. Within a few dozen minutes of training my first baby model (with rather arbitrarily-chosen hyperparameters) started to generate very nice looking descriptions of images that were on the edge of making sense. Sometimes the ratio of how simple your model is to the quality of the results you get out of it blows past your expectations, and this was one of those times. What made this result so shocking at the time was that the common wisdom was that RNNs were supposed to be difficult to train (with more experience Ive in fact reached the opposite conclusion). Fast forward about a year: Im training RNNs all the time and Ive witnessed their power and robustness many times, and yet their magical outputs still find ways of amusing me.",
        "decoding_strategy": "generate",
        "bos_token_id": 0,
        "decoder_start_token_id": 2,
        "early_stopping": true,
        "eos_token_id": 2,
        "forced_bos_token_id": 0,
        "forced_eos_token_id": 2,
        "length_penalty": 2.0,
        "max_length": 142,
        "min_length": 56,
        "no_repeat_ngram_size": 3,
        "num_beams": 4,
        "pad_token_id": 1,
        "do_sample": false
    }' | jq

/usr/bin/curl -X POST localhost:3000/api/v1/summarize \
    -H "Content-Type: application/json" \
    -d '{
        "text": "Theres something magical about Recurrent Neural Networks (RNNs). I still remember when I trained my first recurrent network for Image Captioning. Within a few dozen minutes of training my first baby model (with rather arbitrarily-chosen hyperparameters) started to generate very nice looking descriptions of images that were on the edge of making sense. Sometimes the ratio of how simple your model is to the quality of the results you get out of it blows past your expectations, and this was one of those times. What made this result so shocking at the time was that the common wisdom was that RNNs were supposed to be difficult to train (with more experience Ive in fact reached the opposite conclusion). Fast forward about a year: Im training RNNs all the time and Ive witnessed their power and robustness many times, and yet their magical outputs still find ways of amusing me.",
        "decoding_strategy": "generate",
        "early_stopping": true,
        "length_penalty": 2.0,
        "max_length": 142,
        "min_length": 56,
        "no_repeat_ngram_size": 3,
        "num_beams": 4
    }' | jq

summarize_pipeline(**kwargs)

Summarizes the input text using the Hugging Face pipeline based on given parameters.

Parameters:

Name Type Description Default
**kwargs Any

Keyword arguments containing parameters for summarization.

{}

Returns:

Type Description
Dict[str, Any]

A dictionary containing the input text and its summary.

Example CURL Request for summarization: curl -X POST localhost:3000/api/v1/summarize_pipeline -H "Content-Type: application/json" -d '{"text": "Your long text here"}'