diff --git a/genai/image-generation/imggen-canny-ctrl-type-with-txt-img.js b/genai/image-generation/imggen-canny-ctrl-type-with-txt-img.js new file mode 100644 index 0000000000..e1a94a29b5 --- /dev/null +++ b/genai/image-generation/imggen-canny-ctrl-type-with-txt-img.js @@ -0,0 +1,70 @@ +// Copyright 2025 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 +// +// https://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. + +'use strict'; + +// [START googlegenaisdk_imggen_canny_ctrl_type_with_txt_img] +const {GoogleGenAI, ControlReferenceImage} = require('@google/genai'); + +const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT; +const GOOGLE_CLOUD_LOCATION = + process.env.GOOGLE_CLOUD_LOCATION || 'us-central1'; + +async function generateImage( + outputGcsUri, + projectId = GOOGLE_CLOUD_PROJECT, + location = GOOGLE_CLOUD_LOCATION +) { + const client = new GoogleGenAI({ + vertexai: true, + project: projectId, + location: location, + }); + + const controlReferenceImage = new ControlReferenceImage(); + controlReferenceImage.referenceId = 1; + controlReferenceImage.referenceImage = { + gcsUri: 'gs://cloud-samples-data/generative-ai/image/car_canny.png', + }; + controlReferenceImage.config = { + controlType: 'CONTROL_TYPE_CANNY', + }; + + // TODO(developer): Update and un-comment below line + // outputGcsUri = "gs://your-bucket/your-prefix" + + const response = await client.models.editImage({ + model: 'imagen-3.0-capability-001', + // The '[1]' in the prompt corresponds to the controlReferenceImage.referenceId above. + prompt: 'a watercolor painting of a red car[1] driving on a road', + referenceImages: [controlReferenceImage], + config: { + editMode: 'EDIT_MODE_CONTROLLED_EDITING', + numberOfImages: 1, + safetyFilterLevel: 'BLOCK_MEDIUM_AND_ABOVE', + personGeneration: 'ALLOW_ADULT', + outputGcsUri: outputGcsUri, + }, + }); + + console.log(response.generatedImages[0].image.gcsUri); + // Example response: + // gs://your-bucket/your-prefix + return response.generatedImages[0].image.gcsUri; +} +// [END googlegenaisdk_imggen_canny_ctrl_type_with_txt_img] + +module.exports = { + generateImage, +}; diff --git a/genai/image-generation/imggen-raw-reference-with-txt-img.js b/genai/image-generation/imggen-raw-reference-with-txt-img.js new file mode 100644 index 0000000000..ff22f4cb83 --- /dev/null +++ b/genai/image-generation/imggen-raw-reference-with-txt-img.js @@ -0,0 +1,70 @@ +// Copyright 2025 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 +// +// https://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. + +// [START googlegenaisdk_imggen_raw_reference_with_txt_img] + +'use strict'; + +const {GoogleGenAI, RawReferenceImage} = require('@google/genai'); + +const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT; +const GOOGLE_CLOUD_LOCATION = + process.env.GOOGLE_CLOUD_LOCATION || 'us-central1'; + +async function generateImage( + outputGcsUri, + projectId = GOOGLE_CLOUD_PROJECT, + location = GOOGLE_CLOUD_LOCATION +) { + const client = new GoogleGenAI({ + vertexai: true, + project: projectId, + location: location, + }); + + const referenceImages = new RawReferenceImage(); + referenceImages.referenceId = 1; + referenceImages.referenceImage = { + gcsUri: 'gs://cloud-samples-data/generative-ai/image/teacup-1.png', + }; + + // TODO(developer): Update and un-comment below line + // outputGcsUri = "gs://your-bucket/your-prefix" + + const response = await client.models.editImage({ + model: 'imagen-3.0-capability-001', + prompt: + 'transform the subject in the image so that the teacup[1] is made entirely out of chocolate', + referenceImages: [referenceImages], + config: { + editMode: 'EDIT_MODE_DEFAULT', + numberOfImages: 1, + safetyFilterLevel: 'BLOCK_MEDIUM_AND_ABOVE', + personGeneration: 'ALLOW_ADULT', + outputGcsUri: outputGcsUri, + }, + }); + + console.log(response); + + // Example response: + // gs://your-bucket/your-prefix + + return response.generatedImages[0].image.gcsUri; +} +// [END googlegenaisdk_imggen_raw_reference_with_txt_img] + +module.exports = { + generateImage, +}; diff --git a/genai/image-generation/imggen-scribble-ctrl-type-with-txt-img.js b/genai/image-generation/imggen-scribble-ctrl-type-with-txt-img.js new file mode 100644 index 0000000000..0b42934f9c --- /dev/null +++ b/genai/image-generation/imggen-scribble-ctrl-type-with-txt-img.js @@ -0,0 +1,69 @@ +// Copyright 2025 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 +// +// https://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. + +'use strict'; + +// [START googlegenaisdk_imggen_scribble_ctrl_type_with_txt_img] +const {GoogleGenAI, ControlReferenceImage} = require('@google/genai'); + +const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT; +const GOOGLE_CLOUD_LOCATION = + process.env.GOOGLE_CLOUD_LOCATION || 'us-central1'; + +async function generateImage( + outputGcsUri, + projectId = GOOGLE_CLOUD_PROJECT, + location = GOOGLE_CLOUD_LOCATION +) { + const client = new GoogleGenAI({ + vertexai: true, + project: projectId, + location: location, + }); + + const controlReferenceImage = new ControlReferenceImage(); + controlReferenceImage.referenceId = 1; + controlReferenceImage.referenceImage = { + gcsUri: 'gs://cloud-samples-data/generative-ai/image/car_scribble.png', + }; + controlReferenceImage.config = { + controlType: 'CONTROL_TYPE_SCRIBBLE', + }; + + // TODO(developer): Update and un-comment below line + // outputGcsUri = "gs://your-bucket/your-prefix" + + const response = await client.models.editImage({ + model: 'imagen-3.0-capability-001', + prompt: 'an oil painting showing the side of a red car[1]', + referenceImages: [controlReferenceImage], + config: { + editMode: 'EDIT_MODE_CONTROLLED_EDITING', + numberOfImages: 1, + safetyFilterLevel: 'BLOCK_MEDIUM_AND_ABOVE', + personGeneration: 'ALLOW_ADULT', + outputGcsUri: outputGcsUri, + }, + }); + console.log(response.generatedImages); + + // Example response: + // gs://your-bucket/your-prefix + return response.generatedImages; +} +// [END googlegenaisdk_imggen_scribble_ctrl_type_with_txt_img] + +module.exports = { + generateImage, +}; diff --git a/genai/image-generation/imggen-style-reference-with-txt-img.js b/genai/image-generation/imggen-style-reference-with-txt-img.js new file mode 100644 index 0000000000..163687278e --- /dev/null +++ b/genai/image-generation/imggen-style-reference-with-txt-img.js @@ -0,0 +1,73 @@ +// Copyright 2025 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 +// +// https://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. + +'use strict'; + +// [START googlegenaisdk_imggen_style_reference_with_txt_img] +const {GoogleGenAI, StyleReferenceImage} = require('@google/genai'); + +const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT; +const GOOGLE_CLOUD_LOCATION = + process.env.GOOGLE_CLOUD_LOCATION || 'us-central1'; + +async function generateImage( + outputGcsUri, + projectId = GOOGLE_CLOUD_PROJECT, + location = GOOGLE_CLOUD_LOCATION +) { + const client = new GoogleGenAI({ + vertexai: true, + project: projectId, + location: location, + }); + + // Create a style reference image of a neon sign stored in Google Cloud Storage + // using https://storage.googleapis.com/cloud-samples-data/generative-ai/image/neon.png + + // TODO(developer): Update and un-comment below line + // outputGcsUri = "gs://your-bucket/your-prefix" + + const styleReferenceImage = new StyleReferenceImage(); + styleReferenceImage.referenceId = 1; + styleReferenceImage.referenceImage = { + gcsUri: 'gs://cloud-samples-data/generative-ai/image/neon.png', + }; + styleReferenceImage.config = { + styleDescription: 'neon sign', + }; + + const response = await client.models.editImage({ + model: 'imagen-3.0-capability-001', + prompt: + 'generate an image of a neon sign [1] with the words: have a great day', + referenceImages: [styleReferenceImage], + config: { + editMode: 'EDIT_MODE_CONTROLLED_EDITING', + numberOfImages: 1, + safetyFilterLevel: 'BLOCK_MEDIUM_AND_ABOVE', + personGeneration: 'ALLOW_ADULT', + outputGcsUri: outputGcsUri, + }, + }); + console.log(response.generatedImages); + + // Example response: + // gs://your-bucket/your-prefix + return response.generatedImages; +} +// [END googlegenaisdk_imggen_style_reference_with_txt_img] + +module.exports = { + generateImage, +}; diff --git a/genai/image-generation/imggen-subj-refer-ctrl-refer-with-txt-imgs.js b/genai/image-generation/imggen-subj-refer-ctrl-refer-with-txt-imgs.js new file mode 100644 index 0000000000..e98b9a97b4 --- /dev/null +++ b/genai/image-generation/imggen-subj-refer-ctrl-refer-with-txt-imgs.js @@ -0,0 +1,85 @@ +// Copyright 2025 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 +// +// https://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. + +'use strict'; + +// [START googlegenaisdk_imggen_subj_refer_ctrl_refer_with_txt_imgs] +const { + GoogleGenAI, + ControlReferenceImage, + SubjectReferenceImage, +} = require('@google/genai'); + +const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT; +const GOOGLE_CLOUD_LOCATION = + process.env.GOOGLE_CLOUD_LOCATION || 'us-central1'; + +async function generateImage( + outputGcsUri, + projectId = GOOGLE_CLOUD_PROJECT, + location = GOOGLE_CLOUD_LOCATION +) { + const client = new GoogleGenAI({ + vertexai: true, + project: projectId, + location: location, + }); + + const subjectReferenceImage = new SubjectReferenceImage(); + subjectReferenceImage.referenceId = 1; + subjectReferenceImage.referenceImage = { + gcsUri: 'gs://cloud-samples-data/generative-ai/image/person.png', + }; + subjectReferenceImage.config = { + subjectDescription: 'a headshot of a woman', + subjectType: 'SUBJECT_TYPE_PERSON', + }; + + const controlReferenceImage = new ControlReferenceImage(); + controlReferenceImage.referenceId = 2; + controlReferenceImage.referenceImage = { + gcsUri: 'gs://cloud-samples-data/generative-ai/image/person.png', + }; + controlReferenceImage.config = { + controlType: 'CONTROL_TYPE_FACE_MESH', + }; + + // TODO(developer): Update and un-comment below line + // outputGcsUri = "gs://your-bucket/your-prefix" + + const response = await client.models.editImage({ + model: 'imagen-3.0-capability-001', + prompt: `a portrait of a woman[1] in the pose of the control image[2]in a watercolor style by a professional artist, + light and low-contrast stokes, bright pastel colors, a warm atmosphere, clean background, grainy paper, + bold visible brushstrokes, patchy details`, + referenceImages: [subjectReferenceImage, controlReferenceImage], + config: { + editMode: 'EDIT_MODE_CONTROLLED_EDITING', + numberOfImages: 1, + safetyFilterLevel: 'BLOCK_MEDIUM_AND_ABOVE', + personGeneration: 'ALLOW_ADULT', + outputGcsUri: outputGcsUri, + }, + }); + console.log(response.generatedImages); + + // Example response: + // gs://your-bucket/your-prefix + return response.generatedImages; +} +// [END googlegenaisdk_imggen_subj_refer_ctrl_refer_with_txt_imgs] + +module.exports = { + generateImage, +}; diff --git a/genai/image-generation/imggen-with-txt.js b/genai/image-generation/imggen-with-txt.js new file mode 100644 index 0000000000..003c7563b9 --- /dev/null +++ b/genai/image-generation/imggen-with-txt.js @@ -0,0 +1,63 @@ +// Copyright 2025 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 +// +// https://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. + +'use strict'; + +// [START googlegenaisdk_imggen_with_txt] +const {GoogleGenAI} = require('@google/genai'); +const fs = require('fs'); + +const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT; +const GOOGLE_CLOUD_LOCATION = + process.env.GOOGLE_CLOUD_LOCATION || 'us-central1'; + +async function generateImage( + projectId = GOOGLE_CLOUD_PROJECT, + location = GOOGLE_CLOUD_LOCATION +) { + const client = new GoogleGenAI({ + vertexai: true, + project: projectId, + location: location, + }); + + const image = await client.models.generateImages({ + model: 'imagen-4.0-generate-001', + prompt: 'A dog reading a newspaper', + config: { + imageSize: '2K', + }, + }); + console.log(image.generatedImages[0].image); + console.log('Created output image'); + const outputDir = 'output-folder'; + if (!fs.existsSync(outputDir)) { + fs.mkdirSync(outputDir, {recursive: true}); + } + + const imageBytes = image.generatedImages[0].image.imageBytes; + const buffer = Buffer.from(imageBytes, 'base64'); + const fileName = `${outputDir}/dog-image.png`; + + fs.writeFileSync(fileName, buffer); + + // Example response: + // gs://your-bucket/your-prefix + return image.generatedImages; +} +// [END googlegenaisdk_imggen_with_txt] + +module.exports = { + generateImage, +}; diff --git a/genai/package.json b/genai/package.json index 2c370bd447..f0f5a458df 100644 --- a/genai/package.json +++ b/genai/package.json @@ -13,13 +13,15 @@ "test": "c8 mocha -p -j 2 --timeout 2400000 test/*.test.js test/**/*.test.js" }, "dependencies": { + "@google-cloud/storage": "^7.17.3", "@google/genai": "1.20.0", "axios": "^1.6.2", + "date-fns": "^4.1.0", "google-auth-library": "^10.3.0", "luxon": "^3.7.1", - "proxyquire": "^2.1.3", "node-fetch": "^3.3.2", "openai": "^5.19.1", + "proxyquire": "^2.1.3", "supertest": "^7.0.0" }, "devDependencies": { diff --git a/genai/test/imggen-canny-ctrl-type-with-txt-img.test.js b/genai/test/imggen-canny-ctrl-type-with-txt-img.test.js new file mode 100644 index 0000000000..a72409d607 --- /dev/null +++ b/genai/test/imggen-canny-ctrl-type-with-txt-img.test.js @@ -0,0 +1,41 @@ +// Copyright 2025 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 +// +// https://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. + +'use strict'; + +const {assert} = require('chai'); +const {describe, it} = require('mocha'); + +const projectId = process.env.CAIP_PROJECT_ID; + +const sample = require('../image-generation/imggen-canny-ctrl-type-with-txt-img'); +const location = 'us-central1'; +const {delay} = require('./util'); +const {createOutputGcsUri} = require('./imggen-util'); + +describe('imggen-canny-ctrl-type-with-txt-img', () => { + it('should return an array of generated image URIs', async function () { + this.timeout(180000); + this.retries(4); + const output = await createOutputGcsUri(); + console.log(output.uri); + await delay(this.test); + const generatedFileNames = await sample.generateImage( + output.uri, + projectId, + location + ); + assert(generatedFileNames.length > 0); + }); +}); diff --git a/genai/test/imggen-raw-reference-with-txt-img.test.js b/genai/test/imggen-raw-reference-with-txt-img.test.js new file mode 100644 index 0000000000..39510ae541 --- /dev/null +++ b/genai/test/imggen-raw-reference-with-txt-img.test.js @@ -0,0 +1,50 @@ +// Copyright 2025 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 +// +// https://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. + +'use strict'; + +const {assert} = require('chai'); +const {describe, it} = require('mocha'); + +const projectId = process.env.CAIP_PROJECT_ID; + +const sample = require('../image-generation/imggen-raw-reference-with-txt-img'); +const {delay} = require('./util'); +const {createOutputGcsUri} = require('./imggen-util'); +const location = 'us-central1'; + +describe('imggen-raw-reference-with-txt-img', () => { + it('should return an array of generated image URIs', async function () { + this.timeout(600000); + this.retries(3); + + const output = await createOutputGcsUri(); + console.log('Output GCS URI:', output.uri); + + try { + await delay(this.test); + const generatedFileNames = await sample.generateImage( + output.uri, + projectId, + location + ); + console.log('Generated files:', generatedFileNames); + + assert(generatedFileNames.length > 0); + } catch (err) { + console.error('Image generation failed:', err); + throw err; + } + }); +}); diff --git a/genai/test/imggen-scribble-ctrl-type-with-txt-img.test.js b/genai/test/imggen-scribble-ctrl-type-with-txt-img.test.js new file mode 100644 index 0000000000..6f0d7f55bd --- /dev/null +++ b/genai/test/imggen-scribble-ctrl-type-with-txt-img.test.js @@ -0,0 +1,49 @@ +// Copyright 2025 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 +// +// https://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. + +'use strict'; + +const {assert} = require('chai'); +const {describe, it} = require('mocha'); + +const projectId = process.env.CAIP_PROJECT_ID; +const sample = require('../image-generation/imggen-scribble-ctrl-type-with-txt-img'); +const {delay} = require('./util'); +const {createOutputGcsUri} = require('./imggen-util'); +const location = 'us-central1'; + +describe('imggen-scribble-ctrl-type-with-txt-img', async () => { + it('should generate images from a text prompt with control reference image', async function () { + this.timeout(600000); + this.retries(3); + + const output = await createOutputGcsUri(); + console.log('Output GCS URI:', output.uri); + + try { + await delay(this.test); + const generatedFileNames = await sample.generateImage( + output.uri, + projectId, + location + ); + console.log('Generated files:', generatedFileNames); + + assert(generatedFileNames.length > 0); + } catch (err) { + console.error('Image generation failed:', err); + throw err; + } + }); +}); diff --git a/genai/test/imggen-style-reference-with-txt-img.test.js b/genai/test/imggen-style-reference-with-txt-img.test.js new file mode 100644 index 0000000000..7015d1e519 --- /dev/null +++ b/genai/test/imggen-style-reference-with-txt-img.test.js @@ -0,0 +1,40 @@ +// Copyright 2025 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 +// +// https://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. + +'use strict'; + +const {assert} = require('chai'); +const {describe, it} = require('mocha'); + +const projectId = process.env.CAIP_PROJECT_ID; +const sample = require('../image-generation/imggen-style-reference-with-txt-img'); +const {delay} = require('./util'); +const {createOutputGcsUri} = require('./imggen-util'); +const location = 'us-central1'; + +describe('imggen-style-reference-with-txt-img', async () => { + it('should generate images from a text prompt with style reference', async function () { + this.timeout(180000); + this.retries(4); + const output = await createOutputGcsUri(); + console.log(output.uri); + await delay(this.test); + const generatedFileNames = await sample.generateImage( + output.uri, + projectId, + location + ); + assert(generatedFileNames.length > 0); + }); +}); diff --git a/genai/test/imggen-subj-refer-ctrl-refer-with-txt-imgs.test.js b/genai/test/imggen-subj-refer-ctrl-refer-with-txt-imgs.test.js new file mode 100644 index 0000000000..4b9c0949b1 --- /dev/null +++ b/genai/test/imggen-subj-refer-ctrl-refer-with-txt-imgs.test.js @@ -0,0 +1,40 @@ +// Copyright 2025 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 +// +// https://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. + +'use strict'; + +const {assert} = require('chai'); +const {describe, it} = require('mocha'); + +const projectId = process.env.CAIP_PROJECT_ID; +const sample = require('../image-generation/imggen-subj-refer-ctrl-refer-with-txt-imgs'); +const {delay} = require('./util'); +const {createOutputGcsUri} = require('./imggen-util'); +const location = 'us-central1'; + +describe('imggen-subj-refer-ctrl-refer-with-txt-imgs', async () => { + it('should generate images from a text prompt with subject reference image and control reference image', async function () { + this.timeout(180000); + this.retries(4); + const output = await createOutputGcsUri(); + console.log(output.uri); + await delay(this.test); + const generatedFileNames = await sample.generateImage( + output.uri, + projectId, + location + ); + assert(generatedFileNames.length > 0); + }); +}); diff --git a/genai/test/imggen-util.js b/genai/test/imggen-util.js new file mode 100644 index 0000000000..36e77bb2d8 --- /dev/null +++ b/genai/test/imggen-util.js @@ -0,0 +1,35 @@ +// Copyright 2025 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 +// +// https://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. + +const {Storage} = require('@google-cloud/storage'); +const {format} = require('date-fns'); + +const gcsOutputBucket = 'nodejs-docs-samples-tests'; + +module.exports.createOutputGcsUri = async function () { + const prefix = `text_output/${format(new Date(), 'yyyy-MM-dd-HH-mm-ss')}`; + const gcsUri = `gs://${gcsOutputBucket}/${prefix}`; + + return { + uri: gcsUri, + async cleanup() { + const storage = new Storage(); + const bucket = storage.bucket(gcsOutputBucket); + + const [files] = await bucket.getFiles({prefix}); + await Promise.all(files.map(file => file.delete())); + console.log(`Deleted ${files.length} files from ${prefix}`); + }, + }; +}; diff --git a/genai/test/imggen-with-txt.test.js b/genai/test/imggen-with-txt.test.js new file mode 100644 index 0000000000..2737143851 --- /dev/null +++ b/genai/test/imggen-with-txt.test.js @@ -0,0 +1,32 @@ +// Copyright 2025 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 +// +// https://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. + +'use strict'; + +const {assert} = require('chai'); +const {describe, it} = require('mocha'); + +const projectId = process.env.CAIP_PROJECT_ID; +const sample = require('../image-generation/imggen-with-txt.js'); +const {delay} = require('./util'); + +describe('imggen-with-txt', async () => { + it('should generate images from a text prompt', async function () { + this.timeout(180000); + this.retries(4); + await delay(this.test); + const image = await sample.generateImage(projectId); + assert(image.length > 0); + }); +});