use of com.google.cloud.aiplatform.v1.EndpointName 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());
}
}
}
use of com.google.cloud.aiplatform.v1.EndpointName 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++;
}
}
}
}
use of com.google.cloud.aiplatform.v1.EndpointName 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));
}
}
}
}
use of com.google.cloud.aiplatform.v1.EndpointName in project java-aiplatform by googleapis.
the class UndeployModelSample method undeployModelSample.
static void undeployModelSample(String project, String endpointId, String modelId) throws IOException, InterruptedException, ExecutionException, TimeoutException {
EndpointServiceSettings endpointServiceSettings = EndpointServiceSettings.newBuilder().setEndpoint("us-central1-aiplatform.googleapis.com:443").build();
// the "close" method on the client to safely clean up any remaining background resources.
try (EndpointServiceClient endpointServiceClient = EndpointServiceClient.create(endpointServiceSettings)) {
String location = "us-central1";
EndpointName endpointName = EndpointName.of(project, location, endpointId);
ModelName modelName = ModelName.of(project, location, modelId);
// key '0' assigns traffic for the newly deployed model
// Traffic percentage values must add up to 100
// Leave dictionary empty if endpoint should not accept any traffic
Map<String, Integer> trafficSplit = new HashMap<>();
trafficSplit.put("0", 100);
OperationFuture<UndeployModelResponse, UndeployModelOperationMetadata> operation = endpointServiceClient.undeployModelAsync(endpointName.toString(), modelName.toString(), trafficSplit);
System.out.format("Operation name: %s\n", operation.getInitialFuture().get().getName());
System.out.println("Waiting for operation to finish...");
UndeployModelResponse undeployModelResponse = operation.get(180, TimeUnit.SECONDS);
System.out.format("Undeploy Model Response: %s\n", undeployModelResponse);
}
}
use of com.google.cloud.aiplatform.v1.EndpointName in project java-aiplatform by googleapis.
the class DeployModelCustomTrainedModelSample method deployModelCustomTrainedModelSample.
static void deployModelCustomTrainedModelSample(String project, String endpointId, String model, String deployedModelDisplayName) throws IOException, ExecutionException, InterruptedException {
EndpointServiceSettings settings = EndpointServiceSettings.newBuilder().setEndpoint("us-central1-aiplatform.googleapis.com:443").build();
String location = "us-central1";
// the "close" method on the client to safely clean up any remaining background resources.
try (EndpointServiceClient client = EndpointServiceClient.create(settings)) {
MachineSpec machineSpec = MachineSpec.newBuilder().setMachineType("n1-standard-2").build();
DedicatedResources dedicatedResources = DedicatedResources.newBuilder().setMinReplicaCount(1).setMachineSpec(machineSpec).build();
String modelName = ModelName.of(project, location, model).toString();
DeployedModel deployedModel = DeployedModel.newBuilder().setModel(modelName).setDisplayName(deployedModelDisplayName).setDedicatedResources(dedicatedResources).build();
// key '0' assigns traffic for the newly deployed model
// Traffic percentage values must add up to 100
// Leave dictionary empty if endpoint should not accept any traffic
Map<String, Integer> trafficSplit = new HashMap<>();
trafficSplit.put("0", 100);
EndpointName endpoint = EndpointName.of(project, location, endpointId);
OperationFuture<DeployModelResponse, DeployModelOperationMetadata> response = client.deployModelAsync(endpoint, deployedModel, trafficSplit);
// You can use OperationFuture.getInitialFuture to get a future representing the initial
// response to the request, which contains information while the operation is in progress.
System.out.format("Operation name: %s\n", response.getInitialFuture().get().getName());
// OperationFuture.get() will block until the operation is finished.
DeployModelResponse deployModelResponse = response.get();
System.out.format("deployModelResponse: %s\n", deployModelResponse);
}
}
Aggregations