-
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
Open
monadierickx
wants to merge
36
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.
Open
Changes from all commits
Commits
Show all changes
36 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 b786a5b
Fix SoS tags for example for Swift (#7436)
shepazon c76da74
Swift: Bedrock-Runtime Readme - add custom prerequisite (#7453)
DennisTraub bb4abbc
Update to latest tools release 2025.18.3. (#7441)
Laren-AWS 806c4e3
Bedrock Runtime: Remove explicit references to Llama 3 (#7444)
DennisTraub b5e51f0
Bump aws-cdk-lib from 2.189.0 to 2.189.1 in /resources/cdk/cognito_sc…
dependabot[bot] e38cc77
Python: Bedrock Runtime document understanding examples (#7446)
DennisTraub a8cd3c8
Update to latest tools release 2025.20.1 (#7457)
Laren-AWS 90be677
Update README.md - fixing bedrock runtime crate link (#7461)
martin-conur e5b1a12
Update to latest tools release 2025.1.0 (#7463)
Laren-AWS 17cd139
Java V2: add existing hard-coded example for the SQS extended client …
tkhill-AWS e3a68b1
Java V2 added SES example that shows new header information (#7430)
scmacdon 9d586ae
PHP: Add example for Amazon Nova text models, remove example for Jura…
DennisTraub caa1de9
Update to latest tools release 2025.21.1. (#7464)
Laren-AWS 8f771da
Bump axios and @ailly/cli in /.tools/ailly (#7465)
dependabot[bot] ad43ae1
Entity resolution js (#7438)
brmur 8032afe
Bump org.springframework:spring-context from 6.1.14 to 6.1.20 in /jav…
dependabot[bot] c53d7c4
Bump degenerator and aws-cdk in /resources/cdk/rekognition-sns-video-…
dependabot[bot] 9a49113
Bump axios and @ailly/core in /.tools/ailly (#7466)
dependabot[bot] 356c317
Bump braces from 3.0.2 to 3.0.3 in /gov2/workflows/user_pools_and_lam…
dependabot[bot] 06a6feb
Bump braces from 3.0.2 to 3.0.3 in /.tools/test/stacks/plugin/typescr…
dependabot[bot] 5764c62
Java V2 Add Neptune Basics (#7462)
scmacdon 613fd4f
Bump rack from 3.1.14 to 3.1.16 in /ruby (#7471)
dependabot[bot] 5056620
Swift: code examples for Meta Llama to use Converse and ConverseStrea…
monadierickx 64acdd2
Bump com.fasterxml.jackson.core:jackson-core from 2.12.5 to 2.13.0 in…
dependabot[bot] c35f02d
Bump requests from 2.31.0 to 2.32.4 in /python/example_code/bedrock-a…
dependabot[bot] dc304fc
Bump requests from 2.32.0 to 2.32.4 in /.tools (#7478)
dependabot[bot] 57501a2
Fixed a minor bug by updating a SOS tag in the EC2 Yaml file (#7476)
scmacdon b8af3f5
Fix description in JS scenario (#7481)
brmur 7a8360c
Merge branch 'main' into image-generation
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.