Skip to content

Commit bb3e1ea

Browse files
committed
basic image generation with Nova Canvas
1 parent 2f73caf commit bb3e1ea

File tree

4 files changed

+118
-0
lines changed

4 files changed

+118
-0
lines changed

.doc_gen/metadata/bedrock-runtime_metadata.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,6 +1399,14 @@ bedrock-runtime_InvokeModel_AmazonNovaImageGeneration:
13991399
- description: Create an image with the Amazon Nova Canvas.
14001400
snippet_tags:
14011401
- python.example_code.bedrock-runtime.InvokeModel_AmazonNovaImageGeneration
1402+
Swift:
1403+
versions:
1404+
- sdk_version: 1
1405+
github: swift/example_code/bedrock-runtime
1406+
excerpts:
1407+
- description: Create an image with the Amazon Nova Canvas.
1408+
snippet_tags:
1409+
- swift.example_code.bedrock-runtime.InvokeModel_AmazonNovaImageGeneration
14021410
services:
14031411
bedrock-runtime: {InvokeModel}
14041412

swift/example_code/bedrock-runtime/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ For prerequisites, see the [README](../../README.md#Prerequisites) in the `swift
3333
- [Converse](models/amazon-nova/amazon-nova-text/Sources/Converse/main.swift#L4)
3434
- [ConverseStream](models/amazon-nova/amazon-nova-text/Sources/ConverseStream/main.swift#L4)
3535

36+
### Amazon Nova Canvas
37+
38+
- [InvokeModel](models/amazon-nova/amazon_nova_canvas/Sources/main.swift#L4)
39+
3640
### Anthropic Claude
3741

3842
- [Converse](models/anthropic_claude/Sources/Converse/main.swift#L4)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// swift-tools-version: 6.1
2+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
// The swift-tools-version declares the minimum version of Swift required to build this package.
6+
7+
import PackageDescription
8+
9+
let package = Package(
10+
name: "AmazonNovaCanvas",
11+
// Let Xcode know the minimum Apple platforms supported.
12+
platforms: [
13+
.macOS(.v13),
14+
.iOS(.v15)
15+
],
16+
dependencies: [
17+
// Dependencies declare other packages that this package depends on.
18+
.package(url: "https://github.com/awslabs/aws-sdk-swift", from: "1.2.61")
19+
],
20+
targets: [
21+
// Targets are the basic building blocks of a package, defining a module or a test suite.
22+
// Targets can depend on other targets in this package and products from dependencies.
23+
.executableTarget(
24+
name: "InvokeModel",
25+
dependencies: [
26+
.product(name: "AWSBedrockRuntime", package: "aws-sdk-swift"),
27+
],
28+
path: "Sources"
29+
)
30+
]
31+
)
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
//
4+
// snippet-start:[swift.example_code.bedrock-runtime.InvokeModel_AmazonNovaImageGeneration]
5+
// Use the native inference API to create an image with Amazon Nova Canvas
6+
7+
import AWSBedrockRuntime
8+
import Foundation
9+
10+
struct NovaImageOutput: Decodable {
11+
let images: [Data]
12+
}
13+
14+
func generateImage(_ textPrompt: String, to path: String) async throws {
15+
// Create a Bedrock Runtime client in the AWS Region you want to use.
16+
let config =
17+
try await BedrockRuntimeClient.BedrockRuntimeClientConfiguration(
18+
region: "us-east-1"
19+
)
20+
21+
let client = BedrockRuntimeClient(config: config)
22+
23+
// Set the model ID.
24+
let modelId = "amazon.nova-canvas-v1:0"
25+
26+
// Format the request payload using the model's native structure.
27+
let input = InvokeModelInput(
28+
accept: "application/json",
29+
body: """
30+
{
31+
"textToImageParams": {
32+
"text": "\(textPrompt)"
33+
},
34+
"taskType": "TEXT_IMAGE",
35+
"imageGenerationConfig": {
36+
"cfgScale": 8,
37+
"seed": 42,
38+
"quality": "standard",
39+
"width": 1024,
40+
"height": 1024,
41+
"numberOfImages": 1
42+
}
43+
}
44+
""".data(using: .utf8),
45+
modelId: modelId
46+
)
47+
48+
// Invoke the model with the request.
49+
let response = try await client.invokeModel(input: input)
50+
51+
// Decode the response body.
52+
let titanImage = try JSONDecoder().decode(NovaImageOutput.self, from: response.body!)
53+
54+
// Extract the image data.
55+
let data = titanImage.images.first
56+
guard let data = data else {
57+
print("No image data found")
58+
return
59+
}
60+
61+
// Save the generated image to a local folder.
62+
let fileURL = URL(fileURLWithPath: path)
63+
try data.write(to: fileURL)
64+
print("Image is saved at \(path)")
65+
}
66+
67+
// snippet-end:[swift.example_code.bedrock-runtime.InvokeModel_AmazonNovaImageGeneration]
68+
69+
do {
70+
try await generateImage(
71+
"A tabby cat in a teacup", to: "/Users/monadierickx/Desktop/img/nova_canvas.png"
72+
)
73+
} catch {
74+
print("An error occurred: \(error)")
75+
}

0 commit comments

Comments
 (0)