Search in sources :

Example 21 with SeldonMessage

use of io.seldon.protos.PredictionProtos.SeldonMessage in project seldon-core by SeldonIO.

the class RandomABTestUnitTest method simpleTest.

@Test
public void simpleTest() throws InterruptedException, ExecutionException, InvalidProtocolBufferException {
    PredictorSpec.Builder predictorSpecBuilder = PredictorSpec.newBuilder();
    predictorSpecBuilder.setName("p1");
    predictorSpecBuilder.setReplicas(1);
    predictorSpecBuilder.setComponentSpec(PodTemplateSpec.newBuilder());
    PredictiveUnit.Builder predictiveUnitBuilder = PredictiveUnit.newBuilder();
    predictiveUnitBuilder.setName("1");
    predictiveUnitBuilder.setType(PredictiveUnit.PredictiveUnitType.MODEL);
    predictiveUnitBuilder.setImplementation(PredictiveUnit.PredictiveUnitImplementation.SIMPLE_MODEL);
    PredictiveUnit pu1 = predictiveUnitBuilder.build();
    predictiveUnitBuilder = PredictiveUnit.newBuilder();
    predictiveUnitBuilder.setName("2");
    predictiveUnitBuilder.setType(PredictiveUnit.PredictiveUnitType.MODEL);
    predictiveUnitBuilder.setImplementation(PredictiveUnit.PredictiveUnitImplementation.SIMPLE_MODEL);
    PredictiveUnit pu2 = predictiveUnitBuilder.build();
    predictiveUnitBuilder = PredictiveUnit.newBuilder();
    predictiveUnitBuilder.setName("3");
    predictiveUnitBuilder.setType(PredictiveUnit.PredictiveUnitType.ROUTER);
    predictiveUnitBuilder.setImplementation(PredictiveUnit.PredictiveUnitImplementation.RANDOM_ABTEST);
    predictiveUnitBuilder.addChildren(pu1);
    predictiveUnitBuilder.addChildren(pu2);
    Parameter.Builder pBuilder = Parameter.newBuilder().setName("ratioA").setValue("0.5").setType(ParameterType.FLOAT);
    predictiveUnitBuilder.addParameters(pBuilder.build());
    PredictiveUnit pu3 = predictiveUnitBuilder.build();
    predictorSpecBuilder.setGraph(pu3);
    PredictorSpec predictor = predictorSpecBuilder.build();
    PredictorState predictorState = predictorBean.predictorStateFromPredictorSpec(predictor);
    SeldonMessage p = SeldonMessage.newBuilder().build();
    SeldonMessage predictorReturn = predictorBean.predict(p, predictorState);
    Assert.assertEquals((double) SimpleModelUnit.values[0], predictorReturn.getData().getTensor().getValues(0), 0);
    Assert.assertEquals((double) SimpleModelUnit.values[1], predictorReturn.getData().getTensor().getValues(1), 0);
    Assert.assertEquals((double) SimpleModelUnit.values[2], predictorReturn.getData().getTensor().getValues(2), 0);
}
Also used : SeldonMessage(io.seldon.protos.PredictionProtos.SeldonMessage) PredictiveUnit(io.seldon.protos.DeploymentProtos.PredictiveUnit) Parameter(io.seldon.protos.DeploymentProtos.Parameter) PredictorSpec(io.seldon.protos.DeploymentProtos.PredictorSpec) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 22 with SeldonMessage

use of io.seldon.protos.PredictionProtos.SeldonMessage in project seldon-core by SeldonIO.

the class SimpleModelUnitTest method simpleTestWithImageNoVersion.

@Test
public void simpleTestWithImageNoVersion() throws InterruptedException, ExecutionException, InvalidProtocolBufferException {
    PredictorSpec.Builder PredictorSpecBuilder = PredictorSpec.newBuilder();
    // PredictorSpecBuilder.setEnabled(true);
    PredictorSpecBuilder.setName("p1");
    // PredictorSpecBuilder.setRoot("1");
    PredictorSpecBuilder.setReplicas(1);
    final String imageName = "myimage";
    PodTemplateSpec.Builder ptsBuilder = PodTemplateSpec.newBuilder().setSpec(PodSpec.newBuilder().addContainers(Container.newBuilder().setImage(imageName).setName("1")));
    PredictorSpecBuilder.setComponentSpec(ptsBuilder);
    PredictiveUnit.Builder PredictiveUnitBuilder = PredictiveUnit.newBuilder();
    PredictiveUnitBuilder.setName("1");
    PredictiveUnitBuilder.setType(PredictiveUnit.PredictiveUnitType.MODEL);
    PredictiveUnitBuilder.setImplementation(PredictiveUnit.PredictiveUnitImplementation.SIMPLE_MODEL);
    PredictorSpecBuilder.setGraph(PredictiveUnitBuilder.build());
    PredictorSpec predictor = PredictorSpecBuilder.build();
    PredictorState predictorState = predictorBean.predictorStateFromPredictorSpec(predictor);
    Assert.assertEquals(imageName, predictorState.getRootState().imageName);
    Assert.assertEquals("", predictorState.getRootState().imageVersion);
    SeldonMessage p = SeldonMessage.newBuilder().build();
    SeldonMessage predictorReturn = predictorBean.predict(p, predictorState);
    Assert.assertEquals((double) SimpleModelUnit.values[0], predictorReturn.getData().getTensor().getValues(0), 0);
    Assert.assertEquals((double) SimpleModelUnit.values[1], predictorReturn.getData().getTensor().getValues(1), 0);
    Assert.assertEquals((double) SimpleModelUnit.values[2], predictorReturn.getData().getTensor().getValues(2), 0);
}
Also used : PodTemplateSpec(io.kubernetes.client.proto.V1.PodTemplateSpec) SeldonMessage(io.seldon.protos.PredictionProtos.SeldonMessage) PredictiveUnit(io.seldon.protos.DeploymentProtos.PredictiveUnit) PredictorSpec(io.seldon.protos.DeploymentProtos.PredictorSpec) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 23 with SeldonMessage

use of io.seldon.protos.PredictionProtos.SeldonMessage in project seldon-core by SeldonIO.

the class SimpleModelUnitTest method simpleTestWithImageVersion.

@Test
public void simpleTestWithImageVersion() throws InterruptedException, ExecutionException, InvalidProtocolBufferException {
    PredictorSpec.Builder PredictorSpecBuilder = PredictorSpec.newBuilder();
    // PredictorSpecBuilder.setEnabled(true);
    PredictorSpecBuilder.setName("p1");
    // PredictorSpecBuilder.setRoot("1");
    PredictorSpecBuilder.setReplicas(1);
    final String imageName = "myimage";
    final String imageVersion = "0.1";
    PodTemplateSpec.Builder ptsBuilder = PodTemplateSpec.newBuilder().setSpec(PodSpec.newBuilder().addContainers(Container.newBuilder().setImage(imageName + ":" + imageVersion).setName("1")));
    PredictorSpecBuilder.setComponentSpec(ptsBuilder);
    PredictiveUnit.Builder PredictiveUnitBuilder = PredictiveUnit.newBuilder();
    PredictiveUnitBuilder.setName("1");
    PredictiveUnitBuilder.setType(PredictiveUnit.PredictiveUnitType.MODEL);
    PredictiveUnitBuilder.setImplementation(PredictiveUnit.PredictiveUnitImplementation.SIMPLE_MODEL);
    PredictorSpecBuilder.setGraph(PredictiveUnitBuilder.build());
    PredictorSpec predictor = PredictorSpecBuilder.build();
    PredictorState predictorState = predictorBean.predictorStateFromPredictorSpec(predictor);
    Assert.assertEquals(imageName, predictorState.getRootState().imageName);
    Assert.assertEquals(imageVersion, predictorState.getRootState().imageVersion);
    SeldonMessage p = SeldonMessage.newBuilder().build();
    SeldonMessage predictorReturn = predictorBean.predict(p, predictorState);
    Assert.assertEquals((double) SimpleModelUnit.values[0], predictorReturn.getData().getTensor().getValues(0), 0);
    Assert.assertEquals((double) SimpleModelUnit.values[1], predictorReturn.getData().getTensor().getValues(1), 0);
    Assert.assertEquals((double) SimpleModelUnit.values[2], predictorReturn.getData().getTensor().getValues(2), 0);
}
Also used : PodTemplateSpec(io.kubernetes.client.proto.V1.PodTemplateSpec) SeldonMessage(io.seldon.protos.PredictionProtos.SeldonMessage) PredictiveUnit(io.seldon.protos.DeploymentProtos.PredictiveUnit) PredictorSpec(io.seldon.protos.DeploymentProtos.PredictorSpec) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 24 with SeldonMessage

use of io.seldon.protos.PredictionProtos.SeldonMessage in project seldon-core by SeldonIO.

the class RestClientController method predictions.

@RequestMapping(value = "/api/v0.1/predictions", method = RequestMethod.POST, consumes = "application/json; charset=utf-8", produces = "application/json; charset=utf-8")
public ResponseEntity<String> predictions(RequestEntity<String> requestEntity) {
    SeldonMessage request;
    try {
        SeldonMessage.Builder builder = SeldonMessage.newBuilder();
        ProtoBufUtils.updateMessageBuilderFromJson(builder, requestEntity.getBody());
        request = builder.build();
    } catch (InvalidProtocolBufferException e) {
        logger.error("Bad request", e);
        throw new APIException(ApiExceptionType.ENGINE_INVALID_JSON, requestEntity.getBody());
    }
    try {
        SeldonMessage response = predictionService.predict(request);
        String json = ProtoBufUtils.toJson(response);
        return new ResponseEntity<String>(json, HttpStatus.OK);
    } catch (InterruptedException e) {
        throw new APIException(ApiExceptionType.ENGINE_INTERRUPTED, e.getMessage());
    } catch (ExecutionException e) {
        if (e.getCause().getClass() == APIException.class) {
            throw (APIException) e.getCause();
        } else {
            throw new APIException(ApiExceptionType.ENGINE_EXECUTION_FAILURE, e.getMessage());
        }
    } catch (InvalidProtocolBufferException e) {
        throw new APIException(ApiExceptionType.ENGINE_INVALID_JSON, "");
    }
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) SeldonMessage(io.seldon.protos.PredictionProtos.SeldonMessage) APIException(io.seldon.engine.exception.APIException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ExecutionException(java.util.concurrent.ExecutionException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 25 with SeldonMessage

use of io.seldon.protos.PredictionProtos.SeldonMessage in project seldon-core by SeldonIO.

the class SeldonClientExample method predict.

public void predict() throws InvalidProtocolBufferException {
    SeldonMessage request = SeldonMessage.newBuilder().setData(DefaultData.newBuilder().setTensor(Tensor.newBuilder().addValues(1.0).addShape(1))).build();
    SeldonMessage response = blockingStub.predict(request);
    logger.info(ProtoBufUtils.toJson(response));
}
Also used : SeldonMessage(io.seldon.protos.PredictionProtos.SeldonMessage)

Aggregations

SeldonMessage (io.seldon.protos.PredictionProtos.SeldonMessage)28 Test (org.junit.Test)18 ByteString (com.google.protobuf.ByteString)7 ArrayList (java.util.ArrayList)5 PredictiveUnit (io.seldon.protos.DeploymentProtos.PredictiveUnit)4 PredictorSpec (io.seldon.protos.DeploymentProtos.PredictorSpec)4 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)4 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)3 APIException (io.seldon.engine.exception.APIException)3 HashMap (java.util.HashMap)3 PodTemplateSpec (io.kubernetes.client.proto.V1.PodTemplateSpec)2 DefaultData (io.seldon.protos.PredictionProtos.DefaultData)2 HttpHeaders (org.springframework.http.HttpHeaders)2 ResponseEntity (org.springframework.http.ResponseEntity)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 Value (com.google.protobuf.Value)1 SeldonAPIException (io.seldon.apife.exception.SeldonAPIException)1 PredictorState (io.seldon.engine.predictors.PredictorState)1 Parameter (io.seldon.protos.DeploymentProtos.Parameter)1 IOException (java.io.IOException)1