|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +// |
| 4 | +// snippet-start:[swift.example_code.bedrock-runtime.Scenario_AmazonNova_TextToVideo] |
| 5 | +// This example demonstrates how to use Amazon Nova Reel to generate a video from a text prompt. |
| 6 | +// It shows how to: |
| 7 | +// - Set up the Amazon Bedrock runtime client |
| 8 | +// - Configure a text-to-video request |
| 9 | +// - Submit an asynchronous job for video generation |
| 10 | +// - Poll for job completion status |
| 11 | +// - Access the generated video from S3 |
| 12 | + |
| 13 | +import AWSBedrockRuntime |
| 14 | +import Foundation |
| 15 | +import Smithy |
| 16 | + |
| 17 | +func startTextToVideoGenerationJob( |
| 18 | + bedrockRuntimeClient: BedrockRuntimeClient, prompt: String, outputS3Uri: String |
| 19 | +) async throws -> String? { |
| 20 | + // Specify the model ID for text-to-video generation |
| 21 | + let modelId = "amazon.nova-reel-v1:0" |
| 22 | + |
| 23 | + // Configure the video generation request with additional parameters |
| 24 | + let modelInputSource: [String: Any] = [ |
| 25 | + "taskType": "TEXT_VIDEO", |
| 26 | + "textToVideoParams": [ |
| 27 | + "text": "\(prompt)" |
| 28 | + ], |
| 29 | + "videoGenerationConfig": [ |
| 30 | + "durationSeconds": 6, |
| 31 | + "fps": 24, |
| 32 | + "dimension": "1280x720", |
| 33 | + ], |
| 34 | + ] |
| 35 | + |
| 36 | + let modelInput = try Document.make(from: modelInputSource) |
| 37 | + |
| 38 | + let input = StartAsyncInvokeInput( |
| 39 | + modelId: modelId, |
| 40 | + modelInput: modelInput, |
| 41 | + outputDataConfig: .s3outputdataconfig( |
| 42 | + BedrockRuntimeClientTypes.AsyncInvokeS3OutputDataConfig( |
| 43 | + s3Uri: outputS3Uri |
| 44 | + ) |
| 45 | + ) |
| 46 | + ) |
| 47 | + |
| 48 | + // Invoke the model asynchronously |
| 49 | + let output = try await bedrockRuntimeClient.startAsyncInvoke(input: input) |
| 50 | + return output.invocationArn |
| 51 | +} |
| 52 | + |
| 53 | +func queryJobStatus( |
| 54 | + bedrockRuntimeClient: BedrockRuntimeClient, |
| 55 | + invocationArn: String? |
| 56 | +) async throws -> GetAsyncInvokeOutput { |
| 57 | + try await bedrockRuntimeClient.getAsyncInvoke( |
| 58 | + input: GetAsyncInvokeInput(invocationArn: invocationArn)) |
| 59 | +} |
| 60 | + |
| 61 | +func main() async throws { |
| 62 | + // Create a Bedrock Runtime client |
| 63 | + let config = |
| 64 | + try await BedrockRuntimeClient.BedrockRuntimeClientConfiguration( |
| 65 | + region: "us-east-1" |
| 66 | + ) |
| 67 | + let client = BedrockRuntimeClient(config: config) |
| 68 | + |
| 69 | + // Specify the S3 location for the output video |
| 70 | + let bucket = "s3://REPLACE-WITH-YOUR-S3-BUCKET-NAM" |
| 71 | + |
| 72 | + print("Submitting video generation job...") |
| 73 | + let invocationArn = try await startTextToVideoGenerationJob( |
| 74 | + bedrockRuntimeClient: client, |
| 75 | + prompt: "A pomegranate juice in a railway station", |
| 76 | + outputS3Uri: bucket |
| 77 | + ) |
| 78 | + print("Job started with invocation ARN: \(String(describing:invocationArn))") |
| 79 | + |
| 80 | + // Poll for job completion |
| 81 | + var status: BedrockRuntimeClientTypes.AsyncInvokeStatus? |
| 82 | + var isReady = false |
| 83 | + var hasFailed = false |
| 84 | + |
| 85 | + while !isReady && !hasFailed { |
| 86 | + print("\nPolling job status...") |
| 87 | + status = try await queryJobStatus( |
| 88 | + bedrockRuntimeClient: client, invocationArn: invocationArn |
| 89 | + ).status |
| 90 | + switch status { |
| 91 | + case .completed: |
| 92 | + isReady = true |
| 93 | + print("Video is ready\nCheck S3 bucket: \(bucket)") |
| 94 | + case .failed: |
| 95 | + hasFailed = true |
| 96 | + print("Something went wrong") |
| 97 | + case .inProgress: |
| 98 | + print("Job is in progress...") |
| 99 | + try await Task.sleep(nanoseconds: 15 * 1_000_000_000) // 15 seconds |
| 100 | + default: |
| 101 | + isReady = true |
| 102 | + } |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +do { |
| 107 | + try await main() |
| 108 | +} catch { |
| 109 | + print("An error occurred: \(error)") |
| 110 | +} |
| 111 | + |
| 112 | +// snippet-end:[swift.example_code.bedrock-runtime.Scenario_AmazonNova_TextToVideo] |
0 commit comments