|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package com.example.novareel; |
| 5 | + |
| 6 | +// snippet-start:[bedrock-runtime.java2.NovaReel.VideoGeneration] |
| 7 | +import org.springframework.stereotype.Service; |
| 8 | +import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider; |
| 9 | +import software.amazon.awssdk.core.document.Document; |
| 10 | +import software.amazon.awssdk.regions.Region; |
| 11 | +import software.amazon.awssdk.services.bedrockruntime.BedrockRuntimeAsyncClient; |
| 12 | +import software.amazon.awssdk.services.bedrockruntime.model.*; |
| 13 | + |
| 14 | +import java.util.concurrent.CompletableFuture; |
| 15 | + |
| 16 | +@Service |
| 17 | +public class VideoGenerationService { |
| 18 | + |
| 19 | + public GenerateVideoResponse generateVideo(String prompt) { |
| 20 | + |
| 21 | + // add S3 bucket you want to store your generated videos |
| 22 | + String s3Bucket = "s3://mygeneratedvidoenovatest"; |
| 23 | + |
| 24 | + |
| 25 | + //Create json request as an instance of Document class |
| 26 | + Document novaRequest = prepareDocument(prompt); |
| 27 | + |
| 28 | + // Create request |
| 29 | + StartAsyncInvokeRequest request = StartAsyncInvokeRequest.builder() |
| 30 | + .modelId("amazon.nova-reel-v1:0") |
| 31 | + .modelInput(novaRequest) |
| 32 | + .outputDataConfig(AsyncInvokeOutputDataConfig.builder() |
| 33 | + .s3OutputDataConfig(AsyncInvokeS3OutputDataConfig.builder().s3Uri(s3Bucket).build()) |
| 34 | + .build()) |
| 35 | + .build(); |
| 36 | + |
| 37 | + try (BedrockRuntimeAsyncClient bedrockClient = getBedrockRuntimeAsyncClient()) { |
| 38 | + CompletableFuture<StartAsyncInvokeResponse> startAsyncInvokeResponseCompletableFuture = bedrockClient.startAsyncInvoke(request); |
| 39 | + |
| 40 | + //blocking operation to wait for the AWS API response |
| 41 | + StartAsyncInvokeResponse startAsyncInvokeResponse = startAsyncInvokeResponseCompletableFuture.get(); |
| 42 | + System.out.println("invocation ARN: " + startAsyncInvokeResponse.invocationArn()); |
| 43 | + |
| 44 | + GenerateVideoResponse response = new GenerateVideoResponse(); |
| 45 | + response.setStatus("inProgress"); |
| 46 | + response.setExecutionArn(startAsyncInvokeResponse.invocationArn()); |
| 47 | + |
| 48 | + return response; |
| 49 | + } catch (Exception e) { |
| 50 | + System.out.println(e); |
| 51 | + throw new RuntimeException(e); |
| 52 | + } |
| 53 | + |
| 54 | + } |
| 55 | + |
| 56 | + public GenerateVideoResponse checkGenerationStatus(String invocationArn) { |
| 57 | + GenerateVideoResponse response = new GenerateVideoResponse(); |
| 58 | + |
| 59 | + try (BedrockRuntimeAsyncClient bedrockClient = getBedrockRuntimeAsyncClient()) { |
| 60 | + //creating async request to fetch status by invocation Arn |
| 61 | + GetAsyncInvokeRequest asyncRequest = GetAsyncInvokeRequest.builder().invocationArn(invocationArn).build(); |
| 62 | + |
| 63 | + CompletableFuture<GetAsyncInvokeResponse> asyncInvoke = bedrockClient.getAsyncInvoke(asyncRequest); |
| 64 | + |
| 65 | + //blocking operation to wait for the AWS API response |
| 66 | + GetAsyncInvokeResponse asyncInvokeResponse = asyncInvoke.get(); |
| 67 | + System.out.println("Invocation status =" + asyncInvokeResponse.statusAsString()); |
| 68 | + |
| 69 | + response.setExecutionArn(invocationArn); |
| 70 | + response.setStatus(asyncInvokeResponse.statusAsString()); |
| 71 | + return response; |
| 72 | + } catch (Exception e) { |
| 73 | + e.printStackTrace(); |
| 74 | + throw new RuntimeException(e); |
| 75 | + } |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + private static BedrockRuntimeAsyncClient getBedrockRuntimeAsyncClient() { |
| 80 | + BedrockRuntimeAsyncClient bedrockClient = BedrockRuntimeAsyncClient.builder() |
| 81 | + .region(Region.US_EAST_1) |
| 82 | + .credentialsProvider(ProfileCredentialsProvider.create()) |
| 83 | + .build(); |
| 84 | + return bedrockClient; |
| 85 | + } |
| 86 | + |
| 87 | + private static Document prepareDocument(String prompt) { |
| 88 | + Document textToVideoParams = Document.mapBuilder() |
| 89 | + .putString("text", prompt) |
| 90 | + .build(); |
| 91 | + |
| 92 | + Document videoGenerationConfig = Document.mapBuilder() |
| 93 | + .putNumber("durationSeconds", 6) |
| 94 | + .putNumber("fps", 24) |
| 95 | + .putString("dimension", "1280x720") |
| 96 | + .build(); |
| 97 | + |
| 98 | + Document novaRequest = Document.mapBuilder() |
| 99 | + .putString("taskType", "TEXT_VIDEO") |
| 100 | + .putDocument("textToVideoParams", textToVideoParams) |
| 101 | + .putDocument("videoGenerationConfig", videoGenerationConfig) |
| 102 | + .build(); |
| 103 | + return novaRequest; |
| 104 | + } |
| 105 | +} |
| 106 | +// snippet-end:[bedrock-runtime.java2.NovaReel.VideoGeneration] |
0 commit comments