from transformers import pipeline


class Model:
    def __init__(self, **kwargs):
        self._model = None

    def load(self):
        self._model = pipeline("text-classification")

    def predict(self, model_input):
        return self._model(model_input)

To complete model/model.py, we’ll implement model inference and invoke our finished model.

Run model inference

The Model.predict() function runs every time the model server is called.

We’ll use the text classification model in predict() and return the results:

model/model.py
def predict(self, model_input):
    return self._model(model_input)

Invoke your finished model

After truss watch shows that the server is updated, it’s time to invoke your finished model using truss predict in your terminal:

Invocation

truss predict -d '"Truss is awesome!"'

Response

[
  {
    "label": "POSITIVE",
    "score": 0.999873161315918
  }
]