...
Workflow example with Python processing and Python Connector agents
Workflow Configuration
The sections below provide descriptions of agent configurations for this example workflow.
WS_Handler
The Analysis agent is configured to handle requests from the web services agent. The request is translated into a format required by the machine learned model. Responses from the model are sent back to the web services agent as the result of the prediction.
...
Info |
---|
title | Example - Code for WS_Handler |
---|
|
Code Block |
---|
import ultra.Iris.UFL_Types;
import ultra.ws.Iris.PRF_WebService.Xtraining;
consume {
debug(input);
if (instanceOf(input, WSCycle_predict)) {
WSCycle_predict cycle = (WSCycle_predict) input;
PredictObservation obs = udrCreate(PredictObservation);
obs.observation = listCreate(double, cycle.param.sepal_length, cycle.param.sepal_width,
cycle.param.petal_length, cycle.param.petal_width);
obs.context = cycle;
udrRoute(obs, "observation");
}
if (instanceOf(input, PredictObservation)) {
PredictObservation obs = (PredictObservation) input;
WSCycle_predict cycle = (WSCycle_predict) obs.context;
Response response = udrCreate(Response);
response.iris = obs.prediction;
cycle.response = response;
udrRoute(cycle, "response");
}
} |
|
Predict
The Python processing agent processes UDRs by defining a consume
block, and uses the selected Interpreter profile that is used to configure the Python executables.
...
Info |
---|
title | Example - Code for Predict |
---|
|
Code Block |
---|
import pickle
model = None
targets = None
def consume(input):
if isinstance(input, InstallModel):
global model, targets
model = pickle.loads(input.model)
targets = input.targets
elif isinstance(input, PredictObservation):
if model:
idx = model.predict([input.observation])[0]
input.prediction = targets[idx]
else:
input.prediction = 'please install model'
if input.testing:
debug(input)
udrRoute(input, 'test_prediction')
else:
udrRoute(input, 'prediction')
|
|
Python_Connector
The Python Connector agent is configured to bind on port 3810 from which the data will be received. The types accepted for routing are PredictObservation (Iris.UFL_Types)
and InstallModel (Iris.UFL_Types)
on route r_1.
...
Info |
---|
title | Example - Code for Python Connector API |
---|
|
Code Block |
---|
import pickle
from .UFL_Types import InstallModel
from .UFL_Types import PredictObservation
def install_model(model, targets):
udr = InstallModel(
model=pickle.dumps(model),
targets=targets)
udrRoute(udr)
def predict(observation):
udr = PredictObservation(
observation=observation,
testing=True)
udrRoute(udr)
return udrConsume().prediction
__all__ = ['install_model', 'predict'] |
|
You start the workflow, open the exploration tool of your choice, and run your script.
...