Predict My Message — Stopping Hateful Comments Online (Banner Image)
Predict My Message — Stopping Hateful Comments Online (Banner Image)

Predict My Message — Stop Hateful Comments Online with Perspective API

Communication is extremely vital for everything and everyone on this planet, as we are all interdependent on each other. Effective communication helps us to be cordial and co-exist in peace. However, it’s equally important to be very meticulous when we communicate, and not cause hurt to others with our words. You don't have to search long before you find toxic comments and hate speech online. Recent advancements in AI and sentiment analysis enables us to stop this hateful speech before it is posted.

Greetings!

Hello Makers,

I hope you all are doing good. This is my first ever blog that I am posting here, on the Mendix community platform. I am so excited and happy to share my knowledge and experience with all makers around the world!

First off, let me introduce myself; my name is Vijayabharathi. I am an Advanced Certified Mendix Consultant and I am honored to be working with some of the best brains at MxTechies — APAC’s biggest Mendix Technology Service Provider! Now, let’s dive into the blog without further ado.

Coming up

A brief summary of what I will be discussing in this blog:

1) The Evolution of communication and why healthy conversations are really important.

2) The Perspective API and how to consume Perspective API’s services in Mendix.

There’s an old saying in Tamil :

“Even if nothing else can be restrained, one must control his tongue; if not,

he will suffer because of the harm inflicted by his words.”

- The ancient Tamil poet, Thiruvalluvar.

A brief summary of the evolution of communication

In the beginning, conversations were face-to-face. In due course, they changed to written and telephonic communication. After the digital revolution, email and video chat communications became the order of the day.

You may have heard by now, that some giant social media company is currently working on Metaverse, which is supposed to become the future of communication!

Mark Zuckerberg — Metaverse concept animation

What is metaverse?

“In futurism and science fiction, the metaverse is a hypothetical iteration of the Internet as a single, universal, and immersive virtual world that is facilitated by the use of virtual reality and augmented reality headsets. In colloquial use, a metaverse is a network of 3D virtual worlds focused on social connection.” — Wikipedia

Why are healthy conversations important?

For example, let’s take gaming. Who doesn’t love to play games? In the ’90s, Arcade games like Super Mario, Contra and others were quite popular.

Arcade game — Contra

We didn’t have an option to chat with our friends. We could interact only with the characters in the game. Further down the evolution of games, we can play games with our friends through text, voice chat, etc.

Popular games like Valorant and PUBG mobile have an option to report toxic players. Professional gamers stream their content through streaming platforms like Twitch, YouTube, etc. Gaming streams are watched by a lot of kids. As per a survey, kids aged 16 to 24, watch gaming streams a lot. In group chats, some people may send toxic messages.

In situations like this, everyone watching the stream including the kids can be affected by these kinds of hurtful messages. In most streaming platforms, the Channel Moderator has an eye out for these kinds of ‘trolls’. However, we can’t expect a moderator to spend most of his time being a watchdog.

If we have the option to remove or block the toxic chats automatically, it will be of great help for both Streamers and Viewers. This is the point where Perspective API comes into place.

Perspective API in Mendix:

The Perspective API uses Machine Learning models to identify abusive comments. It will return a toxicity score based on the comments. If the toxicity score is high, we can consider that comment toxic. Let’s deep dive into the integration, and how we can implement this in Mendix.

Predict My Chat Mendix application Chat Interface

Here I have already created the sample Chat interface app using Mendix. To consume the services of Perspective API, we need an API key to continue further. Our efforts become highly crucial at this stage to achieve the desired results. Even Edison found the light bulb after 1000 unsuccessful attempts. Right?

Thomas Alva Edison Motivation Quotes

https://en.wikipedia.org/wiki/Thomas_Edison

Integrating the API:

Click on the link below to go to perspectiveapi.com

Scroll down a little bit, and look for the “Go to developer site” button. Click on the button now.

Once you click on the link, it will open the developer site in a new tab. Next, click on the “Get started” button. It will redirect to the Perspective API documentation site. Here, you can see the step-by-step documentation to request access to the API key.

Pre-requisites:

  • Google account: If you don’t have a google account, click on the link below to create an account in Google. It takes 2 to 5 mins only.

Once you have created a Google account, go to the Google cloud developer console dashboard page by clicking on this link.

https://console.cloud.google.com/getting-started?pli=1

Next, we need to create a project in our Google cloud platform to utilize Perspective comment analyzer API.

Google Cloud Platform Dashboard

Steps are already provided on the Perspective API documentation page to create a GCP project.

Once you are done creating the project, click on the menu icon in the top left corner. it will open the side navigation menu. Click on APIs and Services > Enabled APIs & Services.

Next click on, + Enable API & Services button at the top of the navigation menu. Then search for Perspective Comment Analyzer API.

Once, you found that service, Click and open the service to enable it. You may think, we have completed all the steps to get an API key. But this is not done yet. We need to fill out the Perspective API request form to get an API key through email (within an hour’s time). Because it requires a GCP project ID with enabled Perspective comment analyzer API services.

Just click on this link below now to get your API key within an hour.

https://docs.google.com/forms/d/e/1FAIpQLSdhBBnVVVbXSElby-jhNnEj-Zwpt5toQSCFsJerGfpXW66CuQ/viewform

It’s done now, we have completed almost 50% of the work makers! Awesome & way to go! Now, let’s come back to our Mendix chat application!

Here is my simple chat interface layout which I developed earlier. I am just calling the simple nanoflow in that send button to request our Perspective API to analyze whether the given chat is toxic or not.

PMC Chat Interface Layout — Mendix

This is what my nanoflow looks like. It’s very simple, isn’t it? 😉

Send button Nanoflow image

Here is the interesting part for all the developers which is debugging the flow. On the previous Mendix version V8 and before, we didn’t have an option to debug the nanoflow. Like Thor came up with Storm breaker to fight Thanos, our Mendix team came up with Nanoflow debugger in Mendix v 9 and above.

Thor, the Son of Odin — Avengers Infinity war climax scene

As Mendix developers, we can feel it. This is what we expected for a long time and upvoted in our Mendix forum idea section.

Mendix forum idea — Nanoflow debugging

In this nanoflow, I am getting two input parameters which are the Recipient Account object and the Chat object.

First, I am just retrieving the currently logged-in user account with the help of the Retrieve activity. I know this is not a best practice to use a System token. Instead of that, we can go with the Generalization approach to fetch the currently logged-in user account. I used this approach for the time being.

Next, we have one to one association between Protection mode and Account entity. In the Protection mode entity, we have one Boolean attribute to control whether we need to check for toxic comments or not with the help of this (IsEnabled) Boolean attribute. To improve the User experience perspective, we have given this control to the user. They can decide whether they can send a toxic message or not. But, on the other side, if recipients have enabled this protection, they will not receive that toxic message. Cool, right?

In the next decision activity, I am checking whether the user enabled the protection mode or not. If it’s false, it will check, whether the user sending any image or text message with this chat with the help of the Boolean attribute. If it’s true, it will go on to the next SUB_GenerateToxicScore sub-microflow call activity.

This is the microflow that will request our Perspective API. First, I check whether this chat has an Image or not. To avoid the REST Call error, I just added this validation to send only text messages to this API call.

Our input body JSON structure will look like this below.

{
"comment": {
"text": "'+$Chat/Message+'"
},
"languages": [
"en"
],
"requestedAttributes": {
"TOXICITY": {}
}
}

In the comment object, on the text attribute, we need to pass our text message to check whether it’s toxic or not. In the languages array, we need to mention the language code of the text message. Because Perspective API supports multi-languages like Arabic, Spanish, English, etc.

Perspective API supports multi-languages

I have tested for the English language, because I am unaware of any toxic words in any other language (Just kidding 😉).

We can just leave the requested Attributes object as it is for now. Once our JSON body is ready, we need to send this string variable to the next Call REST API activity.

Perspective API endpoint:

https://commentanalyzer.googleapis.com/v1alpha1/comments:analyze?key='+@ToxicityDetector.CONST_APIKey

This is the endpoint we need to send our POST request to Perspective API with our API key. I just stored my API key in a constant variable (CONST_APIKey).

In the HTTP headers section, we need to add the Content-Type: “Application/JSON”. In the previous section, we just created our JSON body string variable. So, I mentioned the same JSON type in that input HTTP header.

In the Request tab, I just passed that JSON string variable in the parameters section as below:

We have almost completed this configuration. The final part is the Response section. We can use the Import_Mapping process to get the response from Perspective API.

Perspective API response JSON structure will be like this below.

{
"attributeScores":{
"TOXICITY":{
"spanScores":[
{
"begin":0,
"end":6,
"score":{
"value":0.10428705,
"type":"PROBABILITY"
}
}
],
"summaryScore":{
"value":0.10428705,
"type":"PROBABILITY"
}
}
},
"languages":[
"en"
],
"detectedLanguages":[
"ar"
]
}

We can get the toxic score from the SummaryScore object value attribute. Once we get the toxic score from the SummaryScore object, we just need to convert this value into percentage format using the formatDecimal function.

formatDecimal((($SummaryScore/Value)*100),’##.##’)

Once we return the toxic score from the Sub microflow, we can use this score to validate whether it’s exceeding the limit which we set on the decision activity. In this case, I have set 70 as a limit. If the returned toxic score is more than or equal to 70 (x ≥ 70), we can consider that text toxic.

If the condition is true, instead of committing that chat object, we can show a warning message to the user and request them to use appropriate words. If the condition is false, we can fetch the SenderAccount object using the Retrieve activity and map that object into the Chat_SenderAccount association. With this approach, we can block toxic comments or messages automatically.

Below, I have attached the Sample Chat interface application with a working demo. Here, Duke is trying to send a toxic message to Mike, but our Perspective API easily identifies that toxic chat with the help of machine learning models in the backend. The intended message will not be sent to Mike’s account.

Final thoughts

We can conclude with the help of machine learning trained models, that we will be able to identify toxic messages and can block them automatically. We can filter out and delete all toxic messages with a single click. It would be very helpful in scenarios like one-to-one conversations with strangers or group chats amongst communities.

Thanks for your precious time. I hope you guys like this blog, and that it’s helpful in your future projects. I will see you soon in another interesting blog. Have a nice day makers and go make it 😊!!

Read more

From the Publisher -

If you enjoyed this article you can find more like it on our Medium page. For great videos and live sessions, you can go to MxLive or our community Youtube page.

For the makers looking to get started, you can sign up for a free account, and get instant access to learning with our Academy.

Interested in getting more involved with our community? Join us in our Slack community channel.

--

--