-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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
monadierickx
wants to merge
7
commits into
awsdocs:main
Choose a base branch
from
monadierickx:image-generation
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.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
56b1977
Converse and ConverseStream code examples
monadierickx a333a1e
Regenerate documentation
monadierickx ed3fae0
Fixed SDK version mistake and regenerated documentation
monadierickx 2f73caf
Regenerate documentation
monadierickx bb3e1ea
basic image generation with Nova Canvas
monadierickx 0761bc6
use same inference parameters as python example
monadierickx 5a10209
minor changes
monadierickx 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
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
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
31 changes: 31 additions & 0 deletions
31
swift/example_code/bedrock-runtime/models/amazon-nova/amazon_nova_canvas/Package.swift
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,31 @@ | ||
// swift-tools-version: 6.1 | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// The swift-tools-version declares the minimum version of Swift required to build this package. | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "AmazonNovaCanvas", | ||
// Let Xcode know the minimum Apple platforms supported. | ||
platforms: [ | ||
.macOS(.v13), | ||
.iOS(.v15) | ||
], | ||
dependencies: [ | ||
// Dependencies declare other packages that this package depends on. | ||
.package(url: "https://github.com/awslabs/aws-sdk-swift", from: "1.2.61") | ||
], | ||
targets: [ | ||
// Targets are the basic building blocks of a package, defining a module or a test suite. | ||
// Targets can depend on other targets in this package and products from dependencies. | ||
.executableTarget( | ||
name: "InvokeModel", | ||
dependencies: [ | ||
.product(name: "AWSBedrockRuntime", package: "aws-sdk-swift"), | ||
], | ||
path: "Sources" | ||
) | ||
] | ||
) |
76 changes: 76 additions & 0 deletions
76
swift/example_code/bedrock-runtime/models/amazon-nova/amazon_nova_canvas/Sources/main.swift
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,76 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// snippet-start:[swift.example_code.bedrock-runtime.InvokeModel_AmazonNovaImageGeneration] | ||
// Use the native inference API to create an image with Amazon Nova Canvas | ||
|
||
import AWSBedrockRuntime | ||
import AWSSDKIdentity | ||
import Foundation | ||
|
||
struct NovaImageOutput: Decodable { | ||
let images: [Data] | ||
} | ||
|
||
func generateImage(_ textPrompt: String) async throws { | ||
// Create a Bedrock Runtime client in the AWS Region you want to use. | ||
let config = | ||
try await BedrockRuntimeClient.BedrockRuntimeClientConfiguration( | ||
region: "us-east-1" | ||
) | ||
config.awsCredentialIdentityResolver = try SSOAWSCredentialIdentityResolver() | ||
|
||
let client = BedrockRuntimeClient(config: config) | ||
|
||
// Set the model ID. | ||
let modelId = "amazon.nova-canvas-v1:0" | ||
|
||
// Format the request payload using the model's native structure. | ||
let input = InvokeModelInput( | ||
accept: "application/json", | ||
body: """ | ||
{ | ||
"textToImageParams": { | ||
"text": "\(textPrompt)" | ||
}, | ||
"taskType": "TEXT_IMAGE", | ||
"imageGenerationConfig": { | ||
"seed": 42, | ||
"quality": "standard", | ||
"width": 512, | ||
"height": 512, | ||
"numberOfImages": 1 | ||
} | ||
} | ||
""".data(using: .utf8), | ||
modelId: modelId | ||
) | ||
|
||
// Invoke the model with the request. | ||
let response = try await client.invokeModel(input: input) | ||
|
||
// Decode the response body. | ||
let output = try JSONDecoder().decode(NovaImageOutput.self, from: response.body!) | ||
|
||
// Extract the image data. | ||
guard let data = output.images.first else { | ||
print("No image data found") | ||
return | ||
} | ||
|
||
// Save the generated image to a local folder. | ||
let fileURL = URL.documentsDirectory.appending(path: "nova_canvas.png") | ||
print(fileURL) | ||
try data.write(to: fileURL) | ||
print("Image is saved at \(fileURL)") | ||
} | ||
|
||
// snippet-end:[swift.example_code.bedrock-runtime.InvokeModel_AmazonNovaImageGeneration] | ||
|
||
do { | ||
try await generateImage( | ||
"A tabby cat in a teacup" | ||
) | ||
} catch { | ||
print("An error occurred: \(error)") | ||
} |
Binary file added
BIN
+473 KB
...mple_code/bedrock-runtime/models/amazon-nova/amazon_nova_canvas/nova_canvas.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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.