This repository showcases an automated method for the process of changing backgrounds in multiple images. By harnessing the capabilities of generative AI with Amazon Bedrock and the Amazon Titan Image Generator G1 model, combined with Step Functions, this solution efficiently generates images with the desired background.
See the accompanying blog post on the AWS Machine Learning Blog for a detailed description and follow the deployment instructions below to get started.
Warning This application is not ready for production use. It was written for demonstration and educational purposes. Review the Security section of this README and consult with your security team before deploying this stack. No warranty is implied in this example.
Note This architecture creates resources that have costs associated with them. Please see the AWS Pricing page for details and make sure to understand the costs before deploying this stack.
The solutions comprises of the below steps:
- A user uploads multiple images into an Amazon Simple Storage Service (Amazon S3) via a Streamlit web applications.
- The Streamlit web application calls AWS API Gateway REST API Endpoint, it integrates with Amazon Rekognition DetectLabels API which detects labels for each image.
- Upon submission, the Streamlit web application updates Amazon DynamoDB table with image details.
- The DynamoDB update triggers an AWS Lambda function which starts an AWS Step Functions workflow.
- The Step Functions workflow executes the below steps for each image.
- Constructs a request payload for the Amazon Bedrock InvokeModel API.
- Invokes Amazon Bedrock InvokeModel API action.
- Parses image from the response and save it to an S3 location.
- Updates status in DynamoDB table.
- The Step Functions workflow then invokes a Lambda Function to generate a status report.
- Finally, it sends email using Amazon Simple Notification Services (Amazon SNS).
- Create an AWS account if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources.
- AWS CLI installed and configured
- Git Installed
- AWS Serverless Application Model (AWS SAM) installed
- Amazon Bedrock access setup for Amazon Titan Image Generator G1 model
- Python 3.9.9 installed
- Port 8501 opened for Streamlit
- Clone the repo.
git clone https://github.com/aws-samples/automate-image-processing-with-amazon-bedrock
- Run the following command to prepare our serverlress application for deploying to the AWS Cloud. This command creates a
.aws-sam
directory that structures your application in a format and location that next step requries.
sam build
- Now, package and deploy the SAM application. This deployment will be an interactive menu, the information to give the menu is below. Run the following command.
sam deploy --guided
- Stack Name: :
image-processing
- AWS Region:
your current region (i.e. us-west-2, us-east-1)
- Parameter APIName:
leave as default
- Parameter S3BucketName:
leave as default
- Parameter ImagePrefix:
leave as default
- Parameter GeneratedImagePrefix:
leave as default
- Parameter StatusReportPrefix:
leave as default
- Parameter StatusReportURLExpiration:
leave as default
- Parameter ImageProcessingWorkflowName:
leave as default
- Parameter NotificationSNSTopicName:
leave as default
- Parameter NotificationEmail:
Enter email address where you wish to receive notification after image processing is completed
- Parameter BedrockModelId:
leave as default
- Parameter MaxConcurrency:
leave as default
- Confirm changes before deploy:
N
- Allow SAM CLI IAM role creation:
leave as default
- Disable rollback:
leave as default
- Save arguments to configuration file:
leave as default
- SAM configuration file:
leave as default
- SAM configuration environment:
leave as default
-
Verify the SAM template deployed successfully. Also, copy the output values. You will use these details for testing.
-
An email confirmation request from [email protected] will be sent to your provided email address in the previous step to confirm the subscription to Amazon SNS. Please open the email and click on the Confirm subscription link to proceed.
- Navigate to the Amazon Cognito console.
- Find the user pool with an ID matching the output provided by AWS SAM above.
- Under Users, choose Create user.
- Enter an email address and a password that adheres to the password requirements.
- Choose Create user.
-
Go to the
/ui/
directory. -
Update
config.py
with the values from AWS SAM output above. -
Enter the following command to install all of the Python modules and packages listed in the
requirements.txt
from within/ui/
directory
pip install -r requirements.txt
- Launch the Streamlit app with following command from within
ui
directory.
streamlit run app.py
-
Go to the root directory.
-
Run the following command to empty the image bucket. Make sure to replace the
<image-bucket-name>
with the bucket name you copied earlier from AWS SAM output.
aws s3 rm s3://<image-bucket-name> --recursive
- Run the following command to delete the SAM template.
sam delete --stack-name image-processing --no-prompts
See CONTRIBUTING for more information.
This library is licensed under the MIT-0 License. See the LICENSE file.
The solution architecture sample code is provided without any guarantees, and you're not recommended to use it for production-grade workloads. The intention is to provide content to build and learn. Be sure of reading the licensing terms.