Titan Images
Unstable API
0.7.0
@project-lakechain/bedrock-image-generators
The Bedrock image generators package enables developers to use image generation models hosted on Amazon Bedrock, and create images at scale within their pipelines. The Amazon Titan image generator is part of this package and makes it possible to generate images from text, realize image inpainting and outpainting, as well as image-to-image variation.
πΌοΈ Text-to-Image
To generate images as part of your pipelines from a text prompt, you can use the TitanImageGenerator
construct. This construct either takes the content of input text document as a prompt for generating an image, allowing you to nicely chain documents together, or can also take a user-provided prompt.
βΉοΈ The below example demonstrates how to use the Titan image generator to create images from an arbitrary prompt every 5 minutes, using the Scheduler Trigger.
import * as scheduler from '@aws-cdk/aws-scheduler-alpha';import { SchedulerEventTrigger } from '@project-lakechain/scheduler-event-trigger';import { SdxlImageGenerator } from '@project-lakechain/bedrock-image-generators';
class Stack extends cdk.Stack { constructor(scope: cdk.Construct, id: string) { const trigger = new SchedulerEventTrigger.Builder() .withScope(this) .withIdentifier('Trigger') .withScheduleExpression( scheduler.ScheduleExpression.rate(cdk.Duration.minutes(5)) ) .build();
// Every 5 minutes, generate a new image. trigger.pipe(new TitanImageGenerator.Builder() .withScope(this) .withIdentifier('TitanImageGenerator') .withCacheStorage(cache) .withSource(trigger) .withTask(new TextToImageTask.Builder() .withPrompt('A beautiful sunset over the ocean.') .withNegativePrompt('low resolution, low quality') .build() ) .build()); }}
π§βπ¨ Image Inpainting
The Titan model supports image inpainting by modifying a specific area of an image delimited by a mask, with an AI generated image. To implement inpainting within a pipeline, you can use the TitanImageGenerator
by specifying a source image and a mask prompt. Mask prompts identify the area to be painted in natural language, such as βhouseβ or βwindowβ.
π In the below example we create a pipeline that triggers when an image is uploaded to a source S3 bucket, and replace any βhouseβ detected in the image by a βModern houseβ.
Original Image | Inpainted Image |
---|---|
![]() | ![]() |
Example
import { TitanImageGenerator, ImageInpaintingTask } from '@project-lakechain/bedrock-image-generators';
const imageGenerator = new TitanImageGenerator.Builder() .withScope(this) .withIdentifier('ImageGenerator') .withCacheStorage(cache) .withSource(source) // π Specify a data source. .withTask(new ImageInpaintingTask.Builder() .withTextPrompt('Modern house') .withMaskPrompt('house') .build() ) .build()
π¨ Image Outpainting
The Amazon Titan image model also supports outpainting, relative to a mask prompt. Image outpainting refers to painting an existing image outside of the area delimited by the mask image.
π In the below example we create a pipeline that triggers when an image is uploaded to a source S3 bucket, and replaces the environment outside of a βhouseβ with a βbeautiful garden and swimming poolβ.
Original Image | Outpainted Image |
---|---|
![]() | ![]() |
Example
import { TitanImageGenerator, ImageOutpaintingTask } from '@project-lakechain/bedrock-image-generators';
const imageGenerator = new TitanImageGenerator.Builder() .withScope(this) .withIdentifier('ImageGenerator') .withCacheStorage(cache) .withSource(source) // π Specify a data source. .withTask(new ImageOutpaintingTask.Builder() .withTextPrompt('Beautiful garden and swimming pool') .withMaskPrompt('house') .build() ) .build();
β¨ Image Variation
The Titan model also supports taking an image as an input, and transforming that image using a textual prompt into a new image. To perform image variation, you use the TitanImageGenerator
and provide it with a ImageVariationTask
.
Example
import { TitanImageGenerator, ImageVariationTask } from '@project-lakechain/bedrock-image-generators';
const imageGenerator = new TitanImageGenerator.Builder() .withScope(this) .withIdentifier('ImageGenerator') .withCacheStorage(cache) .withSource(trigger) .withSource(source) // π Specify a data source. .withTask(new ImageVariationTask.Builder() .withTextPrompt('A cat smiling') // Reference the current document as the image to transform. .withImage(r.reference(r.document())) .build() ) .build();
π Region Selection
You can specify the AWS region in which you want to invoke Amazon Bedrock using the .withRegion
API.
π By default, the middleware will use the current region in which it is deployed.
import { TitanImageGenerator } from '@project-lakechain/bedrock-image-generators';
const imageGenerator = new TitanImageGenerator.Builder() .withScope(this) .withIdentifier('ImageGenerator') .withCacheStorage(cache) .withSource(trigger) .withRegion('us-east-1') // π Alternate region .withTask(task) .build();
βοΈ Model Parameters
For the different tasks supported by the TitanImageGenerator
, you can specify additional parameters to customize the generation of the images. You pass additional parameters using the .withImageGenerationParameters
API on the task to execute.
import { TitanImageGenerator, ImageOutpaintingTask, ImageGenerationParameters} from '@project-lakechain/bedrock-image-generators';
const imageGenerator = new TitanImageGenerator.Builder() .withScope(this) .withIdentifier('ImageGenerator') .withCacheStorage(cache) .withSource(source) .withTask(new ImageOutpaintingTask.Builder() .withTextPrompt('Beautiful garden and swimming pool') .withMaskPrompt('house') .withImageGenerationParameters(new ImageGenerationParameters.Builder() .withQuality('premium') .withNumberOfImages(10) .build() ) .build() ) .build();
Parameters
Below are the parameters you can pass to the image generation tasks. See the official Titan documentation to understand the effect of available parameters.
Parameter | Description | Default |
---|---|---|
numberOfImages | The number of images to generate. | 1 |
quality | The quality of the generated images. | standard |
cfgScale | Specifies how strongly the generated image should adhere to the prompt. | 8.0 |
height | The height of the generated image. | 1024 |
width | The width of the generated image. | 1024 |
seed | Determines the initial noise setting. | 0 |
ποΈ Architecture
This middleware is based on a Lambda compute and interacts with the Amazon Bedrock service in the specified region to generate images.
π·οΈ Properties
Supported Inputs
Mime Type | Description |
---|---|
text/plain | Text document |
image/png | Image document |
image/jpeg | Image document |
application/json+scheduler | Scheduler event |
Supported Outputs
Mime Type | Description |
---|---|
image/png | Image document |
Supported Compute Types
Type | Description |
---|---|
CPU | This middleware only supports CPU compute. |
π Examples
- Titan Inpainting Pipeline - An example showcasing how to perform image inpainting using Amazon Titan.
- Image Outpainting Pipeline - An example showcasing how to perform image outpainting using Amazon Titan.