|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package com.example.bedrockruntime.models.amazon.nova.canvas |
| 5 | + |
| 6 | +// snippet-start:[bedrock-runtime.kotlin.InvokeModel_AmazonNovaImageGeneration] |
| 7 | + |
| 8 | +import aws.sdk.kotlin.services.bedrockruntime.BedrockRuntimeClient |
| 9 | +import aws.sdk.kotlin.services.bedrockruntime.model.InvokeModelRequest |
| 10 | +import com.example.bedrockruntime.libs.ImageTools.displayImage |
| 11 | +import kotlinx.serialization.Serializable |
| 12 | +import kotlinx.serialization.json.Json |
| 13 | +import java.util.* |
| 14 | + |
| 15 | +/** |
| 16 | + * This example demonstrates how to use Amazon Nova Canvas to generate images. |
| 17 | + * It shows how to: |
| 18 | + * - Set up the Amazon Bedrock runtime client |
| 19 | + * - Configure the image generation parameters |
| 20 | + * - Send a request to generate an image |
| 21 | + * - Process the response and display the generated image |
| 22 | + */ |
| 23 | +suspend fun main() { |
| 24 | + println("Generating image. This may take a few seconds...") |
| 25 | + val imageData = invokeModel() |
| 26 | + displayImage(imageData) |
| 27 | +} |
| 28 | + |
| 29 | +// Data class for parsing the model's response |
| 30 | +@Serializable |
| 31 | +private data class Response(val images: List<String>) |
| 32 | + |
| 33 | +// Configure JSON parser to ignore unknown fields in the response |
| 34 | +private val json = Json { ignoreUnknownKeys = true } |
| 35 | + |
| 36 | +suspend fun invokeModel(): ByteArray { |
| 37 | + // Create and configure the Bedrock runtime client |
| 38 | + BedrockRuntimeClient { region = "us-east-1" }.use { client -> |
| 39 | + |
| 40 | + // Specify the model ID. For the latest available models, see: |
| 41 | + // https://docs.aws.amazon.com/bedrock/latest/userguide/models-supported.html |
| 42 | + val modelId = "amazon.nova-canvas-v1:0" |
| 43 | + |
| 44 | + // Configure the generation parameters and create the request |
| 45 | + // First, set the main parameters: |
| 46 | + // - prompt: Text description of the image to generate |
| 47 | + // - seed: Random number for reproducible generation (0 to 858,993,459) |
| 48 | + val prompt = "A stylized picture of a cute old steampunk robot" |
| 49 | + val seed = (0..858_993_459).random() |
| 50 | + |
| 51 | + // Then, create the request using a template with the following structure: |
| 52 | + // - taskType: TEXT_IMAGE (specifies text-to-image generation) |
| 53 | + // - textToImageParams: Contains the text prompt |
| 54 | + // - imageGenerationConfig: Contains optional generation settings (seed, quality, etc.) |
| 55 | + // For a list of available request parameters, see: |
| 56 | + // https://docs.aws.amazon.com/nova/latest/userguide/image-gen-req-resp-structure.html |
| 57 | + val request = """ |
| 58 | + { |
| 59 | + "taskType": "TEXT_IMAGE", |
| 60 | + "textToImageParams": { |
| 61 | + "text": "$prompt" |
| 62 | + }, |
| 63 | + "imageGenerationConfig": { |
| 64 | + "seed": $seed, |
| 65 | + "quality": "standard" |
| 66 | + } |
| 67 | + } |
| 68 | + """.trimIndent() |
| 69 | + |
| 70 | + // Send the request and process the model's response |
| 71 | + runCatching { |
| 72 | + // Send the request to the model |
| 73 | + val response = client.invokeModel( |
| 74 | + InvokeModelRequest { |
| 75 | + this.modelId = modelId |
| 76 | + body = request.toByteArray() |
| 77 | + }, |
| 78 | + ) |
| 79 | + |
| 80 | + // Parse the response and extract the generated image |
| 81 | + val jsonResponse = response.body.toString(Charsets.UTF_8) |
| 82 | + val parsedResponse = json.decodeFromString<Response>(jsonResponse) |
| 83 | + |
| 84 | + // Extract the generated image and return it as a byte array for better handling |
| 85 | + val base64Image = parsedResponse.images.first() |
| 86 | + return Base64.getDecoder().decode(base64Image) |
| 87 | + }.getOrElse { error -> |
| 88 | + System.err.println("ERROR: Can't invoke '$modelId'. Reason: ${error.message}") |
| 89 | + throw RuntimeException("Failed to generate image with model $modelId", error) |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +// snippet-end:[bedrock-runtime.kotlin.InvokeModel_AmazonNovaImageGeneration] |
0 commit comments