Invoke applications
Inferencing applications can be achieved using any of the methods described below.

Hydrosphere UI

To send a sample request using Hydrosphere UI, open the desired application, and press the Test button at the upper right corner. We will generate dummy inputs based on your model's contract and send an HTTP request to the model's endpoint.
post
/gateway/application/<application_name>
HTTP Inference

gRPC

To send a gRPC request you need to create a specific client.
Python
Java
1
import grpc
2
import hydro_serving_grpc as hs # pip install hydro-serving-grpc
3
​
4
# connect to your ML Lamba instance
5
channel = grpc.insecure_channel("<host>")
6
stub = hs.PredictionServiceStub(channel)
7
​
8
# 1. define a model, that you'll use
9
model_spec = hs.ModelSpec(name="model")
10
​
11
# 2. define tensor_shape for Tensor instance
12
tensor_shape = hs.TensorShapeProto(
13
dim=[hs.TensorShapeProto.Dim(size=-1), hs.TensorShapeProto.Dim(size=2)])
14
​
15
# 3. define tensor with needed data
16
tensor = hs.TensorProto(dtype=hs.DT_DOUBLE, tensor_shape=tensor_shape, double_val=[1,1,1,1])
17
​
18
# 4. create PredictRequest instance
19
request = hs.PredictRequest(model_spec=model_spec, inputs={"x": tensor})
20
​
21
# call Predict method
22
result = stub.Predict(request)
Copied!
1
import com.google.protobuf.Int64Value;
2
import io.grpc.ManagedChannel;
3
import io.grpc.ManagedChannelBuilder;
4
import io.hydrosphere.serving.tensorflow.DataType;
5
import io.hydrosphere.serving.tensorflow.TensorProto;
6
import io.hydrosphere.serving.tensorflow.TensorShapeProto;
7
import io.hydrosphere.serving.tensorflow.api.Model;
8
import io.hydrosphere.serving.tensorflow.api.Predict;
9
import io.hydrosphere.serving.tensorflow.api.PredictionServiceGrpc;
10
​
11
import java.util.Random;
12
​
13
public class HydrosphereClient {
14
​
15
private final String modelName; // Actual model name, registered within Hydrosphere platform
16
private final Int64Value modelVersion; // Model version of the registered model within Hydrosphere platform
17
private final ManagedChannel channel;
18
private final PredictionServiceGrpc.PredictionServiceBlockingStub blockingStub;
19
​
20
public HydrosphereClient2(String target, String modelName, long modelVersion) {
21
this(ManagedChannelBuilder.forTarget(target).build(), modelName, modelVersion);
22
}
23
​
24
HydrosphereClient2(ManagedChannel channel, String modelName, long modelVersion) {
25
this.channel = channel;
26
this.modelName = modelName;
27
this.modelVersion = Int64Value.newBuilder().setValue(modelVersion).build();
28
this.blockingStub = PredictionServiceGrpc.newBlockingStub(this.channel);
29
}
30
​
31
private Model.ModelSpec getModelSpec() {
32
/*
33
Helper method to generate ModelSpec.
34
*/
35
return Model.ModelSpec.newBuilder()
36
.setName(this.modelName)
37
.setVersion(this.modelVersion)
38
.build();
39
}
40
​
41
private TensorProto generateDoubleTensorProto() {
42
/*
43
Helper method generating random TensorProto object for double values.
44
*/
45
return TensorProto.newBuilder()
46
.addDoubleVal(new Random().nextDouble())
47
.setDtype(DataType.DT_DOUBLE)
48
.setTensorShape(TensorShapeProto.newBuilder().build()) // Empty TensorShape indicates scalar shape
49
.build();
50
}
51
​
52
public Predict.PredictRequest generatePredictRequest() {
53
/*
54
PredictRequest is used to define the data passed to the model for inference.
55
*/
56
return Predict.PredictRequest.newBuilder()
57
.putInputs("in", this.generateDoubleTensorProto())
58
.setModelSpec(this.getModelSpec())
59
.build();
60
}
61
​
62
​
63
public Predict.PredictResponse predict(Predict.PredictRequest request) {
64
/*
65
The actual use of RPC method Predict of the PredictionService to invoke prediction.
66
*/
67
return this.blockingStub.predict(request);
68
}
69
​
70
public static void main(String[] args) throws Exception {
71
HydrosphereClient client = new HydrosphereClient("<host>", "example", 2);
72
Predict.PredictRequest request = client.generatePredictRequest();
73
Predict.PredictResponse response = client.predict(request);
74
System.out.println(response);
75
}
76
}
Copied!

Python SDK

You can learn more about our Python SDK here.
1
import hydrosdk as hs
2
​
3
hs_cluster = hs.Cluster(http_address='{HTTP_CLUSTER_ADDRESS}',
4
grpc_address='{GRPC_CLUSTER_ADDRESS}',)
5
​
6
app = hs.Application.find(hs_cluster, "{APP_NAME}")
7
​
8
predictor = adult_servable.predictor()
9
​
10
data = ... # your data
11
predictor.predict(data)
Copied!
Last modified 3mo ago