I've made quite a few videos related to blockchains and cryptocurrencies here on YouTube, and
they've been doing very well.
The only problem is that they also attract a lot of scammers that post comments like
these.
They try to trick people into contacting them on Whatsapp with the promise of making them
a lot of money or recovering their lost Bitcoin wallet keys.
To prevent this from happing, I frequently go through the comments and mark the spam
ones, thinking that YouTube would learn to recognize them and block them straight away.
But that doesn't help at all.
The spammers just keep coming.
So I decided to create my own AI that can keep the comment section clean.
For this, I needed to build a text classifier, a form of machine learning where the computer
learns to assign a category to a piece of text.
In this case, the categories "spam" or "not spam".
Here's my plan: I'll write a script that uses the YouTube Data API to fetch comments and
store them in a CSV file.
Then, I will manually tag each comment as spam or not spam.
This data can then be used to train a text classifier.
Another script can then use this classifier to filter spam comments and delete them using
the same API.
At least...
That's the plan...
I started by writing the Python script that downloads comments from my channel and puts
them into a CSV file.
I let it run for about a minute, and it had already downloaded over 15,000 comments.
Now I had to open the CSV file and manually tag comments as spam or not spam.
I used a 0 to indicate non-spam and 1 to indicate spam.
Going through 15,000 comments is quite a lot of work, so I randomly went through them,
and in total, I marked about 800 comments as spam and 1000 as not spam.
Ideally, I would have the same amount of comments for both categories, but I decided that this
would be sufficient for the first try.
With the dataset prepared, I was ready to train a text classifier.
I decided to try out Apple's CoreML because it's supposedly super easy to use.
So, I created a Playground in Xcode, imported the CSV file, removed the comments that I
hadn't tagged, split into two groups for training and testing, and then finally passed this
data to MLTextClassifier.
This started the training process, and after a few seconds, I had a working CoreML model
which I could test with this handy tool.
I pasted in a newly posted spam comment, and the classifier returned 1, recognizing it
as spam.
Awesome!
But this is where the good news ends.
I want this model to run on a server so that it could periodically clean up the comments.
It turns out Apple doesn't want you to do that.
They want machine learning models to run on their devices.
Not on a server.
I was back to square one, and to be honest, I probably should've used TensorFlow or PyTorch
from the get-go.
But I just couldn't resist trying out CoreML.
So to do it properly, I fired up a Jupyter notebook and, because I'm such an expert,
I followed a tutorial on creating a text classifier with TensorFlow.
I pretty much repeated the same steps as I did before.
Loading the CSV file, deleting comments that hadn't been tagged, and splitting the data
into a training and testing group.
Then, I trained the model, which took a bit longer this time, and then I was ready to
test it again.
I took a spam comment, ran it through the model, and sure enough, it marked it as spam
with a confidence level of over 90%.
Then just to make sure, I wrote a spam comment myself to try and trick the AI.
But it was still pretty confident that it was spam.
At this stage, I was confident.
The model seems to perform quite well, even though the input dataset wasn't very large.
So it could only get better over time.
Alright, back to the plan.
All that's left now is to deploy this part to a server so it can run periodically without
me needing to do anything.
I happen to have a small home server that's ideal for this job.
So, I wrote a Python script that fetches the latest comments from YouTube, runs them through
the trained model, and then deletes comments that it marked as spam.
Just to be sure, I programmed it so that it would only delete comments when it was at
least 80% sure that it was spam.
I let this script run for a few hours, and it was quite effective.
But there's a flaw: if I want to improve the machine learning model over time, I need to
keep track of all the comments it has processed and correct it when needed.
The model could flag genuine comments as spam or spammers could change their writing to
bypass the AI.
I first thought about appending new comments to the same CSV file, but that's not nice
to work with.
So instead, I rewrote the script so that it would store comments to a Google Sheet.
And that made me realize that I don't need the first script anymore because both scripts
were essentially downloading comments from YouTube and putting them in a spreadsheet.
I also adapted the training script to train directly on the data stored in the Google
Sheet.
So now there's only one dataset that's constantly growing.
I then scheduled a task on my server to run the script every hour.
When it does, it downloads the latest comments, runs them through the classifier, deleted
spam comments, and stores the result in the Google Sheet.
Then, every couple of days, I open up the Google Sheet and see if it has misidentified
comments.
I can then correct those mistakes and retrain the classifier.
I'll probably automate this retraining as well.
So, how has it been holding up?
Well, so far, the script has removed 78 spam comments without me needing to do anything.
And it hasn't had any false positives yet!
It did miss 2 spam comments, though, but as I said, I'll solve that by retraining the
model with more data in the future.
So there you have it!
Before ending, I just to say something to the spammers here on YouTube: BRING IT ON!
Oh and YouTube, if you want to hire me, you know how to reach me!
That was it for this video!
I hope you like my little adventure into Machine Learning and if you did, consider subscribing.
The source code of everything I talked about is available on GitHub, so feel free to use
it yourself or to improve it.
Thanks for watching and till next time.