Aloha Quick Tutorial

The Aloha Quick Start Guide demonstrates how to use Wallaroo to determine malicious web sites from their URL.

This tutorial and the assets can be downloaded as part of the Wallaroo Tutorials repository.

Aloha Demo

In this notebook we will walk through a simple pipeline deployment to inference on a model. For this example we will be using an open source model that uses an Aloha CNN LSTM model for classifying Domain names as being either legitimate or being used for nefarious purposes such as malware distribution.

Prerequisites

  • An installed Wallaroo instance.
  • The following Python libraries installed:
    • os
    • wallaroo: The Wallaroo SDK. Included with the Wallaroo JupyterHub service by default.
    • pandas: Pandas, mainly used for Pandas DataFrame
    • pyarrow: PyArrow for Apache Arrow support

Tutorial Goals

For our example, we will perform the following:

  • Create a workspace for our work.
  • Upload the Aloha model.
  • Create a pipeline that can ingest our submitted data, submit it to the model, and export the results
  • Run a sample inference through our pipeline by loading a file
  • Run a sample inference through our pipeline’s URL and store the results in a file.

All sample data and models are available through the Wallaroo Quick Start Guide Samples repository.

Connect to the Wallaroo Instance

The first step is to connect to Wallaroo through the Wallaroo client. The Python library is included in the Wallaroo install and available through the Jupyter Hub interface provided with your Wallaroo environment.

This is accomplished using the wallaroo.Client() command, which provides a URL to grant the SDK permission to your specific Wallaroo environment. When displayed, enter the URL into a browser and confirm permissions. Store the connection into a variable that can be referenced later.

If logging into the Wallaroo instance through the internal JupyterHub service, use wl = wallaroo.Client(). For more information on Wallaroo Client settings, see the Client Connection guide.

import wallaroo
from wallaroo.object import EntityNotFoundError

# to display dataframe tables
from IPython.display import display
# used to display dataframe information without truncating
import pandas as pd
pd.set_option('display.max_colwidth', None)
import pyarrow as pa
# Login through local Wallaroo instance

wl = wallaroo.Client()

Create the Workspace

We will create a workspace to work in and call it the “alohaworkspace”, then set it as current workspace environment. We’ll also create our pipeline in advance as alohapipeline. The model name and the model file will be specified for use in later steps.

workspace_name = f'alohaworkspace'
pipeline_name = f'alohapipeline'
model_name = f'alohamodel'
model_file_name = './alohacnnlstm.zip'
workspace = wl.get_workspace(name=workspace_name, create_if_not_exist=True)

wl.set_current_workspace(workspace)

aloha_pipeline = wl.build_pipeline(pipeline_name)
aloha_pipeline
namealohapipeline
created2024-07-25 19:08:20.480151+00:00
last_updated2024-07-25 19:08:20.480151+00:00
deployed(none)
workspace_id27
workspace_namealohaworkspace
archNone
accelNone
tags
versions70b0cdb2-9956-4ae7-9291-c911c62a7710
steps
publishedFalse

We can verify the workspace is created the current default workspace with the get_current_workspace() command.

wl.get_current_workspace()
{'name': 'alohaworkspace', 'id': 27, 'archived': False, 'created_by': '7ed7ae89-c45d-4ed7-ac5d-11cbccbbfa72', 'created_at': '2024-07-25T19:08:20.320107+00:00', 'models': [], 'pipelines': [{'name': 'alohapipeline', 'create_time': datetime.datetime(2024, 7, 25, 19, 8, 20, 480151, tzinfo=tzutc()), 'definition': '[]'}]}

Upload the Models

Now we will upload our models. Note that for this example we are applying the model from a .ZIP file. The Aloha model is a protobuf file that has been defined for evaluating web pages, and we will configure it to use data in the tensorflow format.

from wallaroo.framework import Framework

model = wl.upload_model(model_name, 
                        model_file_name,
                        framework=Framework.TENSORFLOW
                        )

Deploy a model

Now that we have a model that we want to use we will create a deployment for it.

We will tell the deployment we are using a tensorflow model and give the deployment name and the configuration we want for the deployment.

To do this, we’ll create our pipeline that can ingest the data, pass the data to our Aloha model, and give us a final output. We’ll call our pipeline aloha-test-demo, then deploy it so it’s ready to receive data. The deployment process usually takes about 45 seconds.

  • Note: If you receive an error that the pipeline could not be deployed because there are not enough resources, undeploy any other pipelines and deploy this one again. This command can quickly undeploy all pipelines to regain resources. We recommend not running this command in a production environment since it will cancel any running pipelines:
for p in wl.list_pipelines(): p.undeploy()
aloha_pipeline.add_model_step(model)
namealohapipeline
created2024-07-25 19:08:20.480151+00:00
last_updated2024-07-25 19:08:20.480151+00:00
deployed(none)
workspace_id27
workspace_namealohaworkspace
archNone
accelNone
tags
versions70b0cdb2-9956-4ae7-9291-c911c62a7710
steps
publishedFalse
aloha_pipeline.deploy()
namealohapipeline
created2024-07-25 19:08:20.480151+00:00
last_updated2024-07-25 19:16:45.422112+00:00
deployedTrue
workspace_id27
workspace_namealohaworkspace
archx86
accelnone
tags
versionsf37b947a-7d23-425d-93de-ec6bbb26fa5c, 0ceaace4-0216-40b7-a7d0-0a6ea6acad71, fd15c64b-9d03-40e0-ba6b-7ab6b314362c, 70b0cdb2-9956-4ae7-9291-c911c62a7710
stepsalohamodel
publishedFalse

We can verify that the pipeline is running and list what models are associated with it.

aloha_pipeline.status()
{'status': 'Running',
 'details': [],
 'engines': [{'ip': '10.28.1.16',
   'name': 'engine-6d766c7b5f-lmkn6',
   'status': 'Running',
   'reason': None,
   'details': [],
   'pipeline_statuses': {'pipelines': [{'id': 'alohapipeline',
      'status': 'Running',
      'version': 'f37b947a-7d23-425d-93de-ec6bbb26fa5c'}]},
   'model_statuses': {'models': [{'name': 'alohamodel',
      'sha': 'd71d9ffc61aaac58c2b1ed70a2db13d1416fb9d3f5b891e5e4e2e97180fe22f8',
      'status': 'Running',
      'version': 'b934c221-a40a-4c56-9f49-1b015a24dc80'}]}}],
 'engine_lbs': [{'ip': '10.28.1.17',
   'name': 'engine-lb-6b59985857-hhzgp',
   'status': 'Running',
   'reason': None,
   'details': []}],
 'sidekicks': []}

Inferences

Infer 1 row

Now that the pipeline is deployed and our Aloha model is in place, we’ll perform a smoke test to verify the pipeline is up and running properly. We’ll use the infer_from_file command to load a single encoded URL into the inference engine and print the results back out.

The result should tell us that the tokenized URL is legitimate (0) or fraud (1). This sample data should return close to 1 in out.main.

smoke_test = pd.DataFrame.from_records(
    [
    {
        "text_input":[
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            28,
            16,
            32,
            23,
            29,
            32,
            30,
            19,
            26,
            17
        ]
    }
]
)

result = aloha_pipeline.infer(smoke_test)
display(result.loc[:, ["time","out.main"]])
timeout.main
02024-07-25 19:17:01.744[0.997564]

Infer From File

This time, we’ll give it a bigger set of data to infer. ./data/data_1k.arrow is an Apache Arrow table with 1,000 records in it. Once submitted, we’ll turn the result into a DataFrame and display the first five results.

result = aloha_pipeline.infer_from_file('./data/data_1k.arrow')
display(result.to_pandas().loc[:, ["time","out.main"]])
timeout.main
02024-07-25 19:17:02.697[0.997564]
12024-07-25 19:17:02.697[0.9885122]
22024-07-25 19:17:02.697[0.9993358]
32024-07-25 19:17:02.697[0.99999857]
42024-07-25 19:17:02.697[0.9984837]
.........
9952024-07-25 19:17:02.697[0.9999754]
9962024-07-25 19:17:02.697[0.9999727]
9972024-07-25 19:17:02.697[0.66066873]
9982024-07-25 19:17:02.697[0.9998954]
9992024-07-25 19:17:02.697[0.99999803]

1000 rows × 2 columns

outputs =  result.to_pandas()
display(outputs.loc[:5, ["time","out.main"]])
timeout.main
02024-07-25 19:17:02.697[0.997564]
12024-07-25 19:17:02.697[0.9885122]
22024-07-25 19:17:02.697[0.9993358]
32024-07-25 19:17:02.697[0.99999857]
42024-07-25 19:17:02.697[0.9984837]
52024-07-25 19:17:02.697[1.0]

Batch Inference

Now that our smoke test is successful, let’s really give it some data. We have two inference files we can use:

  • data_1k.arrow: Contains 10,000 inferences
  • data_25k.arrow: Contains 25,000 inferences

When Apache Arrow tables are submitted to a Wallaroo Pipeline, the inference is processed natively as an Arrow table, and the results are returned as an Arrow table. This allows for faster data processing than with JSON files or DataFrame objects.

We’ll pipe the data_25k.arrow file through the aloha_pipeline deployment URL, and place the results in a file named response.arrow. We’ll also display the time this takes. Note that for larger batches of 50,000 inferences or more can be difficult to view in Jupyter Hub because of its size, so we’ll only display the first five rows.

  • IMPORTANT NOTE: The _deployment._url() method will return an internal URL when using Python commands from within the Wallaroo instance - for example, the Wallaroo JupyterHub service. When connecting via an external connection, _deployment._url() returns an external URL. External URL connections requires the authentication be included in the HTTP request, and that Model Endpoints Guide external endpoints are enabled in the Wallaroo configuration options.
inference_url = aloha_pipeline._deployment._url()
inference_url
'https://doc-test.wallarooexample.ai/v1/api/pipelines/infer/alohapipeline-18/alohapipeline'
token = wl.auth.auth_header()['Authorization']
dataFile="./data/data_25k.arrow"
contentType="application/vnd.apache.arrow.file"
!curl -X POST {inference_url} -H \
    "Authorization: {token}" -H \
    "Content-Type:{contentType}" \
    --data-binary @{dataFile} > curl_response.df
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 26.8M  100 22.0M  100 4874k  1873k   404k  0:00:12  0:00:12 --:--:-- 5865k--:--:-- --:--:-- --:--:--     09k  0:00:50  0:00:09  0:00:41 1017k
cc_data_from_file =  pd.read_json('./curl_response.df', orient="records")
display(cc_data_from_file.head(5).loc[:5, ["time","out"]])
timeout
01721935024689{'banjori': [0.0015195814], 'corebot': [0.9829147500000001], 'cryptolocker': [0.012099549000000001], 'dircrypt': [4.7591115e-05], 'gozi': [2.0289312e-05], 'kraken': [0.00031977256999999996], 'locky': [0.011029262000000001], 'main': [0.997564], 'matsnu': [0.010341609], 'pykspa': [0.008038961], 'qakbot': [0.016155047000000002], 'ramdo': [0.00623623], 'ramnit': [0.0009985747000000001], 'simda': [1.7933434e-26], 'suppobox': [1.388995e-27]}
11721935024689{'banjori': [7.447168e-18], 'corebot': [6.7359245e-08], 'cryptolocker': [0.17081994], 'dircrypt': [1.3220122000000002e-09], 'gozi': [1.2758656999999998e-24], 'kraken': [0.22559547], 'locky': [0.34209849999999997], 'main': [0.99999994], 'matsnu': [0.3080186], 'pykspa': [0.1828217], 'qakbot': [3.8022407999999996e-11], 'ramdo': [0.2062254], 'ramnit': [0.15215829], 'simda': [1.1701982e-30], 'suppobox': [3.1513975e-38]}
21721935024689{'banjori': [2.8598757999999993e-21], 'corebot': [9.302004000000001e-08], 'cryptolocker': [0.044453003000000005], 'dircrypt': [6.163770000000001e-09], 'gozi': [8.3496755e-23], 'kraken': [0.48234475], 'locky': [0.26332903], 'main': [1.0], 'matsnu': [0.29800338], 'pykspa': [0.22361776], 'qakbot': [1.5238921e-06], 'ramdo': [0.32820404000000003], 'ramnit': [0.029332504000000002], 'simda': [1.1995622e-31], 'suppobox': [0.0]}
31721935024689{'banjori': [2.1387130000000002e-15], 'corebot': [3.881756e-10], 'cryptolocker': [0.045599725], 'dircrypt': [1.9090367999999998e-07], 'gozi': [1.3140173e-25], 'kraken': [0.5954264], 'locky': [0.17374137], 'main': [0.9999996999999999], 'matsnu': [0.23151578], 'pykspa': [0.17591679999999998], 'qakbot': [1.0876152e-09], 'ramdo': [0.21832279999999998], 'ramnit': [0.012869265000000001], 'simda': [6.1588803e-28], 'suppobox': [1.4386127e-35]}
41721935024689{'banjori': [9.453379e-15], 'corebot': [7.091178e-10], 'cryptolocker': [0.049815144000000006], 'dircrypt': [5.2914135e-09], 'gozi': [7.4132087e-19], 'kraken': [1.5504633e-13], 'locky': [1.0791851e-15], 'main': [0.9999988999999999], 'matsnu': [1.5003075e-15], 'pykspa': [0.33075709999999997], 'qakbot': [2.62589e-07], 'ramdo': [0.5036279], 'ramnit': [0.020393759], 'simda': [0.0], 'suppobox': [2.3292147999999998e-38]}

Undeploy Pipeline

When finished with our tests, we will undeploy the pipeline so we have the Kubernetes resources back for other tasks. Note that if the deployment variable is unchanged aloha_pipeline.deploy() will restart the inference engine in the same configuration as before.

aloha_pipeline.undeploy()
namealohapipeline
created2024-07-25 19:08:20.480151+00:00
last_updated2024-07-25 19:16:45.422112+00:00
deployedFalse
workspace_id27
workspace_namealohaworkspace
archx86
accelnone
tags
versionsf37b947a-7d23-425d-93de-ec6bbb26fa5c, 0ceaace4-0216-40b7-a7d0-0a6ea6acad71, fd15c64b-9d03-40e0-ba6b-7ab6b314362c, 70b0cdb2-9956-4ae7-9291-c911c62a7710
stepsalohamodel
publishedFalse