Skip to content

Commit f25b80c

Browse files
GuinersGuinersgemini-code-assist[bot]iennaegericdong
authored
feat(genai): nano-banana-samples (#4184)
* adding samples * adding samples * adding samples * adding new samples * adding new samples * adding new samples * adding new samples * adding new samples * adding new samples * Update genai/image-generation/imggen-mmflash-locale-aware-with-txt.js Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * Update genai/image-generation/imggen-mmflash-multiple-imgs-with-txt.js Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * fixing samples * fixing samples * fixing samples * fix(genai): fix misspelled word based on suggestion from @gemini-code-assist[bot] Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * sample update * model update * test fix * test fix * test fix * test fix * adding delay and retries for textgen tests * textgen-with-pdf test fix --------- Co-authored-by: Guiners <[email protected]> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Co-authored-by: Jennifer Davis <[email protected]> Co-authored-by: Eric Dong <[email protected]>
1 parent 5fe1ec4 commit f25b80c

35 files changed

+625
-12
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
// [START googlegenaisdk_imggen_mmflash_edit_img_with_txt_img]
18+
const fs = require('fs');
19+
const {GoogleGenAI, Modality} = require('@google/genai');
20+
21+
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
22+
const GOOGLE_CLOUD_LOCATION =
23+
process.env.GOOGLE_CLOUD_LOCATION || 'us-central1';
24+
25+
const FILE_NAME = 'test-data/example-image-eiffel-tower.png';
26+
27+
async function generateImage(
28+
projectId = GOOGLE_CLOUD_PROJECT,
29+
location = GOOGLE_CLOUD_LOCATION
30+
) {
31+
const client = new GoogleGenAI({
32+
vertexai: true,
33+
project: projectId,
34+
location: location,
35+
});
36+
37+
const image = fs.readFileSync(FILE_NAME);
38+
39+
const response = await client.models.generateContent({
40+
model: 'gemini-2.5-flash-image',
41+
contents: [image, 'Edit this image to make it look like a cartoon'],
42+
config: {
43+
responseModalities: [Modality.TEXT, Modality.IMAGE],
44+
},
45+
});
46+
47+
for (const part of response.candidates[0].content.parts) {
48+
if (part.text) {
49+
console.log(`${part.text}`);
50+
} else if (part.inlineData) {
51+
const outputDir = 'output-folder';
52+
if (!fs.existsSync(outputDir)) {
53+
fs.mkdirSync(outputDir, {recursive: true});
54+
}
55+
const imageBytes = Buffer.from(part.inlineData.data, 'base64');
56+
const filename = `${outputDir}/bw-example-image.png`;
57+
fs.writeFileSync(filename, imageBytes);
58+
}
59+
}
60+
61+
// Example response:
62+
// Okay, I will edit this image to give it a cartoonish style, with bolder outlines, simplified details, and more vibrant colors.
63+
return response;
64+
}
65+
66+
// [END googlegenaisdk_imggen_mmflash_edit_img_with_txt_img]
67+
68+
module.exports = {
69+
generateImage,
70+
};
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
// [START googlegenaisdk_imggen_mmflash_locale_aware_with_txt]
18+
const fs = require('fs');
19+
const {GoogleGenAI, Modality} = require('@google/genai');
20+
21+
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
22+
const GOOGLE_CLOUD_LOCATION =
23+
process.env.GOOGLE_CLOUD_LOCATION || 'us-central1';
24+
25+
async function generateImage(
26+
projectId = GOOGLE_CLOUD_PROJECT,
27+
location = GOOGLE_CLOUD_LOCATION
28+
) {
29+
const client = new GoogleGenAI({
30+
vertexai: true,
31+
project: projectId,
32+
location: location,
33+
});
34+
35+
const response = await client.models.generateContent({
36+
model: 'gemini-2.5-flash-image',
37+
contents: 'Generate a photo of a breakfast meal.',
38+
config: {
39+
responseModalities: [Modality.TEXT, Modality.IMAGE],
40+
},
41+
});
42+
43+
console.log(response);
44+
45+
for (const part of response.candidates[0].content.parts) {
46+
if (part.text) {
47+
console.log(`${part.text}`);
48+
} else if (part.inlineData) {
49+
const outputDir = 'output-folder';
50+
if (!fs.existsSync(outputDir)) {
51+
fs.mkdirSync(outputDir, {recursive: true});
52+
}
53+
const imageBytes = Buffer.from(part.inlineData.data, 'base64');
54+
const filename = `${outputDir}/example-breakfast-meal.png`;
55+
fs.writeFileSync(filename, imageBytes);
56+
}
57+
}
58+
59+
// Example response:
60+
// Generates a photo of a vibrant and appetizing breakfast meal.
61+
// The scene will feature a white plate with golden-brown pancakes
62+
// stacked neatly, drizzled with rich maple syrup and ...
63+
64+
return response;
65+
}
66+
67+
// [END googlegenaisdk_imggen_mmflash_locale_aware_with_txt]
68+
69+
module.exports = {
70+
generateImage,
71+
};
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
// [START googlegenaisdk_imggen_mmflash_multiple_imgs_with_txt]
18+
const fs = require('fs');
19+
const {GoogleGenAI, Modality} = require('@google/genai');
20+
21+
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
22+
const GOOGLE_CLOUD_LOCATION =
23+
process.env.GOOGLE_CLOUD_LOCATION || 'us-central1';
24+
25+
async function generateImage(
26+
projectId = GOOGLE_CLOUD_PROJECT,
27+
location = GOOGLE_CLOUD_LOCATION
28+
) {
29+
const client = new GoogleGenAI({
30+
vertexai: true,
31+
project: projectId,
32+
location: location,
33+
});
34+
35+
const response = await client.models.generateContent({
36+
model: 'gemini-2.5-flash-image',
37+
contents: 'Generate 3 images of a cat sitting on a chair.',
38+
config: {
39+
responseModalities: [Modality.TEXT, Modality.IMAGE],
40+
},
41+
});
42+
43+
console.log(response);
44+
45+
const generatedFileNames = [];
46+
let imageCounter = 1;
47+
48+
for (const part of response.candidates[0].content.parts) {
49+
if (part.text) {
50+
console.log(part.text);
51+
} else if (part.inlineData) {
52+
const outputDir = 'output-folder';
53+
if (!fs.existsSync(outputDir)) {
54+
fs.mkdirSync(outputDir, {recursive: true});
55+
}
56+
const imageBytes = Buffer.from(part.inlineData.data, 'base64');
57+
const filename = `${outputDir}/example-cats-0${imageCounter}.png`;
58+
fs.writeFileSync(filename, imageBytes);
59+
generatedFileNames.push(filename);
60+
console.log(`Saved image: ${filename}`);
61+
62+
imageCounter++;
63+
}
64+
}
65+
66+
return generatedFileNames;
67+
}
68+
// Example response:
69+
// Image 1: A fluffy calico cat with striking green eyes is perched elegantly on a vintage wooden
70+
// chair with a woven seat. Sunlight streams through a nearby window, casting soft shadows and
71+
// highlighting the cat's fur.
72+
//
73+
// Image 2: A sleek black cat with intense yellow eyes is sitting upright on a modern, minimalist
74+
// white chair. The background is a plain grey wall, putting the focus entirely on the feline's
75+
// graceful posture.
76+
//
77+
// Image 3: A ginger tabby cat with playful amber eyes is comfortably curled up asleep on a plush,
78+
// oversized armchair upholstered in a soft, floral fabric. A corner of a cozy living room with a
79+
// [END googlegenaisdk_imggen_mmflash_multiple_imgs_with_txt]
80+
81+
module.exports = {
82+
generateImage,
83+
};
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
// [START googlegenaisdk_imggen_mmflash_txt_and_img_with_txt]
18+
const fs = require('fs');
19+
const {GoogleGenAI, Modality} = require('@google/genai');
20+
21+
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
22+
const GOOGLE_CLOUD_LOCATION =
23+
process.env.GOOGLE_CLOUD_LOCATION || 'us-central1';
24+
25+
async function savePaellaRecipe(response) {
26+
const parts = response.candidates[0].content.parts;
27+
28+
let mdText = '';
29+
const outputDir = 'output-folder';
30+
31+
for (let i = 0; i < parts.length; i++) {
32+
const part = parts[i];
33+
34+
if (part.text) {
35+
mdText += part.text + '\n';
36+
} else if (part.inlineData) {
37+
if (!fs.existsSync(outputDir)) {
38+
fs.mkdirSync(outputDir, {recursive: true});
39+
}
40+
const imageBytes = Buffer.from(part.inlineData.data, 'base64');
41+
const imagePath = `example-image-${i + 1}.png`;
42+
const saveImagePath = `${outputDir}/${imagePath}`;
43+
44+
fs.writeFileSync(saveImagePath, imageBytes);
45+
mdText += `![image](./${imagePath})\n`;
46+
}
47+
}
48+
const mdFile = `${outputDir}/paella-recipe.md`;
49+
50+
fs.writeFileSync(mdFile, mdText);
51+
console.log(`Saved recipe to: ${mdFile}`);
52+
}
53+
54+
async function generateImage(
55+
projectId = GOOGLE_CLOUD_PROJECT,
56+
location = GOOGLE_CLOUD_LOCATION
57+
) {
58+
const client = new GoogleGenAI({
59+
vertexai: true,
60+
project: projectId,
61+
location: location,
62+
});
63+
64+
const response = await client.models.generateContent({
65+
model: 'gemini-2.5-flash-image',
66+
contents:
67+
'Generate an illustrated recipe for a paella. Create images to go alongside the text as you generate the recipe',
68+
config: {
69+
responseModalities: [Modality.TEXT, Modality.IMAGE],
70+
},
71+
});
72+
console.log(response);
73+
74+
await savePaellaRecipe(response);
75+
76+
return response;
77+
}
78+
// Example response:
79+
// A markdown page for a Paella recipe(`paella-recipe.md`) has been generated.
80+
// It includes detailed steps and several images illustrating the cooking process.
81+
// [END googlegenaisdk_imggen_mmflash_txt_and_img_with_txt]
82+
83+
module.exports = {
84+
generateImage,
85+
};

genai/image-generation/imggen-mmflash-with-txt.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
2222
const GOOGLE_CLOUD_LOCATION =
2323
process.env.GOOGLE_CLOUD_LOCATION || 'us-central1';
2424

25-
async function generateContent(
25+
async function generateImage(
2626
projectId = GOOGLE_CLOUD_PROJECT,
2727
location = GOOGLE_CLOUD_LOCATION
2828
) {
@@ -43,13 +43,18 @@ async function generateContent(
4343

4444
const generatedFileNames = [];
4545
let imageIndex = 0;
46+
4647
for await (const chunk of response) {
4748
const text = chunk.text;
4849
const data = chunk.data;
4950
if (text) {
5051
console.debug(text);
5152
} else if (data) {
52-
const fileName = `generate_content_streaming_image_${imageIndex++}.png`;
53+
const outputDir = 'output-folder';
54+
if (!fs.existsSync(outputDir)) {
55+
fs.mkdirSync(outputDir, {recursive: true});
56+
}
57+
const fileName = `${outputDir}/generate_content_streaming_image_${imageIndex++}.png`;
5358
console.debug(`Writing response image to file: ${fileName}.`);
5459
try {
5560
fs.writeFileSync(fileName, data);
@@ -60,10 +65,16 @@ async function generateContent(
6065
}
6166
}
6267

68+
// Example response:
69+
// I will generate an image of the Eiffel Tower at night, with a vibrant display of
70+
// colorful fireworks exploding in the dark sky behind it. The tower will be
71+
// illuminated, standing tall as the focal point of the scene, with the bursts of
72+
// light from the fireworks creating a festive atmosphere.
73+
6374
return generatedFileNames;
6475
}
6576
// [END googlegenaisdk_imggen_mmflash_with_txt]
6677

6778
module.exports = {
68-
generateContent,
79+
generateImage,
6980
};

0 commit comments

Comments
 (0)