Example

HTTP

Create HTTP APIs that respond to requests in real-time.

Implement

mkdir text-generator && cd text-generator
touch handler.py requirements.txt text_generator.yaml
# handler.py

from transformers import pipeline

class Handler:
    def __init__(self, config):
        self.model = pipeline(task="text-generation")

    def handle_post(self, payload):
        return self.model(payload["text"])[0]
# requirements.txt

transformers
torch

Deploy

Monitor

Stream logs

Make a request

Delete

gRPC

To make the above API use gRPC as its protocol, make the following changes (the rest of the steps are the same):

Add protobuf file

Create a handler.proto file in your project's directory:

Set the handler.protobuf_path field in the API spec to point to the handler.proto file:

Match RPC service name

Match the name of the RPC service(s) from the protobuf definition (in this case Predict) with what you're defining in the handler's implementation:

Make a gRPC request

Last updated