I created a Deep Learning powered Discord Bot to react with smily 😎 | by Anurag Bhattacharjee | Towards Data Science

I have been reading a lot of paper articles lately, mostly on Deep Learning, RNN, CNN, LSTM[1], Transformer(Attention is all you need)[3], BERT etc. The list is only growing. But the best way to learn anything is to experience it. And also if I deploy a bot with my face, in my office server it can give proxy for me sometimes when I am busy reading. 😜 Just Kidding. As an experienced software developer its just an urge to implement the new knowledge acquired.
But there are some big hindrances. Where will I get so much data to train a Neural Net? Even if I manage data, I will need a machine with hi-fi configuration or at-least with a powerful GPU to train my model with the data. But, if there is a will there is way. 🤓 After a bit of research I found, DeepMoji 🧙🏻‍♂️. We can use pre trained models like DeepMoji to implement our bot. It’s a common practice now a days to work on a pre-trained model and make it better according to the need.
This is a perfect opportunity for me to make a BOT in discord(as my office communication is held in discord) and experience how a NLP process works using a varient of LSTM and attention mechanism. This BOT can listen to all the messages being received and react with emoticons.
DeepMoji is a model built on Keras and the backend on Tensorflow. Deepmoji is based on this paper[2] developped by Bjarke Felbo, Alan Mislove, Anders Søgaard, Iyad Rahwan and Sune Lehmann.
This paper shows that by extending the distant supervision to a more diverse set of noisy labels, the models can learn richer representations. Through emoji prediction on a dataset of 1.2 billion tweets containing one of 64 common emojis they have obtained state-of-the-art performance on 8 benchmark datasets within sentiment, emotion and sarcasm detection using a single pretrained model.
TorchMoji is a pyTorch implementation of the DeepMoji model built by huggingface. DeepMoji is built on python 2.7. So we will use TochMoji which is a implementation in python3. So we will use torchmoji for our implementation.
The most hectic part for me in this journey was the setup. Because I couldn’t find any proper example to implement torchMoji in a project. All the examples are the test cases used in jupyter notebook. But I am particularly interested in implementing it inside a project using my VS Code so that I can connect it with my discord BOT and send a response from there.
You can see my full code here: https://github.com/anuragbhattacharjee/discord-nlp-smily-bot/
*** Giving ⭐️ Star on github repositories helps us both grow. You will get all the updates of the project and I will get the inspiration to update the project.
So Let’s Start.
Setting up discord bot is straight forward. Navigate to application page. Create an application and then create a bot under your application.
After creating the BOT you will see their are several settings for the BOT. You will be needing your `BOT Token` under your Bot settings to connect it from your code.
If you go to the OAuth2 settings you can create a link with needed permission and by clicking the link a discord server owner can add the bot to their server. For testing purpose I created a server for myself and added the bot first before testing it in my office server.
At this point you should see the bot has joined in your server. But it’s offline. Because we are yet to connect it to our code to give it a life. 🤖❤
If you had any problem creating the discord bot, follow detail visual steps in the link below:
discordpy.readthedocs.io
I spent a lot of time here. Because the torchMoji was saying to use Conda to brew the project. But I was facing a round of errors while trying to download all the dependencies.
Though you can use any of the environment management tool as you prefer (conda/venv), you just need to know how to use it. Conda is mostly preffered while using in data science because they set up all the environment packages needed in one go. Which is easier. But Venv is more flexible I think.
Anyways, after failing a few trial and error, I finally found a way. I downloaded the torchMoji project and used it in my project as a package. I uses venv for my environment management. You can follow along my github repo for the setup. https://github.com/anuragbhattacharjee/discord-nlp-smily-bot/
After the setup is done. Now is the time to make some tests. I had to make some changes in the torchMoji library to work properly in my code module.
Now writing a code to use the torchMoji to predict a smily is pretty straight forward:
We can test it by running it in terminal
python3>>> from emojize import Emojize>>> e = Emojize()>>> e.predict("I am doing great today!"):smile:
This is where the fun begins. After your project is set, it’s now time to connect to your bot and make some tests.
To connect to discord we need to install discord.py package which should have already been installed if you have installed the requirements.txt.
Now to connect to discord bot we need to make an .env file and put our secret key in the .env file as DISCORD_TOKEN. I have made a env.demo file and pushed it in my git repo so that you can just use it. 😁
After the token is set, we can connect to discord.Client() and listen to several events of the client.
I have made a simple implementation just by sending a smily message and reacting to all messages. But we can make it smarter by inspecting the message if I have been mentioned or if there is my name in the message just to make it more human, because what kinda developer replies to every message in the thread. 🤷🏻‍♂️
But I am leaving it up to implement for your need. Don’t forget to fork and star the repo if needed.
Here are the details discord events that you can use:
discordpy.readthedocs.io
References:
[1] LONG SHORT􏰀-TERM MEMORY  by Hochreiter and Schmidhuber, 1997; Sutskever et al., 2014􏰅􏰊􏰊􏰈 https://www.bioinf.jku.at/publications/older/2604.pdf
[2] Using millions of emoji occurrences to learn any-domain representations for detecting sentiment, emotion and sarcasmby Bjarke Felbo, Alan Mislove, Anders Søgaard, Iyad Rahwan, Sune Lehmannhttps://arxiv.org/abs/1708.00524
[3] Attention Is All You Needby Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N. Gomez, Lukasz Kaiser, Illia Polosukhinhttps://arxiv.org/abs/1706.03762
[4] DeepMoji https://github.com/bfelbo/DeepMoji
[5] TorchMoji https://github.com/huggingface/torchMoji
I hope you enjoyed this blog. This is the second story of the series that I am writing on several topics of NLP. If you want to read more of my blogs than you can follow me on here in medium.
I am Anurag. Working professionally as a Software Engineer since 2014. I am currently working on an NLU micro-service for my current employer. Recently I am studying research paper to understand more about NLP. In my free time, I like to develop web and mobile apps that simple and useful. You can connect with me here:
🔗 https://www.linkedin.com/in/anuragbhattacharjee/🕸 http://anuragbhattacharjee.com
Happy Coding 😀