From a667a2b9fa37f0c29374884f51b4bb9a9bcd7cbd Mon Sep 17 00:00:00 2001 From: Angel Caamal Date: Wed, 28 Oct 2026 16:43:39 +0000 Subject: [PATCH] refactor(java): remove unused aiplatform/image samples --- .../EditImageInpaintingInsertMaskSample.java | 127 ------------------ .../EditImageInpaintingRemoveMaskSample.java | 125 ----------------- .../imagen/EditImageMaskFreeSample.java | 116 ---------------- .../EditImageOutpaintingMaskSample.java | 126 ----------------- .../imagen/GenerateImageSample.java | 105 --------------- .../GetShortFormImageCaptionsSample.java | 103 -------------- .../GetShortFormImageResponsesSample.java | 105 --------------- ...itImageInpaintingInsertMaskSampleTest.java | 68 ---------- ...itImageInpaintingRemoveMaskSampleTest.java | 68 ---------- .../imagen/EditImageMaskFreeSampleTest.java | 66 --------- .../EditImageOutpaintingMaskSampleTest.java | 68 ---------- .../imagen/GenerateImageSampleTest.java | 64 --------- .../GetShortFormImageCaptionsSampleTest.java | 59 -------- .../GetShortFormImageResponsesSampleTest.java | 60 --------- 14 files changed, 1260 deletions(-) delete mode 100644 aiplatform/src/main/java/aiplatform/imagen/EditImageInpaintingInsertMaskSample.java delete mode 100644 aiplatform/src/main/java/aiplatform/imagen/EditImageInpaintingRemoveMaskSample.java delete mode 100644 aiplatform/src/main/java/aiplatform/imagen/EditImageMaskFreeSample.java delete mode 100644 aiplatform/src/main/java/aiplatform/imagen/EditImageOutpaintingMaskSample.java delete mode 100644 aiplatform/src/main/java/aiplatform/imagen/GenerateImageSample.java delete mode 100644 aiplatform/src/main/java/aiplatform/imagen/GetShortFormImageCaptionsSample.java delete mode 100644 aiplatform/src/main/java/aiplatform/imagen/GetShortFormImageResponsesSample.java delete mode 100644 aiplatform/src/test/java/aiplatform/imagen/EditImageInpaintingInsertMaskSampleTest.java delete mode 100644 aiplatform/src/test/java/aiplatform/imagen/EditImageInpaintingRemoveMaskSampleTest.java delete mode 100644 aiplatform/src/test/java/aiplatform/imagen/EditImageMaskFreeSampleTest.java delete mode 100644 aiplatform/src/test/java/aiplatform/imagen/EditImageOutpaintingMaskSampleTest.java delete mode 100644 aiplatform/src/test/java/aiplatform/imagen/GenerateImageSampleTest.java delete mode 100644 aiplatform/src/test/java/aiplatform/imagen/GetShortFormImageCaptionsSampleTest.java delete mode 100644 aiplatform/src/test/java/aiplatform/imagen/GetShortFormImageResponsesSampleTest.java diff --git a/aiplatform/src/main/java/aiplatform/imagen/EditImageInpaintingInsertMaskSample.java b/aiplatform/src/main/java/aiplatform/imagen/EditImageInpaintingInsertMaskSample.java deleted file mode 100644 index a36c984d7f5..00000000000 --- a/aiplatform/src/main/java/aiplatform/imagen/EditImageInpaintingInsertMaskSample.java +++ /dev/null @@ -1,127 +0,0 @@ -/* - * Copyright 2024 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package aiplatform.imagen; - -// [START generativeaionvertexai_imagen_edit_image_inpainting_insert_mask] - -import com.google.api.gax.rpc.ApiException; -import com.google.cloud.aiplatform.v1.EndpointName; -import com.google.cloud.aiplatform.v1.PredictResponse; -import com.google.cloud.aiplatform.v1.PredictionServiceClient; -import com.google.cloud.aiplatform.v1.PredictionServiceSettings; -import com.google.gson.Gson; -import com.google.protobuf.InvalidProtocolBufferException; -import com.google.protobuf.Value; -import com.google.protobuf.util.JsonFormat; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.Base64; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; - -public class EditImageInpaintingInsertMaskSample { - - public static void main(String[] args) throws IOException { - // TODO(developer): Replace these variables before running the sample. - String projectId = "my-project-id"; - String location = "us-central1"; - String inputPath = "/path/to/my-input.png"; - String maskPath = "/path/to/my-mask.png"; - String prompt = - ""; // The text prompt describing what you want to see inserted in the mask area. - - editImageInpaintingInsertMask(projectId, location, inputPath, maskPath, prompt); - } - - // Edit an image using a mask file. Inpainting can insert the object designated by the prompt - // into the masked area. - public static PredictResponse editImageInpaintingInsertMask( - String projectId, String location, String inputPath, String maskPath, String prompt) - throws ApiException, IOException { - final String endpoint = String.format("%s-aiplatform.googleapis.com:443", location); - PredictionServiceSettings predictionServiceSettings = - PredictionServiceSettings.newBuilder().setEndpoint(endpoint).build(); - - // Initialize client that will be used to send requests. This client only needs to be created - // once, and can be reused for multiple requests. - try (PredictionServiceClient predictionServiceClient = - PredictionServiceClient.create(predictionServiceSettings)) { - - final EndpointName endpointName = - EndpointName.ofProjectLocationPublisherModelName( - projectId, location, "google", "imagegeneration@006"); - - // Encode image and mask to Base64 - String imageBase64 = - Base64.getEncoder().encodeToString(Files.readAllBytes(Paths.get(inputPath))); - String maskBase64 = - Base64.getEncoder().encodeToString(Files.readAllBytes(Paths.get(maskPath))); - - // Create the image and image mask maps - Map imageMap = new HashMap<>(); - imageMap.put("bytesBase64Encoded", imageBase64); - - Map maskMap = new HashMap<>(); - maskMap.put("bytesBase64Encoded", maskBase64); - Map imageMaskMap = new HashMap<>(); - imageMaskMap.put("image", maskMap); - - Map instancesMap = new HashMap<>(); - instancesMap.put("prompt", prompt); // [ "prompt", "" ] - instancesMap.put( - "image", imageMap); // [ "image", [ "bytesBase64Encoded", "iVBORw0KGgo...==" ] ] - instancesMap.put( - "mask", - imageMaskMap); // [ "mask", [ "image", [ "bytesBase64Encoded", "iJKDF0KGpl...==" ] ] ] - instancesMap.put("editMode", "inpainting-insert"); // [ "editMode", "inpainting-insert" ] - Value instances = mapToValue(instancesMap); - - // Optional parameters - Map paramsMap = new HashMap<>(); - paramsMap.put("sampleCount", 1); - Value parameters = mapToValue(paramsMap); - - PredictResponse predictResponse = - predictionServiceClient.predict( - endpointName, Collections.singletonList(instances), parameters); - - for (Value prediction : predictResponse.getPredictionsList()) { - Map fieldsMap = prediction.getStructValue().getFieldsMap(); - if (fieldsMap.containsKey("bytesBase64Encoded")) { - String bytesBase64Encoded = fieldsMap.get("bytesBase64Encoded").getStringValue(); - Path tmpPath = Files.createTempFile("imagen-", ".png"); - Files.write(tmpPath, Base64.getDecoder().decode(bytesBase64Encoded)); - System.out.format("Image file written to: %s\n", tmpPath.toUri()); - } - } - return predictResponse; - } - } - - private static Value mapToValue(Map map) throws InvalidProtocolBufferException { - Gson gson = new Gson(); - String json = gson.toJson(map); - Value.Builder builder = Value.newBuilder(); - JsonFormat.parser().merge(json, builder); - return builder.build(); - } -} - -// [END generativeaionvertexai_imagen_edit_image_inpainting_insert_mask] diff --git a/aiplatform/src/main/java/aiplatform/imagen/EditImageInpaintingRemoveMaskSample.java b/aiplatform/src/main/java/aiplatform/imagen/EditImageInpaintingRemoveMaskSample.java deleted file mode 100644 index 146afdd11fa..00000000000 --- a/aiplatform/src/main/java/aiplatform/imagen/EditImageInpaintingRemoveMaskSample.java +++ /dev/null @@ -1,125 +0,0 @@ -/* - * Copyright 2024 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package aiplatform.imagen; - -// [START generativeaionvertexai_imagen_edit_image_inpainting_remove_mask] - -import com.google.api.gax.rpc.ApiException; -import com.google.cloud.aiplatform.v1.EndpointName; -import com.google.cloud.aiplatform.v1.PredictResponse; -import com.google.cloud.aiplatform.v1.PredictionServiceClient; -import com.google.cloud.aiplatform.v1.PredictionServiceSettings; -import com.google.gson.Gson; -import com.google.protobuf.InvalidProtocolBufferException; -import com.google.protobuf.Value; -import com.google.protobuf.util.JsonFormat; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.Base64; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; - -public class EditImageInpaintingRemoveMaskSample { - - public static void main(String[] args) throws IOException { - // TODO(developer): Replace these variables before running the sample. - String projectId = "my-project-id"; - String location = "us-central1"; - String inputPath = "/path/to/my-input.png"; - String maskPath = "/path/to/my-mask.png"; - String prompt = ""; // The text prompt describing the entire image. - - editImageInpaintingRemoveMask(projectId, location, inputPath, maskPath, prompt); - } - - // Edit an image using a mask file. Inpainting can remove an object from the masked area. - public static PredictResponse editImageInpaintingRemoveMask( - String projectId, String location, String inputPath, String maskPath, String prompt) - throws ApiException, IOException { - final String endpoint = String.format("%s-aiplatform.googleapis.com:443", location); - PredictionServiceSettings predictionServiceSettings = - PredictionServiceSettings.newBuilder().setEndpoint(endpoint).build(); - - // Initialize client that will be used to send requests. This client only needs to be created - // once, and can be reused for multiple requests. - try (PredictionServiceClient predictionServiceClient = - PredictionServiceClient.create(predictionServiceSettings)) { - - final EndpointName endpointName = - EndpointName.ofProjectLocationPublisherModelName( - projectId, location, "google", "imagegeneration@006"); - - // Encode image and mask to Base64 - String imageBase64 = - Base64.getEncoder().encodeToString(Files.readAllBytes(Paths.get(inputPath))); - String maskBase64 = - Base64.getEncoder().encodeToString(Files.readAllBytes(Paths.get(maskPath))); - - // Create the image and image mask maps - Map imageMap = new HashMap<>(); - imageMap.put("bytesBase64Encoded", imageBase64); - - Map maskMap = new HashMap<>(); - maskMap.put("bytesBase64Encoded", maskBase64); - Map imageMaskMap = new HashMap<>(); - imageMaskMap.put("image", maskMap); - - Map instancesMap = new HashMap<>(); - instancesMap.put("prompt", prompt); // [ "prompt", "" ] - instancesMap.put( - "image", imageMap); // [ "image", [ "bytesBase64Encoded", "iVBORw0KGgo...==" ] ] - instancesMap.put( - "mask", - imageMaskMap); // [ "mask", [ "image", [ "bytesBase64Encoded", "iJKDF0KGpl...==" ] ] ] - instancesMap.put("editMode", "inpainting-remove"); // [ "editMode", "inpainting-remove" ] - Value instances = mapToValue(instancesMap); - - // Optional parameters - Map paramsMap = new HashMap<>(); - paramsMap.put("sampleCount", 1); - Value parameters = mapToValue(paramsMap); - - PredictResponse predictResponse = - predictionServiceClient.predict( - endpointName, Collections.singletonList(instances), parameters); - - for (Value prediction : predictResponse.getPredictionsList()) { - Map fieldsMap = prediction.getStructValue().getFieldsMap(); - if (fieldsMap.containsKey("bytesBase64Encoded")) { - String bytesBase64Encoded = fieldsMap.get("bytesBase64Encoded").getStringValue(); - Path tmpPath = Files.createTempFile("imagen-", ".png"); - Files.write(tmpPath, Base64.getDecoder().decode(bytesBase64Encoded)); - System.out.format("Image file written to: %s\n", tmpPath.toUri()); - } - } - return predictResponse; - } - } - - private static Value mapToValue(Map map) throws InvalidProtocolBufferException { - Gson gson = new Gson(); - String json = gson.toJson(map); - Value.Builder builder = Value.newBuilder(); - JsonFormat.parser().merge(json, builder); - return builder.build(); - } -} - -// [END generativeaionvertexai_imagen_edit_image_inpainting_remove_mask] diff --git a/aiplatform/src/main/java/aiplatform/imagen/EditImageMaskFreeSample.java b/aiplatform/src/main/java/aiplatform/imagen/EditImageMaskFreeSample.java deleted file mode 100644 index 3084713df22..00000000000 --- a/aiplatform/src/main/java/aiplatform/imagen/EditImageMaskFreeSample.java +++ /dev/null @@ -1,116 +0,0 @@ -/* - * Copyright 2024 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package aiplatform.imagen; - -// [START generativeaionvertexai_imagen_edit_image_mask_free] - -import com.google.api.gax.rpc.ApiException; -import com.google.cloud.aiplatform.v1.EndpointName; -import com.google.cloud.aiplatform.v1.PredictResponse; -import com.google.cloud.aiplatform.v1.PredictionServiceClient; -import com.google.cloud.aiplatform.v1.PredictionServiceSettings; -import com.google.gson.Gson; -import com.google.protobuf.InvalidProtocolBufferException; -import com.google.protobuf.Value; -import com.google.protobuf.util.JsonFormat; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.Base64; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; - -public class EditImageMaskFreeSample { - - public static void main(String[] args) throws IOException { - // TODO(developer): Replace these variables before running the sample. - String projectId = "my-project-id"; - String location = "us-central1"; - String inputPath = "/path/to/my-input.png"; - String prompt = ""; // The text prompt describing what you want to see. - - editImageMaskFree(projectId, location, inputPath, prompt); - } - - // Edit an image without using a mask. The edit is applied to the entire image and is saved to a - // new file. - public static PredictResponse editImageMaskFree( - String projectId, String location, String inputPath, String prompt) - throws ApiException, IOException { - final String endpoint = String.format("%s-aiplatform.googleapis.com:443", location); - PredictionServiceSettings predictionServiceSettings = - PredictionServiceSettings.newBuilder().setEndpoint(endpoint).build(); - - // Initialize client that will be used to send requests. This client only needs to be created - // once, and can be reused for multiple requests. - try (PredictionServiceClient predictionServiceClient = - PredictionServiceClient.create(predictionServiceSettings)) { - - final EndpointName endpointName = - EndpointName.ofProjectLocationPublisherModelName( - projectId, location, "google", "imagegeneration@002"); - - // Convert the image to Base64 and create the image map - String imageBase64 = - Base64.getEncoder().encodeToString(Files.readAllBytes(Paths.get(inputPath))); - Map imageMap = new HashMap<>(); - imageMap.put("bytesBase64Encoded", imageBase64); - - Map instancesMap = new HashMap<>(); - instancesMap.put("prompt", prompt); // [ "prompt", "" ] - instancesMap.put( - "image", imageMap); // [ "image", [ "bytesBase64Encoded", "iVBORw0KGgo...==" ] ] - Value instances = mapToValue(instancesMap); - - Map paramsMap = new HashMap<>(); - // Optional parameters - paramsMap.put("seed", 1); - // Controls the strength of the prompt. - // 0-9 (low strength), 10-20 (medium strength), 21+ (high strength) - paramsMap.put("guidanceScale", 21); - paramsMap.put("sampleCount", 1); - Value parameters = mapToValue(paramsMap); - - PredictResponse predictResponse = - predictionServiceClient.predict( - endpointName, Collections.singletonList(instances), parameters); - - for (Value prediction : predictResponse.getPredictionsList()) { - Map fieldsMap = prediction.getStructValue().getFieldsMap(); - if (fieldsMap.containsKey("bytesBase64Encoded")) { - String bytesBase64Encoded = fieldsMap.get("bytesBase64Encoded").getStringValue(); - Path tmpPath = Files.createTempFile("imagen-", ".png"); - Files.write(tmpPath, Base64.getDecoder().decode(bytesBase64Encoded)); - System.out.format("Image file written to: %s\n", tmpPath.toUri()); - } - } - return predictResponse; - } - } - - private static Value mapToValue(Map map) throws InvalidProtocolBufferException { - Gson gson = new Gson(); - String json = gson.toJson(map); - Value.Builder builder = Value.newBuilder(); - JsonFormat.parser().merge(json, builder); - return builder.build(); - } -} - -// [END generativeaionvertexai_imagen_edit_image_mask_free] diff --git a/aiplatform/src/main/java/aiplatform/imagen/EditImageOutpaintingMaskSample.java b/aiplatform/src/main/java/aiplatform/imagen/EditImageOutpaintingMaskSample.java deleted file mode 100644 index 979c6063ec5..00000000000 --- a/aiplatform/src/main/java/aiplatform/imagen/EditImageOutpaintingMaskSample.java +++ /dev/null @@ -1,126 +0,0 @@ -/* - * Copyright 2024 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package aiplatform.imagen; - -// [START generativeaionvertexai_imagen_edit_image_outpainting_mask] - -import com.google.api.gax.rpc.ApiException; -import com.google.cloud.aiplatform.v1.EndpointName; -import com.google.cloud.aiplatform.v1.PredictResponse; -import com.google.cloud.aiplatform.v1.PredictionServiceClient; -import com.google.cloud.aiplatform.v1.PredictionServiceSettings; -import com.google.gson.Gson; -import com.google.protobuf.InvalidProtocolBufferException; -import com.google.protobuf.Value; -import com.google.protobuf.util.JsonFormat; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.Base64; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; - -public class EditImageOutpaintingMaskSample { - - public static void main(String[] args) throws IOException { - // TODO(developer): Replace these variables before running the sample. - String projectId = "my-project-id"; - String location = "us-central1"; - String inputPath = "/path/to/my-input.png"; - String maskPath = "/path/to/my-mask.png"; - String prompt = ""; // The optional text prompt describing what you want to see inserted. - - editImageOutpaintingMask(projectId, location, inputPath, maskPath, prompt); - } - - // Edit an image using a mask file. Outpainting lets you expand the content of a base image to fit - // a larger or differently sized mask canvas. - public static PredictResponse editImageOutpaintingMask( - String projectId, String location, String inputPath, String maskPath, String prompt) - throws ApiException, IOException { - final String endpoint = String.format("%s-aiplatform.googleapis.com:443", location); - PredictionServiceSettings predictionServiceSettings = - PredictionServiceSettings.newBuilder().setEndpoint(endpoint).build(); - - // Initialize client that will be used to send requests. This client only needs to be created - // once, and can be reused for multiple requests. - try (PredictionServiceClient predictionServiceClient = - PredictionServiceClient.create(predictionServiceSettings)) { - - final EndpointName endpointName = - EndpointName.ofProjectLocationPublisherModelName( - projectId, location, "google", "imagegeneration@006"); - - // Encode image and mask to Base64 - String imageBase64 = - Base64.getEncoder().encodeToString(Files.readAllBytes(Paths.get(inputPath))); - String maskBase64 = - Base64.getEncoder().encodeToString(Files.readAllBytes(Paths.get(maskPath))); - - // Create the image and image mask maps - Map imageMap = new HashMap<>(); - imageMap.put("bytesBase64Encoded", imageBase64); - - Map maskMap = new HashMap<>(); - maskMap.put("bytesBase64Encoded", maskBase64); - Map imageMaskMap = new HashMap<>(); - imageMaskMap.put("image", maskMap); - - Map instancesMap = new HashMap<>(); - instancesMap.put("prompt", prompt); // [ "prompt", "" ] - instancesMap.put( - "image", imageMap); // [ "image", [ "bytesBase64Encoded", "iVBORw0KGgo...==" ] ] - instancesMap.put( - "mask", - imageMaskMap); // [ "mask", [ "image", [ "bytesBase64Encoded", "iJKDF0KGpl...==" ] ] ] - instancesMap.put("editMode", "outpainting"); // [ "editMode", "outpainting" ] - Value instances = mapToValue(instancesMap); - - // Optional parameters - Map paramsMap = new HashMap<>(); - paramsMap.put("sampleCount", 1); - Value parameters = mapToValue(paramsMap); - - PredictResponse predictResponse = - predictionServiceClient.predict( - endpointName, Collections.singletonList(instances), parameters); - - for (Value prediction : predictResponse.getPredictionsList()) { - Map fieldsMap = prediction.getStructValue().getFieldsMap(); - if (fieldsMap.containsKey("bytesBase64Encoded")) { - String bytesBase64Encoded = fieldsMap.get("bytesBase64Encoded").getStringValue(); - Path tmpPath = Files.createTempFile("imagen-", ".png"); - Files.write(tmpPath, Base64.getDecoder().decode(bytesBase64Encoded)); - System.out.format("Image file written to: %s\n", tmpPath.toUri()); - } - } - return predictResponse; - } - } - - private static Value mapToValue(Map map) throws InvalidProtocolBufferException { - Gson gson = new Gson(); - String json = gson.toJson(map); - Value.Builder builder = Value.newBuilder(); - JsonFormat.parser().merge(json, builder); - return builder.build(); - } -} - -// [END generativeaionvertexai_imagen_edit_image_outpainting_mask] diff --git a/aiplatform/src/main/java/aiplatform/imagen/GenerateImageSample.java b/aiplatform/src/main/java/aiplatform/imagen/GenerateImageSample.java deleted file mode 100644 index c3899e60990..00000000000 --- a/aiplatform/src/main/java/aiplatform/imagen/GenerateImageSample.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * Copyright 2024 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package aiplatform.imagen; - -// [START generativeaionvertexai_imagen_generate_image] - -import com.google.api.gax.rpc.ApiException; -import com.google.cloud.aiplatform.v1.EndpointName; -import com.google.cloud.aiplatform.v1.PredictResponse; -import com.google.cloud.aiplatform.v1.PredictionServiceClient; -import com.google.cloud.aiplatform.v1.PredictionServiceSettings; -import com.google.gson.Gson; -import com.google.protobuf.InvalidProtocolBufferException; -import com.google.protobuf.Value; -import com.google.protobuf.util.JsonFormat; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.Base64; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; - -public class GenerateImageSample { - - public static void main(String[] args) throws IOException { - // TODO(developer): Replace these variables before running the sample. - String projectId = "my-project-id"; - String location = "us-central1"; - String prompt = ""; // The text prompt describing what you want to see. - - generateImage(projectId, location, prompt); - } - - // Generate an image using a text prompt using an Imagen model - public static PredictResponse generateImage(String projectId, String location, String prompt) - throws ApiException, IOException { - final String endpoint = String.format("%s-aiplatform.googleapis.com:443", location); - PredictionServiceSettings predictionServiceSettings = - PredictionServiceSettings.newBuilder().setEndpoint(endpoint).build(); - - // Initialize client that will be used to send requests. This client only needs to be created - // once, and can be reused for multiple requests. - try (PredictionServiceClient predictionServiceClient = - PredictionServiceClient.create(predictionServiceSettings)) { - - final EndpointName endpointName = - EndpointName.ofProjectLocationPublisherModelName( - projectId, location, "google", "imagen-3.0-generate-001"); - - Map instancesMap = new HashMap<>(); - instancesMap.put("prompt", prompt); - Value instances = mapToValue(instancesMap); - - Map paramsMap = new HashMap<>(); - paramsMap.put("sampleCount", 1); - // You can't use a seed value and watermark at the same time. - // paramsMap.put("seed", 100); - // paramsMap.put("addWatermark", false); - paramsMap.put("aspectRatio", "1:1"); - paramsMap.put("safetyFilterLevel", "block_some"); - paramsMap.put("personGeneration", "allow_adult"); - Value parameters = mapToValue(paramsMap); - - PredictResponse predictResponse = - predictionServiceClient.predict( - endpointName, Collections.singletonList(instances), parameters); - - for (Value prediction : predictResponse.getPredictionsList()) { - Map fieldsMap = prediction.getStructValue().getFieldsMap(); - if (fieldsMap.containsKey("bytesBase64Encoded")) { - String bytesBase64Encoded = fieldsMap.get("bytesBase64Encoded").getStringValue(); - Path tmpPath = Files.createTempFile("imagen-", ".png"); - Files.write(tmpPath, Base64.getDecoder().decode(bytesBase64Encoded)); - System.out.format("Image file written to: %s\n", tmpPath.toUri()); - } - } - return predictResponse; - } - } - - private static Value mapToValue(Map map) throws InvalidProtocolBufferException { - Gson gson = new Gson(); - String json = gson.toJson(map); - Value.Builder builder = Value.newBuilder(); - JsonFormat.parser().merge(json, builder); - return builder.build(); - } -} - -// [END generativeaionvertexai_imagen_generate_image] diff --git a/aiplatform/src/main/java/aiplatform/imagen/GetShortFormImageCaptionsSample.java b/aiplatform/src/main/java/aiplatform/imagen/GetShortFormImageCaptionsSample.java deleted file mode 100644 index b52e40bfbf3..00000000000 --- a/aiplatform/src/main/java/aiplatform/imagen/GetShortFormImageCaptionsSample.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * Copyright 2024 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package aiplatform.imagen; - -// [START generativeaionvertexai_imagen_get_short_form_image_captions] - -import com.google.api.gax.rpc.ApiException; -import com.google.cloud.aiplatform.v1.EndpointName; -import com.google.cloud.aiplatform.v1.PredictResponse; -import com.google.cloud.aiplatform.v1.PredictionServiceClient; -import com.google.cloud.aiplatform.v1.PredictionServiceSettings; -import com.google.gson.Gson; -import com.google.protobuf.InvalidProtocolBufferException; -import com.google.protobuf.Value; -import com.google.protobuf.util.JsonFormat; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.Base64; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; - -public class GetShortFormImageCaptionsSample { - - public static void main(String[] args) throws IOException { - // TODO(developer): Replace these variables before running the sample. - String projectId = "my-project-id"; - String location = "us-central1"; - String inputPath = "/path/to/my-input.png"; - - getShortFormImageCaptions(projectId, location, inputPath); - } - - // Get the short form captions for an image - public static PredictResponse getShortFormImageCaptions( - String projectId, String location, String inputPath) throws ApiException, IOException { - final String endpoint = String.format("%s-aiplatform.googleapis.com:443", location); - PredictionServiceSettings predictionServiceSettings = - PredictionServiceSettings.newBuilder().setEndpoint(endpoint).build(); - - // Initialize client that will be used to send requests. This client only needs to be created - // once, and can be reused for multiple requests. - try (PredictionServiceClient predictionServiceClient = - PredictionServiceClient.create(predictionServiceSettings)) { - - final EndpointName endpointName = - EndpointName.ofProjectLocationPublisherModelName( - projectId, location, "google", "imagetext@001"); - - // Encode image to Base64 - String imageBase64 = - Base64.getEncoder().encodeToString(Files.readAllBytes(Paths.get(inputPath))); - - // Create the image map - Map imageMap = new HashMap<>(); - imageMap.put("bytesBase64Encoded", imageBase64); - - Map instancesMap = new HashMap<>(); - instancesMap.put("image", imageMap); - Value instances = mapToValue(instancesMap); - - // Optional parameters - Map paramsMap = new HashMap<>(); - paramsMap.put("language", "en"); - paramsMap.put("sampleCount", 2); - Value parameters = mapToValue(paramsMap); - - PredictResponse predictResponse = - predictionServiceClient.predict( - endpointName, Collections.singletonList(instances), parameters); - - for (Value prediction : predictResponse.getPredictionsList()) { - System.out.println(prediction.getStringValue()); - } - return predictResponse; - } - } - - private static Value mapToValue(Map map) throws InvalidProtocolBufferException { - Gson gson = new Gson(); - String json = gson.toJson(map); - Value.Builder builder = Value.newBuilder(); - JsonFormat.parser().merge(json, builder); - return builder.build(); - } -} - -// [END generativeaionvertexai_imagen_get_short_form_image_captions] diff --git a/aiplatform/src/main/java/aiplatform/imagen/GetShortFormImageResponsesSample.java b/aiplatform/src/main/java/aiplatform/imagen/GetShortFormImageResponsesSample.java deleted file mode 100644 index 19f29ab313f..00000000000 --- a/aiplatform/src/main/java/aiplatform/imagen/GetShortFormImageResponsesSample.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * Copyright 2024 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package aiplatform.imagen; - -// [START generativeaionvertexai_imagen_get_short_form_image_responses] - -import com.google.api.gax.rpc.ApiException; -import com.google.cloud.aiplatform.v1.EndpointName; -import com.google.cloud.aiplatform.v1.PredictResponse; -import com.google.cloud.aiplatform.v1.PredictionServiceClient; -import com.google.cloud.aiplatform.v1.PredictionServiceSettings; -import com.google.gson.Gson; -import com.google.protobuf.InvalidProtocolBufferException; -import com.google.protobuf.Value; -import com.google.protobuf.util.JsonFormat; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.Base64; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; - -public class GetShortFormImageResponsesSample { - - public static void main(String[] args) throws IOException { - // TODO(developer): Replace these variables before running the sample. - String projectId = "my-project-id"; - String location = "us-central1"; - String inputPath = "/path/to/my-input.png"; - String prompt = ""; // The question about the contents of the image. - - getShortFormImageResponses(projectId, location, inputPath, prompt); - } - - // Get the short form responses to a question about an image - public static PredictResponse getShortFormImageResponses( - String projectId, String location, String inputPath, String prompt) - throws ApiException, IOException { - final String endpoint = String.format("%s-aiplatform.googleapis.com:443", location); - PredictionServiceSettings predictionServiceSettings = - PredictionServiceSettings.newBuilder().setEndpoint(endpoint).build(); - - // Initialize client that will be used to send requests. This client only needs to be created - // once, and can be reused for multiple requests. - try (PredictionServiceClient predictionServiceClient = - PredictionServiceClient.create(predictionServiceSettings)) { - - final EndpointName endpointName = - EndpointName.ofProjectLocationPublisherModelName( - projectId, location, "google", "imagetext@001"); - - // Encode image to Base64 - String imageBase64 = - Base64.getEncoder().encodeToString(Files.readAllBytes(Paths.get(inputPath))); - - // Create the image map - Map imageMap = new HashMap<>(); - imageMap.put("bytesBase64Encoded", imageBase64); - - Map instancesMap = new HashMap<>(); - instancesMap.put("prompt", prompt); - instancesMap.put("image", imageMap); - Value instances = mapToValue(instancesMap); - - // Optional parameters - Map paramsMap = new HashMap<>(); - paramsMap.put("sampleCount", 2); - Value parameters = mapToValue(paramsMap); - - PredictResponse predictResponse = - predictionServiceClient.predict( - endpointName, Collections.singletonList(instances), parameters); - - for (Value prediction : predictResponse.getPredictionsList()) { - System.out.println(prediction.getStringValue()); - } - return predictResponse; - } - } - - private static Value mapToValue(Map map) throws InvalidProtocolBufferException { - Gson gson = new Gson(); - String json = gson.toJson(map); - Value.Builder builder = Value.newBuilder(); - JsonFormat.parser().merge(json, builder); - return builder.build(); - } -} - -// [END generativeaionvertexai_imagen_get_short_form_image_responses] diff --git a/aiplatform/src/test/java/aiplatform/imagen/EditImageInpaintingInsertMaskSampleTest.java b/aiplatform/src/test/java/aiplatform/imagen/EditImageInpaintingInsertMaskSampleTest.java deleted file mode 100644 index e808007e1df..00000000000 --- a/aiplatform/src/test/java/aiplatform/imagen/EditImageInpaintingInsertMaskSampleTest.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright 2024 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package aiplatform.imagen; - -import static com.google.common.truth.Truth.assertThat; -import static junit.framework.TestCase.assertNotNull; - -import com.google.cloud.aiplatform.v1.PredictResponse; -import com.google.protobuf.Value; -import java.io.IOException; -import java.util.Map; -import org.junit.BeforeClass; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -@RunWith(JUnit4.class) -public class EditImageInpaintingInsertMaskSampleTest { - - private static final String PROJECT = System.getenv("GOOGLE_CLOUD_PROJECT"); - private static final String INPUT_FILE = "resources/woman.png"; - private static final String MASK_FILE = "resources/woman_inpainting_insert_mask.png"; - private static final String PROMPT = "hat"; - - private static void requireEnvVar(String varName) { - String errorMessage = - String.format("Environment variable '%s' is required to perform these tests.", varName); - assertNotNull(errorMessage, System.getenv(varName)); - } - - @BeforeClass - public static void checkRequirements() { - requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); - requireEnvVar("GOOGLE_CLOUD_PROJECT"); - } - - @Test - public void testEditImageInpaintingInsertMaskSample() throws IOException { - PredictResponse response = - EditImageInpaintingInsertMaskSample.editImageInpaintingInsertMask( - PROJECT, "us-central1", INPUT_FILE, MASK_FILE, PROMPT); - assertThat(response).isNotNull(); - - Boolean imageBytes = false; - for (Value prediction : response.getPredictionsList()) { - Map fieldsMap = prediction.getStructValue().getFieldsMap(); - if (fieldsMap.containsKey("bytesBase64Encoded")) { - imageBytes = true; - break; - } - } - assertThat(imageBytes).isTrue(); - } -} diff --git a/aiplatform/src/test/java/aiplatform/imagen/EditImageInpaintingRemoveMaskSampleTest.java b/aiplatform/src/test/java/aiplatform/imagen/EditImageInpaintingRemoveMaskSampleTest.java deleted file mode 100644 index cb528dc2d67..00000000000 --- a/aiplatform/src/test/java/aiplatform/imagen/EditImageInpaintingRemoveMaskSampleTest.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright 2024 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package aiplatform.imagen; - -import static com.google.common.truth.Truth.assertThat; -import static junit.framework.TestCase.assertNotNull; - -import com.google.cloud.aiplatform.v1.PredictResponse; -import com.google.protobuf.Value; -import java.io.IOException; -import java.util.Map; -import org.junit.BeforeClass; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -@RunWith(JUnit4.class) -public class EditImageInpaintingRemoveMaskSampleTest { - - private static final String PROJECT = System.getenv("GOOGLE_CLOUD_PROJECT"); - private static final String INPUT_FILE = "resources/volleyball_game.png"; - private static final String MASK_FILE = "resources/volleyball_game_inpainting_remove_mask.png"; - private static final String PROMPT = "volleyball game"; - - private static void requireEnvVar(String varName) { - String errorMessage = - String.format("Environment variable '%s' is required to perform these tests.", varName); - assertNotNull(errorMessage, System.getenv(varName)); - } - - @BeforeClass - public static void checkRequirements() { - requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); - requireEnvVar("GOOGLE_CLOUD_PROJECT"); - } - - @Test - public void testEditImageInpaintingRemoveMaskSample() throws IOException { - PredictResponse response = - EditImageInpaintingRemoveMaskSample.editImageInpaintingRemoveMask( - PROJECT, "us-central1", INPUT_FILE, MASK_FILE, PROMPT); - assertThat(response).isNotNull(); - - Boolean imageBytes = false; - for (Value prediction : response.getPredictionsList()) { - Map fieldsMap = prediction.getStructValue().getFieldsMap(); - if (fieldsMap.containsKey("bytesBase64Encoded")) { - imageBytes = true; - break; - } - } - assertThat(imageBytes).isTrue(); - } -} diff --git a/aiplatform/src/test/java/aiplatform/imagen/EditImageMaskFreeSampleTest.java b/aiplatform/src/test/java/aiplatform/imagen/EditImageMaskFreeSampleTest.java deleted file mode 100644 index 42c3bc53b5a..00000000000 --- a/aiplatform/src/test/java/aiplatform/imagen/EditImageMaskFreeSampleTest.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright 2024 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package aiplatform.imagen; - -import static com.google.common.truth.Truth.assertThat; -import static junit.framework.TestCase.assertNotNull; - -import com.google.cloud.aiplatform.v1.PredictResponse; -import com.google.protobuf.Value; -import java.io.IOException; -import java.util.Map; -import org.junit.BeforeClass; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -@RunWith(JUnit4.class) -public class EditImageMaskFreeSampleTest { - - private static final String PROJECT = System.getenv("GOOGLE_CLOUD_PROJECT"); - private static final String INPUT_FILE = "resources/cat.png"; - private static final String PROMPT = "a dog"; - - private static void requireEnvVar(String varName) { - String errorMessage = - String.format("Environment variable '%s' is required to perform these tests.", varName); - assertNotNull(errorMessage, System.getenv(varName)); - } - - @BeforeClass - public static void checkRequirements() { - requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); - requireEnvVar("GOOGLE_CLOUD_PROJECT"); - } - - @Test - public void testEditImageMaskFreeSample() throws IOException { - PredictResponse response = - EditImageMaskFreeSample.editImageMaskFree(PROJECT, "us-central1", INPUT_FILE, PROMPT); - assertThat(response).isNotNull(); - - Boolean imageBytes = false; - for (Value prediction : response.getPredictionsList()) { - Map fieldsMap = prediction.getStructValue().getFieldsMap(); - if (fieldsMap.containsKey("bytesBase64Encoded")) { - imageBytes = true; - break; - } - } - assertThat(imageBytes).isTrue(); - } -} \ No newline at end of file diff --git a/aiplatform/src/test/java/aiplatform/imagen/EditImageOutpaintingMaskSampleTest.java b/aiplatform/src/test/java/aiplatform/imagen/EditImageOutpaintingMaskSampleTest.java deleted file mode 100644 index e080d96f073..00000000000 --- a/aiplatform/src/test/java/aiplatform/imagen/EditImageOutpaintingMaskSampleTest.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright 2024 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package aiplatform.imagen; - -import static com.google.common.truth.Truth.assertThat; -import static junit.framework.TestCase.assertNotNull; - -import com.google.cloud.aiplatform.v1.PredictResponse; -import com.google.protobuf.Value; -import java.io.IOException; -import java.util.Map; -import org.junit.BeforeClass; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -@RunWith(JUnit4.class) -public class EditImageOutpaintingMaskSampleTest { - - private static final String PROJECT = System.getenv("GOOGLE_CLOUD_PROJECT"); - private static final String INPUT_FILE = "resources/roller_skaters.png"; - private static final String MASK_FILE = "resources/roller_skaters_mask.png"; - private static final String PROMPT = "city with skyscrapers"; - - private static void requireEnvVar(String varName) { - String errorMessage = - String.format("Environment variable '%s' is required to perform these tests.", varName); - assertNotNull(errorMessage, System.getenv(varName)); - } - - @BeforeClass - public static void checkRequirements() { - requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); - requireEnvVar("GOOGLE_CLOUD_PROJECT"); - } - - @Test - public void testEditImageOutpaintingMaskSample() throws IOException { - PredictResponse response = - EditImageOutpaintingMaskSample.editImageOutpaintingMask( - PROJECT, "us-central1", INPUT_FILE, MASK_FILE, PROMPT); - assertThat(response).isNotNull(); - - Boolean imageBytes = false; - for (Value prediction : response.getPredictionsList()) { - Map fieldsMap = prediction.getStructValue().getFieldsMap(); - if (fieldsMap.containsKey("bytesBase64Encoded")) { - imageBytes = true; - break; - } - } - assertThat(imageBytes).isTrue(); - } -} diff --git a/aiplatform/src/test/java/aiplatform/imagen/GenerateImageSampleTest.java b/aiplatform/src/test/java/aiplatform/imagen/GenerateImageSampleTest.java deleted file mode 100644 index f52204c9774..00000000000 --- a/aiplatform/src/test/java/aiplatform/imagen/GenerateImageSampleTest.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright 2024 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package aiplatform.imagen; - -import static com.google.common.truth.Truth.assertThat; -import static junit.framework.TestCase.assertNotNull; - -import com.google.cloud.aiplatform.v1.PredictResponse; -import com.google.protobuf.Value; -import java.io.IOException; -import java.util.Map; -import org.junit.BeforeClass; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -@RunWith(JUnit4.class) -public class GenerateImageSampleTest { - - private static final String PROJECT = System.getenv("GOOGLE_CLOUD_PROJECT"); - private static final String PROMPT = "a dog reading a newspaper"; - - private static void requireEnvVar(String varName) { - String errorMessage = - String.format("Environment variable '%s' is required to perform these tests.", varName); - assertNotNull(errorMessage, System.getenv(varName)); - } - - @BeforeClass - public static void checkRequirements() { - requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); - requireEnvVar("GOOGLE_CLOUD_PROJECT"); - } - - @Test - public void testGenerateImageSample() throws IOException { - PredictResponse response = GenerateImageSample.generateImage(PROJECT, "us-central1", PROMPT); - assertThat(response).isNotNull(); - - Boolean imageBytes = false; - for (Value prediction : response.getPredictionsList()) { - Map fieldsMap = prediction.getStructValue().getFieldsMap(); - if (fieldsMap.containsKey("bytesBase64Encoded")) { - imageBytes = true; - break; - } - } - assertThat(imageBytes).isTrue(); - } -} diff --git a/aiplatform/src/test/java/aiplatform/imagen/GetShortFormImageCaptionsSampleTest.java b/aiplatform/src/test/java/aiplatform/imagen/GetShortFormImageCaptionsSampleTest.java deleted file mode 100644 index 889d3abc735..00000000000 --- a/aiplatform/src/test/java/aiplatform/imagen/GetShortFormImageCaptionsSampleTest.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright 2024 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package aiplatform.imagen; - -import static com.google.common.truth.Truth.assertThat; -import static junit.framework.TestCase.assertNotNull; - -import com.google.cloud.aiplatform.v1.PredictResponse; -import com.google.protobuf.Value; -import java.io.IOException; -import org.junit.BeforeClass; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -@RunWith(JUnit4.class) -public class GetShortFormImageCaptionsSampleTest { - - private static final String PROJECT = System.getenv("GOOGLE_CLOUD_PROJECT"); - private static final String INPUT_FILE = "resources/cat.png"; - - private static void requireEnvVar(String varName) { - String errorMessage = - String.format("Environment variable '%s' is required to perform these tests.", varName); - assertNotNull(errorMessage, System.getenv(varName)); - } - - @BeforeClass - public static void checkRequirements() { - requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); - requireEnvVar("GOOGLE_CLOUD_PROJECT"); - } - - @Test - public void testGetShortFormImageCaptionsSample() throws IOException { - PredictResponse response = - GetShortFormImageCaptionsSample.getShortFormImageCaptions( - PROJECT, "us-central1", INPUT_FILE); - assertThat(response).isNotNull(); - - for (Value prediction : response.getPredictionsList()) { - assertThat(prediction.getStringValue().contains("cat")); - } - } -} diff --git a/aiplatform/src/test/java/aiplatform/imagen/GetShortFormImageResponsesSampleTest.java b/aiplatform/src/test/java/aiplatform/imagen/GetShortFormImageResponsesSampleTest.java deleted file mode 100644 index 2a675df8fc3..00000000000 --- a/aiplatform/src/test/java/aiplatform/imagen/GetShortFormImageResponsesSampleTest.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright 2024 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package aiplatform.imagen; - -import static com.google.common.truth.Truth.assertThat; -import static junit.framework.TestCase.assertNotNull; - -import com.google.cloud.aiplatform.v1.PredictResponse; -import com.google.protobuf.Value; -import java.io.IOException; -import org.junit.BeforeClass; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -@RunWith(JUnit4.class) -public class GetShortFormImageResponsesSampleTest { - - private static final String PROJECT = System.getenv("GOOGLE_CLOUD_PROJECT"); - private static final String INPUT_FILE = "resources/cat.png"; - private static final String PROMPT = "What breed of cat is this a picture of?"; - - private static void requireEnvVar(String varName) { - String errorMessage = - String.format("Environment variable '%s' is required to perform these tests.", varName); - assertNotNull(errorMessage, System.getenv(varName)); - } - - @BeforeClass - public static void checkRequirements() { - requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); - requireEnvVar("GOOGLE_CLOUD_PROJECT"); - } - - @Test - public void testGetShortFormImageResponsesSample() throws IOException { - PredictResponse response = - GetShortFormImageResponsesSample.getShortFormImageResponses( - PROJECT, "us-central1", INPUT_FILE, PROMPT); - assertThat(response).isNotNull(); - - for (Value prediction : response.getPredictionsList()) { - assertThat(prediction.getStringValue().contains("tabby")); - } - } -}