Git Product home page Git Product logo

mlnetacidemo's Introduction

Deploying .NET Machine Learning Models With ML.NET, ASP.NET Core, Docker and Azure Container Instances

This is a sample solution of how machine learning models built with ML.NET framework can be exposed to clients via an ASP.NET Core Web API that has been packaged into a Docker container and deployed to Azure Container Instances service. A more detailed walk-through can be found in this blog post at the following link

Requirements

Docker
Azure CLI
.NET Core 2.0
Docker Hub Account

Project Instructions

  1. Build The Project
  2. Create Model
  3. Test Web API Locally
  4. Package Web API
  5. Test Web API Docker Image Locally
  6. Push Docker Image to Docker Hub
  7. Deploy to Azure Container Instances
  8. Test Deployed Application

1. Build The Solution

Clone Repository

git clone https://github.com/lqdev/mlnetacidemo.git

Build Solution

cd mlnetacidemo
dotnet restore
dotnet build

2. Create Model

Before we start our API, we need to create our machine learning model. This is done via the model project.

dotnet run -p model/model.csproj

3. Test Web API Locally

The model project should have created a file called model.zip inside of its directory. Copy model.zip file inside the model project directory to the api project directory.

Start Web API

dotnet run -p api/api.csproj

Send Test Request

Use POSTMAN or Insomnia REST Clients to send a POST request to http://localhost:5000/api/predict that includes the following body

{
	"SepalLength": 3.3,
	"SepalWidth": 1.6,
	"PetalLength": 0.2,
	"PetalWidth": 5.1,
}

4. Package Web API

Using the Docker CLI tool, and your own Docker Hub username and image name of your choice, the following command will create a Docker image of the application using the Dockerfile in the root solution directory.

docker build -t <DOCKERUSERNAME>/<IMAGENAME>:latest .

5. Test Web API Docker Image Locally

Start Docker Image

Using your DockerHub username and recently created image name from the last example, enter the following command to start the Docker image locally and bind it to port 5000.

docker run -p 5000:80 <DOCKERUSERNAME>/<IMAGENAME>:latest

Send Test Request

Use POSTMAN or Insomnia REST Clients to send a POST request to http://localhost:5000/api/predict that includes the following body

{
	"SepalLength": 3.3,
	"SepalWidth": 1.6,
	"PetalLength": 0.2,
	"PetalWidth": 5.1,
}

To stop the container, use Ctrl + C

6. Push Docker Image to Docker Hub

To make our Docker image accessible to everyone else and the Azure Container Instance service, we need to push it to Docker Hub. This can be done with the following command

docker login
docker push <DOCKERUSERNAME>/<IMAGENAME>:latest

7. Deploy to Azure Container Instances

Prepare Manifest File

The azuredeploy.json file helps define the deployment configurations for the application. However, in order to specify your Docker image as the one that will be deployed, the containerimage property needs to be replaced with your Docker Hub username and name of the image you just pushed to Docker Hub.

Deploy Application

Login

az login

Create Resource Group

az group create --name mlnetacidemogroup --location eastus

Deploy

az group deployment create --resource-group mlnetacidemogroup --template-file azuredeploy.json

8. Test Deployed Application

Give it a few minutes for your deployment to initialize. If the deployment was successful, you should see some output on the command line. Look for the ContainerIPv4Address property. This is the IP Address where your container is accessible. In POSTMAN or Insomnia, replace the URL to which you previously made a POST request to with http://<ContainerIPv4Address>/api/predict where ContainerIPv4Address is the value that was returned to the command line after the deployment. If successful, the response should be just like previous requests Iris-virginica.

Once you’re finished, you can clean up resources with the following command:

az group delete --name mlnetacidemogroup

mlnetacidemo's People

Contributors

lqdev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

mlnetacidemo's Issues

License missing

Repo is missing a license file. Having the license file will help this repo spread. I'd recommend a MIT license, which is what my personal repos are under, along with our ML.NET repo. Expanding the README.md with parts (or the full demo) from your blog will also help it spread.

Fantastic work, btw.

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.