-
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
Open
Guiners
wants to merge
25
commits into
GoogleCloudPlatform:main
Choose a base branch
from
Guiners:sample/september-samples
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+615
−11
Open
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
2797156
adding samples
fd5df9b
adding samples
e6ef36b
adding samples
6f39e66
adding new samples
6fab88c
adding new samples
03bc38d
adding new samples
e461248
adding new samples
3712a69
adding new samples
8a40955
adding new samples
1cb2bc5
Merge branch 'main' into sample/september-samples
Guiners d7d32a7
Update genai/image-generation/imggen-mmflash-locale-aware-with-txt.js
Guiners 9c8c4c1
Update genai/image-generation/imggen-mmflash-multiple-imgs-with-txt.js
Guiners efe7dae
fixing samples
6210b63
fixing samples
dc920b3
fixing samples
ab800eb
fix(genai): fix misspelled word based on suggestion from @gemini-code…
iennae 1567d53
sample update
1a3f67a
Merge remote-tracking branch 'origin/sample/september-samples' into s…
39cffec
Merge branch 'main' into sample/september-samples
Guiners cd1c112
model update
557ddf9
test fix
53cf93f
Merge branch 'main' into sample/september-samples
Guiners aebb45a
test fix
db96b0f
test fix
792f37b
test fix
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
genai/image-generation/imggen-mmflash-edit-img-with-txt-img.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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_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 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', | ||
contents: [image, 'Edit this image to make it look like a cartoon'], | ||
config: { | ||
responseModalities: [Modality.TEXT, Modality.IMAGE], | ||
}, | ||
}); | ||
|
||
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`; | ||
fs.writeFileSync(filename, imageBytes); | ||
} | ||
} | ||
|
||
// Example response: | ||
// Okay, I will edit this image to give it a cartoonish style, with bolder outlines, simplified details, and more vibrant colors. | ||
return response; | ||
} | ||
|
||
// [END googlegenaisdk_imggen_mmflash_edit_img_with_txt_img] | ||
|
||
module.exports = { | ||
generateImage, | ||
}; |
71 changes: 71 additions & 0 deletions
71
genai/image-generation/imggen-mmflash-locale-aware-with-txt.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// 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_locale_aware_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'; | ||
|
||
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', | ||
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); | ||
} | ||
} | ||
|
||
// 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 ... | ||
|
||
return response; | ||
} | ||
|
||
// [END googlegenaisdk_imggen_mmflash_locale_aware_with_txt] | ||
|
||
module.exports = { | ||
generateImage, | ||
}; |
83 changes: 83 additions & 0 deletions
83
genai/image-generation/imggen-mmflash-multiple-imgs-with-txt.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// 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'; | ||
|
||
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', | ||
contents: 'Generate 3 images of a cat sitting on a chair.', | ||
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, | ||
}; |
85 changes: 85 additions & 0 deletions
85
genai/image-generation/imggen-mmflash-txt-and-img-with-txt.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_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'; | ||
|
||
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 recipe to: ${mdFile}`); | ||
} | ||
|
||
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', | ||
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, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.