use of com.google.cloud.aiplatform.v1.schema.predict.instance.TextClassificationPredictionInstance in project java-aiplatform by googleapis.
the class PredictTextClassificationSingleLabelSample method predictTextClassificationSingleLabel.
static void predictTextClassificationSingleLabel(String project, String content, String endpointId) 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);
TextClassificationPredictionInstance predictionInstance = TextClassificationPredictionInstance.newBuilder().setContent(content).build();
List<Value> instances = new ArrayList<>();
instances.add(ValueConverter.toValue(predictionInstance));
PredictResponse predictResponse = predictionServiceClient.predict(endpointName, instances, ValueConverter.EMPTY_VALUE);
System.out.println("Predict Text Classification Response");
System.out.format("\tDeployed Model Id: %s\n", predictResponse.getDeployedModelId());
System.out.println("Predictions:\n\n");
for (Value prediction : predictResponse.getPredictionsList()) {
ClassificationPredictionResult.Builder resultBuilder = ClassificationPredictionResult.newBuilder();
// Display names and confidences values correspond to
// IDs in the ID list.
ClassificationPredictionResult result = (ClassificationPredictionResult) ValueConverter.fromValue(resultBuilder, prediction);
int counter = 0;
for (Long id : result.getIdsList()) {
System.out.printf("Label ID: %d\n", id);
System.out.printf("Label: %s\n", result.getDisplayNames(counter));
System.out.printf("Confidence: %.4f\n", result.getConfidences(counter));
counter++;
}
}
}
}
Aggregations