forked from awsdocs/aws-doc-sdk-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.swift
More file actions
76 lines (64 loc) · 2.27 KB
/
main.swift
File metadata and controls
76 lines (64 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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)")
}