Search in sources :

Example 1 with SeldonMessage

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

the class RestClientController method prediction.

@RequestMapping(value = "/api/v0.1/predictions", method = RequestMethod.POST, consumes = "application/json; charset=utf-8", produces = "application/json; charset=utf-8")
public ResponseEntity<String> prediction(RequestEntity<String> requestEntity, Principal principal) {
    String clientId = principal.getName();
    String json = requestEntity.getBody();
    logger.info(String.format("[%s] [%s] [%s] [%s]", "POST", requestEntity.getUrl().getPath(), clientId, json));
    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 SeldonAPIException(ApiExceptionType.APIFE_INVALID_JSON, requestEntity.getBody());
    }
    HttpStatus httpStatus = HttpStatus.OK;
    // At present passes JSON string. Could use gRPC?
    String ret = predictionService.predict(json, clientId);
    SeldonMessage response;
    try {
        SeldonMessage.Builder builder = SeldonMessage.newBuilder();
        ProtoBufUtils.updateMessageBuilderFromJson(builder, ret);
        response = builder.build();
    } catch (InvalidProtocolBufferException e) {
        logger.error("Bad response", e);
        throw new SeldonAPIException(ApiExceptionType.APIFE_INVALID_RESPONSE_JSON, requestEntity.getBody());
    }
    kafkaProducer.send(clientId, RequestResponse.newBuilder().setRequest(request).setResponse(response).build());
    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.setContentType(MediaType.APPLICATION_JSON);
    ResponseEntity<String> responseEntity = new ResponseEntity<String>(ret, responseHeaders, httpStatus);
    return responseEntity;
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) SeldonMessage(io.seldon.protos.PredictionProtos.SeldonMessage) SeldonAPIException(io.seldon.apife.exception.SeldonAPIException) HttpStatus(org.springframework.http.HttpStatus) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with SeldonMessage

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

the class InternalPredictionService method queryREST.

private SeldonMessage queryREST(String path, String dataString, PredictiveUnitState state, Endpoint endpoint, boolean isDefault) {
    long timeNow = System.currentTimeMillis();
    URI uri;
    try {
        URIBuilder builder = new URIBuilder().setScheme("http").setHost(endpoint.getServiceHost()).setPort(endpoint.getServicePort()).setPath("/" + path);
        uri = builder.build();
    } catch (URISyntaxException e) {
        throw new APIException(APIException.ApiExceptionType.ENGINE_INVALID_ENDPOINT_URL, "Host: " + endpoint.getServiceHost() + " port:" + endpoint.getServicePort());
    }
    try {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        headers.add(MODEL_NAME_HEADER, state.name);
        headers.add(MODEL_IMAGE_HEADER, state.imageName);
        headers.add(MODEL_VERSION_HEADER, state.imageVersion);
        MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
        map.add("json", dataString);
        map.add("isDefault", Boolean.toString(isDefault));
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);
        logger.info("Requesting " + uri.toString());
        ResponseEntity<String> httpResponse = restTemplate.postForEntity(uri, request, String.class);
        try {
            if (httpResponse.getStatusCode().is2xxSuccessful()) {
                SeldonMessage.Builder builder = SeldonMessage.newBuilder();
                String response = httpResponse.getBody();
                logger.info(response);
                JsonFormat.parser().ignoringUnknownFields().merge(response, builder);
                return builder.build();
            } else {
                logger.error("Couldn't retrieve prediction from external prediction server -- bad http return code: " + httpResponse.getStatusCode());
                throw new APIException(APIException.ApiExceptionType.ENGINE_MICROSERVICE_ERROR, String.format("Bad return code %d", httpResponse.getStatusCode()));
            }
        } finally {
            if (logger.isDebugEnabled())
                logger.debug("External prediction server took " + (System.currentTimeMillis() - timeNow) + "ms");
        }
    } catch (IOException e) {
        logger.error("Couldn't retrieve prediction from external prediction server - ", e);
        throw new APIException(APIException.ApiExceptionType.ENGINE_MICROSERVICE_ERROR, e.toString());
    } catch (Exception e) {
        logger.error("Couldn't retrieve prediction from external prediction server - ", e);
        throw new APIException(APIException.ApiExceptionType.ENGINE_MICROSERVICE_ERROR, e.toString());
    } finally {
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) APIException(io.seldon.engine.exception.APIException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) IOException(java.io.IOException) URIBuilder(org.apache.http.client.utils.URIBuilder) APIException(io.seldon.engine.exception.APIException) SeldonMessage(io.seldon.protos.PredictionProtos.SeldonMessage) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap)

Example 3 with SeldonMessage

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

the class TestPredictionProto method parse_json_extra_fields.

@Test
public void parse_json_extra_fields() throws InvalidProtocolBufferException {
    String json = "{\"x\":1.0,\"request\":{\"values\":[[1.0],[2.0]]}}";
    SeldonMessage.Builder builder = SeldonMessage.newBuilder();
    ProtoBufUtils.updateMessageBuilderFromJson(builder, json);
    SeldonMessage request = builder.build();
    String json2 = ProtoBufUtils.toJson(request);
    System.out.println(json2);
}
Also used : SeldonMessage(io.seldon.protos.PredictionProtos.SeldonMessage) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

Example 4 with SeldonMessage

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

the class TestPredictionProto method customBytesRequest.

@Test
public void customBytesRequest() throws InvalidProtocolBufferException {
    String customData = "{\"c\":1.0}";
    SeldonMessage.Builder b = SeldonMessage.newBuilder();
    b.setBinData(ByteString.copyFrom(customData.getBytes()));
    SeldonMessage request = b.build();
    String json = ProtoBufUtils.toJson(request);
    System.out.println(json);
    SeldonMessage.Builder b2 = SeldonMessage.newBuilder();
    ProtoBufUtils.updateMessageBuilderFromJson(b2, json);
    SeldonMessage request2 = b2.build();
    String custom = request2.getBinData().toString(StandardCharsets.UTF_8);
    System.out.println(custom);
    String json2 = ProtoBufUtils.toJson(request2);
    System.out.println(json2);
    Assert.assertEquals(json, json2);
}
Also used : SeldonMessage(io.seldon.protos.PredictionProtos.SeldonMessage) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

Example 5 with SeldonMessage

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

the class TestPredictionProto method customStringRequest.

@Test
public void customStringRequest() throws InvalidProtocolBufferException {
    String customData = "{\"c\":1.0}";
    SeldonMessage.Builder b = SeldonMessage.newBuilder();
    b.setStrData(customData);
    SeldonMessage request = b.build();
    String json = ProtoBufUtils.toJson(request);
    System.out.println(json);
    SeldonMessage.Builder b2 = SeldonMessage.newBuilder();
    ProtoBufUtils.updateMessageBuilderFromJson(b2, json);
    SeldonMessage request2 = b2.build();
    String json2 = ProtoBufUtils.toJson(request2);
    System.out.println(json2);
    Assert.assertEquals(json, json2);
}
Also used : SeldonMessage(io.seldon.protos.PredictionProtos.SeldonMessage) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

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