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;
}
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 {
}
}
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);
}
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);
}
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);
}
Aggregations