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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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]