Code Monkey home page Code Monkey logo

aws-samples / automate-image-processing-with-amazon-bedrock Goto Github PK

View Code? Open in Web Editor NEW
7.0 2.0 2.0 24.22 MB

Automate process to change image background using Amazon Bedrock and AWS Step Functions

Home Page: https://aws.amazon.com/blogs/machine-learning/automate-the-process-to-change-image-backgrounds-using-amazon-bedrock-and-aws-step-functions/

License: MIT No Attribution

Python 100.00%
ai amazon-titan aws-step-functions generative-ai amazon-bedrock

automate-image-processing-with-amazon-bedrock's Introduction

Automate the process to change image background using Amazon Bedrock and AWS Step Functions

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.

How the application works

Architecture

The solutions comprises of the below steps:

  1. A user uploads multiple images into an Amazon Simple Storage Service (Amazon S3) via a Streamlit web applications.
  2. The Streamlit web application calls AWS API Gateway REST API Endpoint, it integrates with Amazon Rekognition DetectLabels API which detects labels for each image.
  3. Upon submission, the Streamlit web application updates Amazon DynamoDB table with image details.
  4. The DynamoDB update triggers an AWS Lambda function which starts an AWS Step Functions workflow.
  5. 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.
  6. The Step Functions workflow then invokes a Lambda Function to generate a status report.
  7. Finally, it sends email using Amazon Simple Notification Services (Amazon SNS).

Deployment instructions

Prerequisites

Deploy the application with AWS SAM

  1. Clone the repo.
git clone https://github.com/aws-samples/automate-image-processing-with-amazon-bedrock
  1. 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
  1. 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
  1. Verify the SAM template deployed successfully. Also, copy the output values. You will use these details for testing.

  2. 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.

Create a user in the Amazon Cognito user pool

  1. Navigate to the Amazon Cognito console.
  2. Find the user pool with an ID matching the output provided by AWS SAM above.
  3. Under Users, choose Create user.
  4. Enter an email address and a password that adheres to the password requirements.
  5. Choose Create user.

Test

  1. Go to the /ui/ directory.

  2. Update config.py with the values from AWS SAM output above.

  3. 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
  1. Launch the Streamlit app with following command from within ui directory.
streamlit run app.py

Clean up

  1. Go to the root directory.

  2. 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
  1. Run the following command to delete the SAM template.
sam delete --stack-name image-processing --no-prompts 

Security

See CONTRIBUTING for more information.

License

This library is licensed under the MIT-0 License. See the LICENSE file.

DISCLAIMER

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.

automate-image-processing-with-amazon-bedrock's People

Contributors

amazon-auto avatar makvac-aws avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

automate-image-processing-with-amazon-bedrock's Issues

Image size limit

The Streamlit application suggest files of up to 200MB can be uploaded. However the image is subsequently sent to api-gateway to Rekognition for label detection and therefore the api-gateway payload size is limited to 10MB causing files larger than that to return an HTTP 413. There is also no error handling on the api-gateway request so it manifests itself as a stack trace "json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)" on the reponse.json() call

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.