Skip to content

Commit

Permalink
Merge pull request #895 from DyfanJones/main
Browse files Browse the repository at this point in the history
add bedrock nova example
  • Loading branch information
DyfanJones authored Feb 11, 2025
2 parents 132bc38 + 6c3c341 commit abe6472
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/packaging.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:
pkg_version: ${{ steps.build.outputs.pkg_version }}

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Install R
uses: r-lib/actions/setup-r@v2
Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/pr-commands.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- uses: r-lib/actions/pr-fetch@v2
with:
Expand Down Expand Up @@ -52,7 +52,7 @@ jobs:
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- uses: r-lib/actions/pr-fetch@v2
with:
Expand Down Expand Up @@ -89,14 +89,14 @@ jobs:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
VERSION: ${{ github.event.comment.body }}
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- uses: r-lib/actions/pr-fetch@v2
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}

- uses: r-lib/actions/setup-r@v2

- name: Version
id: version
run: |
Expand All @@ -122,7 +122,7 @@ jobs:
writeLines(lines, desc)
}
}
paws_update_version("cran")
shell: Rscript {0}

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/regen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Install R
uses: r-lib/actions/setup-r@v2
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Install R
uses: r-lib/actions/setup-r@v2
Expand Down Expand Up @@ -67,7 +67,7 @@ jobs:
shell: Rscript {0}

- name: Cache R packages
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: ${{ env.R_LIBS_USER }}
key: ${{ runner.os }}-${{ matrix.config.R }}-1-${{ hashFiles('.github/depends.Rds') }}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/amazon_nova_example/image_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
137 changes: 137 additions & 0 deletions examples/aws_nova_background_generation_guide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
## Background replacement with Amazon Bedrock in R: A Step-by-Step Guide

Developed from [05_background_replacement_using_mask_prompt](https://github.com/aws-samples/amazon-nova-samples/blob/main/multimodal-generation/image-generation/notebook/05_background_replacement_using_mask_prompt.ipynb)

### 1. Load Necessary Libraries

First, we need to load the required libraries for AWS services, JSON handling, base64 encoding/decoding, and image processing.

```r
library(paws)
library(yyjsonr)
library(base64enc)
library(magick)
```

### 2. Define the Image Generation Function

Next, we define a helper function `generate_images` that will handle the image generation process using Amazon Bedrock.

```r
# Function to generate images using Amazon Nova
generate_images <- function(
inference_params,
output_directory,
model_id = "amazon.nova-canvas-v1:0",
client = NULL) {
# Create client if not provided
client <- client %||% bedrockruntime(region = "us-east-1")

# Invoke the model with the provided parameters
resp <- client$invoke_model(
body = write_json_str(inference_params, auto_unbox = TRUE),
modelId = model_id,
accept = "application/json",
contentType = "application/json"
)

# Create the output directory if it doesn't exist
dir.create(output_directory, recursive = TRUE, showWarnings = FALSE)

# Read the response body and decode the images
resp_body <- read_json_raw(resp$body)
img_raw <- lapply(resp_body$images, base64decode)
img <- lapply(img_raw, image_read)

# Save the request and response to JSON files
write_json_file(
inference_params,
file.path(output_directory, "request.json"),
auto_unbox = TRUE, pretty = TRUE
)
write_json_file(
resp_body,
file.path(output_directory, "response_body.json"),
auto_unbox = TRUE, pretty = TRUE
)

# Save the generated images to the output directory
for (i in seq_along(img)) {
image_write(
img[[i]], file.path(output_directory, sprintf("image_%s.png", i))
)
}
}
```

### 3. Set Up Directories and Source Image

We define the directories and specify the source image that we want to use for the image generation task.

```r
# Define directories and source image
DIR <- "amazon_nova_example"
SOURCE_IMAGE <- file.path(DIR, "images/amazon-coffee-maker-1.png")
```

### 4. Encode the Source Image

The source image is encoded to base64 format, which is required for the inference parameters.

```r
# Encode the source image to base64
SOURCE_IMAGE_BASE64 = base64encode(SOURCE_IMAGE)
```

### 5. Define Inference Parameters

We set up the parameters for the image generation task, including the task type, description, mask prompt, and configuration.

```r
# Define inference parameters for the image generation task
INFERENCE_PARAMS <- list(
taskType= "OUTPAINTING",
outPaintingParams= list(
image= SOURCE_IMAGE_BASE64,
text= "a coffee maker in a sparse stylish kitchen, a single plate of pastries next to the coffee maker, a single cup of coffee. highly detailed, highest quality, product imagery", # Description of the background to generate
maskPrompt= "coffee maker", # The element(s) to keep
outPaintingMode= "PRECISE" # "DEFAULT" softens the mask. "PRECISE" keeps it sharp.
),
imageGenerationConfig = list(
numberOfImages= 1, # Number of variations to generate. 1 to 5.
quality= "standard", # Allowed values are "standard" and "premium"
cfgScale= 7.0, # How closely the prompt will be followed
seed= 123L # Set Seed
)
)
```

### 6. Generate a Unique ID for the Output Directory

We create a unique ID for the output directory to ensure that each run of the script saves its results in a separate folder.

```r
# Generate a unique ID for the output directory
GENERATION_ID <- strftime(Sys.time(), format = "%Y-%m-%d_%H-%M-%S")
OUTPUT_DIR <- file.path(DIR, "output", GENERATION_ID)
```

### 7. Call the Function to Generate Images

Finally, we call the `generate_images` function with the defined parameters and output directory to generate the images.

```r
# Call the function to generate images
generate_images(INFERENCE_PARAMS, OUTPUT_DIR)
```

<div style="display: flex; justify-content: space-around;">
<div style="text-align: center;">
<img src="amazon_nova_example/amazon-coffee-maker-1.png" alt="Input Image" style="max-width: 100%;">
<figcaption>Figure 1: Input Image</figcaption>
</div>
<div style="text-align: center;">
<img src="amazon_nova_example/image_1.png" alt="Output Image" style="max-width: 100%;">
<figcaption>Figure 2: Output Image</figcaption>
</div>
</div>

0 comments on commit abe6472

Please sign in to comment.