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

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .doc_gen/metadata/bedrock-runtime_metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1399,6 +1399,14 @@ bedrock-runtime_InvokeModel_AmazonNovaImageGeneration:
- description: Create an image with the Amazon Nova Canvas.
snippet_tags:
- python.example_code.bedrock-runtime.InvokeModel_AmazonNovaImageGeneration
Swift:
versions:
- sdk_version: 1
github: swift/example_code/bedrock-runtime
excerpts:
- description: Create an image with Amazon Nova Canvas.
snippet_tags:
- swift.example_code.bedrock-runtime.InvokeModel_AmazonNovaImageGeneration
services:
bedrock-runtime: {InvokeModel}

Expand Down
4 changes: 4 additions & 0 deletions swift/example_code/bedrock-runtime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ For prerequisites, see the [README](../../README.md#Prerequisites) in the `swift
- [Converse](models/amazon-nova/amazon-nova-text/Sources/Converse/main.swift#L4)
- [ConverseStream](models/amazon-nova/amazon-nova-text/Sources/ConverseStream/main.swift#L4)

### Amazon Nova Canvas

- [InvokeModel](models/amazon-nova/amazon_nova_canvas/Sources/main.swift#L4)

### Anthropic Claude

- [Converse](models/anthropic_claude/Sources/Converse/main.swift#L4)
Expand Down
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"
)
]
)
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)")
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.