-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(genai): add image generation samples (1) #3276
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
jdomingr
wants to merge
3
commits into
GoogleCloudPlatform:main
Choose a base branch
from
jdomingr:genai-sdk-imggeneration-samples-p1
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.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
.../api/GenAI.Samples.Tests/ImageGeneration/ImageGenMultimodalFlashLocaleAwareWithTxtTest.cs
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,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. | ||
| */ | ||
|
|
||
| using System.IO; | ||
| using System.Threading.Tasks; | ||
| using Xunit; | ||
|
|
||
| [Collection(nameof(GenAIFixture))] | ||
| public class ImageGenMultimodalFlashLocaleAwareWithTxtTest | ||
| { | ||
| private readonly GenAIFixture _fixture; | ||
| private readonly ImageGenMultimodalFlashLocaleAwareWithTxt _sample; | ||
|
|
||
| public ImageGenMultimodalFlashLocaleAwareWithTxtTest(GenAIFixture fixture) | ||
| { | ||
| _fixture = fixture; | ||
| _sample = new ImageGenMultimodalFlashLocaleAwareWithTxt(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task TestImgGenMmFlashLocaleAwareWithTxt() | ||
| { | ||
| FileInfo response = await _sample.GenerateContent(_fixture.ProjectId); | ||
| Assert.NotNull(response); | ||
| Assert.True(response.Length > 0); | ||
| } | ||
| } |
39 changes: 39 additions & 0 deletions
39
...api/GenAI.Samples.Tests/ImageGeneration/ImageGenMultimodalFlashMultipleImgsWithTxtTest.cs
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,39 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Threading.Tasks; | ||
| using Xunit; | ||
|
|
||
| [Collection(nameof(GenAIFixture))] | ||
| public class ImageGenMultimodalFlashMultipleImgsWithTxtTest | ||
| { | ||
| private readonly GenAIFixture _fixture; | ||
| private readonly ImageGenMultimodalFlashMultipleImgsWithTxt _sample; | ||
|
|
||
| public ImageGenMultimodalFlashMultipleImgsWithTxtTest(GenAIFixture fixture) | ||
| { | ||
| _fixture = fixture; | ||
| _sample = new ImageGenMultimodalFlashMultipleImgsWithTxt(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task TestImgGenMmFlashMultipleImgsWithTxt() | ||
| { | ||
| List<string> response = await _sample.GenerateContent(_fixture.ProjectId); | ||
| Assert.NotEmpty(response); | ||
| } | ||
| } |
40 changes: 40 additions & 0 deletions
40
genai/api/GenAI.Samples.Tests/ImageGeneration/ImageGenMultimodalFlashWithTxtTest.cs
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,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. | ||
| */ | ||
|
|
||
| using System.IO; | ||
| using System.Threading.Tasks; | ||
| using Xunit; | ||
|
|
||
| [Collection(nameof(GenAIFixture))] | ||
| public class ImageGenMultimodalFlashWithTxtTest | ||
| { | ||
| private readonly GenAIFixture _fixture; | ||
| private readonly ImageGenMultimodalFlashWithTxt _sample; | ||
|
|
||
| public ImageGenMultimodalFlashWithTxtTest(GenAIFixture fixture) | ||
| { | ||
| _fixture = fixture; | ||
| _sample = new ImageGenMultimodalFlashWithTxt(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task TestImgGenMmFlashWithTxt() | ||
| { | ||
| FileInfo response = await _sample.GenerateContent(_fixture.ProjectId); | ||
| Assert.NotNull(response); | ||
| Assert.True(response.Length > 0); | ||
| } | ||
| } |
65 changes: 65 additions & 0 deletions
65
genai/api/GenAI.Samples/ImageGeneration/ImageGenMultimodalFlashLocaleAwareWithTxt.cs
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,65 @@ | ||
| /* | ||
| * 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_mmflash_locale_aware_with_txt] | ||
|
|
||
| using Google.GenAI; | ||
| using Google.GenAI.Types; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Threading.Tasks; | ||
|
|
||
| public class ImageGenMultimodalFlashLocaleAwareWithTxt | ||
| { | ||
| public async Task<FileInfo> GenerateContent( | ||
| string projectId = "your-project-id", | ||
| string location = "global", | ||
| string model = "gemini-2.5-flash-image") | ||
| { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some of the samples have a try-catch to catch the errors. I think that we should aim for that here as well just to 1. showcase some simple error handling, and 2. keep consistency with other languages that have a try-catch as well. |
||
| await using var client = new Client(project: projectId, location: location, vertexAI: true); | ||
|
|
||
| GenerateContentResponse response = await client.Models.GenerateContentAsync( | ||
| model: model, | ||
| contents: "Generate a photo of a breakfast meal.", | ||
| config: new GenerateContentConfig { ResponseModalities = new List<string> { "TEXT", "IMAGE" } }); | ||
|
|
||
| var outputFilename = "example-breakfast-meal.png"; | ||
| FileInfo fileInfo = null; | ||
|
|
||
| List<Part> parts = response.Candidates?[0]?.Content?.Parts ?? new List<Part>(); | ||
|
|
||
| foreach (Part part in parts) | ||
| { | ||
| if (!string.IsNullOrEmpty(part.Text)) | ||
| { | ||
| Console.WriteLine(part.Text); | ||
| } | ||
| else if (part.InlineData?.Data != null) | ||
| { | ||
| File.WriteAllBytes(outputFilename, part.InlineData.Data); | ||
| fileInfo = new FileInfo(Path.GetFullPath(outputFilename)); | ||
| Console.WriteLine($"Created output image using {fileInfo.Length} bytes"); | ||
| } | ||
| } | ||
| // Example response: | ||
| // Here's a photo of a breakfast meal: | ||
| // | ||
| // Created output image using 1369476 bytes | ||
| return fileInfo; | ||
| } | ||
| } | ||
| // [END googlegenaisdk_imggen_mmflash_locale_aware_with_txt] | ||
64 changes: 64 additions & 0 deletions
64
genai/api/GenAI.Samples/ImageGeneration/ImageGenMultimodalFlashMultipleImgsWithTxt.cs
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,64 @@ | ||
| /* | ||
| * 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_mmflash_multiple_imgs_with_txt] | ||
|
|
||
| using Google.GenAI; | ||
| using Google.GenAI.Types; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Threading.Tasks; | ||
|
|
||
| public class ImageGenMultimodalFlashMultipleImgsWithTxt | ||
| { | ||
| public async Task<List<string>> GenerateContent( | ||
| string projectId = "your-project-id", | ||
| string location = "global", | ||
| string model = "gemini-2.5-flash-image") | ||
| { | ||
| await using var client = new Client(project: projectId, location: location, vertexAI: true); | ||
|
|
||
| GenerateContentResponse response = await client.Models.GenerateContentAsync( | ||
| model: model, | ||
| contents: "Generate 3 images of a cat sitting on a chair.", | ||
| config: new GenerateContentConfig { ResponseModalities = new List<string> { "TEXT", "IMAGE" } }); | ||
|
|
||
| List<Part> parts = response.Candidates?[0]?.Content?.Parts ?? new List<Part>(); | ||
|
|
||
| var generatedFiles = new List<string>(); | ||
| int imageCounter = 1; | ||
|
|
||
| foreach (Part part in parts) | ||
| { | ||
| if (!string.IsNullOrEmpty(part.Text)) | ||
| { | ||
| Console.WriteLine(part.Text); | ||
| } | ||
| else if (part.InlineData?.Data != null) | ||
| { | ||
| var filename = $"example-cats-0{imageCounter}.png"; | ||
jdomingr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| File.WriteAllBytes(filename, part.InlineData.Data); | ||
| generatedFiles.Add(filename); | ||
| imageCounter++; | ||
| } | ||
| } | ||
| // Example response: | ||
| // Here are three images of cats sitting on chairs. | ||
| return generatedFiles; | ||
| } | ||
| } | ||
| // [END googlegenaisdk_imggen_mmflash_multiple_imgs_with_txt] | ||
80 changes: 80 additions & 0 deletions
80
genai/api/GenAI.Samples/ImageGeneration/ImageGenMultimodalFlashWithTxt.cs
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,80 @@ | ||
| /* | ||
| * 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_mmflash_with_txt] | ||
|
|
||
| using Google.GenAI; | ||
| using Google.GenAI.Types; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Threading.Tasks; | ||
|
|
||
| public class ImageGenMultimodalFlashWithTxt | ||
| { | ||
| public async Task<FileInfo> GenerateContent( | ||
| string projectId = "your-project-id", | ||
| string location = "global", | ||
| string model = "gemini-2.5-flash-image") | ||
| { | ||
| await using var client = new Client(project: projectId, location: location, vertexAI: true); | ||
|
|
||
| var contentConfig = new GenerateContentConfig | ||
| { | ||
| ResponseModalities = new List<string> { "TEXT", "IMAGE" }, | ||
| CandidateCount = 1, | ||
| SafetySettings = new List<SafetySetting> | ||
| { | ||
| new SafetySetting | ||
| { | ||
| Method = HarmBlockMethod.PROBABILITY, | ||
| Category = HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT, | ||
| Threshold = HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| GenerateContentResponse response = await client.Models.GenerateContentAsync( | ||
| model: model, | ||
| contents: "Generate an image of the Eiffel tower with fireworks in the background.", | ||
| config: contentConfig); | ||
|
|
||
| var outputFile = "example-image-eiffel-tower.png"; | ||
| FileInfo fileInfo = null; | ||
|
|
||
| List<Part> parts = response.Candidates?[0]?.Content?.Parts ?? new List<Part>(); | ||
|
|
||
| foreach (Part part in parts) | ||
| { | ||
| if (!string.IsNullOrEmpty(part.Text)) | ||
| { | ||
| Console.WriteLine(part.Text); | ||
| } | ||
| else if (part.InlineData?.Data != null) | ||
| { | ||
| File.WriteAllBytes(outputFile, part.InlineData.Data); | ||
| fileInfo = new FileInfo(Path.GetFullPath(outputFile)); | ||
| Console.WriteLine($"Created output image using {fileInfo.Length} bytes"); | ||
| } | ||
| } | ||
| // Example response: | ||
| // Absolutely! Here's the Eiffel Tower with fireworks: | ||
| // | ||
| // Created output image using 1897804 bytes | ||
| return fileInfo; | ||
| } | ||
| } | ||
| // [END googlegenaisdk_imggen_mmflash_with_txt] |
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.