Skip to content

Swift: Added example code for text to image generation with Amazon Nova Canvas for Bedrock #7452

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

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

monadierickx
Copy link
Contributor

This pull request includes one code example for image generation with Nova Canvas.


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@github-actions github-actions bot added the Swift This issue relates to the AWS SDK for Swift label May 19, 2025
@sebsto
Copy link

sebsto commented May 19, 2025

I think you need to remove the Llama code from this PR. Start from the main branch. Otherwise looks good, I left a few comments at code level.

Copy link

@sebsto sebsto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some suggestions and questions

- sdk_version: 1
github: swift/example_code/bedrock-runtime
excerpts:
- description: Create an image with the Amazon Nova Canvas.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Create an image with the Amazon Nova Canvas model".

or

"Create an image with Amazon Nova Canvas."

import AWSBedrockRuntime
import Foundation

struct NovaImageOutput: Decodable {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the minimal aspect of it.


// Extract the image data.
let data = titanImage.images.first
guard let data = data else {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

guard let data = titanImage.images.first
and delete line 54

// Save the generated image to a local folder.
let fileURL = URL(fileURLWithPath: path)
try data.write(to: fileURL)
print("Image is saved at \(path)")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you test it ? It's real, usable data or base64 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested it and it generated and saved the image!


do {
try await generateImage(
"A tabby cat in a teacup", to: "/Users/monadierickx/Desktop/img/nova_canvas.png"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's not use your laptop specific directory :-)
try ~/Desktop/img/nova_canvas.png

If URL or write(to:) can not resolve the ~ as $HOME, let's use a different approach

let homeURL = FileManager.default.homeDirectoryForCurrentUser
let imageURL = homeURL.appending(["Desktop", "nova_canvas", "image.png"])

(this is from memory, be sure it compiles)

Also, verify it works when the nova_canvas directory does not exist (or maybe just skip it an use ~/Desktop this one exists on macOS (but will not work on iOS or Linux)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry I forgot! 😳

@DennisTraub DennisTraub self-requested a review May 20, 2025 07:19
@@ -423,6 +423,14 @@ bedrock-runtime_Converse_MetaLlama:
- description: Send a text message to Meta Llama, using Bedrock's Converse API.
snippet_tags:
- javascript.v3.bedrock-runtime.Converse_MetaLlama
Swift:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change appears to be carried over from PR #7434. Could you please remove it to keep PRs isolated? Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will do this!

@@ -708,6 +716,14 @@ bedrock-runtime_ConverseStream_MetaLlama:
- description: Send a text message to Meta Llama, using Bedrock's Converse API and process the response stream in real-time.
snippet_tags:
- javascript.v3.bedrock-runtime.ConverseStream_MetaLlama
Swift:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above, please remove.

Copy link
Contributor

@DennisTraub DennisTraub left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Several changes in this PR appear to be carried over from PR #7434. Please remove these unrelated modifications to maintain isolated PRs. Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Carried over from #7434. Please remove.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Carried over from #7434. Please remove.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Carried over from #7434. Please remove.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Swift This issue relates to the AWS SDK for Swift
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants