Skip to content

Commit 801dd21

Browse files
authored
Swift: Added video generation example code for Nova Reel, Amazon Bedrock (#7459)
* nova reel example code
1 parent 9ed0108 commit 801dd21

File tree

4 files changed

+155
-0
lines changed

4 files changed

+155
-0
lines changed

.doc_gen/metadata/bedrock-runtime_metadata.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,6 +1504,14 @@ bedrock-runtime_Scenario_AmazonNova_TextToVideo:
15041504
- description: Use Amazon Nova Reel to generate a video from a text prompt.
15051505
snippet_tags:
15061506
- python.example_code.bedrock-runtime.Scenario_AmazonNova_TextToVideo
1507+
Swift:
1508+
versions:
1509+
- sdk_version: 1
1510+
github: swift/example_code/bedrock-runtime
1511+
excerpts:
1512+
- description: Use Amazon Nova Reel to generate a video from a text prompt.
1513+
snippet_tags:
1514+
- swift.example_code.bedrock-runtime.Scenario_AmazonNova_TextToVideo
15071515
services:
15081516
bedrock-runtime: {GetAsyncInvoke, StartAsyncInvoke}
15091517

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 Reel
40+
41+
- [Text-to-video](models/amazon-nova/amazon-nova-reel/Sources/main.swift#L4)
42+
3943
### Anthropic Claude
4044

4145
- [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: "AmazonNovaVideo",
11+
platforms: [
12+
.macOS(.v13),
13+
.iOS(.v15)
14+
],
15+
dependencies: [
16+
// Dependencies declare other packages that this package depends on.
17+
.package(url: "https://github.com/awslabs/aws-sdk-swift", from: "1.2.61"),
18+
.package(url: "https://github.com/smithy-lang/smithy-swift", from: "0.118.0")
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: "TextToVideo",
25+
dependencies: [
26+
.product(name: "AWSBedrockRuntime", package: "aws-sdk-swift"),
27+
.product(name: "Smithy", package: "smithy-swift")
28+
]
29+
)
30+
]
31+
)
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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.Scenario_AmazonNova_TextToVideo]
5+
// This example demonstrates how to use Amazon Nova Reel to generate a video from a text prompt.
6+
// It shows how to:
7+
// - Set up the Amazon Bedrock runtime client
8+
// - Configure a text-to-video request
9+
// - Submit an asynchronous job for video generation
10+
// - Poll for job completion status
11+
// - Access the generated video from S3
12+
13+
import AWSBedrockRuntime
14+
import Foundation
15+
import Smithy
16+
17+
func startTextToVideoGenerationJob(
18+
bedrockRuntimeClient: BedrockRuntimeClient, prompt: String, outputS3Uri: String
19+
) async throws -> String? {
20+
// Specify the model ID for text-to-video generation
21+
let modelId = "amazon.nova-reel-v1:0"
22+
23+
// Configure the video generation request with additional parameters
24+
let modelInputSource: [String: Any] = [
25+
"taskType": "TEXT_VIDEO",
26+
"textToVideoParams": [
27+
"text": "\(prompt)"
28+
],
29+
"videoGenerationConfig": [
30+
"durationSeconds": 6,
31+
"fps": 24,
32+
"dimension": "1280x720",
33+
],
34+
]
35+
36+
let modelInput = try Document.make(from: modelInputSource)
37+
38+
let input = StartAsyncInvokeInput(
39+
modelId: modelId,
40+
modelInput: modelInput,
41+
outputDataConfig: .s3outputdataconfig(
42+
BedrockRuntimeClientTypes.AsyncInvokeS3OutputDataConfig(
43+
s3Uri: outputS3Uri
44+
)
45+
)
46+
)
47+
48+
// Invoke the model asynchronously
49+
let output = try await bedrockRuntimeClient.startAsyncInvoke(input: input)
50+
return output.invocationArn
51+
}
52+
53+
func queryJobStatus(
54+
bedrockRuntimeClient: BedrockRuntimeClient,
55+
invocationArn: String?
56+
) async throws -> GetAsyncInvokeOutput {
57+
try await bedrockRuntimeClient.getAsyncInvoke(
58+
input: GetAsyncInvokeInput(invocationArn: invocationArn))
59+
}
60+
61+
func main() async throws {
62+
// Create a Bedrock Runtime client
63+
let config =
64+
try await BedrockRuntimeClient.BedrockRuntimeClientConfiguration(
65+
region: "us-east-1"
66+
)
67+
let client = BedrockRuntimeClient(config: config)
68+
69+
// Specify the S3 location for the output video
70+
let bucket = "s3://REPLACE-WITH-YOUR-S3-BUCKET-NAM"
71+
72+
print("Submitting video generation job...")
73+
let invocationArn = try await startTextToVideoGenerationJob(
74+
bedrockRuntimeClient: client,
75+
prompt: "A pomegranate juice in a railway station",
76+
outputS3Uri: bucket
77+
)
78+
print("Job started with invocation ARN: \(String(describing:invocationArn))")
79+
80+
// Poll for job completion
81+
var status: BedrockRuntimeClientTypes.AsyncInvokeStatus?
82+
var isReady = false
83+
var hasFailed = false
84+
85+
while !isReady && !hasFailed {
86+
print("\nPolling job status...")
87+
status = try await queryJobStatus(
88+
bedrockRuntimeClient: client, invocationArn: invocationArn
89+
).status
90+
switch status {
91+
case .completed:
92+
isReady = true
93+
print("Video is ready\nCheck S3 bucket: \(bucket)")
94+
case .failed:
95+
hasFailed = true
96+
print("Something went wrong")
97+
case .inProgress:
98+
print("Job is in progress...")
99+
try await Task.sleep(nanoseconds: 15 * 1_000_000_000) // 15 seconds
100+
default:
101+
isReady = true
102+
}
103+
}
104+
}
105+
106+
do {
107+
try await main()
108+
} catch {
109+
print("An error occurred: \(error)")
110+
}
111+
112+
// snippet-end:[swift.example_code.bedrock-runtime.Scenario_AmazonNova_TextToVideo]

0 commit comments

Comments
 (0)