├── beat.mp3 ├── kanye_west.rap ├── requirements.txt ├── speech.py ├── kanye_west.rhymes ├── neural_rap.txt ├── README.md ├── model.py └── documented_model.py /beat.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/robbiebarrat/rapping-neural-network/HEAD/beat.mp3 -------------------------------------------------------------------------------- /kanye_west.rap: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/robbiebarrat/rapping-neural-network/HEAD/kanye_west.rap -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | pronouncing==0.1.2 2 | pyttsx==1.1 3 | markovify==0.5.4 4 | Keras==1.2.2 5 | numpy==1.12.1 6 | -------------------------------------------------------------------------------- /speech.py: -------------------------------------------------------------------------------- 1 | import pyttsx 2 | import subprocess 3 | import threading 4 | from time import sleep 5 | from threading import Thread 6 | 7 | # VARIABLES 8 | beat_to_play = "beat.mp3" 9 | slowdown_rate = 35 # higher value means slower speech... keep it between ~50 and 0 depending on how fast the beat is. 10 | intro = 24 #amount in seconds it takes from the start of the beat to when the rapping should begin... how many seconds the AI waits to start rapping. 11 | 12 | 13 | def play_mp3(path): 14 | subprocess.Popen(['mpg123', '-q', path]).wait() 15 | 16 | 17 | engine = pyttsx.init() 18 | 19 | def letters(input): 20 | valids = [] 21 | for character in input: 22 | if character.isalpha() or character == "," or character == "'" or character == " ": 23 | valids.append(character) 24 | return ''.join(valids) 25 | 26 | lyrics = open("neural_rap.txt").read().split("\n") #this reads lines from a file called 'neural_rap.txt' 27 | #print lyrics 28 | rate = engine.getProperty('rate') 29 | engine.setProperty('rate', rate - slowdown_rate) 30 | voices = engine.getProperty('voices') 31 | 32 | wholesong = "" 33 | for i in lyrics: 34 | wholesong += i + " ... " 35 | 36 | 37 | def sing(): 38 | for line in wholesong.split(" ... "): 39 | if line == "..." or line == "": 40 | for i in range(18): 41 | engine.say(" ") 42 | else: 43 | engine.say(str(line)) 44 | engine.runAndWait() 45 | 46 | def beat(): 47 | play_mp3(beat_to_play) 48 | 49 | 50 | Thread(target=beat).start() 51 | sleep(intro) # just waits a little bit to start talking 52 | Thread(target=sing).start() 53 | -------------------------------------------------------------------------------- /kanye_west.rhymes: -------------------------------------------------------------------------------- 1 | 2 | s' 3 | 10 4 | 30 5 | 1 6 | 11 7 | 31 8 | 12 9 | 32 10 | k2 11 | 4 12 | 5 13 | 05 14 | 25 15 | 45 16 | 55 17 | 26 18 | g6 19 | 7 20 | 8 21 | 9 22 | 99 23 | a 24 | aa 25 | ba 26 | ca 27 | da 28 | ga 29 | ha 30 | ia 31 | ja 32 | ka 33 | la 34 | ma 35 | na 36 | pa 37 | ra 38 | sa 39 | ta 40 | va 41 | ya 42 | za 43 | ab 44 | eb 45 | ib 46 | ob 47 | ub 48 | ac 49 | fc 50 | ic 51 | nc 52 | oc 53 | ad 54 | ed 55 | id 56 | ld 57 | nd 58 | od 59 | rd 60 | be 61 | ce 62 | de 63 | ee 64 | fe 65 | ge 66 | he 67 | ie 68 | ke 69 | le 70 | me 71 | ne 72 | oe 73 | pe 74 | re 75 | se 76 | te 77 | ve 78 | ye 79 | ze 80 | af 81 | ef 82 | ff 83 | if 84 | lf 85 | of 86 | ag 87 | eg 88 | gg 89 | ig 90 | ng 91 | og 92 | rg 93 | ug 94 | ah 95 | ch 96 | gh 97 | hh 98 | oh 99 | ph 100 | sh 101 | th 102 | uh 103 | ai 104 | ci 105 | gi 106 | ii 107 | ki 108 | li 109 | mi 110 | ni 111 | ri 112 | ti 113 | zi 114 | pj 115 | tj 116 | ck 117 | ek 118 | lk 119 | nk 120 | ok 121 | rk 122 | sk 123 | al 124 | el 125 | hl 126 | il 127 | ll 128 | ol 129 | rl 130 | tl 131 | ul 132 | xl 133 | am 134 | bm 135 | em 136 | im 137 | lm 138 | mm 139 | om 140 | rm 141 | sm 142 | um 143 | an 144 | en 145 | gn 146 | in 147 | mn 148 | nn 149 | on 150 | rn 151 | un 152 | wn 153 | yn 154 | ao 155 | co 156 | do 157 | eo 158 | fo 159 | go 160 | io 161 | ko 162 | lo 163 | mo 164 | no 165 | oo 166 | po 167 | ro 168 | to 169 | vo 170 | zo 171 | ap 172 | ep 173 | ip 174 | lp 175 | mp 176 | op 177 | pp 178 | rp 179 | up 180 | ar 181 | cr 182 | er 183 | ir 184 | jr 185 | or 186 | pr 187 | rr 188 | ur 189 | 's 190 | 0s 191 | as 192 | bs 193 | cs 194 | ds 195 | es 196 | gs 197 | hs 198 | is 199 | js 200 | ks 201 | ls 202 | ms 203 | ns 204 | os 205 | ps 206 | rs 207 | ss 208 | ts 209 | us 210 | vs 211 | ws 212 | ys 213 | 't 214 | at 215 | ct 216 | et 217 | ft 218 | ht 219 | it 220 | lt 221 | nt 222 | ot 223 | pt 224 | rt 225 | st 226 | tt 227 | ut 228 | au 229 | cu 230 | ru 231 | uu 232 | lv 233 | ov 234 | ew 235 | lw 236 | ow 237 | ww 238 | ax 239 | ex 240 | ix 241 | ox 242 | ay 243 | by 244 | cy 245 | dy 246 | ey 247 | fy 248 | gy 249 | hy 250 | ky 251 | ly 252 | my 253 | ny 254 | oy 255 | ry 256 | sy 257 | ty 258 | vy 259 | zy 260 | ez 261 | iz 262 | oz 263 | tz 264 | yz 265 | zz -------------------------------------------------------------------------------- /neural_rap.txt: -------------------------------------------------------------------------------- 1 | And bow so hard when it wasn't his?! 2 | I'm living in the ride, throw a movie 3 | Ayo, I'm sitting on top of this fake-ass party 4 | Me drown sorrow in that ghetto university 5 | A creative way to promote some pussy 6 | Sometimes it's so un-Camry 7 | You got a history 8 | How much they don't ball out, boy 9 | Stop running up my money 10 | Don't worry about my ebony 11 | And my only enemy 12 | About how I feel like Christmas officially 13 | So are we speaking metaphorically? 14 | And I'm about to get along with you sexually 15 | And it's still so lonely 16 | You know it's especially 17 | It's a party in your face looking all Captain Crunchy 18 | I'm not too laughy 19 | My girl a superstar all from a page of your body 20 | But I'm a douche then put me on the keys right now she just some lady 21 | Come and meet me in your eyes, that you, try not to act moody! 22 | They see a black street like Chauncey 23 | I played Ready for the one, baby 24 | We killing the game be 25 | Fuck you and all the broke motherfuckers passed away 26 | That's why I spit it so hard I could buy my way 27 | And got the hundred with the text say 28 | Ya'll ride the beat go “duh duh da” 29 | Made to make it hard for her girl birthday 30 | A hundred dollar bills, this ain't okay no more today 31 | I went and told her, “Get away” 32 | It feel like O'Shea 33 | I'll show you the next 34 | You would think these niggas soon as I did, all the concerts so owww 35 | I thought you could unfollow 36 | Rose gold Jesus piece with the groupies in the studio 37 | I know she love Beyonce, let me on Rodeo 38 | On one of my life like there's no tomorrow 39 | So you could unswallow 40 | Inspiration for they life, they souls, and they just not new 41 | And what we do 42 | I hit the radio them faggots ain't let me upgrade you 43 | So many records in my eyes, whoo! 44 | If I'm unfaithful, blame it on the past all we got that, that thing go 45 | You can't say no 46 | And I hang up and you don't speak to me though 47 | I'll move my family we know 48 | One time let it crash, burn slow 49 | Lick the black, get the C.R.E.A.M., same story, yo 50 | She went from being Joe Blow 51 | If I lay low 52 | I done did dirt and went to church and let the douja blow 53 | I'mma get a nut 54 | Why you trying to cut 55 | Dog, I was going through problems, you gotta fake em out 56 | Let me remind you, you the shit out anyway, I hope they post it 57 | Knew I was a Jeremy Scott? 58 | Just tell me when I got a man or not 59 | What if we both honest 60 | Telling me that I let you know he's stressed 61 | I met him at the hottie first 62 | Now that I'm gassed 63 | And the days been crazy and I start 64 | Let's make a priest faint 65 | Turn the radio down I mean ahhhhh I'm breaking out the word can't 66 | In love with the drop off outside of the night, you ain't me, what you felt 67 | They tryna make it melt 68 | That's when I see my only focus is staying on some silly shit 69 | Seen with some bullshit 70 | Kanye the most press kit 71 | You see how I feel your spirit 72 | Cold as the chorus hit 73 | As kids we used to do it with the kids laughed 74 | Dad cracked a joke, all the cars, the clothes, the lights, the stress of life left 75 | The kids been calling you the other got Bridget 76 | Realized I got weed, drink, and yeah we fucked 77 | Fronting though: yo, I lived that 78 | Downtown mixing fabrics tryna find where to raise my kids at 79 | Cause they make noise 80 | And kept little brother at bay, at a ring from Tiffany's 81 | Just a few toys 82 | We used to pull em up outta pills, rob that CVS 83 | I'm so Lazer, I'm so Flava, I'm so self-conscious 84 | Feelin better than the music is the flow that is only obvious 85 | Historically I'm kicking bitches out like we famous 86 | 4-door - do to be spontaneous 87 | First class with the Christmas gifts 88 | And I'm like, if you follow the charts 89 | Or rock some mink boots in the land where niggas plug like outlets 90 | AHHHHH, get out of hits 91 | I'mma make the donuts 92 | Basquiats, she learning a new fitted, and some tints? 93 | It seems we living no limits 94 | Make you touch the exhibits 95 | You still ratchet cause they need more seats 96 | And see if Jessica Stam got the facts to back this 97 | I mean, we walked in our business 98 | When he came in first class 99 | She said dude, that's cool I fucked the waitress 100 | Always said what the cops taught us 101 | I put him all in the presence of greatness 102 | What the, what the fuck you hard on the screen that's not ours 103 | You gotta love it like it was for all Of these years? 104 | We all went to church and let the dream turn to Kruegers 105 | Just forget I said WOWZERS! 106 | Grab your remote, for the World's game, this is the pioneers 107 | Too Short called, told me stick to the top like elevators 108 | Begging one of these hoes doin sit-ups 109 | I played Ready for the church steps 110 | And I love you for that Benz I push miracle whips 111 | Chased the good life my whole family whips; no Volvos 112 | My freshman year I was into trios 113 | And I'll never let him back in a room full of no's? 114 | I'm about to have twins 115 | With all the years and the TV screens 116 | Get away from my spins 117 | Your boy back on his green like croutons 118 | It's a party in your life for seasons 119 | I guess that's why they got it all really means? 120 | Who's the new Jesus chains? 121 | Got a middle finger pedestrians 122 | This nigga graduated at the airport and followed your dreams 123 | Real nigga back to their arms 124 | If you put that glacier on your beams 125 | Act now and the screams 126 | Hey baby what it is with females 127 | You drunk and hot girls 128 | It's like the promised land of the bills 129 | That chick know she bad, tell by the ice and jewels 130 | Now everybody got the facts about these plaques 131 | 30,000 feet up and you and I can catch the beat rocks 132 | This the real millionnaires or the stacks 133 | Bow on our income tax 134 | Look at my life to Stephen King's 135 | I know of some things she don't wanna do the ugliest things 136 | The Thriller version of Mase next to fags 137 | If somebody woulda told me you was so Fugees 138 | I guess it's hard to find where the tissue is 139 | I know they rhinestones 140 | For the tracks I done lost and made the new slaves 141 | And tell me where I come in deuces 142 | So here's a few pairs of new Airs 143 | Bought the chain throw off the plane, Konnichiwa, bitches 144 | Homie, I don't use rubbers, need more drinks and less lights 145 | But with my folks 146 | And in her eyes 147 | Bet they give me scoliosis until I comatoses 148 | And you know to get you waves 149 | Hard with all these girls just fighting over the damn lames 150 | Really Doe told you to the skies 151 | I mean the bitch so fresh I had a belt in two classes 152 | I'm Kan, the Louis Vuitton feathers, now that some fly maids 153 | Runaway slaves all on the leaves 154 | And I was in limbo I raised the gas prices 155 | Yeezy, Yeezy, take a lot more than a buzz 156 | And I know he love you then they got white friends 157 | That's good for my love and have hater kids 158 | Like a porn star, I'm a Chicagoan till Chicago ends 159 | Would you ride with Ne-Yo, if he wasn't talking about coke and birds 160 | The opportunity, the proper top of our body 161 | Soon as this beat was 162 | Thinking back to Vegas 163 | Especially if you acquire those Alaia's 164 | Is hip hop just a virgin, a baby 165 | We above the law, we don't really go away 166 | I wish I could buy my way 167 | And I know I did it when we don't care what people say 168 | Bottle after bottle till we got is today 169 | Modern day MJ with a porn star, I'm a straight up to the operas 170 | She got a couple of seconds? 171 | A million out the Benz 172 | So they could never say we did the numbers 173 | The doctor said I hit the movies 174 | And excuse me miss, I forgot your Ray Bans 175 | Was to make up for days 176 | I mean, you can brag to all your relatives 177 | Yeah I got the money on clothes with logos 178 | Yeah we still gonna annihilate the festivals 179 | That if you let her 180 | Where do the CPR 181 | Maybe you could unfollow 182 | Soon as the winter 183 | Cause with my girl acting like Mr 184 | Wedding in June, what could be your damn liar 185 | I just blessed to be down with the yay as a steam that power 186 | Get it, roll the weed and let the water wash over my caesar 187 | Dawg, if I could build a new sweater 188 | Grammy night, damn right, we got that, that thing clear 189 | I'm way up, I feel like V.A. or the hangover? 190 | I sold my soul through the wire 191 | Probably be cremated before I get more older 192 | Make it feel like it's things I see my daughter 193 | Ohhh, why she says she gave me his number 194 | Who said her favorite rapper was the best summer ever 195 | Old folks talking bout Linda, from last September 196 | We ball in 2 seats, and you out of my Jeep, but not no Kia 197 | We give a damn if you look at me for? 198 | I'll touch every curve of your favorite author 199 | And we don't live here 200 | Last night ain't go to class; Bueller 201 | I had to let you know that now it's over 202 | I heard that when I get a hold of you 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Rapping-neural-network 2 | This is a generative art project I made for my high school's programming club - which ~I'm the president/founder of~ I was the president/founder of until I graduated. 3 | 4 | It's a neural network that has been trained on Kanye West's discography, and can use any lyrics you feed it and write a new song *word by word* that rhymes and has a flow (to an extent). 5 | 6 | Quartz did a really nice profile on me and the program here; https://qz.com/920091/a-west-virginia-teen-taught-himself-how-to-build-a-rapping-ai-using-kanye-west-lyrics/ 7 | 8 | ## UPDATE: Here's a Google Colab notebook that runs the project: https://goo.gl/GHSWzZ 9 | 10 | # Let's hear something it made 11 | Okay... Here's a song it wrote *word by word* using kanye's discography - excuse the vulgarities, the neural network wrote it and not me. 12 | https://soundcloud.com/rapping_neural_network/networks-with-attitude 13 | 14 | # Lyrics to above song 15 |
16 | Click to expand 17 | 18 | Bust a playa with the kids I never had 19 | 20 | All his time, all he had, all he had, all he had 21 | 22 | Most you rappers don't even stop to get the most press kit 23 | 24 | Playas is jealous cause we got the whole city lit 25 | 26 | But without it I'd be worried if they playing that bullshit 27 | 28 | You wanna complain about the nights even wilder 29 | 30 | I swear to God I hope you have got to hear 31 | 32 | I'll touch every curve of your favorite author 33 | 34 | No more wasting time, you can't roam without Caesar 35 | 36 | Back when Gucci was the best summer ever 37 | 38 | Before Cam got the hundred with the peer pressure 39 | 40 | She walking around looking like Herve Leger 41 | 42 | So next time I'm in between but way more fresher 43 | 44 | And they say you never know, never never NEVER 45 | 46 | ... 47 | 48 | You the number one I'mma beat my brother 49 | 50 | And I know a sign when I heard it's the magic hour 51 | 52 | Get Olga Kurylenko, tell her to do better 53 | 54 | That know we get them hammers, go on, call the lawyer 55 | 56 | 57 | But still supported me when I get richer 58 | 59 | This my first pair of shoes, I made the Bulls play better 60 | 61 | Or use my arrogance as a wholesaler 62 | 63 | Prince Williams ain't do it can't be your damn liar 64 | 65 | You say I dress white, but my broad way thicker 66 | 67 | If I be Don C, we got that, that thing clear 68 | 69 | I dropped out of your body like a wrestler 70 | 71 | I can't believe I'm back to a cold killer 72 | 73 | Lady Eloise I need another lover 74 | 75 | He loved Jesus when he off the power 76 | 77 | So I pour the potion, so we gone dress whiter 78 | 79 | Old folks talking bout Linda, from last September 80 | 81 | Might spend 50 racks on my life like a fucking loser 82 | 83 | ... 84 | 85 | He don't even stop to get this difficult 86 | 87 | She told me that I stayed at home with my own vault 88 | 89 | She's so precious with the space for the safe belt 90 | 91 | Girl he had the strangest feeling lately 92 | 93 | Fuck you playa I know it's especially 94 | 95 | But let some black people to think logically 96 | 97 | Fire Marshall said I could give you this feeling 98 | 99 | And wrote hooks about slaves that the youth is missing 100 | 101 | I know this part right here, history in the ring 102 | 103 | Well I guess she was messin wit me when I'm cumming 104 | 105 | I'm way better than some head on a chain gang 106 | 107 | On a scale of this, and now you doing your thang 108 | 109 | Y'all I know you're living your life so exciting 110 | 111 | Started a little blog just to say nothing 112 | 113 | I'mma need a fix, girl you was celebrating 114 | 115 | Mayonnaise colored Benz I get my engine revving 116 | 117 | And my chick in that old lady on Boomerang 118 | 119 | Wifey gonna kill me, I do a gangbang 120 | 121 | I put an angel in your life so exciting 122 | 123 | Right when I do it right if you was celebrating 124 | 125 | I was in Benzes, I was still at Burger King 126 | 127 | It feel like this but playas don't know what you're drinking 128 | 129 | Really Doe told you come on homie they wilding 130 | 131 | I swear this right here, feel free to sing along 132 | 133 | Shoulda known that was gonna come as it's good I'm young 134 | 135 | ... 136 | 137 | These playas read the pimp manual, but I just want your girl you was clubbin' 138 | 139 | First I spin around and vomit, then I made it from the day you just pretendin' 140 | 141 | But I bet you they respect the name Kanye from the heart, y'all all frontin' 142 | 143 | We in the same thing like a fat trainer takin a bite or somethin 144 | 145 | Abbey Lee too, I'm a jerk, you need that happy beginnin', middle and endin' 146 | 147 | That mean I forgot better shit than you ever heard about all this name callin' 148 | 149 | Cause I can never be as laid back as this flow end, I'mma let Mos begin 150 | 151 | And I bet you they respect the name Kanye from the heart, y'all all frontin' 152 | 153 | ... 154 | 155 | My mama used to stay recession free 156 | 157 | All my friends says implants is a beat from Ye 158 | 159 | I want is what I do, act more stupidly 160 | 161 | With no response make you wait longer than A.C. 162 | 163 | Loud as a shorty I looked up to this degree 164 | 165 | Young Walt Disney, I'ma tell you once ting 166 | 167 | Straight to jail, yo, in a Bentley shining 168 | 169 | Why you trying to make it just ring and ring 170 | 171 | Now why would I listen to T-Pain and sing 172 | 173 | Everything I throw them all laughing 174 | 175 | So glad I ain't gotta borrow nothing 176 | 177 | So I promised her everything 178 | 179 | I've been waiting on this rocket, Yao Ming 180 | 181 | I don't drink the drama that your dude bring 182 | 183 | Kanye West is the making of a romantic 184 | 185 | Play strings for the World's game, this is tragic 186 | 187 | ...and this is the making of a romantic 188 | 189 | I done wore designers I won't get specific 190 | 191 | The layers to my roots, I'm like a paraplegic 192 | 193 | Come on, let's take a lot more than the music 194 | 195 | I mean, after all the way we was magic 196 | 197 | ... 198 | 199 |
200 | 201 | ## Setup 202 | 203 | Install (with python 2.x) 204 | 205 | pip install -U -r requirements.txt 206 | 207 | ## Usage 208 | 209 | ### See [documented_model.py](https://raw.githubusercontent.com/robbiebarrat/rapping-neural-network/master/documented_model.py) for notes as python comments. 210 | 211 | ### Data preperation 212 | **If you'd like to use Kanye's lyrics - skip this section** 213 | `Lyrics.txt` comes with Kanye's entire discography in it. You can either use this, or fill it with other lyrics. 214 | 215 | Guide to using your own lyrics with `lyrics.txt` 216 | * Avoid including things like "[bridge]" or "[intro]" 217 | 218 | * Seperate each line by a newline 219 | 220 | * Avoid non alphanumeric characters (besides basic punctuation) 221 | 222 | * You don't have to retype everything - just copy and paste from some lyrics website 223 | 224 | ### Training 225 | **Skip this part if you are using the default kanye lines** 226 | 227 | * In `model.py`, change the variable `artist` to the name of the new artist you've used in `lyrics.txt` 228 | 229 | * In `model.py`, change the variable `train_mode` to `True` 230 | 231 | * Run the program with `python model.py`, and allow training to finish. 232 | 233 | ### Generating raps 234 | 235 | * In `model.py`, if you've trained a new network, the variable `train_mode` will be `True`, set this back to `False` 236 | 237 | * Run the program with `python model.py` 238 | 239 | * The rap will be written to the output of your terminal, and also to a file called `neural_rap.txt` 240 | 241 | ### Performing raps 242 | 243 | * speech.py will "rap" the generated songs with a text to speech over a generic rap beat (`beat.mp3`), just run `python speech.py` 244 | 245 | ## How it works 246 | 247 | Alright, so basically a markov chain will look at the lyrics you entered and generate new lines. Then, it feeds this to a recurrent neural net that will generate a sequence of tuples in the format of 248 | 249 | (desired rhyme, desired count of syllables) 250 | 251 | The program will then sift through the lines the markov chain generated, and match the lines to their corresponding tuples. The result is the rap. 252 | 253 | # Future goals: 254 | 255 | 1. Use lyrebird.ai to have it rap in Kanye's voice... I'll probably have to hire a Kanye voice impersonator to supply me with 5 minutes of audio to train the net with though... Either that or jailbreak an old iPod and use it to do text to speech with Siri's voice. 256 | 257 | 258 | 2. Bring back the seperate 'verses' and appropriate pauses that the first version had. 259 | 260 | 261 | 3. Generative rap beats that it can rap over. 262 | 263 | 264 | 265 | Once I get the lyrebird.ai thing working where it can rap and imitate someone's voice, I really want to do some type of 'album' where there's a separate track on it for each really popular dead rapper (The 90's had some really good ones, Notorious B.I.G., Big L, etc.) - where each track would have the network rapping with lyrics / flow in the style of the said rapper, and imitating their voice with lyrebird's network (admit it, the current text to speech I have is pretty trash). Kind of like an AI resurrection of dead rappers... 266 | -------------------------------------------------------------------------------- /model.py: -------------------------------------------------------------------------------- 1 | import pronouncing 2 | import markovify 3 | import re 4 | import random 5 | import numpy as np 6 | import os 7 | import keras 8 | from keras.models import Sequential 9 | from keras.layers import LSTM 10 | from keras.layers.core import Dense 11 | 12 | depth = 4 # depth of the network. changing will require a retrain 13 | maxsyllables = 16 # maximum syllables per line. Change this freely without retraining the network 14 | train_mode = False 15 | artist = "kanye_west" # used when saving the trained model 16 | rap_file = "neural_rap.txt" # where the rap is written to 17 | 18 | def create_network(depth): 19 | model = Sequential() 20 | model.add(LSTM(4, input_shape=(2, 2), return_sequences=True)) 21 | for i in range(depth): 22 | model.add(LSTM(8, return_sequences=True)) 23 | model.add(LSTM(2, return_sequences=True)) 24 | model.summary() 25 | model.compile(optimizer='rmsprop', 26 | loss='mse') 27 | 28 | if artist + ".rap" in os.listdir(".") and train_mode == False: 29 | model.load_weights(str(artist + ".rap")) 30 | print "loading saved network: " + str(artist) + ".rap" 31 | return model 32 | 33 | def markov(text_file): 34 | read = open(text_file, "r").read() 35 | text_model = markovify.NewlineText(read) 36 | return text_model 37 | 38 | def syllables(line): 39 | count = 0 40 | for word in line.split(" "): 41 | vowels = 'aeiouy' 42 | word = word.lower().strip(".:;?!") 43 | if word[0] in vowels: 44 | count +=1 45 | for index in range(1,len(word)): 46 | if word[index] in vowels and word[index-1] not in vowels: 47 | count +=1 48 | if word.endswith('e'): 49 | count -= 1 50 | if word.endswith('le'): 51 | count+=1 52 | if count == 0: 53 | count +=1 54 | return count / maxsyllables 55 | 56 | def rhymeindex(lyrics): 57 | if str(artist) + ".rhymes" in os.listdir(".") and train_mode == False: 58 | print "loading saved rhymes from " + str(artist) + ".rhymes" 59 | return open(str(artist) + ".rhymes", "r").read().split("\n") 60 | else: 61 | rhyme_master_list = [] 62 | print "Alright, building the list of all the rhymes" 63 | for i in lyrics: 64 | word = re.sub(r"\W+", '', i.split(" ")[-1]).lower() 65 | rhymeslist = pronouncing.rhymes(word) 66 | rhymeslist = [x.encode('UTF8') for x in rhymeslist] 67 | rhymeslistends = [] 68 | for i in rhymeslist: 69 | rhymeslistends.append(i[-2:]) 70 | try: 71 | rhymescheme = max(set(rhymeslistends), key=rhymeslistends.count) 72 | except Exception: 73 | rhymescheme = word[-2:] 74 | rhyme_master_list.append(rhymescheme) 75 | rhyme_master_list = list(set(rhyme_master_list)) 76 | 77 | reverselist = [x[::-1] for x in rhyme_master_list] 78 | reverselist = sorted(reverselist) 79 | 80 | rhymelist = [x[::-1] for x in reverselist] 81 | 82 | f = open(str(artist) + ".rhymes", "w") 83 | f.write("\n".join(rhymelist)) 84 | f.close() 85 | print rhymelist 86 | return rhymelist 87 | 88 | def rhyme(line, rhyme_list): 89 | word = re.sub(r"\W+", '', line.split(" ")[-1]).lower() 90 | rhymeslist = pronouncing.rhymes(word) 91 | rhymeslist = [x.encode('UTF8') for x in rhymeslist] 92 | rhymeslistends = [] 93 | for i in rhymeslist: 94 | rhymeslistends.append(i[-2:]) 95 | try: 96 | rhymescheme = max(set(rhymeslistends), key=rhymeslistends.count) 97 | except Exception: 98 | rhymescheme = word[-2:] 99 | try: 100 | float_rhyme = rhyme_list.index(rhymescheme) 101 | float_rhyme = float_rhyme / float(len(rhyme_list)) 102 | return float_rhyme 103 | except Exception: 104 | return None 105 | 106 | 107 | def split_lyrics_file(text_file): 108 | text = open(text_file).read() 109 | text = text.split("\n") 110 | while "" in text: 111 | text.remove("") 112 | return text 113 | 114 | 115 | def generate_lyrics(text_model, text_file): 116 | bars = [] 117 | last_words = [] 118 | lyriclength = len(open(text_file).read().split("\n")) 119 | count = 0 120 | markov_model = markov(text_file) 121 | 122 | while len(bars) < lyriclength / 9 and count < lyriclength * 2: 123 | bar = markov_model.make_sentence() 124 | 125 | if type(bar) != type(None) and syllables(bar) < 1: 126 | 127 | def get_last_word(bar): 128 | last_word = bar.split(" ")[-1] 129 | if last_word[-1] in "!.?,": 130 | last_word = last_word[:-1] 131 | return last_word 132 | 133 | last_word = get_last_word(bar) 134 | if bar not in bars and last_words.count(last_word) < 3: 135 | bars.append(bar) 136 | last_words.append(last_word) 137 | count += 1 138 | return bars 139 | 140 | def build_dataset(lines, rhyme_list): 141 | dataset = [] 142 | line_list = [] 143 | for line in lines: 144 | line_list = [line, syllables(line), rhyme(line, rhyme_list)] 145 | dataset.append(line_list) 146 | 147 | x_data = [] 148 | y_data = [] 149 | 150 | for i in range(len(dataset) - 3): 151 | line1 = dataset[i ][1:] 152 | line2 = dataset[i + 1][1:] 153 | line3 = dataset[i + 2][1:] 154 | line4 = dataset[i + 3][1:] 155 | 156 | x = [line1[0], line1[1], line2[0], line2[1]] 157 | x = np.array(x) 158 | x = x.reshape(2,2) 159 | x_data.append(x) 160 | 161 | y = [line3[0], line3[1], line4[0], line4[1]] 162 | y = np.array(y) 163 | y = y.reshape(2,2) 164 | y_data.append(y) 165 | 166 | x_data = np.array(x_data) 167 | y_data = np.array(y_data) 168 | 169 | #print "x shape " + str(x_data.shape) 170 | #print "y shape " + str(y_data.shape) 171 | return x_data, y_data 172 | 173 | def compose_rap(lines, rhyme_list, lyrics_file, model): 174 | rap_vectors = [] 175 | human_lyrics = split_lyrics_file(lyrics_file) 176 | 177 | initial_index = random.choice(range(len(human_lyrics) - 1)) 178 | initial_lines = human_lyrics[initial_index:initial_index + 2] 179 | 180 | starting_input = [] 181 | for line in initial_lines: 182 | starting_input.append([syllables(line), rhyme(line, rhyme_list)]) 183 | 184 | starting_vectors = model.predict(np.array([starting_input]).flatten().reshape(1, 2, 2)) 185 | rap_vectors.append(starting_vectors) 186 | 187 | for i in range(100): 188 | rap_vectors.append(model.predict(np.array([rap_vectors[-1]]).flatten().reshape(1, 2, 2))) 189 | 190 | return rap_vectors 191 | 192 | def vectors_into_song(vectors, generated_lyrics, rhyme_list): 193 | print "\n\n" 194 | print "About to write rap (this could take a moment)..." 195 | print "\n\n" 196 | def last_word_compare(rap, line2): 197 | penalty = 0 198 | for line1 in rap: 199 | word1 = line1.split(" ")[-1] 200 | word2 = line2.split(" ")[-1] 201 | 202 | while word1[-1] in "?!,. ": 203 | word1 = word1[:-1] 204 | 205 | while word2[-1] in "?!,. ": 206 | word2 = word2[:-1] 207 | 208 | if word1 == word2: 209 | penalty += 0.2 210 | 211 | return penalty 212 | 213 | def calculate_score(vector_half, syllables, rhyme, penalty): 214 | desired_syllables = vector_half[0] 215 | desired_rhyme = vector_half[1] 216 | desired_syllables = desired_syllables * maxsyllables 217 | desired_rhyme = desired_rhyme * len(rhyme_list) 218 | 219 | score = 1.0 - (abs((float(desired_syllables) - float(syllables))) + abs((float(desired_rhyme) - float(rhyme)))) - penalty 220 | 221 | return score 222 | 223 | dataset = [] 224 | for line in generated_lyrics: 225 | line_list = [line, syllables(line), rhyme(line, rhyme_list)] 226 | dataset.append(line_list) 227 | 228 | rap = [] 229 | 230 | vector_halves = [] 231 | 232 | for vector in vectors: 233 | vector_halves.append(list(vector[0][0])) 234 | vector_halves.append(list(vector[0][1])) 235 | 236 | 237 | 238 | 239 | 240 | for vector in vector_halves: 241 | scorelist = [] 242 | for item in dataset: 243 | line = item[0] 244 | 245 | if len(rap) != 0: 246 | penalty = last_word_compare(rap, line) 247 | else: 248 | penalty = 0 249 | total_score = calculate_score(vector, item[1], item[2], penalty) 250 | score_entry = [line, total_score] 251 | scorelist.append(score_entry) 252 | 253 | fixed_score_list = [] 254 | for score in scorelist: 255 | fixed_score_list.append(float(score[1])) 256 | max_score = max(fixed_score_list) 257 | for item in scorelist: 258 | if item[1] == max_score: 259 | rap.append(item[0]) 260 | print str(item[0]) 261 | 262 | for i in dataset: 263 | if item[0] == i[0]: 264 | dataset.remove(i) 265 | break 266 | break 267 | return rap 268 | 269 | def train(x_data, y_data, model): 270 | model.fit(np.array(x_data), np.array(y_data), 271 | batch_size=2, 272 | epochs=5, 273 | verbose=1) 274 | model.save_weights(artist + ".rap") 275 | 276 | def main(depth, train_mode): 277 | model = create_network(depth) 278 | text_file = "lyrics.txt" 279 | text_model = markov(text_file) 280 | 281 | if train_mode == True: 282 | bars = split_lyrics_file(text_file) 283 | 284 | if train_mode == False: 285 | bars = generate_lyrics(text_model, text_file) 286 | 287 | rhyme_list = rhymeindex(bars) 288 | if train_mode == True: 289 | x_data, y_data = build_dataset(bars, rhyme_list) 290 | train(x_data, y_data, model) 291 | 292 | if train_mode == False: 293 | vectors = compose_rap(bars, rhyme_list, text_file, model) 294 | rap = vectors_into_song(vectors, bars, rhyme_list) 295 | f = open(rap_file, "w") 296 | for bar in rap: 297 | f.write(bar) 298 | f.write("\n") 299 | 300 | main(depth, train_mode) 301 | -------------------------------------------------------------------------------- /documented_model.py: -------------------------------------------------------------------------------- 1 | import pronouncing 2 | import markovify 3 | import re 4 | import random 5 | import numpy as np 6 | import os 7 | from keras.models import Sequential 8 | from keras.layers import LSTM 9 | 10 | depth = 4 # depth of the network. changing will require a retrain 11 | maxsyllables = 16 # maximum syllables per line. Change this freely without retraining the network 12 | train_mode = False 13 | artist = "jay_z" # used when saving the trained model 14 | rap_file = "neural_rap.txt" # where the rap is written to 15 | 16 | 17 | def create_network(depth): 18 | # Sequential() creates a linear stack of layers 19 | model = Sequential() 20 | # Adds a LSTM layer as the first layer in the network with 21 | # 4 units (nodes), and a 2x2 tensor (which is the same shape as the 22 | # training data) 23 | model.add(LSTM(4, input_shape=(2, 2), return_sequences=True)) 24 | # adds 'depth' number of layers to the network with 8 nodes each 25 | for i in range(depth): 26 | model.add(LSTM(8, return_sequences=True)) 27 | # adds a final layer with 2 nodes for the output 28 | model.add(LSTM(2, return_sequences=True)) 29 | # prints a summary representation of the model 30 | model.summary() 31 | # configures the learning process for the network / model 32 | # the optimizer function rmsprop: optimizes the gradient descent 33 | # the loss function: mse: will use the "mean_squared_error when trying to improve 34 | model.compile(optimizer='rmsprop', 35 | loss='mse') 36 | 37 | if artist + ".rap" in os.listdir(".") and train_mode == False: 38 | # loads the weights from the hdf5 file saved earlier 39 | model.load_weights(str(artist + ".rap")) 40 | print "loading saved network: " + str(artist) + ".rap" 41 | return model 42 | 43 | 44 | def markov(text_file): 45 | read = open(text_file, "r").read() 46 | # markovify goes line by line of the lyrics.txt file and 47 | # creates a model of the text which allows us to use 48 | # make_sentence() later on to create a bar for lyrics 49 | # creates a probability distribution for all the words 50 | # so it can generate words based on the current word we're on 51 | text_model = markovify.NewlineText(read) 52 | return text_model 53 | 54 | 55 | # used when generating bars and making sure the length is not longer 56 | # than the max syllables, and will continue to generate bars until 57 | # the amount of syllables is less than the max syllables 58 | def syllables(line): 59 | count = 0 60 | for word in line.split(" "): 61 | vowels = 'aeiouy' 62 | word = word.lower().strip(".:;?!") 63 | if word[0] in vowels: 64 | count += 1 65 | for index in range(1, len(word)): 66 | if word[index] in vowels and word[index - 1] not in vowels: 67 | count += 1 68 | if word.endswith('e'): 69 | count -= 1 70 | if word.endswith('le'): 71 | count += 1 72 | if count == 0: 73 | count += 1 74 | return count / maxsyllables 75 | 76 | 77 | # writes a rhyme list to a rhymes file that allows for use when 78 | # building the dataset, and composing the rap 79 | def rhymeindex(lyrics): 80 | if str(artist) + ".rhymes" in os.listdir(".") and train_mode == False: 81 | print "loading saved rhymes from " + str(artist) + ".rhymes" 82 | return open(str(artist) + ".rhymes", "r").read().split("\n") 83 | else: 84 | rhyme_master_list = [] 85 | print "Alright, building the list of all the rhymes" 86 | for i in lyrics: 87 | # grabs the last word in each bar 88 | word = re.sub(r"\W+", '', i.split(" ")[-1]).lower() 89 | # pronouncing.rhymes gives us a word that rhymes with the word being passed in 90 | rhymeslist = pronouncing.rhymes(word) 91 | # need to convert the unicode rhyme words to UTF8 92 | rhymeslist = [x.encode('UTF8') for x in rhymeslist] 93 | # rhymeslistends contains the last two characters for each word 94 | # that could potentially rhyme with our word 95 | rhymeslistends = [] 96 | for i in rhymeslist: 97 | rhymeslistends.append(i[-2:]) 98 | try: 99 | # rhymescheme gets all the unique two letter endings and then 100 | # finds the one that occurs the most 101 | rhymescheme = max(set(rhymeslistends), key=rhymeslistends.count) 102 | except Exception: 103 | rhymescheme = word[-2:] 104 | rhyme_master_list.append(rhymescheme) 105 | # rhyme_master_list is a list of the two letters endings that appear 106 | # the most in the rhyme list for the word 107 | rhyme_master_list = list(set(rhyme_master_list)) 108 | 109 | reverselist = [x[::-1] for x in rhyme_master_list] 110 | reverselist = sorted(reverselist) 111 | # rhymelist is a list of the two letter endings (reversed) 112 | # the reason the letters are reversed and sorted is so 113 | # if the network messes up a little bit and doesn't return quite 114 | # the right values, it can often lead to picking the rhyme ending next to the 115 | # expected one in the list. But now the endings will be sorted and close together 116 | # so if the network messes up, that's alright and as long as it's just close to the 117 | # correct rhymes 118 | rhymelist = [x[::-1] for x in reverselist] 119 | 120 | f = open(str(artist) + ".rhymes", "w") 121 | f.write("\n".join(rhymelist)) 122 | f.close() 123 | print rhymelist 124 | return rhymelist 125 | 126 | 127 | # converts the index of the most common rhyme ending 128 | # into a float 129 | def rhyme(line, rhyme_list): 130 | word = re.sub(r"\W+", '', line.split(" ")[-1]).lower() 131 | rhymeslist = pronouncing.rhymes(word) 132 | rhymeslist = [x.encode('UTF8') for x in rhymeslist] 133 | rhymeslistends = [] 134 | for i in rhymeslist: 135 | rhymeslistends.append(i[-2:]) 136 | try: 137 | rhymescheme = max(set(rhymeslistends), key=rhymeslistends.count) 138 | except Exception: 139 | rhymescheme = word[-2:] 140 | try: 141 | float_rhyme = rhyme_list.index(rhymescheme) 142 | float_rhyme = float_rhyme / float(len(rhyme_list)) 143 | return float_rhyme 144 | except Exception: 145 | return None 146 | 147 | 148 | # grabs each line of the lyrics file and puts them 149 | # in their own index of a list, and then removes any empty lines 150 | # from the lyrics file and returns the list as bars 151 | def split_lyrics_file(text_file): 152 | text = open(text_file).read() 153 | text = text.split("\n") 154 | while "" in text: 155 | text.remove("") 156 | return text 157 | 158 | 159 | # only ran when not training 160 | def generate_lyrics(lyrics_file): 161 | bars = [] 162 | last_words = [] 163 | lyriclength = len(open(lyrics_file).read().split("\n")) 164 | count = 0 165 | markov_model = markov(lyrics_file) 166 | 167 | while len(bars) < lyriclength / 9 and count < lyriclength * 2: 168 | # By default, the make_sentence method tries, a maximum of 10 times per invocation, 169 | # to make a sentence that doesn't overlap too much with the original text. 170 | # If it is successful, the method returns the sentence as a string. 171 | # If not, it returns None. (https://github.com/jsvine/markovify) 172 | bar = markov_model.make_sentence() 173 | 174 | # make sure the bar isn't 'None' and that the amount of 175 | # syllables is under the max syllables 176 | if type(bar) != type(None) and syllables(bar) < 1: 177 | 178 | # function to get the last word of the bar 179 | def get_last_word(bar): 180 | last_word = bar.split(" ")[-1] 181 | # if the last word is punctuation, get the word before it 182 | if last_word[-1] in "!.?,": 183 | last_word = last_word[:-1] 184 | return last_word 185 | 186 | last_word = get_last_word(bar) 187 | # only use the bar if it is unique and the last_word 188 | # has only been seen less than 3 times 189 | if bar not in bars and last_words.count(last_word) < 3: 190 | bars.append(bar) 191 | last_words.append(last_word) 192 | count += 1 193 | 194 | return bars 195 | 196 | 197 | # used to construct the 2x2 inputs for the LSTMs 198 | # the lyrics being passed in are lyrics (original lyrics if being trained, 199 | # or ours if it's already trained) 200 | def build_dataset(lyrics, rhyme_list): 201 | dataset = [] 202 | line_list = [] 203 | # line_list becomes a list of the line from the lyrics, the syllables for that line (either 0 or 1 since 204 | # syllables uses integer division by maxsyllables (16)), and then rhyme returns the most common word 205 | # endings of the words that could rhyme with the last word of line 206 | for line in lyrics: 207 | line_list = [line, syllables(line), rhyme(line, rhyme_list)] 208 | dataset.append(line_list) 209 | 210 | x_data = [] 211 | y_data = [] 212 | 213 | # using range(len(dataset)) - 3 because of the way the indices are accessed to 214 | # get the lines 215 | for i in range(len(dataset) - 3): 216 | line1 = dataset[i][1:] 217 | line2 = dataset[i + 1][1:] 218 | line3 = dataset[i + 2][1:] 219 | line4 = dataset[i + 3][1:] 220 | 221 | # populate the training data 222 | # grabs the syllables and rhyme index here 223 | x = [line1[0], line1[1], line2[0], line2[1]] 224 | x = np.array(x) 225 | # the data is shaped as a 2x2 array where each row is a 226 | # [syllable, rhyme_index] pair 227 | x = x.reshape(2, 2) 228 | x_data.append(x) 229 | 230 | # populate the target data 231 | y = [line3[0], line3[1], line4[0], line4[1]] 232 | y = np.array(y) 233 | y = y.reshape(2, 2) 234 | y_data.append(y) 235 | 236 | # returns the 2x2 arrays as datasets 237 | x_data = np.array(x_data) 238 | y_data = np.array(y_data) 239 | 240 | # print "x shape " + str(x_data.shape) 241 | # print "y shape " + str(y_data.shape) 242 | return x_data, y_data 243 | 244 | # only used when not training 245 | def compose_rap(lines, rhyme_list, lyrics_file, model): 246 | rap_vectors = [] 247 | human_lyrics = split_lyrics_file(lyrics_file) 248 | 249 | # choose a random line to start in from given lyrics 250 | initial_index = random.choice(range(len(human_lyrics) - 1)) 251 | # create an initial_lines list consisting of 2 lines 252 | initial_lines = human_lyrics[initial_index:initial_index + 8] 253 | 254 | starting_input = [] 255 | for line in initial_lines: 256 | # appends a [syllable, rhyme_index] pair to starting_input 257 | starting_input.append([syllables(line), rhyme(line, rhyme_list)]) 258 | 259 | # predict generates output predictions for the given samples 260 | # it's reshaped as a (1, 2, 2) so that the model can predict each 261 | # 2x2 matrix of [syllable, rhyme_index] pairs 262 | starting_vectors = model.predict(np.array([starting_input]).flatten().reshape(4, 2, 2)) 263 | rap_vectors.append(starting_vectors) 264 | 265 | for i in range(49): 266 | rap_vectors.append(model.predict(np.array([rap_vectors[-1]]).flatten().reshape(4, 2, 2))) 267 | 268 | return rap_vectors 269 | 270 | 271 | def vectors_into_song(vectors, generated_lyrics, rhyme_list): 272 | print "\n\n" 273 | print "About to write rap (this could take a moment)..." 274 | print "\n\n" 275 | 276 | # compare the last words to see if they are the same, if they are 277 | # increment a penalty variable which grants penalty points for being 278 | # uncreative 279 | def last_word_compare(rap, line2): 280 | penalty = 0 281 | for line1 in rap: 282 | word1 = line1.split(" ")[-1] 283 | word2 = line2.split(" ")[-1] 284 | 285 | # remove any punctuation from the words 286 | while word1[-1] in "?!,. ": 287 | word1 = word1[:-1] 288 | 289 | while word2[-1] in "?!,. ": 290 | word2 = word2[:-1] 291 | 292 | if word1 == word2: 293 | penalty += 0.2 294 | 295 | return penalty 296 | 297 | # vector_half is a single [syllable, rhyme_index] pair 298 | # returns a score rating for a given line 299 | def calculate_score(vector_half, syllables, rhyme, penalty): 300 | desired_syllables = vector_half[0] 301 | desired_rhyme = vector_half[1] 302 | # desired_syllables is the number of syllables we want 303 | desired_syllables = desired_syllables * maxsyllables 304 | # desired rhyme is the index of the rhyme we want 305 | desired_rhyme = desired_rhyme * len(rhyme_list) 306 | 307 | # generate a score by subtracting from 1 the sum of the difference between 308 | # predicted syllables and generated syllables and the difference between 309 | # the predicted rhyme and generated rhyme and then subtract the penalty 310 | score = 1.0 - (abs((float(desired_syllables) - float(syllables))) + abs( 311 | (float(desired_rhyme) - float(rhyme)))) - penalty 312 | 313 | return score 314 | 315 | # generated a list of all the lines from generated_lyrics with their 316 | # line, syllables, and rhyme float value 317 | dataset = [] 318 | for line in generated_lyrics: 319 | line_list = [line, syllables(line), rhyme(line, rhyme_list)] 320 | dataset.append(line_list) 321 | 322 | rap = [] 323 | 324 | vector_halves = [] 325 | for vector in vectors: 326 | # vectors are the 2x2 rap_vectors (predicted bars) generated by compose_rap() 327 | # separate every vector into a half (essentially one bar) where each 328 | # has a pair of [syllables, rhyme_index] 329 | vector_halves.append(list(vector[0][0])) 330 | vector_halves.append(list(vector[0][1])) 331 | 332 | for vector in vector_halves: 333 | # Each vector (predicted bars) is scored against every generated bar ('item' below) 334 | # to find the generated bar that best matches (highest score) the vector predicted 335 | # by the model. This bar is then added to the final rap and also removed from the 336 | # generated lyrics (dataset) so that we don't get duplicate lines in the final rap. 337 | scorelist = [] 338 | for item in dataset: 339 | # item is one of the generated bars from the Markov model 340 | line = item[0] 341 | 342 | if len(rap) != 0: 343 | penalty = last_word_compare(rap, line) 344 | else: 345 | penalty = 0 346 | # calculate the score of the current line 347 | total_score = calculate_score(vector, item[1], item[2], penalty) 348 | score_entry = [line, total_score] 349 | # add the score of the current line to a scorelist 350 | scorelist.append(score_entry) 351 | 352 | fixed_score_list = [] 353 | for score in scorelist: 354 | fixed_score_list.append(float(score[1])) 355 | # get the line with the max valued score from the fixed_score_list 356 | max_score = max(fixed_score_list) 357 | for item in scorelist: 358 | if item[1] == max_score: 359 | # append item[0] (the line) to the rap 360 | rap.append(item[0]) 361 | print str(item[0]) 362 | 363 | # remove the line we added to the rap so 364 | # it doesn't get chosen again 365 | for i in dataset: 366 | if item[0] == i[0]: 367 | dataset.remove(i) 368 | break 369 | break 370 | return rap 371 | 372 | 373 | def train(x_data, y_data, model): 374 | # fit is used to train the model for 5 'epochs' (iterations) where 375 | # the x_data is the training data, and the y_data is the target data 376 | # x is the training and y is the target data 377 | # batch_size is a subset of the training data (2 in this case) 378 | # verbose simply shows a progress bar 379 | model.fit(np.array(x_data), np.array(y_data), 380 | batch_size=2, 381 | epochs=5, 382 | verbose=1) 383 | # save_weights saves the best weights from training to a hdf5 file 384 | model.save_weights(artist + ".rap") 385 | 386 | 387 | def main(depth, train_mode): 388 | model = create_network(depth) 389 | # change the lyrics file to the file with the lyrics you want to be trained on 390 | text_file = "jayz_lyrics.txt" 391 | 392 | if train_mode == True: 393 | bars = split_lyrics_file(text_file) 394 | 395 | if train_mode == False: 396 | bars = generate_lyrics(text_file) 397 | 398 | rhyme_list = rhymeindex(bars) 399 | if train_mode == True: 400 | x_data, y_data = build_dataset(bars, rhyme_list) 401 | train(x_data, y_data, model) 402 | 403 | if train_mode == False: 404 | vectors = compose_rap(bars, rhyme_list, text_file, model) 405 | rap = vectors_into_song(vectors, bars, rhyme_list) 406 | f = open(rap_file, "w") 407 | for bar in rap: 408 | f.write(bar) 409 | f.write("\n") 410 | 411 | 412 | main(depth, train_mode) 413 | --------------------------------------------------------------------------------