|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +'use strict'; |
| 16 | + |
| 17 | +/** |
| 18 | + * Sanitize/Screen PDF file content using the Model Armor API. |
| 19 | + * |
| 20 | + * @param {string} projectId - Google Cloud project ID. |
| 21 | + * @param {string} locationId - Google Cloud location. |
| 22 | + * @param {string} templateId - The template ID used for sanitization. |
| 23 | + * @param {string} pdfContentFilename - Path to a PDF file. |
| 24 | + */ |
| 25 | +async function screenPdfFile( |
| 26 | + projectId, |
| 27 | + locationId, |
| 28 | + templateId, |
| 29 | + pdfContentFilename |
| 30 | +) { |
| 31 | + // [START modelarmor_screen_pdf_file] |
| 32 | + /** |
| 33 | + * TODO(developer): Uncomment these variables before running the sample. |
| 34 | + */ |
| 35 | + // const projectId = process.env.PROJECT_ID || 'your-project-id'; |
| 36 | + // const locationId = process.env.LOCATION_ID || 'us-central1'; |
| 37 | + // const templateId = process.env.TEMPLATE_ID || 'template-id'; |
| 38 | + // const pdfContentFilename = 'path/to/file.pdf'; |
| 39 | + |
| 40 | + // Imports the Model Armor library |
| 41 | + const modelarmor = require('@google-cloud/modelarmor'); |
| 42 | + const {ModelArmorClient} = modelarmor.v1; |
| 43 | + const {protos} = modelarmor; |
| 44 | + const ByteItemType = |
| 45 | + protos.google.cloud.modelarmor.v1.ByteDataItem.ByteItemType; |
| 46 | + |
| 47 | + const fs = require('fs'); |
| 48 | + |
| 49 | + const pdfContent = fs.readFileSync(pdfContentFilename); |
| 50 | + const pdfContentBase64 = pdfContent.toString('base64'); |
| 51 | + |
| 52 | + const client = new ModelArmorClient({ |
| 53 | + apiEndpoint: `modelarmor.${locationId}.rep.googleapis.com`, |
| 54 | + }); |
| 55 | + |
| 56 | + const request = { |
| 57 | + name: `projects/${projectId}/locations/${locationId}/templates/${templateId}`, |
| 58 | + userPromptData: { |
| 59 | + byteItem: { |
| 60 | + byteDataType: ByteItemType.PDF, |
| 61 | + byteData: pdfContentBase64, |
| 62 | + }, |
| 63 | + }, |
| 64 | + }; |
| 65 | + |
| 66 | + const [response] = await client.sanitizeUserPrompt(request); |
| 67 | + console.log(JSON.stringify(response, null, 2)); |
| 68 | + return response; |
| 69 | + // [END modelarmor_screen_pdf_file] |
| 70 | +} |
| 71 | + |
| 72 | +module.exports = screenPdfFile; |
0 commit comments