Building an End-to-End Hate Speech Detection System on Google Cloud

Subrahmanya Joshi
Nerd For Tech
Published in
5 min readMar 11, 2023

--

Photo by Mercedes Mehling on Unsplash

The Internet is an incredibly vast place. The amount of information there is ridiculously huge and so is the number of people interacting with it. Social Media Platforms are a major part of the modern internet in which people create, share, and/or exchange information and ideas with other people. We see all kinds of people and it’s pretty common for uncivilized people to abuse/harass other creators/people. Obviously, this is harmful to their mental health and most social media platforms have some kind of active hate/abuse speech detection systems running all the time.

In this article, I will be explaining how to build a real-time, end-to-end hate speech detection system that runs on Google Cloud Infrastructure. I will be using YouTube comments as my main data source.

Architecture

The pipeline is built and runs on Google Cloud Dataflow. Dataflow is Google Cloud’s offering for streaming analytics. It also has autoscaling, so there’s no need to worry about adding/removing resources.

The pipeline needs to process the comments in real-time and in a streaming fashion. Hence we will create Google Cloud Pub/Sub topics and connect them to the pipeline.

For the actual classification of the comments, we will use Google Cloud’s Natural Language Processing API’s Sentiment Analysis endpoint. Based on the sentiment score of a comment we can take a decision as to whether the speech is hateful or not. Hateful comments will be having extremely negative sentiments. The pipeline will consume the comments from YouTube videos in a streaming fashion from a Pub/Sub topic, and the results can be sent either to another PubSub topic or can be stored in the BigQuery tables.

Here is an overview of the pipeline running in dataflow

Source: Image by the author.

Components of the pipeline

1. From Pubsub

This is a prebuilt component that connects the pipeline to the specified Pub/Sub topic and starts consuming the data.

2. Load Comments

The messages consumed from the previous step will be in bytes format. These are converted to JSON dictionary format in this step.

3. Preprocess Comments

This step applies various preprocessing techniques to the actual comment text and transforms it. These techniques help to improve results. Some of the techniques that I have used:

  • Removing extra spaces, hashtags, and new line characters.
  • Removing hyper reference strings in comments.
  • Removing ‘@’ mentions and URLs.
  • Removing extra spaces, special characters, and numbers.
  • Removing emojis.
  • Expanding short-form words like ain’t, I’m, can’t, etc.

4. Detect Sentiments

The Google Cloud NLP API’s sentiment analysis feature is used to get a sentiment value for the preprocessed comment text received from the previous step.

5. Prepare Results

The comment is classified as normal or hate speech based on whether the sentiment value is less than -0.6 or not. (This value was chosen after running a lot of experiments). This step also deletes some unwanted fields in the message dictionary.

6. Divide Results

The purpose of this step is to separate hate speech and normal speech comments. This needs to be done as BigQuery has separate tables for each of them.

7. Data sinks

The pipeline currently has two data sinks. Pub/Sub and BigQuery. Hate speech comments are sent both to a Pub/Sub topic and a BigQuery table. Normal speech comments are sent only to a BigQuery table.

The hate comments being sent to the Pub/Sub topic can then be consumed by any downstream application and proper actions can be taken against the person who posted it. The data in BigQuery tables can be used for analytical purposes.

The system is real-time and is a complete end-to-end as from one end any application can keep sending the comments to the input topic and the results will be available on the other end right after that without much delay.

In my experiments, I have created a simple script that scrapes comments and replies from youtube videos related to a specific topic and sends them to the Pub/Sub topic.

Once we build and launch the pipeline on DataFlow, it connects to the input topic and keeps waiting for the data indefinitely. As soon as the producer script starts sending scraped comments to the topic, they will be consumed and processed by the pipeline. The pipeline also scales automatically according to the frequency of the incoming data.

Implementations

  • Pipeline Components
  • Pipeline
  • YouTube comment scraper and Pub/Sub producer

Results

I ran the pipeline with the comments fetched from videos related to the ‘Johnny Depp and Amber Heard’ topic. I chose this topic specifically because during their trial, amber heard seems to have gotten harassed everywhere on social media. The following are the screenshots of BigQuery tables for hate speech and normal speech results.

Hate Speech comments

Source: Image by the author.

Normal Speech comments

Source: Image by the author.

Conclusion

As can be seen from the results, the system is pretty good at detecting hate speeches. Obviously, it’s not perfect and there’s so much room for improvement as we are using a sentiment analysis tool and not something that was specifically built to detect hate and abusive speeches. This system also struggles to properly classify sarcastic comments. The next step is to develop a custom model specifically for hate speech detection and use that in place of the sentiment analysis tool.

Thanks

--

--

Subrahmanya Joshi
Nerd For Tech

AIOps (Artificial Intelligence for IT Operations) Engineer at HPE | Google Cloud Certified Machine Learning and Data Engineer.