Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save junwan01/bd3f043d9ba077ba8d3f8fc80f7e81ca to your computer and use it in GitHub Desktop.
Save junwan01/bd3f043d9ba077ba8d3f8fc80f7e81ca to your computer and use it in GitHub Desktop.
String host = "localhost";
int port = 8501;
// the model's name.
String modelName = "cool_model";
// model's version
long modelVersion = 123456789;
// assume this model takes input of free text, and make some sentiment prediction.
String modelInput = "some text input to make prediction with";
// create a channel
ManagedChannel channel = ManagedChannelBuilder.forAddress(host, port).usePlaintext().build();
PredictionServiceGrpc.PredictionServiceBlockingStub stub = PredictionServiceGrpc.newBlockingStub(channel);
// create a modelspec
Model.ModelSpec.Builder modelSpecBuilder = Model.ModelSpec.newBuilder();
modelSpecBuilder.setName(modelName);
modelSpecBuilder.setVersion(Int64Value.of(modelVersion));
modelSpecBuilder.setSignatureName("serving_default");
Predict.PredictRequest.Builder builder = Predict.PredictRequest.newBuilder();
builder.setModelSpec(modelSpecBuilder);
// create the TensorProto and request
TensorProto.Builder tensorProtoBuilder = TensorProto.newBuilder();
tensorProtoBuilder.setDtype(DataType.DT_STRING);
TensorShapeProto.Builder tensorShapeBuilder = TensorShapeProto.newBuilder();
tensorShapeBuilder.addDim(TensorShapeProto.Dim.newBuilder().setSize(1));
tensorProtoBuilder.setTensorShape(tensorShapeBuilder.build());
tensorProtoBuilder.addStringVal(ByteString.copyFromUtf8(modelInput));
TensorProto tp = tensorProtoBuilder.build();
builder.putInputs("inputs", tp);
Predict.PredictRequest request = builder.build();
Predict.PredictResponse response = stub.predict(request);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment