Valentine's Day is coming up and both love and machine learning are in the air. Some would use flower petals to determine if someone loves them or not, but developers might use a tool like TensorFlow. This post will go over how to perform binary text classification with neural networks using Twilio and TensorFlow in Python. Text +16782767139 to test out this text classification.

Prerequisites

Setup

Activate a virtual environment in Python 3 and download this requirements.txt file. Be sure to use Python 3.6.x for TensorFlow. On the command line run pip3 install -r requirements.txt to import all the necessary libraries and then to import nltk , make a new directory with mkdir nltk_data , cd into it and then run python3 -m nltk.downloader . You should see a window like this, select all packages as shown in the screenshot below:

Your Flask app will need to be visible from the web so Twilio can send requests to it. Ngrok simplifies this. With Ngrok installed, run ngrok http 5000 in the directory your code is in.

You should see the screen above. Grab that ngrok URL to configure your Twilio number:

Prepare Training Data

Make a new file called data.json to contain two arrays of phrases corresponding to labels: either "loves me" or "loves me not". Feel free to modify phrases to the arrays or add your own (the more training data, the better--this is not close to being enough but it's a fun start.)