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
112 lines (98 loc) · 3.53 KB
/
main.swift
File metadata and controls
112 lines (98 loc) · 3.53 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
//
// snippet-start:[swift.example_code.bedrock-runtime.Scenario_AmazonNova_TextToVideo]
// This example demonstrates how to use Amazon Nova Reel to generate a video from a text prompt.
// It shows how to:
// - Set up the Amazon Bedrock runtime client
// - Configure a text-to-video request
// - Submit an asynchronous job for video generation
// - Poll for job completion status
// - Access the generated video from S3
import AWSBedrockRuntime
import Foundation
import Smithy
func startTextToVideoGenerationJob(
bedrockRuntimeClient: BedrockRuntimeClient, prompt: String, outputS3Uri: String
) async throws -> String? {
// Specify the model ID for text-to-video generation
let modelId = "amazon.nova-reel-v1:0"
// Configure the video generation request with additional parameters
let modelInputSource: [String: Any] = [
"taskType": "TEXT_VIDEO",
"textToVideoParams": [
"text": "\(prompt)"
],
"videoGenerationConfig": [
"durationSeconds": 6,
"fps": 24,
"dimension": "1280x720",
],
]
let modelInput = try Document.make(from: modelInputSource)
let input = StartAsyncInvokeInput(
modelId: modelId,
modelInput: modelInput,
outputDataConfig: .s3outputdataconfig(
BedrockRuntimeClientTypes.AsyncInvokeS3OutputDataConfig(
s3Uri: outputS3Uri
)
)
)
// Invoke the model asynchronously
let output = try await bedrockRuntimeClient.startAsyncInvoke(input: input)
return output.invocationArn
}
func queryJobStatus(
bedrockRuntimeClient: BedrockRuntimeClient,
invocationArn: String?
) async throws -> GetAsyncInvokeOutput {
try await bedrockRuntimeClient.getAsyncInvoke(
input: GetAsyncInvokeInput(invocationArn: invocationArn))
}
func main() async throws {
// Create a Bedrock Runtime client
let config =
try await BedrockRuntimeClient.BedrockRuntimeClientConfiguration(
region: "us-east-1"
)
let client = BedrockRuntimeClient(config: config)
// Specify the S3 location for the output video
let bucket = "s3://REPLACE-WITH-YOUR-S3-BUCKET-NAM"
print("Submitting video generation job...")
let invocationArn = try await startTextToVideoGenerationJob(
bedrockRuntimeClient: client,
prompt: "A pomegranate juice in a railway station",
outputS3Uri: bucket
)
print("Job started with invocation ARN: \(String(describing:invocationArn))")
// Poll for job completion
var status: BedrockRuntimeClientTypes.AsyncInvokeStatus?
var isReady = false
var hasFailed = false
while !isReady && !hasFailed {
print("\nPolling job status...")
status = try await queryJobStatus(
bedrockRuntimeClient: client, invocationArn: invocationArn
).status
switch status {
case .completed:
isReady = true
print("Video is ready\nCheck S3 bucket: \(bucket)")
case .failed:
hasFailed = true
print("Something went wrong")
case .inProgress:
print("Job is in progress...")
try await Task.sleep(nanoseconds: 15 * 1_000_000_000) // 15 seconds
default:
isReady = true
}
}
}
do {
try await main()
} catch {
print("An error occurred: \(error)")
}
// snippet-end:[swift.example_code.bedrock-runtime.Scenario_AmazonNova_TextToVideo]