Skip to content

On-premise users: click in-app to access the full platform documentation for your version of DataRobot.

Use Scoring Code with Azure ML

You must complete the following before importing Scoring Code models to Azure ML:

To import a Scoring Code model to Azure ML:

  1. Login to Azure with the login command.

    az login
    
  2. If you have not yet created a resource group, you can create one using this command:

    az group create --location --name [--subscription] [--tags]
    

    For example:

    az group create --location westus2 --name myresourcegroup
    
  3. If you do not have an existing container registry that you want to use for storing custom Docker images, you must create one. If you want to use a DataRobot Docker image instead of building your own, you do not need to create a container registry. Instead, skip ahead to step 6.

    Create a container with the following command:

    az acr create --name --resource-group --sku {Basic | Classic | Premium | Standard}
    [--admin-enabled {false | true}] [--default-action {Allow | Deny}] [--location]
    [--subscription] [--tags] [--workspace]
    

    For example:

    az acr create --name mycontainerregistry --resource-group myresourcegroup --sku Basic
    
  4. Set up admin access using the following commands:

    az acr update --name --admin-enabled {false | true}
    

    For example:

    az acr update --name mycontainerregistry --admin-enabled true
    

    And print the registry credentials:

    az acr credential show --name
    

    For example:

    az acr credential show --name mycontainerregistry
    

    Returns:

    {
      "passwords": [
        {
          "name": "password",
          "value": <password>
        },
        {
          "name": "password2",
          "value": <password>
        }
      ],
      "username": mycontainerregistry
    }
    
  5. Upload a custom Docker image that runs Java:

    az acr build --registry [--auth-mode {Default | None}] [--build-arg] [--file] [--image]
    [--no-format] [--no-logs] [--no-push] [--no-wait] [--platform] [--resource-group]
    [--secret-build-arg] [--subscription] [--target] [--timeout] []
    

    For example:

    az acr build --registry mycontainerregistry --image myImage:1 --resource-group myresourcegroup --file Dockerfile .
    

    The following is an example of a custom Docker image. Reference the Microsoft documentation to read more about building an image.

    FROM ubuntu:16.04
    
    ARG CONDA_VERSION=4.5.12
    ARG PYTHON_VERSION=3.6
    
    ENV LANG=C.UTF-8 LC_ALL=C.UTF-8
    ENV PATH /opt/miniconda/bin:$PATH
    
    RUN apt-get update --fix-missing && \
        apt-get install -y wget bzip2 && \
        apt-get clean && \
        rm -rf /var/lib/apt/lists/*
    
    RUN wget --quiet https://repo.anaconda.com/miniconda/Miniconda3-${CONDA_VERSION}-Linux-x86_64.sh -O ~/miniconda.sh && \
        /bin/bash ~/miniconda.sh -b -p /opt/miniconda && \
        rm ~/miniconda.sh && \
        /opt/miniconda/bin/conda clean -tipsy
    
    RUN conda install -y conda=${CONDA_VERSION} python=${PYTHON_VERSION} && \
        conda clean -aqy && \
        rm -rf /opt/miniconda/pkgs && \
        find / -type d -name __pycache__ -prune -exec rm -rf {} \;
    
    RUN apt-get update && \
        apt-get upgrade -y && \
        apt-get install software-properties-common -y && \
        add-apt-repository ppa:openjdk-r/ppa -y && \
        apt-get update -q && \
        apt-get install -y openjdk-11-jdk && \
        apt-get clean
    
  6. If you have not already created a workspace, use the following command to create one. Otherwise, skip to step 7.

    az ml workspace create --workspace-name [--application-insights] [--container-registry]
    [--exist-ok] [--friendly-name] [--keyvault] [--location] [--resource-group] [--sku]
    [--storage-account] [--yes]
    

    For example:

    az ml workspace create --workspace-name myworkspace --resource-group myresourcegroup
    
  7. Register your Scoring Code model to the Azure model storage.

    Note

    Make sure you have exported your Scoring Code JAR file from DataRobot before proceeding. You can download the JAR file from the Leaderboard or from a deployment.

    az ml model register --name [--asset-path][--cc] [--description][--experiment-name] [--gb][--gc] [--model-framework][--model-framework-version] [--model-path][--output-metadata-file] [--path][--property] [--resource-group][--run-id] [--run-metadata-file][--sample-input-dataset-id] [--sample-output-dataset-id][--tag] [--workspace-name][-v]

    For example, to register model named codegenmodel:

    az ml model register --name codegenmodel --model-path 5cd071deef881f011a334c2f.jar --resource-group myresourcegroup --workspace-name myworkspace
    
  8. Prepare two configs and a Python entry script that will execute the prediction.

    Below are some examples of configs with a Python entry script.

    • deploymentconfig.json:

      {
          "computeType": "aci",
          "containerResourceRequirements": {
              "cpu": 0.5,
              "memoryInGB": 1.0
          },
          "authEnabled": true,
          "sslEnabled": false,
          "appInsightsEnabled": false
      }
      
    • inferenceconfig.json (if you are not using a DataRobot Docker image):

      {
          "entryScript": "score.py",
          "runtime": "python",
          "enableGpu": false,
          "baseImage": "<container-registry-name>.azurecr.io/<Docker-image-name>",
          "baseImageRegistry": {
              "address": "<container-registry-name>.azurecr.io",
              "password": <password from the step 2>,
              "username": <container-registry-name>
          }
      }
      
    • inferenceconfig.json(if you are using a DataRobot Docker image):

      {
          "entryScript": "score.py",
          "runtime": "python",
          "enableGpu": false,
          "baseImage": "datarobotdev/scoring-inference-code-azure:latest",
          "baseImageRegistry": {
              "address": "registry.hub.docker.com"
          }
      }
      
    • score.py:

      import os
      import subprocess
      import tempfile
      import json
      from azureml.core import Model
      
      # Called when the deployed service starts
      
      def init():
      pass
      
      # Handle requests to the service
      
      def run(data):
          try:
              result_csv = ''
              data = json.loads(data)
              # Access your model registered in step 6
              model_path = Model.get_model_path('codegenmodel')
              with tempfile.NamedTemporaryFile() as output_file:
                  p = subprocess.run(['java', '-jar', model_path, 'csv', '--input=-',
      '--output={}'.format(output_file.name)], input=bytearray(data['csv'].encode('utf-8')), stdout=subprocess.PIPE)
                  with open(output_file.name) as result_file:
                      result_csv = result_file.read()
      
      # Return prediction
      
      return result_csv
      except Exception as e:
          error = str(e)
          return error
      
  9. Create a new prediction endpoint:

    az ml model deploy --name [--ae] [--ai] [--ar] [--as] [--at] [--autoscale-max-replicas]
    [--autoscale-min-replicas] [--base-image] [--base-image-registry] [--cc] [--cf]
    [--collect-model-data] [--compute-target] [--compute-type] [--cuda-version] [--dc]
    [--description] [--dn] [--ds] [--ed] [--eg] [--entry-script] [--environment-name]
    [--environment-version] [--failure-threshold] [--gb] [--gc] [--ic] [--id] [--kp]
    [--ks] [--lo] [--max-request-wait-time] [--model] [--model-metadata-file] [--namespace]
    [--no-wait] [--nr] [--overwrite] [--path] [--period-seconds] [--pi] [--po] [--property]
    [--replica-max-concurrent-requests] [--resource-group] [--rt] [--sc] [--scoring-timeout-ms]
    [--sd] [--se] [--sk] [--sp] [--st] [--tag] [--timeout-seconds] [--token-auth-enabled]
    [--workspace-name] [-v]
    

    For example, to create a new endpoint with the name myservice:

    az ml model deploy --name myservice --model codegenmodel:1 --compute-target akscomputetarget --ic inferenceconfig.json --dc deploymentconfig.json --resource-group myresourcegroup --workspace-name myworkspace
    
  10. Get a token to make prediction requests:

    az ml service get-keys --name [--path] [--resource-group] [--workspace-name] [-v]
    

    For example:

    az ml service get-keys --name myservice --resource-group myresourcegroup --workspace-name myworkspace
    

    This command returns a JSON response:

    {
        "primaryKey": <key>,
        "secondaryKey": <key>
    }
    

You can now make prediction requests using Azure.


Updated August 6, 2024