-
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
Draft
monadierickx
wants to merge
3
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.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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.