Code Monkey home page Code Monkey logo

ellerbrock / lambda-refarch-imagemoderationchatbot Goto Github PK

View Code? Open in Web Editor NEW

This project forked from aws-samples/lambda-refarch-image-moderation-chatbot

0.0 1.0 1.0 2.48 MB

The Image Moderator Chatbot serverless reference architecture demonstrates how to leverage Amazon Rekognition's image moderation deep learning feature to automatically remove messages containing explicit or suggestive images from channels of popular chat apps using Amazon API Gateway, AWS Lambda, and Amazon Rekognition.

License: Apache License 2.0

Python 100.00%

lambda-refarch-imagemoderationchatbot's Introduction

Serverless Reference Architecture: Image Moderation Chatbot

Administrators of large channels in popular chat apps can struggle to protect their users from trolls posting explicit or suggestive images. The Image Moderation Chatbot Serverless reference architecture solves this problem by using Amazon API Gateway, AWS Lambda, and Amazon Rekognition's image moderation deep learning feature to check images contained in messages posted to channels for explicit or suggestive content. Image moderation provides a hierarchical list of labels for each image with confidence scores to enable fine-grained control over what images to allow. Images found to contain explicit or suggestive content labels above a minimum confidence interval are automatically removed by the bot, and a message explaining the removal is posted by the bot to the originating channel.

This example is intended to work with Slack, but could also be modified to work with other popular chat apps such as Facebook Messenger.

This repository contains sample code for all the Lambda functions depicted in the diagram below as well as an AWS CloudFormation template for creating the functions and related resources.

To see some of the other powerful features of Amazon Rekognition in action check out the Image Recognition and Processing Backend Serverless reference architecture

screenshot for instruction

Walkthrough of the Architecture

  1. A user posts a message containing an image to a chat app channel that’s monitored by a chatbot.
  2. The chat app posts the event to an Amazon API Gateway API for the chatbot.
  3. The chatbot validates the event. This event triggers an AWS Lambda function that downloads the image.
  4. Amazon Rekognition’s image moderation feature checks the image for suggestive or explicit content.
  5. The chat app API deletes an image containing explicit or suggestive content from the chat channel.
  6. The chatbot uses the chat app API to post a message to the chat channel detailing deletion of the image.

Running the Example

Preparing Slack

First make sure you're logged in to Slack, then follow these instructions to prep your bot:

  1. Create an app (Documentation)
  2. From the Basic Information tab under Settings take note of the Verification Token as it will be required later
  3. Navigate to the OAuth & Permissions tab under Features
  4. Under the Permissions Scopes section add the following permission scopes
    • channels:history
    • chat:write:bot
    • files:read
    • files:write:user
  5. Click Save Changes
  6. Click Install App to Team then Authorize then note the OAuth Access Token as it will be required later

Launching the Bot Backend on AWS

Option 1: Launch the CloudFormation Template in US West - Oregon (us-west-2)

The backend infrastructure can be deployed in US West - Oregon (us-west-2) using the provided CloudFormation template. Click Launch Stack to launch the template in the US West - Oregon (us-west-2) region in your account:

Launch Stack into Oregon with CloudFormation

(On the last page of the wizard, make sure to:

  1. Click the checkboxes to give AWS CloudFormation permission to "create IAM resources" and "create IAM resources with custom names"
  2. Follow the instructions to "Create Change Set"
  3. Click "Execute" )

Option 2: Launch the CloudFormation Template Manually

If you would like to deploy the template manually, you need a S3 bucket in the target region, and then package the Lambda functions into that S3 bucket by using the aws cloudformation package utility.

Set environment variables for later commands to use:

S3BUCKET=[REPLACE_WITH_YOUR_BUCKET]
REGION=[REPLACE_WITH_YOUR_REGION]
STACKNAME=[REPLACE_WITH_DESIRED_NAME]
VTOKEN=[REPLACE_WITH_VERIFICATION_TOKEN]
ATOKEN=[REPLACE_WITH_OAUTH_ACCESS_TOKEN]

Then go to the cloudformation folder and use the aws cloudformation package utility

cd cloudformation

aws cloudformation package --region $REGION --s3-bucket $S3BUCKET --template image_moderator.serverless.yaml --output-template-file image_moderator.output.yaml

Last, deploy the stack with the resulting yaml (image_moderator.output.yaml) through the CloudFormation Console or command line:

aws cloudformation deploy --region $REGION --template-file image_moderator.output.yaml --stack-name $STACKNAME --capabilities CAPABILITY_NAMED_IAM --parameter-overrides VerificationToken=$VTOKEN AccessToken=$ATOKEN

Finalize Slack Event Subscription

  1. Navigate to the created stack in the CloudFormation console and note the value for the RequestURL output from the created stack as it will be required later
  2. Return to the Slack app settings page for the Slack app created earlier
  3. Navigate to the Event Subscriptions tab under Features and enable events
  4. In the Request URL field enter the RequestURL value noted earlier
  5. Click Add Team Event and select message.channels
  6. Click Save Changes

Testing the Example

To test the example open your Slack bot and attempt to upload the sample images from the Amazon Rekognition console demo, which can be downloaded from the links below:

testing of example gif

Cleaning Up the Stack Resources

To remove all resources created by this example, do the following:

  1. Delete the CloudFormation stack.
  2. Delete the CloudWatch log groups associated with each Lambda function created by the CloudFormation stack.

CloudFormation Template Resources

The following sections explain all of the resources created by the CloudFormation template provided with this example.

AWS Lambda

  • ImageModeratorFunction - Lambda function that validates incoming Slack event messages, checks them for images containing explicit content, and orchestrates the removal of images found to contain explicit content from Slack.
  • ImageModeratorFunctionImageModeratorAPIPostPermissionTest - Implicitly created Lambda permission, allows API Gateway Test stage to call Lambda function.
  • ImageModeratorFunctionImageModeratorAPIPostPermissionProd - Implicitly created Lambda permission, allows API Gateway Prod stage to call Lambda function.

AWS IAM

  • LambdaRekognitionRole - IAM Role with policy that allows Lambda function to invoke "rekognition:DetectLabels" API call and write log messages to CloudWatch Logs.

Amazon API Gateway

  • ImageModeratorAPI: - API for image moderation chatbot
  • ImageModeratorAPIProdStage - Implicitly created production stage for API
  • ImageModeratorAPIDeploymentXXXXXXXXX - Implicitly created deployment for production stage of API

License

This reference architecture sample is licensed under Apache 2.0.

lambda-refarch-imagemoderationchatbot's People

Contributors

ovalba avatar robperc avatar

Watchers

 avatar

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.