Switch web API seamlessly with ABEJA CLI

Introduction

In this page, we explain how to use ABEJA Platform to launch a sample model (Keras) for image identification as a web API and update an existing endpoint seamlessly.

Update the endpoint which is created in the start guide “Create web API using CLI”.

Download sample model(Keras) for image classification

Download the source code from here and extract the file in an empty directory.

All command need to be execute from the extracted directory location.

This is an example of using ResNet50 image classification model of Keras. The flow of using this model is describes below.

$ tar -zxvf default.tar.gz
$ cd default
$ ls -l
total 388192
-rw-r--r--@ 1 abeja  staff         97  9  8 10:57 README.txt
-rw-r--r--@ 1 abeja  staff        218  8 31 13:31 exec.py
-rw-r--r--@ 1 abeja  staff       1566  8 29 20:30 imagenet_utils.py
-rw-r--r--@ 1 abeja  staff        916  8 31 13:31 main.py
-rw-r--r--@ 1 abeja  staff      12369  8 30 04:23 resnet50.py
-rw-r--r--@ 1 abeja  staff  102853048  8 29 20:26 resnet50_weights_tf_dim_ordering_tf_kernels.h5
-rw-r--r--@ 1 abeja  staff   94653016  8 30 04:08 resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5
-rw-r--r--@ 1 abeja  staff    1208476  9 19 13:38 sample.jpg

The execution environment of ABEJA Platform is Python 3.5 or higher.

Create Model Version

Create model version by using create-version command.

--model_id is the identification of model. Get model_id from create model.

$ abeja model create-version --model_id {MODEL_ID} --version 0.0.2 \
                             --image abeja-inc/all-cpu:18.10 --handler main:handler
{
    "version": "0.0.2",
    "version_id": "ver-2222222222222222"
}

Create Service

Create a service for a sample model of image classification.

--deployment_id is the identifier of deployment created in Create Deployment

--version_id is the identifier of the model version created just before.

$ abeja model create-service --deployment_id {DEPLOYMENT_ID} --version_id {VERSION_ID}
{
    "created_at": "2018-05-02T07:28:56.826724Z",
    "deployment_id": "1111111111111",
    "instance_number": 1,
    "instance_type": "cpu-0.25",
    "metrics_url": "https://p.datadoghq.com/sb/c61020b12-f5c8335865e20280252bf6b035a1fc96",
    "model_version": "0.0.2",
    "model_version_id": "ver-2222222222222222",
    "modified_at": "2018-05-02T07:28:58.679953Z",
    "service_id": "ser-2222222222222222",
    "status": "IN_PROGRESS",
    "user_env_vars": {}
}

Confirm the created service operation

First confirm the activation status of the service then check the service operation.

Confirm using simple checking command

Use the check-endpoint-image command to test the web API.

--deployment_id is the identifier of deployment created in Create Deployment, --service_id is the identifier of the service created just before.

$ abeja model check-endpoint-image --deployment_id {DEPLOYMENT_ID} --service_id {SERVICE_ID} \
                                   --type jpeg --image_path sample.jpg
[
    {
        "id": "n04356056",
        "label": "sunglasses",
        "probability": 0.5109500885009766
    },
    {
        "id": "n03617480",
        "label": "kimono",
        "probability": 0.06765035539865494
    },
    {
        "id": "n04350905",
        "label": "suit",
        "probability": 0.06371305137872696
    },
    {
        "id": "n03630383",
        "label": "lab_coat",
        "probability": 0.048062920570373535
    },
    {
        "id": "n03594734",
        "label": "jean",
        "probability": 0.03560170158743858
    }
]

If the deployed web API is not yet available, the response will be an HTTP error (503).

Using curl

To confirm the service operation using curl make the following request.

$ curl --user user-{ABEJA-PLATFORM-USER}:{PERSONAL-ACCESS-TOKEN} \
       -H 'Content-Type: image/jpeg' \
       -XPOST --data-binary  @sample.jpg \
       https://{ORGANIZATION_NAME}.api.abeja.io/deployments/{DEPLOYMENT_ID}/services/{SERVICE_ID}

Update Endpoint

Switch endpoint from simple calculation web API to image classification web API. Create web API using CLI described here.

$ abeja model update-endpoint --deployment_id {DEPLOYMENT_ID} --endpoint_id {ENDPOINT_ID} \
                              --service_id {SERVICE_ID}
{
    "message": "pnt-1111111111111111 updated"
}

Confirm the operation of the updated endpoint

We will confirm that the simple calculation web API works for image classification what we have switched.

Confirm using simple test tool

Use check-endpoint-image command to check the endpoint created as default alias.

--service_id is not necessary to check the operation of the service.

$ abeja model check-endpoint-image --deployment_id {DEPLOYMENT_ID} \
                                   --type jpeg --image_path sample.jpg
[
    {
        "id": "n04356056",
        "label": "sunglasses",
        "probability": 0.5109500885009766
    },
    {
        "id": "n03617480",
        "label": "kimono",
        "probability": 0.06765035539865494
    },
    {
        "id": "n04350905",
        "label": "suit",
        "probability": 0.06371305137872696
    },
    {
        "id": "n03630383",
        "label": "lab_coat",
        "probability": 0.048062920570373535
    },
    {
        "id": "n03594734",
        "label": "jean",
        "probability": 0.03560170158743858
    }
]

Using curl

By using curl, you can send a request and check the default endpoint with the following command.

$ curl --user user-{ABEJA-PLATFORM-USER}:{PERSONAL-ACCESS-TOKEN} \
       -H 'Content-Type: image/jpeg' \
       -XPOST --data-binary  @sample.jpg \
       https://{ORGANIZATION_NAME}.api.abeja.io/deployments/{DEPLOYMENT_ID}