Coder Social home page Coder Social logo

sagemaker-multimodel-r's Introduction

R build status Lifecycle: experimental

Sagemaker Multi-Model Server for R

Sagemaker is AWS's machine learning product. It is mostly targeted at Python users, but it's possible to deploy and train models using any language via docker containers.

This package is an implementation of the API contract for handling multiple models in the same Sagemaker Endpoint. It uses Plumber and a dummy XGBoost model.

Everything is a WORK IN PROGRESS and subject to change!

Why would you use this?

Use this implementation of the Multi-Model server if you want to use Sagemaker and:

  • your pipeline (training/feature eng) is in R and you want to keep it that way
  • serve multiple models in one instance to save costs
  • want to use a different version or a model not available from AWS

How to use it?

Locally

To use the server locally build the docker container and launch it:

docker build -t sagemaker-multimodel-server . && \
docker run -p 8080:8080 sagemaker-multimodel-server serve

tell the container to load a model (modify the path first!):

curl --location --request POST 'http://localhost:8080/models' \
--header 'Content-Type: application/json' \
--data-raw '{
    "model_name": "no_cyl",
    "url": "<local path to the repo>/example_models/no_cyl/model"
}'

you should receive a 200, which means the container is ready for scoring:

curl --location --request POST 'http://localhost:8080/models/no_cyl/invoke' \
--header 'Content-Type: application/json' \
--data-raw '{
    "data_for_inference": [
        {
            "mpg": 21,
            "disp": 160,
            "hp": 110,
            "drat": 3.90,
            "wt": 2.620,
            "qsec": 16.46,
            "vs": 0,
            "gear": 4,
            "carb": 4
        }
    ]
}'

On AWS

I'll assume you have an AWS account and know how Sagemaker works in general. If you don't, please take a look at their docs.

Example of a full implementation Image of AWS Sagemaker infrastructure

You'll need an Elastic Container Registry (ECR) repository to keep the docker image used in Sagemaker. After creating the repository build the image locally, then tag and push it to the ECR repository

> docker tag sagemaker-multimodel-server <aws_account_id>.dkr.ecr.<aws_region>.amazonaws.com/<your_repo>
> docker push <aws_account_id>.dkr.ecr.<aws_region>.amazonaws.com/<your_repo>

You'll also need an S3 bucket where you have to upload a .tar.gz archive containing the model.xgb file inside the example_models folder.

With these elements in place, you can now create a Sagemaker Model pointing to the ECR image and the S3 bucket, a Sagemaker Endpoint Configuration and finally a Sagemaker Endpoint.

To finally use the Endpoint you still need to create a Lambda to call the endpoint (no way to avoid Python here AFAIK):

import os
import boto3
import json

runtime = boto3.client('runtime.sagemaker')

def lambda_handler(event, context):

    response = runtime.invoke_endpoint(EndpointName = '<your_endpoint_name>',
                                       ContentType = 'application/json',
                                       Body = json.dumps(event),
                                       TargetModel  = 'no_cyl.tar.gz')
                                       
    result = json.loads(response['Body'].read().decode())

    return result

to test the whole setup add a test to the lambda using the same payload also used for local testing.

See also

sagemaker-multimodel-r's People

Contributors

jcpsantiago avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Forkers

edanesh

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.