Skip to content

Commit

Permalink
feat: nova canvas
Browse files Browse the repository at this point in the history
  • Loading branch information
yuukiok committed Dec 11, 2024
1 parent b41eb70 commit 185d98b
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 16 deletions.
1 change: 1 addition & 0 deletions docs/DEPLOY_OPTION.md
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@ Prompt optimization のサポート状況は [こちら](https://docs.aws.amazon
"imageGenerationModelIds": [
"amazon.titan-image-generator-v2:0",
"amazon.titan-image-generator-v1",
"amazon.nova-canvas-v1:0",
"stability.stable-diffusion-xl-v1"
],
```
Expand Down
22 changes: 13 additions & 9 deletions packages/cdk/lambda/utils/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
Model,
PromptTemplate,
StableDiffusionParams,
TitanImageParams,
AmazonImageParams,
UnrecordedMessage,
ConverseInferenceParams,
UsecaseConverseInferenceParams,
Expand Down Expand Up @@ -444,7 +444,7 @@ const createBodyImageStabilityAI2024Model = (params: GenerateImageParams) => {
return JSON.stringify(body);
};

const createBodyImageTitanImage = (params: GenerateImageParams) => {
const createBodyImageAmazonImage = (params: GenerateImageParams) => {
// TODO: Support inpainting and outpainting too
const imageGenerationConfig = {
numberOfImages: 1,
Expand All @@ -454,7 +454,7 @@ const createBodyImageTitanImage = (params: GenerateImageParams) => {
cfgScale: params.cfgScale,
seed: params.seed % 214783648, // max for titan image
};
let body: Partial<TitanImageParams> = {};
let body: Partial<AmazonImageParams> = {};
if (params.initImage && params.maskMode === undefined) {
body = {
taskType: 'IMAGE_VARIATION',
Expand Down Expand Up @@ -552,13 +552,13 @@ const extractOutputImageStabilityAI2024Model = (
}
};

const extractOutputImageTitanImage = (
const extractOutputImageAmazonImage = (
response: BedrockImageGenerationResponse | StabilityAI2024ModelResponse
) => {
if ('images' in response) {
return response.images[0];
} else {
throw new Error('Unexpected response type for Titan Image');
throw new Error('Unexpected response type for Amazon Image');
}
};
// テキスト生成に関する、各のModel のパラメーターや関数の定義
Expand Down Expand Up @@ -979,12 +979,16 @@ export const BEDROCK_IMAGE_GEN_MODELS: {
extractOutputImage: extractOutputImageStabilityAI2024Model,
},
'amazon.titan-image-generator-v1': {
createBodyImage: createBodyImageTitanImage,
extractOutputImage: extractOutputImageTitanImage,
createBodyImage: createBodyImageAmazonImage,
extractOutputImage: extractOutputImageAmazonImage,
},
'amazon.titan-image-generator-v2:0': {
createBodyImage: createBodyImageTitanImage,
extractOutputImage: extractOutputImageTitanImage,
createBodyImage: createBodyImageAmazonImage,
extractOutputImage: extractOutputImageAmazonImage,
},
'amazon.nova-canvas-v1:0': {
createBodyImage: createBodyImageAmazonImage,
extractOutputImage: extractOutputImageAmazonImage,
},
};

Expand Down
1 change: 1 addition & 0 deletions packages/cdk/lib/construct/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export class Api extends Construct {
'stability.stable-image-ultra-v1:0',
'amazon.titan-image-generator-v2:0',
'amazon.titan-image-generator-v1',
'amazon.nova-canvas-v1:0',
'meta.llama3-8b-instruct-v1:0',
'meta.llama3-70b-instruct-v1:0',
'meta.llama3-1-8b-instruct-v1:0',
Expand Down
4 changes: 3 additions & 1 deletion packages/types/src/image.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ export type StabilityAI2024ModelParams = {

// Titan Image
// https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-titan-image.html
export type TitanImageParams = {
// Amazon Nova
// https://docs.aws.amazon.com/nova/latest/userguide/image-gen-access.html
export type AmazonImageParams = {
taskType: 'TEXT_IMAGE' | 'INPAINTING' | 'OUTPAINTING' | 'IMAGE_VARIATION';
textToImageParams?: {
text: string;
Expand Down
24 changes: 18 additions & 6 deletions packages/web/src/pages/GenerateImagePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ import { GenerateImageParams } from 'generative-ai-use-cases-jp';

const MAX_SAMPLE = 7;

const TITAN_MODELS = {
V1: 'amazon.titan-image-generator-v1',
V2: 'amazon.titan-image-generator-v2:0',
const AMAZON_MODELS = {
TITAN_V1: 'amazon.titan-image-generator-v1',
TITAN_V2: 'amazon.titan-image-generator-v2:0',
NOVA_CANVAS: 'amazon.nova-canvas-v1:0',
};
const STABILITY_AI_MODELS = {
STABLE_DIFFUSION_XL: 'stability.stable-diffusion-xl-v1',
Expand Down Expand Up @@ -89,7 +90,7 @@ const modelInfo: Record<string, ModelInfo> = {
supportedModes: [GENERATION_MODES.TEXT_IMAGE],
resolutionPresets: stabilityAi2024ModelPresets,
},
[TITAN_MODELS.V1]: {
[AMAZON_MODELS.TITAN_V1]: {
supportedModes: [
GENERATION_MODES.TEXT_IMAGE,
GENERATION_MODES.IMAGE_VARIATION,
Expand All @@ -98,7 +99,16 @@ const modelInfo: Record<string, ModelInfo> = {
],
resolutionPresets: defaultModelPresets,
},
[TITAN_MODELS.V2]: {
[AMAZON_MODELS.TITAN_V2]: {
supportedModes: [
GENERATION_MODES.TEXT_IMAGE,
GENERATION_MODES.IMAGE_VARIATION,
GENERATION_MODES.INPAINTING,
GENERATION_MODES.OUTPAINTING,
],
resolutionPresets: defaultModelPresets,
},
[AMAZON_MODELS.NOVA_CANVAS]: {
supportedModes: [
GENERATION_MODES.TEXT_IMAGE,
GENERATION_MODES.IMAGE_VARIATION,
Expand Down Expand Up @@ -431,7 +441,9 @@ const GenerateImagePage: React.FC = () => {
const maskPromptSupported = useMemo(() => {
// TODO: Remove Hard Coding
return (
imageGenModelId === TITAN_MODELS.V1 || imageGenModelId === TITAN_MODELS.V2
imageGenModelId === AMAZON_MODELS.TITAN_V1 ||
imageGenModelId === AMAZON_MODELS.TITAN_V2 ||
imageGenModelId === AMAZON_MODELS.NOVA_CANVAS
);
}, [imageGenModelId]);

Expand Down

0 comments on commit 185d98b

Please sign in to comment.