-
Notifications
You must be signed in to change notification settings - Fork 2k
feat(genai): nano-banana-samples #4184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 10 commits
2797156
fd5df9b
e6ef36b
6f39e66
6fab88c
03bc38d
e461248
3712a69
8a40955
1cb2bc5
d7d32a7
9c8c4c1
efe7dae
6210b63
dc920b3
ab800eb
1567d53
1a3f67a
39cffec
cd1c112
557ddf9
53cf93f
aebb45a
db96b0f
792f37b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// 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_mmflash_edit_img_with_txt_img] | ||
const fs = require('fs'); | ||
const {GoogleGenAI, Modality} = require('@google/genai'); | ||
|
||
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT; | ||
// const GOOGLE_CLOUD_LOCATION = | ||
// process.env.GOOGLE_CLOUD_LOCATION || 'us-central1'; | ||
|
||
const GOOGLE_CLOUD_LOCATION = 'global'; | ||
const FILE_NAME = 'test-data/example-image-eiffel-tower.png'; | ||
|
||
async function generateImage( | ||
projectId = GOOGLE_CLOUD_PROJECT, | ||
location = GOOGLE_CLOUD_LOCATION | ||
) { | ||
const client = new GoogleGenAI({ | ||
vertexai: true, | ||
project: projectId, | ||
location: location, | ||
}); | ||
|
||
const image = fs.readFileSync(FILE_NAME); | ||
|
||
const response = await client.models.generateContent({ | ||
model: 'gemini-2.5-flash-image-preview', | ||
contents: [image, 'Edit this image to make it look like a cartoon'], | ||
config: { | ||
responseModalities: [Modality.TEXT, Modality.IMAGE], | ||
}, | ||
}); | ||
|
||
console.log(response); | ||
Guiners marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
for (const part of response.candidates[0].content.parts) { | ||
if (part.text) { | ||
console.log(`${part.text}`); | ||
} else if (part.inlineData) { | ||
const outputDir = 'output-folder'; | ||
if (!fs.existsSync(outputDir)) { | ||
fs.mkdirSync(outputDir, { recursive: true }); | ||
} | ||
const imageBytes = Buffer.from(part.inlineData.data, 'base64'); | ||
const filename = `${outputDir}/bw-example-image.png`; | ||
Guiners marked this conversation as resolved.
Show resolved
Hide resolved
Guiners marked this conversation as resolved.
Show resolved
Hide resolved
|
||
fs.writeFileSync(filename, imageBytes); | ||
} | ||
} | ||
|
||
return response; | ||
} | ||
// Example response: | ||
// Okay, I will edit this image to give it a cartoonish style, with bolder outlines, simplified details, and more vibrant colors. | ||
Guiners marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
// [END googlegenaisdk_imggen_mmflash_edit_img_with_txt_img] | ||
|
||
module.exports = { | ||
generateImage, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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_mmflash_edit_img_with_txt_img] | ||
Guiners marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
const fs = require('fs'); | ||
const {GoogleGenAI, Modality} = 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( | ||
projectId = GOOGLE_CLOUD_PROJECT, | ||
location = GOOGLE_CLOUD_LOCATION | ||
) { | ||
const client = new GoogleGenAI({ | ||
vertexai: true, | ||
project: projectId, | ||
location: location, | ||
}); | ||
|
||
const response = await client.models.generateContent({ | ||
model: 'gemini-2.5-flash-image-preview', | ||
contents: 'Generate a photo of a breakfast meal.', | ||
config: { | ||
responseModalities: [Modality.TEXT, Modality.IMAGE], | ||
}, | ||
}); | ||
|
||
console.log(response); | ||
|
||
for (const part of response.candidates[0].content.parts) { | ||
if (part.text) { | ||
console.log(`${part.text}`); | ||
} else if (part.inlineData) { | ||
const outputDir = 'output-folder'; | ||
if (!fs.existsSync(outputDir)) { | ||
fs.mkdirSync(outputDir, { recursive: true }); | ||
} | ||
const imageBytes = Buffer.from(part.inlineData.data, 'base64'); | ||
const filename = `${outputDir}/example-breakfast-meal.png`; | ||
fs.writeFileSync(filename, imageBytes); | ||
} | ||
} | ||
|
||
return response; | ||
} | ||
// Example response: | ||
// Generates a photo of a vibrant and appetizing breakfast meal. | ||
// The scene will feature a white plate with golden-brown pancakes | ||
// stacked neatly, drizzled with rich maple syrup and ... | ||
// [END googlegenaisdk_imggen_mmflash_edit_img_with_txt_img] | ||
|
||
module.exports = { | ||
generateImage, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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_mmflash_multiple_imgs_with_txt] | ||
const fs = require('fs'); | ||
const {GoogleGenAI, Modality} = require('@google/genai'); | ||
|
||
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT; | ||
// const GOOGLE_CLOUD_LOCATION = | ||
// process.env.GOOGLE_CLOUD_LOCATION || 'us-central1'; | ||
|
||
const GOOGLE_CLOUD_LOCATION = 'global'; | ||
|
||
async function generateImage( | ||
projectId = GOOGLE_CLOUD_PROJECT, | ||
location = GOOGLE_CLOUD_LOCATION | ||
) { | ||
const client = new GoogleGenAI({ | ||
vertexai: true, | ||
project: projectId, | ||
location: location, | ||
}); | ||
|
||
const response = await client.models.generateContent({ | ||
model: 'gemini-2.5-flash-image-preview', | ||
contents: 'Generate 3 images a cat sitting on a chair.', | ||
Guiners marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
config: { | ||
responseModalities: [Modality.TEXT, Modality.IMAGE], | ||
}, | ||
}); | ||
|
||
console.log(response); | ||
|
||
const generatedFileNames = []; | ||
let imageCounter = 1; | ||
|
||
for (const part of response.candidates[0].content.parts) { | ||
if (part.text) { | ||
console.log(part.text); | ||
} else if (part.inlineData) { | ||
const outputDir = 'output-folder'; | ||
if (!fs.existsSync(outputDir)) { | ||
fs.mkdirSync(outputDir, { recursive: true }); | ||
} | ||
const imageBytes = Buffer.from(part.inlineData.data, 'base64'); | ||
const filename = `${outputDir}/example-cats-0${imageCounter}.png`; | ||
fs.writeFileSync(filename, imageBytes); | ||
generatedFileNames.push(filename); | ||
console.log(`Saved image: ${filename}`); | ||
|
||
imageCounter++; | ||
} | ||
} | ||
|
||
return generatedFileNames; | ||
} | ||
// Example response: | ||
// Image 1: A fluffy calico cat with striking green eyes is perched elegantly on a vintage wooden | ||
// chair with a woven seat. Sunlight streams through a nearby window, casting soft shadows and | ||
// highlighting the cat's fur. | ||
// | ||
// Image 2: A sleek black cat with intense yellow eyes is sitting upright on a modern, minimalist | ||
// white chair. The background is a plain grey wall, putting the focus entirely on the feline's | ||
// graceful posture. | ||
// | ||
// Image 3: A ginger tabby cat with playful amber eyes is comfortably curled up asleep on a plush, | ||
// oversized armchair upholstered in a soft, floral fabric. A corner of a cozy living room with a | ||
// [END googlegenaisdk_imggen_mmflash_multiple_imgs_with_txt] | ||
|
||
module.exports = { | ||
generateImage, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// 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_mmflash_txt_and_img_with_txt] | ||
const fs = require('fs'); | ||
const {GoogleGenAI, Modality} = require('@google/genai'); | ||
|
||
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT; | ||
// const GOOGLE_CLOUD_LOCATION = | ||
// process.env.GOOGLE_CLOUD_LOCATION || 'us-central1'; | ||
Guiners marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
const GOOGLE_CLOUD_LOCATION = 'global'; | ||
|
||
async function savePaellaRecipe(response) { | ||
const parts = response.candidates[0].content.parts; | ||
|
||
let mdText = ''; | ||
const outputDir = 'output-folder'; | ||
|
||
for (let i = 0; i < parts.length; i++) { | ||
const part = parts[i]; | ||
|
||
if (part.text) { | ||
mdText += part.text + '\n'; | ||
} else if (part.inlineData) { | ||
if (!fs.existsSync(outputDir)) { | ||
fs.mkdirSync(outputDir, { recursive: true }); | ||
} | ||
const imageBytes = Buffer.from(part.inlineData.data, 'base64'); | ||
const imagePath = `example-image-${i + 1}.png`; | ||
const saveImagePath = `${outputDir}/${imagePath}`; | ||
|
||
fs.writeFileSync(saveImagePath, imageBytes); | ||
mdText += `\n`; | ||
} | ||
} | ||
const mdFile = `${outputDir}/paella-recipe.md`; | ||
|
||
fs.writeFileSync(mdFile, mdText); | ||
console.log(`Saved recepie to: ${mdFile}`); | ||
iennae marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
|
||
async function generateImage( | ||
projectId = GOOGLE_CLOUD_PROJECT, | ||
location = GOOGLE_CLOUD_LOCATION | ||
) { | ||
const client = new GoogleGenAI({ | ||
vertexai: true, | ||
project: projectId, | ||
location: location, | ||
}); | ||
|
||
const response = await client.models.generateContent({ | ||
model: 'gemini-2.5-flash-image-preview', | ||
contents: | ||
'Generate an illustrated recipe for a paella. Create images to go alongside the text as you generate the recipe', | ||
config: { | ||
responseModalities: [Modality.TEXT, Modality.IMAGE], | ||
}, | ||
}); | ||
console.log(response); | ||
|
||
await savePaellaRecipe(response); | ||
|
||
return response; | ||
} | ||
// Example response: | ||
// A markdown page for a Paella recipe(`paella-recipe.md`) has been generated. | ||
// It includes detailed steps and several images illustrating the cooking process. | ||
// [END googlegenaisdk_imggen_mmflash_txt_and_img_with_txt] | ||
|
||
module.exports = { | ||
generateImage, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,17 +22,17 @@ const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT; | |
const GOOGLE_CLOUD_LOCATION = | ||
process.env.GOOGLE_CLOUD_LOCATION || 'us-central1'; | ||
|
||
async function generateContent( | ||
async function generateImage( | ||
projectId = GOOGLE_CLOUD_PROJECT, | ||
location = GOOGLE_CLOUD_LOCATION | ||
) { | ||
const ai = new GoogleGenAI({ | ||
const client = new GoogleGenAI({ | ||
vertexai: true, | ||
project: projectId, | ||
location: location, | ||
}); | ||
|
||
const response = await ai.models.generateContentStream({ | ||
const response = await client.models.generateContentStream({ | ||
model: 'gemini-2.0-flash-exp', | ||
|
||
contents: | ||
'Generate an image of the Eiffel tower with fireworks in the background.', | ||
|
@@ -43,13 +43,18 @@ async function generateContent( | |
|
||
const generatedFileNames = []; | ||
let imageIndex = 0; | ||
|
||
for await (const chunk of response) { | ||
const text = chunk.text; | ||
const data = chunk.data; | ||
if (text) { | ||
console.debug(text); | ||
} else if (data) { | ||
const fileName = `generate_content_streaming_image_${imageIndex++}.png`; | ||
const outputDir = 'output-folder'; | ||
if (!fs.existsSync(outputDir)) { | ||
fs.mkdirSync(outputDir, { recursive: true }); | ||
} | ||
const fileName = `${outputDir}/generate_content_streaming_image_${imageIndex++}.png`; | ||
console.debug(`Writing response image to file: ${fileName}.`); | ||
try { | ||
fs.writeFileSync(fileName, data); | ||
|
@@ -65,5 +70,5 @@ async function generateContent( | |
// [END googlegenaisdk_imggen_mmflash_with_txt] | ||
|
||
module.exports = { | ||
generateContent, | ||
generateImage, | ||
}; |
Uh oh!
There was an error while loading. Please reload this page.