A.lexa is going to be the new number one in hip-hop charts with her freestyle! This neural network is trained to rap in three different styles depending on the time period, and the best, it is all voiced by Amazon's Alexa.
Well, as said before, this is a neural network. More specifically, it is a recurrent neural network that use Markov chains to analyze and generate lines. The neural network then generate tuples with the desired rhyme and number of syllables. After that it goes through the lines generated by the Markov chains and match them to the tuples.
In our case we used three sets of inputs for training the network. That is, we used rap lyrics from 1990 to 1999, from 2000 to 2009 and from 2010 to 2017. After we have the rap, it is used as output in an Alexa skill, so she will rap the generated lyrics to us.
Before going straight to the fun part, you need a setup. There is a file called requirements.txt where you will find all the things you need to install in order to get this running.
A.lexa neural network accepts any types of lyrics that you would want to generate, so be creative! The only requirements are:
- Must be one file with all the lyrics
- Have only the lyrics, do not include "[intro]" or "[bridge]" aspects
- Last, avoid alphanumeric characters that aren't basic punctuation (. , ! ?)
The neural network is based off the one developed by robbiebarrat. Here is the link to his original repo: rapping neural network.
The network training is done by model.py as a base. The changes made in model_modern.py, model_nineties.py and model_two_thousands.py are just the artist
and rap_file
. This is done because we wanted to generate different trainings for the time periods. However, only one file named model.py is enough. On this file there are four main variables that need to be set:
train_mode
(line 14): Tells if the file will be run in training mode or no, it is a boolean value. In this case it should be set toTrue
. In model.py there are four main fields that are important to change:artist
(line 15): Will be the name of the .rap file, which is the result of the training of the neural.rap_file
(line 16): Is the file in which the rap will be written into.text_file
(line 289): It is where the program will get the lyrics to be trained or generated.
There are other variables at the begging that can be changed for program convenience and customization:
depth
: Changes the depth of the networkmaxsyllables
: Maximum number of syllabus per line
Now that you have customized your program, run python model.py
and let it work its magic.
Now you have a fully trained neural model! You can set the value of train_mode
back to False
if you don't wish to train any other models.
Now, you can see that two files were generated in the process, one ending in .rap and another ending in .rhymes. The first one is a saved network from previous training, and is used to generate lyrics with the set that it was trained for. The second extension is a list of rhymes that were identified on during training, so they can be used for the generated lyrics to rhyme too!
Now, to have your rap generated just run python model.py
! Just make sure that train_mode
is set to False
. You will see the generated lyrics on the terminal, and also written in the value of your rap_file
.
For integration with Alexa, we used Flask_ask and ngrok, as well as the Alexa Skill Kit from Amazon. For the integration we created a Custom Skill for Alexa, so we could input our values and all the commands that would trigger Alexa's rap. The files associated with the Alexa's skill for each time is alexa_frestyle_time.py.
To learn how to create a skill, you can access the tutorials in the Amazon page.
But instead of using a Lambda function from AWS services, we preffered to go with Flask_ask, which has the whole documentation plus tutorials online.
Generating the lyrics
- 2010-2017:
$ python model_modern.py
- 2000-2009:
$ python model_two_thousands.py
- 1990-1999:
$ python model_nineties.py
Ngrok Server
First you will need to setup a ngrok server in a seperate terminal window.
./ngrok http 5000
Depending on which era freestyle you would like to run: - 2010-2017:
$ python alexa_freestyle_modern.py
- 2000-2009:
$ python alexa_freestyle_twothousand.py
- 1990-1999:
$ python alexa_freestye_nineties.py
Speaking into Alexa
Once the Alexa is coneected and all setup, all that is required now is to say the specified keyword for the specific era you would like to hear a freestyle from. - 2010-2017: 'spit some fire'
- 2000-2009: 'freestyle two thousand'
- 1990-1999: 'give me a throwback freestyle'