use of com.google.cloud.aiplatform.v1.PredictRequest in project java-aiplatform by googleapis.
the class PredictCustomTrainedModelSample method predictCustomTrainedModel.
static void predictCustomTrainedModel(String project, String endpointId, String instance) throws IOException {
PredictionServiceSettings predictionServiceSettings = PredictionServiceSettings.newBuilder().setEndpoint("us-central1-aiplatform.googleapis.com:443").build();
// the "close" method on the client to safely clean up any remaining background resources.
try (PredictionServiceClient predictionServiceClient = PredictionServiceClient.create(predictionServiceSettings)) {
String location = "us-central1";
EndpointName endpointName = EndpointName.of(project, location, endpointId);
ListValue.Builder listValue = ListValue.newBuilder();
JsonFormat.parser().merge(instance, listValue);
List<Value> instanceList = listValue.getValuesList();
PredictRequest predictRequest = PredictRequest.newBuilder().setEndpoint(endpointName.toString()).addAllInstances(instanceList).build();
PredictResponse predictResponse = predictionServiceClient.predict(predictRequest);
System.out.println("Predict Custom Trained model Response");
System.out.format("\tDeployed Model Id: %s\n", predictResponse.getDeployedModelId());
System.out.println("Predictions");
for (Value prediction : predictResponse.getPredictionsList()) {
System.out.format("\tPrediction: %s\n", prediction);
}
}
}
use of com.google.cloud.aiplatform.v1.PredictRequest in project java-automl by googleapis.
the class LanguageEntityExtractionPredict method predict.
static void predict(String projectId, String modelId, String content) throws IOException {
// the "close" method on the client to safely clean up any remaining background resources.
try (PredictionServiceClient client = PredictionServiceClient.create()) {
// Get the full path of the model.
ModelName name = ModelName.of(projectId, "us-central1", modelId);
// For available mime types, see:
// https://cloud.google.com/automl/docs/reference/rest/v1/projects.locations.models/predict#textsnippet
TextSnippet textSnippet = TextSnippet.newBuilder().setContent(content).setMimeType(// Types: text/plain, text/html
"text/plain").build();
ExamplePayload payload = ExamplePayload.newBuilder().setTextSnippet(textSnippet).build();
PredictRequest predictRequest = PredictRequest.newBuilder().setName(name.toString()).setPayload(payload).build();
PredictResponse response = client.predict(predictRequest);
for (AnnotationPayload annotationPayload : response.getPayloadList()) {
System.out.format("Text Extract Entity Type: %s\n", annotationPayload.getDisplayName());
System.out.format("Text score: %.2f\n", annotationPayload.getTextExtraction().getScore());
TextSegment textSegment = annotationPayload.getTextExtraction().getTextSegment();
System.out.format("Text Extract Entity Content: %s\n", textSegment.getContent());
System.out.format("Text Start Offset: %s\n", textSegment.getStartOffset());
System.out.format("Text End Offset: %s\n\n", textSegment.getEndOffset());
}
}
}
use of com.google.cloud.aiplatform.v1.PredictRequest in project java-automl by googleapis.
the class LanguageSentimentAnalysisPredict method predict.
static void predict(String projectId, String modelId, String content) throws IOException {
// the "close" method on the client to safely clean up any remaining background resources.
try (PredictionServiceClient client = PredictionServiceClient.create()) {
// Get the full path of the model.
ModelName name = ModelName.of(projectId, "us-central1", modelId);
// For available mime types, see:
// https://cloud.google.com/automl/docs/reference/rest/v1/projects.locations.models/predict#textsnippet
TextSnippet textSnippet = TextSnippet.newBuilder().setContent(content).setMimeType(// Types: text/plain, text/html
"text/plain").build();
ExamplePayload payload = ExamplePayload.newBuilder().setTextSnippet(textSnippet).build();
PredictRequest predictRequest = PredictRequest.newBuilder().setName(name.toString()).setPayload(payload).build();
PredictResponse response = client.predict(predictRequest);
for (AnnotationPayload annotationPayload : response.getPayloadList()) {
System.out.format("Predicted class name: %s\n", annotationPayload.getDisplayName());
System.out.format("Predicted sentiment score: %d\n", annotationPayload.getTextSentiment().getSentiment());
}
}
}
use of com.google.cloud.aiplatform.v1.PredictRequest in project java-automl by googleapis.
the class VisionClassificationPredict method predict.
static void predict(String projectId, String modelId, String filePath) throws IOException {
// the "close" method on the client to safely clean up any remaining background resources.
try (PredictionServiceClient client = PredictionServiceClient.create()) {
// Get the full path of the model.
ModelName name = ModelName.of(projectId, "us-central1", modelId);
ByteString content = ByteString.copyFrom(Files.readAllBytes(Paths.get(filePath)));
Image image = Image.newBuilder().setImageBytes(content).build();
ExamplePayload payload = ExamplePayload.newBuilder().setImage(image).build();
PredictRequest predictRequest = PredictRequest.newBuilder().setName(name.toString()).setPayload(payload).putParams("score_threshold", // [0.0-1.0] Only produce results higher than this value
"0.8").build();
PredictResponse response = client.predict(predictRequest);
for (AnnotationPayload annotationPayload : response.getPayloadList()) {
System.out.format("Predicted class name: %s\n", annotationPayload.getDisplayName());
System.out.format("Predicted class score: %.2f\n", annotationPayload.getClassification().getScore());
}
}
}
use of com.google.cloud.aiplatform.v1.PredictRequest in project java-automl by googleapis.
the class VisionObjectDetectionPredict method predict.
static void predict(String projectId, String modelId, String filePath) throws IOException {
// the "close" method on the client to safely clean up any remaining background resources.
try (PredictionServiceClient client = PredictionServiceClient.create()) {
// Get the full path of the model.
ModelName name = ModelName.of(projectId, "us-central1", modelId);
ByteString content = ByteString.copyFrom(Files.readAllBytes(Paths.get(filePath)));
Image image = Image.newBuilder().setImageBytes(content).build();
ExamplePayload payload = ExamplePayload.newBuilder().setImage(image).build();
PredictRequest predictRequest = PredictRequest.newBuilder().setName(name.toString()).setPayload(payload).putParams("score_threshold", // [0.0-1.0] Only produce results higher than this value
"0.5").build();
PredictResponse response = client.predict(predictRequest);
for (AnnotationPayload annotationPayload : response.getPayloadList()) {
System.out.format("Predicted class name: %s\n", annotationPayload.getDisplayName());
System.out.format("Predicted class score: %.2f\n", annotationPayload.getImageObjectDetection().getScore());
BoundingPoly boundingPoly = annotationPayload.getImageObjectDetection().getBoundingBox();
System.out.println("Normalized Vertices:");
for (NormalizedVertex vertex : boundingPoly.getNormalizedVerticesList()) {
System.out.format("\tX: %.2f, Y: %.2f\n", vertex.getX(), vertex.getY());
}
}
}
}
Aggregations