Skip to content

Swift: Added video generation example code for Nova Reel, Amazon Bedrock #7459

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 33 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
23e27c6
nova reel example code
monadierickx May 24, 2025
570ec9f
mistake fixed
monadierickx May 25, 2025
6850f5f
Regenerate documentation
monadierickx May 25, 2025
91874c7
mistake fixed
monadierickx Jun 16, 2025
22bde58
Fix SoS tags for example for Swift (#7436)
shepazon May 19, 2025
10793d0
Swift: Bedrock-Runtime Readme - add custom prerequisite (#7453)
DennisTraub May 20, 2025
829f0af
Update to latest tools release 2025.18.3. (#7441)
Laren-AWS May 20, 2025
526eb86
Bedrock Runtime: Remove explicit references to Llama 3 (#7444)
DennisTraub May 20, 2025
28a58ad
Bump aws-cdk-lib from 2.189.0 to 2.189.1 in /resources/cdk/cognito_sc…
dependabot[bot] May 21, 2025
e76ed29
Python: Bedrock Runtime document understanding examples (#7446)
DennisTraub May 21, 2025
8f0cbd7
Update to latest tools release 2025.20.1 (#7457)
Laren-AWS May 23, 2025
bfd4aef
Update README.md - fixing bedrock runtime crate link (#7461)
martin-conur May 27, 2025
a1199b9
Update to latest tools release 2025.1.0 (#7463)
Laren-AWS May 29, 2025
944acfd
Java V2: add existing hard-coded example for the SQS extended client …
tkhill-AWS May 29, 2025
e5f22e5
Java V2 added SES example that shows new header information (#7430)
scmacdon May 29, 2025
927ce73
PHP: Add example for Amazon Nova text models, remove example for Jura…
DennisTraub May 29, 2025
0d7d171
Update to latest tools release 2025.21.1. (#7464)
Laren-AWS May 30, 2025
6fa1c49
Bump axios and @ailly/cli in /.tools/ailly (#7465)
dependabot[bot] Jun 2, 2025
b8974fb
Entity resolution js (#7438)
brmur Jun 2, 2025
776b683
Bump org.springframework:spring-context from 6.1.14 to 6.1.20 in /jav…
dependabot[bot] Jun 3, 2025
31ee18a
Bump degenerator and aws-cdk in /resources/cdk/rekognition-sns-video-…
dependabot[bot] Jun 3, 2025
004db83
Bump axios and @ailly/core in /.tools/ailly (#7466)
dependabot[bot] Jun 3, 2025
c6e7ead
Bump braces from 3.0.2 to 3.0.3 in /gov2/workflows/user_pools_and_lam…
dependabot[bot] Jun 3, 2025
6f50260
Bump braces from 3.0.2 to 3.0.3 in /.tools/test/stacks/plugin/typescr…
dependabot[bot] Jun 4, 2025
f18cd3b
Java V2 Add Neptune Basics (#7462)
scmacdon Jun 5, 2025
5c2bb58
Bump rack from 3.1.14 to 3.1.16 in /ruby (#7471)
dependabot[bot] Jun 6, 2025
dc8d4cf
Swift: code examples for Meta Llama to use Converse and ConverseStrea…
monadierickx Jun 6, 2025
954864e
Bump com.fasterxml.jackson.core:jackson-core from 2.12.5 to 2.13.0 in…
dependabot[bot] Jun 9, 2025
d4f8051
Bump requests from 2.31.0 to 2.32.4 in /python/example_code/bedrock-a…
dependabot[bot] Jun 10, 2025
f6b19b2
Bump requests from 2.32.0 to 2.32.4 in /.tools (#7478)
dependabot[bot] Jun 10, 2025
7a37a05
Fixed a minor bug by updating a SOS tag in the EC2 Yaml file (#7476)
scmacdon Jun 10, 2025
b24b403
Fix description in JS scenario (#7481)
brmur Jun 12, 2025
e47606e
Merge branch 'main' into nova-video
monadierickx Jun 16, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .doc_gen/metadata/bedrock-runtime_metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1488,6 +1488,14 @@ bedrock-runtime_Scenario_AmazonNova_TextToVideo:
- description: Use Amazon Nova Reel to generate a video from a text prompt.
snippet_tags:
- python.example_code.bedrock-runtime.Scenario_AmazonNova_TextToVideo
Swift:
versions:
- sdk_version: 1
github: swift/example_code/bedrock-runtime
excerpts:
- description: Use Amazon Nova Reel to generate a video from a text prompt.
snippet_tags:
- swift.example_code.bedrock-runtime.Scenario_AmazonNova_TextToVideo
services:
bedrock-runtime: {GetAsyncInvoke, StartAsyncInvoke}

Expand Down
4 changes: 4 additions & 0 deletions swift/example_code/bedrock-runtime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ For prerequisites, see the [README](../../README.md#Prerequisites) in the `swift
- [Converse](models/amazon-nova/amazon-nova-text/Sources/Converse/main.swift#L4)
- [ConverseStream](models/amazon-nova/amazon-nova-text/Sources/ConverseStream/main.swift#L4)

### Amazon Nova Reel

- [Text-to-video](models/amazon-nova/amazon_nova_reel/Sources/main.swift#L4)

### Anthropic Claude

- [Converse](models/anthropic_claude/Sources/Converse/main.swift#L4)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// swift-tools-version: 6.1
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
//
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "AmazonNovaVideo",
platforms: [
.macOS(.v13),
.iOS(.v15)
],
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(url: "https://github.com/awslabs/aws-sdk-swift", from: "1.2.61"),
.package(url: "https://github.com/smithy-lang/smithy-swift", from: "0.118.0")
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
// Targets can depend on other targets in this package and products from dependencies.
.executableTarget(
name: "TextToVideo",
dependencies: [
.product(name: "AWSBedrockRuntime", package: "aws-sdk-swift"),
.product(name: "Smithy", package: "smithy-swift")
]
)
]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,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(f"Job started with invocation ARN: {invocation_arn}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This syntax doesn't work. Isn't this a Python syntax? Shouldn't this instead be print("Job started with invocation ARN: \(invocation_arn)")?

If it works for you, I'd be interested to know how, since I get syntax errors on these lines:

 76 |         outputS3Uri: bucket
 77 |     )
 78 |     print(f"Job started with invocation ARN: {invocation_arn}")
    |            `- error: expected ',' separator
 79 |
 80 |     // Poll for job completion

/Users/ericsh/tmp/reel/Sources/main.swift:78:11: error: cannot find 'f' in scope

 76 |         outputS3Uri: bucket
 77 |     )
 78 |     print(f"Job started with invocation ARN: {invocation_arn}")
    |           `- error: cannot find 'f' in scope
 79 |
 80 |     // Poll for job completion

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once this issue is fixed, LGTM.


// 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]