Giter Site home page Giter Site logo

ibm / cognitive-moderator-service Goto Github PK

View Code? Open in Web Editor NEW
20.0 17.0 25.0 17.64 MB

Create a cognitive moderator chatbot for anger detection, natural language understanding and explicit images removal

Home Page: https://developer.ibm.com/patterns/build-a-cognitive-moderator-microservice/

License: Apache License 2.0

Python 100.00%
watson watson-natural-language-classifier watson-visual-recognition slack-app slack-bot code watson-developer-cloud ibmcode ibm-cloud-functions ibm-cloud

cognitive-moderator-service's Introduction

Create a cognitive moderator chatbot for anger detection, natural language understanding and explicit images removal

WARNING: This repository is no longer maintained.

This repository will not be updated. The repository will be kept available in read-only mode.

In this code pattern, we will create a chatbot using IBM functions and Watson services. The chatbot flow will be enhanced by using Natural Language Understanding to identify angry and disrespectful messages

When the reader has completed this journey, they will understand how to:

  • Create a chatbot that integrates with Slack via IBM Functions
  • Use Watson Natural Understanding to detect emotions in a conversation
  • Identify entities with Watson Natural Language Understanding

Flow

  1. The user interacts from the Slack app and either sends a text or uploads an image.
  2. The text or image that is used in the Slack for conversation is then passed to an IBM function API by a bot. The API is a call to an IBM Function that categorizes the text or images based on the response of Watson Visual Recognition or Watson Natural Language Processing.
  3. (Optional) Watson Visual Recognition categorizes the uploaded image using default and explicit classifier. (Deprecated, see Maximo Visual Inspection instead).
  4. Watson Natural Language Processing categorizes the text, if text is send as part of slack communication.
  5. IBM function then gets the response and if the text is not polite, a message is sent by the bot to the Slack user to be more polite using Slack post message API. If the image used is explicit, the image will be deleted by the IBM function using Slack files delete API.

Included components

  • IBM Functions: IBM Cloud Functions (based on Apache OpenWhisk) is a Function-as-a-Service (FaaS) platform which executes functions in response to incoming events and costs nothing when not in use.
  • IBM Watson Natural Language Understanding: Analyze text to extract meta-data from content such as concepts, entities, keywords, categories, sentiment, emotion, relations, semantic roles, using natural language understanding.

Featured technologies

  • Slack Apps Customize functionality for your own workspace or build a beautiful bot to share with the world.
  • Slack Bots Enable conversations between users and apps in Slack by building bots.

Watch the Video

Note: The video shows the Slack bot using Watson Visual Recognition to remove explicit images. Currently Watson Visual Recognition is discontinued. Existing instances are supported until 1 December 2021, but as of 7 January 2021, you can't create instances. Any instance that is provisioned on 1 December 2021 will be deleted. Please view the Maximo Visual Inspection trial as a way to get started with image classification.

Steps

  1. Clone the repo
  2. Create Natural Language Understanding service with IBM Cloud
  3. Create Slack App and Bot for a workspace
  4. Deploy the function to IBM Cloud
  5. Test using slack

1. Clone the repo

Clone the cognitive-moderator-service repo locally. In a terminal, run:

$ git clone https://github.com/IBM/cognitive-moderator-service
$ cd cognitive-moderator-service

2. Create natural language understanding service with IBM Cloud

If you do not already have a IBM Cloud account, sign up for IBM Cloud. Create the following services:

Make note of the service credentials when creating services which will be later used when creating a function.

3. Create Slack App and Bot for a workspace

  • Go to Your Apps using the following link: https://api.slack.com/apps and click Create New App and fill out the App Name and the Workspace you would like to use it for and click Create App.

  • After the app is created, you will be taken to a page where you can configure the Slack app. Make note of Verification Token which will be used later in the function.

  • Create bot user from Bot Users. Provide a Display Name and Default Username for the bot and click Save Changes.

  • From OAuth and Permissions, Save the OAuth Access Token for later use by the IBM cloud function.

Select Permissions Scopes that will be used by the bot that we will be creating next.

  • Once this is done make sure you reinstall or install app from Install App section.

4. Deploy the function to IBM Cloud

Once the credentials for both IBM Cloud and Slack are noted, we can now deploy the function to IBM Cloud.

Copy params.sample.json to params.json using cp params.sample.json params.json and replace the values with the credentials you have noted in proper place holders. You can leave the VISUAL_RECOGNITION_IAM_APIKEY blank.

NOTE: NLU_URL is optional. You can also remove the NLU_URL key/value from the below json and it uses the global URL part of the SDK.

{
    "NLU_APIKEY": "<NLU apikey>",
    "NLU_URL": "<NLU url>",

    "VISUAL_RECOGNITION_IAM_APIKEY": "<Visual Recognition IAM API Key, or you can leave this blank>",

    "SLACK_VERIFICATION_TOKEN": "<Slack Verification Token>",
    "SLACK_ACCESS_TOKEN": "<Slack OAuth Access Token>",

    "SLACK_MESSAGE_POST_URL": "https://slack.com/api/chat.postMessage",
    "SLACK_MESSAGE_DELETE_URL": "https://slack.com/api/files.delete"
}

Install the [IBM cloud CLI)(https://cloud.ibm.com/docs/cli/index.html#overview) if you haven't already, including the IBM Cloud Functions CLI plugin.

  • Deploy the function to IBM cloud. Make sure you are in the project directory then, from Terminal run:
ibmcloud wsk action create WatsonModerator functions/bot_moderator_function.py --param-file params.json --kind python:3.7 --web true

Note: The above command pushes the function with python 3.7 runtime and the function is written to suit the runtime.

If you want to use python 3.6, comment out or remove the below code:

response = response.get_result() (line 122) from the function and then run

ibmcloud wsk action create WatsonModerator functions/bot_moderator_function.py --param-file params.json --kind python:3.6 --web true

  • After the function is created, you can run following command to update the function if there are any changes:
ibmcloud wsk action update WatsonModerator functions/bot_moderator_function.py --param-file params.json --kind python:3.7

Now you can login to IBM Cloud and see the function by going to IBM functions, click Actions. You should see the function in the list of functions in this page.

  • Expose the function so that it can be accessed using an API
  1. Click the APIs from the IBM Cloud Functions page and click Create Managed API.

  2. Provide API Name, Base path for API

  3. Click Create Operation and in the dialog box that appears, provide the following like in the figure below and click create. Then click Save & Expose at the bottom right.

  4. From the API Explorer select the API URL and save it for later use.

  • Add Event Subscriptions for your app by clicking on to Add features and functionality from the main page of the app.

Add the API URL of the function from IBM Cloud to the Request URL in Slack App

Make sure the URL is verified. To be verified the API needs to return a response to the request that Slack sends to this Request URL with challenge parameter.

In the Event Subscriptions page, enable it by turning on using the on/off toggle button and Add Workspace and Bot Events in subsequent sections. Add Following events for both:

  1. message.channels
  2. message.group
  3. message.im

Note that you need to Reinstall App after you make any changes to the app for example: Request URL.

5. Test using slack

  • Test Case: Usage of rude messages

Now you can use slack to test. If rude message are sent which NLU categorized as anger or disgust you will see a message from the bot that you created from Slack.

Sample Output

Note: The video shows the Slack bot using Watson Visual Recognition to remove explicit images. Currently Watson Visual Recognition is discontinued. Existing instances are supported until 1 December 2021, but as of 7 January 2021, you can't create instances. Any instance that is provisioned on 1 December 2021 will be deleted. Please view the Maximo Visual Inspection trial as a way to get started with image classification.

Troubleshooting

  • You can monitor logs and see the output of the each request and/or call to function from the monitor page of the function.

  • You can change the code at runtime and run the function, by clicking Code from left navigation menu of the function page. It will open up an editor with the code which you can change and save. To run click Invoke.

  • You can also change parameters at runtime by clicking Parameters from left navigation menu of the function page.

Links

Learn more

  • Artificial Intelligence Code Patterns: Enjoyed this Code Pattern? Check out our other AI Code Patterns.
  • AI and Data Code Pattern Playlist: Bookmark our playlist with all of our Code Pattern videos
  • With Watson: Want to take your Watson app to the next level? Looking to utilize Watson Brand assets? Join the With Watson program to leverage exclusive brand, marketing, and tech resources to amplify and accelerate your Watson embedded commercial solution.

License

This code pattern is licensed under the Apache Software License, Version 2. Separate third party code objects invoked within this code pattern are licensed by their respective providers pursuant to their own separate licenses. Contributions are subject to the Developer Certificate of Origin, Version 1.1 (DCO) and the Apache Software License, Version 2.

Apache Software License (ASL) FAQ

cognitive-moderator-service's People

Contributors

dolph avatar horeaporutiu avatar imgbot[bot] avatar jamaya2001 avatar kaiiyer avatar kant avatar ljbennett62 avatar rhagarty avatar sanjeevghimire avatar scottdangelo avatar stevemart avatar upkarlidder avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cognitive-moderator-service's Issues

Object of type DetailedResponse is not JSON serializable

The issue is that response of type DetailedResponse and not serializable. Need to call get_results() on it to get the actual JSON result object. This will also fix DetailedResponse does not have attribute get error on line 123.

Creating PR.

https://github.com/IBM/cognitive-moderator-service/blob/master/functions/bot_moderator_function.py#L123

    "2019-01-29T04:11:28.808556283Z stderr: File \"/action/1/src/main__.py\", line 122, in main",
    "2019-01-29T04:11:28.80886719Z  stderr: print('response:'+json.dumps(response, indent=2))",
    "2019-01-29T04:11:28.808874836Z stderr: File \"/usr/local/lib/python3.7/json/__init__.py\", line 238, in dumps",
    "2019-01-29T04:11:28.80887967Z  stderr: **kw).encode(obj)",
    "2019-01-29T04:11:28.808884211Z stderr: File \"/usr/local/lib/python3.7/json/encoder.py\", line 201, in encode",
    "2019-01-29T04:11:28.808899403Z stderr: chunks = list(chunks)",
    "2019-01-29T04:11:28.808904451Z stderr: File \"/usr/local/lib/python3.7/json/encoder.py\", line 438, in _iterencode",
    "2019-01-29T04:11:28.808908996Z stderr: o = _default(o)",
    "2019-01-29T04:11:28.808913483Z stderr: File \"/usr/local/lib/python3.7/json/encoder.py\", line 179, in default",
    "2019-01-29T04:11:28.808917881Z stderr: raise TypeError(f'Object of type {o.__class__.__name__} '",
    "2019-01-29T04:11:28.808922437Z stderr: TypeError: Object of type DetailedResponse is not JSON serializable",
    "2019-01-29T04:11:28.808926873Z stderr: "

See here: https://cloud.ibm.com/apidocs/natural-language-understanding?language=python#data-handling

image

Error 500

I'm getting an error of 500 while posting url of my api on slack "Request URl" section. Its showing my url is not validate.
error3

Event Subscriptions page does not save without a request URL

The readme states For now leave the Request URL empty. We will fill it in when the IBM Cloud Function is exposed through an API. However, I cannot leave the page without entering the URL. If I do, all the information I entered is blanked out.

I suggest moving the Events Subscription steps to after creating the API URL. The rest of the slack instructions can be done before.

Error when not specifying the language for short sentences

I get the following error when entering a short sentence in slack:

    "2019-01-29T04:06:48.204324872Z stderr: Traceback (most recent call last):",
    "2019-01-29T04:06:48.204388111Z stderr: File \"/action/1/src/exec__.py\", line 64, in <module>",
    "2019-01-29T04:06:48.204397934Z stderr: res = main(payload)",
    "2019-01-29T04:06:48.204403216Z stderr: File \"/action/1/src/main__.py\", line 118, in main",
    "2019-01-29T04:06:48.204408612Z stderr: limit=2))",
    "2019-01-29T04:06:48.204413341Z stderr: File \"/usr/local/lib/python3.7/site-packages/watson_developer_cloud/natural_language_understanding_v1.py\", line 199, in analyze",
    "2019-01-29T04:06:48.204418246Z stderr: accept_json=True)",
    "2019-01-29T04:06:48.204422708Z stderr: File \"/usr/local/lib/python3.7/site-packages/watson_developer_cloud/watson_service.py\", line 495, in request",
    "2019-01-29T04:06:48.204427318Z stderr: info=error_info, httpResponse=response)",
    "2019-01-29T04:06:48.204431786Z stderr: watson_developer_cloud.watson_service.WatsonApiException: Error: not enough text for language id, Code: 422 , X-dp-watson-tran-id: gateway02-2418498013 , X-global-transaction-id: ffea405d5c4fd158902759dd",

I propose to add en as language and add instructions in README to change that if needed. Creating PR.

NLU_api

NLU service is now provided with api key instead of username and password. What changes i have to do in code?
error4

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.