Code Monkey home page Code Monkey logo

amazon-rekognition-and-amazon-appflow-image-moderation-using-aws-cdk-for-slack's Introduction

Leveraging Amazon Rekognition and Amazon AppFlow for Slack image moderation using AWS Solutions Constructs

Table of Contents

  1. Overview
    1. About
    2. Use Cases
    3. Architecture Diagram
  2. Requirements
    1. General
    2. Slack
    3. AWS CDK
    4. Environment Variable Prep
  3. Deployment
  4. Testing
    1. Trigger Violations
    2. Checking Results
  5. Cleanup

Overview

The code in this repo automates the solution in the "Moderating Image Content in Slack with Amazon Rekognition and Amazon AppFlow" Technical Guide

About

This project deploys a fully serverless pipeline to moderate images posted to a Slack channel using the AWS CDK along with AWS Solutions Constructs. Services used are: Amazon Rekognition, Amazon AppFlow, AWS Lambda,Amazon S3 and Amazon SQS

Use Cases

Ensuring all aspects of the virtual work environment are inclusive and safe is a priority for many organizations. Sharing images can be a powerful way to effectively convey concepts and thoughts. There are many popular ways to analyze text, but images present a different challenge. Organizations need a way to detect and react to posted images that violate company guidelines.

The content moderation strategy in this solution identifies images that violate sample chosen guidelines:

Images that contain themes of tobacco or alcohol using Amazon Rekognition content moderation.

Images that contain the following disallowed words using using Amazon Rekognition text detection:

  • security
  • private

These guidelines can be customized in the process-new-images/index.py file to fit your requirements.

Architecture Diagram

Architecture Diagram

Requirements

General

Slack app

Create a Slack app and install the Slack app into your Slack workspace Follow the steps in the "Creating the Slack app in your Slack workspace" section of the Moderating Image Content in Slack with Amazon Rekognition and Amazon AppFlow Technical Guide

In your Slack Workspace, create a channel called 'testing-slack-moderation' (or something else you prefer)

AWS CDK

Install the AWS CDK - Getting Started Guide

Environment Variable Prep

Export the following environment variables in your terminal session. These are used by the CDK to configure the solution during deployment.

  • SlackClientID (Obtained from the steps above when creating your Slack app)
  • SlackClientSecret (Obtained from the steps above when creating your Slack app)
  • SlackWorkspaceInstanceURL (Based on your Slack workspace name)
  • SlackChannelParamID (Obtained by navigating to your Slack workspace and channel in the web interface of Slack. It is the last portion of the URL (following the last forward slash) ) Channel ID
  • SlackOAuthAccessToken (Obtained from your Slack app's management page. Under the Features menu select the OAuth & Permissions option. Be sure to use the "User OAuth Token".)

E.g.:

export SlackClientID=####...###
export SlackClientSecret=...
export SlackWorkspaceInstanceURL=https://<<WORKSPACENAME>>.slack.com
export SlackChannelParamID=<<ChannelID>>
export SlackOAuthAccessToken=xoxp-...

Deployment

This project is set up like a standard Python AWS CDK project.

To manually create a virtualenv on MacOS and Linux:

$ python3 -m venv .venv

After the init process completes and the virtualenv is created, you can use the following step to activate your virtualenv.

$ source .venv/bin/activate

Once the virtualenv is activated, you can install the required dependencies.

$ pip install -r requirements.txt

Deploy the stack

cdk deploy

Testing

Trigger Violations

Post the following images to the moderated Slack Channel:

This image contains the disallowed word "private": https://i.imgur.com/uuAY133.png

This image which contains the disallowed "Tobacco" theme: https://i.imgur.com/XgAtyWU.png

Note:

If you are pasting the same link numerous times during testing, you may receive a "Pssst! I didn’t unfurl <> because it was already shared in this channel quite recently ..." error. You will need to click "Show Preview Anyway" for the image to be processed again.

Avoid using images over 2 MB because Slack doesn't expand them by default.

Checking results

Wait approximately 2 mins and then check the "new-violation-findings" SQS Queue.

In the AWS Console, navigate to Amazon SQS --> Queues -->new-violation-findings

SQS Queue

Click "Send and receive messages"

Scroll down the the Receive Messages window and click "Poll for messages"

SQS Queue

From there, you can select a message and view its details

SQS Queue

You can get more information by selection the Attributes menu

SQS Queue

You can also use the following to check violations via the CLI:

Set your AWS ACCOUNT ID

AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)

Set and export your default region to the region of your choice. - Ex:

export AWS_REGION=us-east-1

Run the following command to retrieve messages:

aws sqs receive-message --queue-url https://sqs.$AWS_REGION.amazonaws.com/$AWS_ACCOUNT_ID/new-violation-findings --attribute-names All --message-attribute-names All --max-number-of-messages 10

You should see messages with the following violations:

            <snip>
            "Body": "Image with \"private\" found",
            "Attributes": {
            <snip>
            "MessageAttributes": {
                "slack_msg_id": {
                    "StringValue": "<ID>",
                    "DataType": "String"
                },
                "url": {
                    "StringValue": "https://i.imgur.com/uuAY133.png",
                    "DataType": "String"
                }
            }
            <snip>
            "Body": "Image with \"Tobacco\" found",
            "Attributes": {
            <snip>
            "MessageAttributes": {
                "slack_msg_id": {
                    "StringValue": "<ID>",
                    "DataType": "String"
                },
                "url": {
                    "StringValue": "https://i.imgur.com/XgAtyWU.png",
                    "DataType": "String"
                }
            }

Clean up

Delete the CDK stack:

cdk destroy

amazon-rekognition-and-amazon-appflow-image-moderation-using-aws-cdk-for-slack's People

Contributors

amazon-auto avatar

Stargazers

 avatar  avatar

Watchers

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