Search in sources :

Example 1 with TextExtractionPredictionResult

use of com.google.cloud.aiplatform.v1.schema.predict.prediction.TextExtractionPredictionResult in project java-aiplatform by googleapis.

the class PredictTextEntityExtractionSample method predictTextEntityExtraction.

static void predictTextEntityExtraction(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";
        String jsonString = "{\"content\": \"" + content + "\"}";
        EndpointName endpointName = EndpointName.of(project, location, endpointId);
        TextExtractionPredictionInstance instance = TextExtractionPredictionInstance.newBuilder().setContent(content).build();
        List<Value> instances = new ArrayList<>();
        instances.add(ValueConverter.toValue(instance));
        PredictResponse predictResponse = predictionServiceClient.predict(endpointName, instances, ValueConverter.EMPTY_VALUE);
        System.out.println("Predict Text Entity Extraction Response");
        System.out.format("\tDeployed Model Id: %s\n", predictResponse.getDeployedModelId());
        System.out.println("Predictions");
        for (Value prediction : predictResponse.getPredictionsList()) {
            TextExtractionPredictionResult.Builder resultBuilder = TextExtractionPredictionResult.newBuilder();
            TextExtractionPredictionResult result = (TextExtractionPredictionResult) ValueConverter.fromValue(resultBuilder, prediction);
            for (int i = 0; i < result.getIdsCount(); i++) {
                long textStartOffset = result.getTextSegmentStartOffsets(i);
                long textEndOffset = result.getTextSegmentEndOffsets(i);
                String entity = content.substring((int) textStartOffset, (int) textEndOffset);
                System.out.format("\tEntity: %s\n", entity);
                System.out.format("\tEntity type: %s\n", result.getDisplayNames(i));
                System.out.format("\tConfidences: %f\n", result.getConfidences(i));
                System.out.format("\tIDs: %d\n", result.getIds(i));
            }
        }
    }
}
Also used : TextExtractionPredictionInstance(com.google.cloud.aiplatform.v1.schema.predict.instance.TextExtractionPredictionInstance) ArrayList(java.util.ArrayList) PredictResponse(com.google.cloud.aiplatform.v1.PredictResponse) PredictionServiceClient(com.google.cloud.aiplatform.v1.PredictionServiceClient) PredictionServiceSettings(com.google.cloud.aiplatform.v1.PredictionServiceSettings) TextExtractionPredictionResult(com.google.cloud.aiplatform.v1.schema.predict.prediction.TextExtractionPredictionResult) EndpointName(com.google.cloud.aiplatform.v1.EndpointName) Value(com.google.protobuf.Value)

Aggregations

EndpointName (com.google.cloud.aiplatform.v1.EndpointName)1 PredictResponse (com.google.cloud.aiplatform.v1.PredictResponse)1 PredictionServiceClient (com.google.cloud.aiplatform.v1.PredictionServiceClient)1 PredictionServiceSettings (com.google.cloud.aiplatform.v1.PredictionServiceSettings)1 TextExtractionPredictionInstance (com.google.cloud.aiplatform.v1.schema.predict.instance.TextExtractionPredictionInstance)1 TextExtractionPredictionResult (com.google.cloud.aiplatform.v1.schema.predict.prediction.TextExtractionPredictionResult)1 Value (com.google.protobuf.Value)1 ArrayList (java.util.ArrayList)1