Skip to content

Commit 708c376

Browse files
authored
Swift: Added example code for text to image generation with Amazon Nova Canvas for Bedrock (#7452)
* Converse and ConverseStream code examples
1 parent 29d8410 commit 708c376

File tree

5 files changed

+119
-0
lines changed

5 files changed

+119
-0
lines changed

.doc_gen/metadata/bedrock-runtime_metadata.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,6 +1321,14 @@ bedrock-runtime_InvokeModel_AmazonNovaImageGeneration:
13211321
- description: Create an image with the Amazon Nova Canvas.
13221322
snippet_tags:
13231323
- python.example_code.bedrock-runtime.InvokeModel_AmazonNovaImageGeneration
1324+
Swift:
1325+
versions:
1326+
- sdk_version: 1
1327+
github: swift/example_code/bedrock-runtime
1328+
excerpts:
1329+
- description: Create an image with Amazon Nova Canvas.
1330+
snippet_tags:
1331+
- swift.example_code.bedrock-runtime.InvokeModel_AmazonNovaImageGeneration
13241332
services:
13251333
bedrock-runtime: {InvokeModel}
13261334

swift/example_code/bedrock-runtime/README.md

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

39+
### Amazon Nova Canvas
40+
41+
- [InvokeModel](models/amazon-nova/amazon-nova-canvas/Sources/main.swift#L4)
42+
3943
### Amazon Nova Reel
4044

4145
- [Text-to-video](models/amazon-nova/amazon-nova-reel/Sources/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: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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 AWSSDKIdentity
9+
import Foundation
10+
11+
struct NovaImageOutput: Decodable {
12+
let images: [Data]
13+
}
14+
15+
func generateImage(_ textPrompt: String) async throws {
16+
// Create a Bedrock Runtime client in the AWS Region you want to use.
17+
let config =
18+
try await BedrockRuntimeClient.BedrockRuntimeClientConfiguration(
19+
region: "us-east-1"
20+
)
21+
config.awsCredentialIdentityResolver = try SSOAWSCredentialIdentityResolver()
22+
23+
let client = BedrockRuntimeClient(config: config)
24+
25+
// Set the model ID.
26+
let modelId = "amazon.nova-canvas-v1:0"
27+
28+
// Format the request payload using the model's native structure.
29+
let input = InvokeModelInput(
30+
accept: "application/json",
31+
body: """
32+
{
33+
"textToImageParams": {
34+
"text": "\(textPrompt)"
35+
},
36+
"taskType": "TEXT_IMAGE",
37+
"imageGenerationConfig": {
38+
"seed": 42,
39+
"quality": "standard",
40+
"width": 512,
41+
"height": 512,
42+
"numberOfImages": 1
43+
}
44+
}
45+
""".data(using: .utf8),
46+
modelId: modelId
47+
)
48+
49+
// Invoke the model with the request.
50+
let response = try await client.invokeModel(input: input)
51+
52+
// Decode the response body.
53+
let output = try JSONDecoder().decode(NovaImageOutput.self, from: response.body!)
54+
55+
// Extract the image data.
56+
guard let data = output.images.first else {
57+
print("No image data found")
58+
return
59+
}
60+
61+
// Save the generated image to a local folder.
62+
let fileURL = URL.documentsDirectory.appending(path: "nova_canvas.png")
63+
print(fileURL)
64+
try data.write(to: fileURL)
65+
print("Image is saved at \(fileURL)")
66+
}
67+
68+
// snippet-end:[swift.example_code.bedrock-runtime.InvokeModel_AmazonNovaImageGeneration]
69+
70+
do {
71+
try await generateImage(
72+
"A tabby cat in a teacup"
73+
)
74+
} catch {
75+
print("An error occurred: \(error)")
76+
}
473 KB
Loading

0 commit comments

Comments
 (0)