Develop and publish Text Generating Twitter bot

Source: Deep Learning on Medium

Develop and publish Text Generating Twitter bot

Link to GitHub Repo

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.

Pic taken from — http://www.wildml.com/2015/09/recurrent-neural-networks-tutorial-part-1-introduction-to-rnns/

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
def clean_text(blob):
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:
tweets.append(i)

Data Training:

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)
train_model('textgenrnn_weights.hdf5')

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.

Generating 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',
config_path='textgenrnn_config.json')
def get_tweet(tweet_length=140):
return tweet_gen.generate(max_gen_length=tweet_length,top_n=5,
temperature=0.5,return_as_list=True,n=1)[0]

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)
auth.set_access_token(a_k,a_s)
api = tweepy.API(auth)
def post_tweet():
try:
text = get_tweet()
api.update_status(text)
except:
print('tweet failed')
while True:
post_tweet()
time.sleep(2*60)
# this will make bot post tweet after every 2 hour

Deploying bot:

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:

  1. Make a new file named Procfile in project root, note that this file does not have any extensions
  2. 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

Thanks for reading the article and good luck for your projects.