Skip to content

Commit 0c94057

Browse files
Merge pull request #27 from jeremyLabrado/fix/migrate-to-nova-canvas
Migrate from deprecated Stable Diffusion to Amazon Nova Canvas
2 parents 67f0021 + bd6a4aa commit 0c94057

File tree

4 files changed

+38
-45
lines changed

4 files changed

+38
-45
lines changed

README.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ The architecture of the application can be split in 4 blocks:
6464

6565
#### Product Summary and Generative Recipe:
6666

67-
- **Implementation**: Using AWS Lambda for server-side logic, Amazon Bedrock as a generative artificial intelligence (GenAI) building platform, Anthropic Claude as Large Language Models (LLM) and Stable Diffusion XL from StabilityAI as diffusion model for generating images.
67+
- **Implementation**: Using AWS Lambda for server-side logic, Amazon Bedrock as a generative artificial intelligence (GenAI) building platform, Anthropic Claude as Large Language Models (LLM) and Amazon Nova Canvas for generating images.
6868

6969
- **AI Model Development**: Choosing the LLM model had an impact on response quality and latency. Ultimately, we chose Anthropic Claude 3 Haiku as a good ratio between latency and quality.
7070

71-
- **AI-Generated Images**: Prompting for an image is very sensitive and was a challenge to generate an image that truly highlights the nutritive features of products. To craft the prompt, we used a first LLM to generate the prompt based on product nutritive features. This technique is similar to a self-querying for vector databases. Using multi-shot prompt-engineering also helped a lot to improve the quality of the prompt.
71+
- **AI-Generated Images**: Prompting for an image is very sensitive and was a challenge to generate an image that truly highlights the nutritive features of products. To craft the prompt, we used a first LLM to generate the prompt based on product nutritive features. This technique is similar to a self-querying for vector databases. Using multi-shot prompt-engineering also helped a lot to improve the quality of the prompt. Images are generated using Amazon Nova Canvas.
7272

7373

7474

@@ -164,7 +164,7 @@ Each recipe must also respect the user's dietary restrictions and allergies.
164164
}
165165
```
166166

167-
- **Image Generation**: Once we have the recipe title and description, we use Stable Diffusion to generate the image of the recipe. We generate images in parallel to reduce the latency of the response.
167+
- **Image Generation**: Once we have the recipe title and description, we use Amazon Nova Canvas to generate the image of the recipe. We generate images in parallel to reduce the latency of the response.
168168

169169

170170

@@ -193,7 +193,7 @@ The output format is a Markdown file to faciliate the display of the recipe on t
193193
- **Challenge**: The selection of the Language Model (LM) significantly influenced both response latency and quality, posing a critical decision point.
194194

195195
- **Solution**: Following a comprehensive assessment of various models, we've chosen the following Anthropic Claude models for different components within the app:
196-
- **Barcode scanning image generation**: Utilizing Stable Diffusion XL.
196+
- **Barcode scanning image generation**: Utilizing Amazon Nova Canvas.
197197
- **Recipe fridge photograph food aliment detection**: Utilizing Anthropic Claude 3 Sonnet to extract food ingredients from images.
198198
- **Recipe proposals, Product ingredients**: Leveraging Anthropic Claude 3 Sonnet. Sonnet was selected for cases where the output needed to be parsed for other tasks or displayed using HTML components.
199199
- **Product summary, and recipe steps**: Utilizing Anthropic Claude 3 Haiku was favored for enhancing user experience by displaying results in streaming mode, and because the output was solely intended for display, enabling us to designate the output type as markdown.
@@ -242,7 +242,7 @@ The output format is a Markdown file to faciliate the display of the recipe on t
242242
**Illustrated Use Cases of the GenAi Application**
243243

244244
- **Text generation** - Amazon Bedrock utilizes Anthropic Claude 3 Haiku to generate the product summary.
245-
- **Text to image** - Amazon Bedrock utilizes Stable Diffusion XL from StabilityAI to generate an image of the product.
245+
- **Text to image** - Amazon Bedrock utilizes Amazon Nova Canvas to generate an image of the product.
246246
- **Image to text** - Anthropic Claude 3 Sonnet is leveraged by Amazon Bedrock to identify food elements in the image.
247247

248248

@@ -300,8 +300,6 @@ npm run dev
300300

301301
- [AWS CLI 2+](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html) must be installed on the deployment machine. ([Instructions](https://nodejs.org/en/download/))
302302

303-
- Request access to Anthropic Claude models and Stable Diffusion XL on Amazon Bedrock
304-
- Follow [these steps](https://docs.aws.amazon.com/bedrock/latest/userguide/model-access.html) to gain access to Claude and SDXL models used in this app
305303

306304
## Resources
307305

lambda/barcode_image/index.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -126,33 +126,35 @@ def get_image(prompt):
126126
"""
127127

128128
body=json.dumps({
129-
"text_prompts": [
130-
{
131-
"text": prompt
129+
"taskType": "TEXT_IMAGE",
130+
"textToImageParams": {
131+
"text": f"{prompt}, professional food photography, studio lighting, clean composition, high resolution, editorial quality, commercial product photography style"
132+
},
133+
"imageGenerationConfig": {
134+
"numberOfImages": 1,
135+
"quality": "premium",
136+
"height": 1024,
137+
"width": 1024,
138+
"cfgScale": 8.0,
139+
"seed": 0
132140
}
133-
],
134-
"cfg_scale": 10,
135-
"seed": 0,
136-
"steps": 35,
137-
"samples" : 1,
138-
"style_preset" : "photographic"
139141
})
140142

141143

142144
accept = "application/json"
143145
content_type = "application/json"
144-
model_id = 'stability.stable-diffusion-xl-v1'
146+
model_id = 'amazon.nova-canvas-v1:0'
145147

146-
logger.debug("Generating image with SDXL model ", model_id)
148+
logger.debug(f"Generating image with Nova Canvas model {model_id}")
147149

148150
response = bedrock.invoke_model(
149151
body=body, modelId=model_id, accept=accept, contentType=content_type
150152
)
151153
response_body = json.loads(response.get("body").read())
152154

153-
base64_image = response_body.get("artifacts")[0].get("base64")
155+
base64_image = response_body.get("images")[0]
154156

155-
logger.debug("Successfully generated image withvthe SDXL 1.0 model %s", model_id)
157+
logger.debug("Successfully generated image with Nova Canvas model %s", model_id)
156158

157159
return base64_image
158160

lambda/recipe_proposals/index.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,25 @@
2727

2828
def call_bedrock_thread(prompt, model_id, accept, content_type):
2929
body=json.dumps({
30-
"text_prompts": [
31-
{
32-
"text": f"Close up picture of tasty {prompt}"
30+
"taskType": "TEXT_IMAGE",
31+
"textToImageParams": {
32+
"text": f"Professional food photography of {prompt}, styled for cookbook, natural lighting, shallow depth of field, appetizing presentation on elegant plate, high resolution, culinary magazine quality"
33+
},
34+
"imageGenerationConfig": {
35+
"numberOfImages": 1,
36+
"quality": "premium",
37+
"height": 1024,
38+
"width": 1024,
39+
"cfgScale": 8.0,
40+
"seed": 0
3341
}
34-
],
35-
"cfg_scale": 10,
36-
"seed": 0,
37-
"steps": 35,
38-
"samples" : 1,
39-
"style_preset" : "photographic"
4042
})
4143

4244
response = bedrock_rt.invoke_model(
4345
body=body, modelId=model_id, accept=accept, contentType=content_type
4446
)
4547
response_body = json.loads(response.get("body").read())
46-
base64_image = response_body.get("artifacts")[0].get("base64")
48+
base64_image = response_body.get("images")[0]
4749
return base64_image
4850

4951
def upload_image_to_s3(image_bytes):
@@ -71,7 +73,7 @@ def generate_images_recipes(prompt_list:list):
7173

7274
accept = "application/json"
7375
content_type = "application/json"
74-
model_id = 'stability.stable-diffusion-xl-v1'
76+
model_id = 'amazon.nova-canvas-v1:0'
7577

7678
partial_generate_image = partial(
7779
call_bedrock_thread,
@@ -80,7 +82,7 @@ def generate_images_recipes(prompt_list:list):
8082
content_type=content_type
8183
)
8284

83-
logger.debug("Generating images with SDXL model {}".format(model_id))
85+
logger.debug(f"Generating images with Nova Canvas model {model_id}")
8486

8587

8688
with concurrent.futures.ThreadPoolExecutor() as executor:

package-lock.json

Lines changed: 4 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)