-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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
monadierickx
wants to merge
33
commits into
awsdocs:main
Choose a base branch
from
monadierickx:nova-video
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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 570ec9f
mistake fixed
monadierickx 6850f5f
Regenerate documentation
monadierickx 91874c7
mistake fixed
monadierickx 22bde58
Fix SoS tags for example for Swift (#7436)
shepazon 10793d0
Swift: Bedrock-Runtime Readme - add custom prerequisite (#7453)
DennisTraub 829f0af
Update to latest tools release 2025.18.3. (#7441)
Laren-AWS 526eb86
Bedrock Runtime: Remove explicit references to Llama 3 (#7444)
DennisTraub 28a58ad
Bump aws-cdk-lib from 2.189.0 to 2.189.1 in /resources/cdk/cognito_sc…
dependabot[bot] e76ed29
Python: Bedrock Runtime document understanding examples (#7446)
DennisTraub 8f0cbd7
Update to latest tools release 2025.20.1 (#7457)
Laren-AWS bfd4aef
Update README.md - fixing bedrock runtime crate link (#7461)
martin-conur a1199b9
Update to latest tools release 2025.1.0 (#7463)
Laren-AWS 944acfd
Java V2: add existing hard-coded example for the SQS extended client …
tkhill-AWS e5f22e5
Java V2 added SES example that shows new header information (#7430)
scmacdon 927ce73
PHP: Add example for Amazon Nova text models, remove example for Jura…
DennisTraub 0d7d171
Update to latest tools release 2025.21.1. (#7464)
Laren-AWS 6fa1c49
Bump axios and @ailly/cli in /.tools/ailly (#7465)
dependabot[bot] b8974fb
Entity resolution js (#7438)
brmur 776b683
Bump org.springframework:spring-context from 6.1.14 to 6.1.20 in /jav…
dependabot[bot] 31ee18a
Bump degenerator and aws-cdk in /resources/cdk/rekognition-sns-video-…
dependabot[bot] 004db83
Bump axios and @ailly/core in /.tools/ailly (#7466)
dependabot[bot] c6e7ead
Bump braces from 3.0.2 to 3.0.3 in /gov2/workflows/user_pools_and_lam…
dependabot[bot] 6f50260
Bump braces from 3.0.2 to 3.0.3 in /.tools/test/stacks/plugin/typescr…
dependabot[bot] f18cd3b
Java V2 Add Neptune Basics (#7462)
scmacdon 5c2bb58
Bump rack from 3.1.14 to 3.1.16 in /ruby (#7471)
dependabot[bot] dc8d4cf
Swift: code examples for Meta Llama to use Converse and ConverseStrea…
monadierickx 954864e
Bump com.fasterxml.jackson.core:jackson-core from 2.12.5 to 2.13.0 in…
dependabot[bot] d4f8051
Bump requests from 2.31.0 to 2.32.4 in /python/example_code/bedrock-a…
dependabot[bot] f6b19b2
Bump requests from 2.32.0 to 2.32.4 in /.tools (#7478)
dependabot[bot] 7a37a05
Fixed a minor bug by updating a SOS tag in the EC2 Yaml file (#7476)
scmacdon b24b403
Fix description in JS scenario (#7481)
brmur e47606e
Merge branch 'main' into nova-video
monadierickx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
swift/example_code/bedrock-runtime/models/amazon-nova/amazon_nova_reel/Package.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
] | ||
) | ||
] | ||
) |
112 changes: 112 additions & 0 deletions
112
swift/example_code/bedrock-runtime/models/amazon-nova/amazon_nova_reel/Sources/main.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}") | ||
|
||
// 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] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
There was a problem hiding this comment.
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.