Skip to content

πŸ€–βœοΈ A Twitter bot written in Python trained with a recurrent neural network.

Notifications You must be signed in to change notification settings

anniedotexe/RNN-Twitter-Bot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

24 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

RNN Twitter Bot πŸ€–βœοΈ

Python 3.6 | 3.7 Open Source? Yes!

A Twitter bot written in Python. The recurrent neural network Twitter bot tweets generated text with a temperature of 0.7 from the trained model.

Important Notice

Starting February 9, we will no longer support free access to the Twitter API, both v2 and v1.1. A paid basic tier will be available instead 🧡

β€” Twitter Dev (@TwitterDev) February 2, 2023

Table of Contents


Getting Started

Make sure to follow Twitter's Automation Rules to avoid getting your account banned.

Prerequisites

To Train the Model

To Run the Bot

  • Python 3.6 or 3.7
  • Pip - a python package manager
    • Download this file, open a command prompt and navigate to the folder containing the get-pip.py installer, and run python get-pip.py to install
      • Run pip --version to check if it has installed correctly
  • Tweepy - an easy-to-use python library for accessing Twitter's API
    • Run pip install tweepy
  • textgenrnn - a python3 module to easily train your own text-generating neural network of any size and complexity on any text dataset with a few lines of code, or quickly train on a text using a pretrained model
    • Run pip install textgenrnn

Instructions

  1. Apply for Twitter Developer Access with the account you want the bot to be used for.

  2. Create a new Twitter Application to generate your private keys, secrets, and tokens.

Keys and Secrets

  • Make sure the app settings has Read and Write permissions.

App Permissions

  1. Create a file named credentials.py to hold your private information using the format below.
TWITTER_API_KEY="xxxx"
TWITTER_API_KEY_SECRET="xxxx"
TWITTER_ACCESS_TOKEN="xxxx"
TWITTER_ACCESS_TOKEN_SECRET="xxxx"
  1. Adjustments you can make in config.py to tweak the bot to your liking. (Make sure to follow Twitter's Automation Rules to avoid getting your account banned.)

    • model_name - (MUST CHANGE) Name of the model used for the weights and configuration files you downloaded from Colaboratory. Model Name
    • temperature - Level of randomness for the predicted text. Higher temperature will generate crazier text.
    • prefix - Set a prefix if you want each generated text to start with a given seed text.
    • n - Number of texts to generate.
    • max_gen_length - Maximum number of characters for each text generated.
    • max_tweet_length - Maximum number of characters for each tweet to have.
      • If the generated text is too long, it will be split into multiple tweets at this length.
      • Set to 280 (Twitter's character limit for tweets) if you are not adding anything to the end of the tweets.
    • add_to_tweet - Optional if you want to add text to end of every tweet.
      • Set to None if you do not want to use.
    • delay - Time to wait in between each tweet in seconds.
    • min_tweet_length - Minimum number of characters in the tweet, will ignore tweets that are not long enough.
      • Set to 0 if this is not relevant to you.
  2. Use the textgenrnn Colaboratory Notebook to train your RNN.

  3. Download the weights and configuration files from the Colaboratory Notebook (as shown below) and put them in the model directory

Download Weight and Config Files

  1. Install all required packages needed to run the script
pip install -r requirements.txt
  1. Run the script. Enjoy your Twitter bot!
python twitter-ai-bot.py

File Structure

RNN-Twitter-Bot
|-- model
|   |-- yourmodelname_config.json
|   |-- yourmodelname_vocab.json
|   `-- yourmodelname_weights.hdf5
|-- credentials.py
`-- twitter-ai-bot.py

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. Please make sure to update tests as appropriate.

How To Contribute

  1. Fork the repository to your own Github account.
  2. Clone the project to your machine.
  3. Create a branch locally with a succinct but descriptive name.
  4. Commit changes to the branch.
  5. Following any formatting and testing guidelines specific to this repo.
  6. Push changes to your fork.
  7. Open a Pull Request in my repository.

Creator / Maintainer

Annie Wu (anniedotexe)

If you have any questions, comments, or concerns, feel free to contact me below.

Connect via Email

This project was created for educational purposes of learning development, documentation, and deployment and for personal and open-source use.

Default values of the project are used to run the Twitter account @dickinson_rnn, a bot that was trained to generate text with Emily Dickinson poems.

If you like my content or find this code useful, give it a ⭐ or support me by buying me a coffee β˜•πŸ˜€

Buy Me A Coffee


Acknowledgments


Additional Information