Source: Deep Learning on Medium
Develop and publish Text Generating Twitter bot
I saw many twitter bots who imitates US president Donald trump and published fake tweets generated using Neural Networks particularly RNNs.
Therefore I thought it would be cool if I build something similar for Indian prime minister Narendra Modi.
Before going into details of developing twitter bot, lets learn about RNNs.
Recurrent Neural Network:
RNN or recurrent neural network are a type of Neural Network where the output from previous step is fed into next state.
RNNs essentially acts as a kind of memory which stores sequential information, therefore they are widely used for text processing tasks
To develop are bot we will use textgenrnn package, click here for github repo.
Although the architecture implemented by this package can be implemented independently but still this package contains awesome helper functions that makes the task at hand easy.
Data preparation :
To prepare data for training we will need tweets of Narendra Modi.
Tweets can be extracted in excel sheet using website : exporttweet
After downloading the file use following code to clear the data:
import pandas as pd
import numpy as np
import re# Importing the data
data = pd.read_excel('../data/narendramodi_user_tweets.xlsx')# Saving only Tweets made in English
data = data[data['Language']=='English']['Text']#Function to clean tweet text
text = re.sub('http://\S+|https://\S+', '', blob)
text = text.replace('\n', ' ')
return text.strip()# Cleaning the text
data = data.apply(clean_text)# Converting data to list for training
tweets = 
for i in data:
After importing the data it is time to train it using textgenrnn
Textgenrnn has a default configuration and it works pretty well but it can be modified, but we will use default one as it gives good results for our purposes
from textgenrnn import textgenrnndef train_model(name='new_model.hdf5'):
speech_list = get_tweets_list()speech_gen = textgenrnn()speech_gen.train_new_model(speech_list)speech_gen.save(name)
This will take some time depending upon your configuration and hardware.
This step will generate 3 files : a weight file that will have your provided name, a vocab file named textgenrnn_vocab and a config file named textgenrnn_config.
The next step is to generate tweets.
To generate tweets we need to load the 3 generated file and use generate function provided by textgenrnn library
from textgenrnn import textgenrnntweet_gen = textgenrnn(weights_path='NarendraModi_Tweets.hdf5',vocab_path='textgenrnn_vocab.json',
This step will generate a tweet that can be published to a twitter account to generate tweets like this:
Before we can start publishing our tweets using our bot we will need to get api keys.
Twitter keys can be generated by using following steps.
Posting to twitter:
Up to this moment we have trained the model to generate tweets and build a method to generate tweets too.
Lets build the bot now:
import tweepyc_k = 'Save consumer key in this variable'
c_s = 'Save consumer secret in this variable'
a_k = 'Save access key in this variable'
a_s = 'Save access secret in this variable'
auth = tweepy.OAuthHandler(c_k,c_s)
api = tweepy.API(auth)def post_tweet():
text = get_tweet()
print('tweet failed')while True:
# this will make bot post tweet after every 2 hour
Since we have developed the bot, now we can deploy it to heroku
But before uploading to heroku we will need to make Procfile as follows:
- Make a new file named Procfile in project root, note that this file does not have any extensions
- Put following lines in Procfile: worker: python bot.py
After completing these steps we will need to do go to project root, open upon command line tool and use these following lines :
$ git init
$ git add .
$ git commit -m "My first commit"
$ heroku create
$ git remote -v
$ git push heroku master
Note : Before performing above steps you will need to install git and heroku-cli on your computer and login to heroku. more info