|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package modelarmor; |
| 18 | + |
| 19 | +// [START modelarmor_screen_pdf_file] |
| 20 | + |
| 21 | +import com.google.cloud.modelarmor.v1.ByteDataItem; |
| 22 | +import com.google.cloud.modelarmor.v1.ByteDataItem.ByteItemType; |
| 23 | +import com.google.cloud.modelarmor.v1.DataItem; |
| 24 | +import com.google.cloud.modelarmor.v1.ModelArmorClient; |
| 25 | +import com.google.cloud.modelarmor.v1.ModelArmorSettings; |
| 26 | +import com.google.cloud.modelarmor.v1.SanitizeUserPromptRequest; |
| 27 | +import com.google.cloud.modelarmor.v1.SanitizeUserPromptResponse; |
| 28 | +import com.google.cloud.modelarmor.v1.TemplateName; |
| 29 | +import com.google.protobuf.ByteString; |
| 30 | +import com.google.protobuf.util.JsonFormat; |
| 31 | +import java.io.IOException; |
| 32 | +import java.nio.file.Files; |
| 33 | +import java.nio.file.Paths; |
| 34 | + |
| 35 | +public class ScreenPdfFile { |
| 36 | + |
| 37 | + public static void main(String[] args) throws IOException { |
| 38 | + // TODO(developer): Replace these variables before running the sample. |
| 39 | + |
| 40 | + // Specify the Google Project ID. |
| 41 | + String projectId = "your-project-id"; |
| 42 | + // Specify the location ID. For example, us-central1. |
| 43 | + String locationId = "your-location-id"; |
| 44 | + // Specify the template ID. |
| 45 | + String templateId = "your-template-id"; |
| 46 | + // Specify the PDF file path. Replace with your PDF file path. |
| 47 | + String pdfFilePath = "src/main/resources/test_sample.pdf"; |
| 48 | + |
| 49 | + screenPdfFile(projectId, locationId, templateId, pdfFilePath); |
| 50 | + } |
| 51 | + |
| 52 | + public static SanitizeUserPromptResponse screenPdfFile(String projectId, String locationId, |
| 53 | + String templateId, String pdfFilePath) throws IOException { |
| 54 | + |
| 55 | + // Endpoint to call the Model Armor server. |
| 56 | + String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId); |
| 57 | + ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint) |
| 58 | + .build(); |
| 59 | + |
| 60 | + try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) { |
| 61 | + // Build the resource name of the template. |
| 62 | + String name = TemplateName.of(projectId, locationId, templateId).toString(); |
| 63 | + |
| 64 | + // Read the PDF file content and encode it to Base64. |
| 65 | + byte[] fileContent = Files.readAllBytes(Paths.get(pdfFilePath)); |
| 66 | + |
| 67 | + // Prepare the request. |
| 68 | + DataItem userPromptData = DataItem.newBuilder() |
| 69 | + .setByteItem( |
| 70 | + ByteDataItem.newBuilder() |
| 71 | + .setByteDataType(ByteItemType.PDF) |
| 72 | + .setByteData(ByteString.copyFrom(fileContent)) |
| 73 | + .build()) |
| 74 | + .build(); |
| 75 | + |
| 76 | + SanitizeUserPromptRequest request = |
| 77 | + SanitizeUserPromptRequest.newBuilder() |
| 78 | + .setName(name) |
| 79 | + .setUserPromptData(userPromptData) |
| 80 | + .build(); |
| 81 | + |
| 82 | + // Send the request and get the response. |
| 83 | + SanitizeUserPromptResponse response = client.sanitizeUserPrompt(request); |
| 84 | + |
| 85 | + // Print the sanitization result. |
| 86 | + System.out.println("Result for the provided PDF file: " |
| 87 | + + JsonFormat.printer().print(response.getSanitizationResult())); |
| 88 | + |
| 89 | + return response; |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | +// [END modelarmor_screen_pdf_file] |
0 commit comments