diff --git a/.doc_gen/metadata/bedrock-runtime_metadata.yaml b/.doc_gen/metadata/bedrock-runtime_metadata.yaml index b8ebf05e497..428df053c46 100644 --- a/.doc_gen/metadata/bedrock-runtime_metadata.yaml +++ b/.doc_gen/metadata/bedrock-runtime_metadata.yaml @@ -1504,6 +1504,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} diff --git a/swift/example_code/bedrock-runtime/README.md b/swift/example_code/bedrock-runtime/README.md index 73ada678fec..433be634504 100644 --- a/swift/example_code/bedrock-runtime/README.md +++ b/swift/example_code/bedrock-runtime/README.md @@ -36,6 +36,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) diff --git a/swift/example_code/bedrock-runtime/models/amazon-nova/amazon-nova-reel/Package.swift b/swift/example_code/bedrock-runtime/models/amazon-nova/amazon-nova-reel/Package.swift new file mode 100644 index 00000000000..2ac9ca5d756 --- /dev/null +++ b/swift/example_code/bedrock-runtime/models/amazon-nova/amazon-nova-reel/Package.swift @@ -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") + ] + ) + ] +) diff --git a/swift/example_code/bedrock-runtime/models/amazon-nova/amazon-nova-reel/Sources/main.swift b/swift/example_code/bedrock-runtime/models/amazon-nova/amazon-nova-reel/Sources/main.swift new file mode 100644 index 00000000000..9ea24bbc452 --- /dev/null +++ b/swift/example_code/bedrock-runtime/models/amazon-nova/amazon-nova-reel/Sources/main.swift @@ -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("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]