use of com.google.cloud.aiplatform.v1.PredictionServiceSettings 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.PredictionServiceSettings in project java-aiplatform by googleapis.
the class PredictImageClassificationSample method predictImageClassification.
static void predictImageClassification(String project, String fileName, String endpointId) throws IOException {
PredictionServiceSettings settings = 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(settings)) {
String location = "us-central1";
EndpointName endpointName = EndpointName.of(project, location, endpointId);
byte[] contents = Base64.encodeBase64(Files.readAllBytes(Paths.get(fileName)));
String content = new String(contents, StandardCharsets.UTF_8);
ImageClassificationPredictionInstance predictionInstance = ImageClassificationPredictionInstance.newBuilder().setContent(content).build();
List<Value> instances = new ArrayList<>();
instances.add(ValueConverter.toValue(predictionInstance));
ImageClassificationPredictionParams predictionParams = ImageClassificationPredictionParams.newBuilder().setConfidenceThreshold((float) 0.5).setMaxPredictions(5).build();
PredictResponse predictResponse = predictionServiceClient.predict(endpointName, instances, ValueConverter.toValue(predictionParams));
System.out.println("Predict Image Classification Response");
System.out.format("\tDeployed Model Id: %s\n", predictResponse.getDeployedModelId());
System.out.println("Predictions");
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++;
}
}
}
}
use of com.google.cloud.aiplatform.v1.PredictionServiceSettings in project java-aiplatform by googleapis.
the class PredictTabularClassificationSample method predictTabularClassification.
static void predictTabularClassification(String instance, String project, 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);
ListValue.Builder listValue = ListValue.newBuilder();
JsonFormat.parser().merge(instance, listValue);
List<Value> instanceList = listValue.getValuesList();
Value parameters = Value.newBuilder().setListValue(listValue).build();
PredictResponse predictResponse = predictionServiceClient.predict(endpointName, instanceList, parameters);
System.out.println("Predict Tabular Classification Response");
System.out.format("\tDeployed Model Id: %s\n", predictResponse.getDeployedModelId());
System.out.println("Predictions");
for (Value prediction : predictResponse.getPredictionsList()) {
TabularClassificationPredictionResult.Builder resultBuilder = TabularClassificationPredictionResult.newBuilder();
TabularClassificationPredictionResult result = (TabularClassificationPredictionResult) ValueConverter.fromValue(resultBuilder, prediction);
for (int i = 0; i < result.getClassesCount(); i++) {
System.out.printf("\tClass: %s", result.getClasses(i));
System.out.printf("\tScore: %f", result.getScores(i));
}
}
}
}
use of com.google.cloud.aiplatform.v1.PredictionServiceSettings in project java-aiplatform by googleapis.
the class PredictTextSentimentAnalysisSample method predictTextSentimentAnalysis.
static void predictTextSentimentAnalysis(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);
Value parameter = Value.newBuilder().setNumberValue(0).setNumberValue(5).build();
Value.Builder instance = Value.newBuilder();
JsonFormat.parser().merge(jsonString, instance);
List<Value> instances = new ArrayList<>();
instances.add(instance.build());
PredictResponse predictResponse = predictionServiceClient.predict(endpointName, instances, parameter);
System.out.println("Predict Text Sentiment Analysis 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.PredictionServiceSettings in project java-aiplatform by googleapis.
the class PredictTabularRegressionSample method predictTabularRegression.
static void predictTabularRegression(String instance, String project, 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);
ListValue.Builder listValue = ListValue.newBuilder();
JsonFormat.parser().merge(instance, listValue);
List<Value> instanceList = listValue.getValuesList();
Value parameters = Value.newBuilder().setListValue(listValue).build();
PredictResponse predictResponse = predictionServiceClient.predict(endpointName, instanceList, parameters);
System.out.println("Predict Tabular Regression Response");
System.out.format("\tDisplay Model Id: %s\n", predictResponse.getDeployedModelId());
System.out.println("Predictions");
for (Value prediction : predictResponse.getPredictionsList()) {
TabularRegressionPredictionResult.Builder resultBuilder = TabularRegressionPredictionResult.newBuilder();
TabularRegressionPredictionResult result = (TabularRegressionPredictionResult) ValueConverter.fromValue(resultBuilder, prediction);
System.out.printf("\tUpper bound: %f\n", result.getUpperBound());
System.out.printf("\tLower bound: %f\n", result.getLowerBound());
System.out.printf("\tValue: %f\n", result.getValue());
}
}
}
Aggregations