Skip to content

Instantly share code, notes, and snippets.

View wezleysherman's full-sized avatar

Wezley Sherman wezleysherman

View GitHub Profile
def compare_bars(input_bar, artists_bars):
'''
input_bars are the fire bars our AI generates
artists_bars are the original bars for the artist
The lower the score the better! We want unique bars
'''
# Converts sentences to matrix of token counts
avg_dist = 0
total_counted = 0
''' Rhyme density is calculated by taking the number of rhymed syllables and divide it by total number of syllables'''
def calc_rhyme_density(bars):
total_syllables = 0
rhymed_syllables = 0
for bar in bars:
for word in bar.split():
p = pronouncing.phones_for_word(word)
if len(p) == 0:
break
syllables = pronouncing.syllable_count(p[0])
def score_bar(input_bar, artists_bars, artists_avg_readability, artists_avg_rhyme_idx):
gen_readability = textstat.automated_readability_index(input_bar)
gen_rhyme_idx = calc_rhyme_density(input_bar)
comp_bars = compare_bars(input_bar, artists_bars)
# Scores based off readability, rhyme index, and originality. The lower the score the better.
bar_score = (artists_avg_readability - gen_readability) + (artists_avg_rhyme_idx - gen_rhyme_idx) + comp_bars
return bar_score
def generate_bar(seed_phrase, model, length_of_bar):
for i in range(length_of_bar):
seed_tokens = pad_sequences(tokenizer.texts_to_sequences([seed_phrase]), maxlen=29)
output_p = model.predict(seed_tokens)
output_word = np.argmax(output_p, axis=1)[0]-1
seed_phrase += " " + str(list(tokenizer.word_index.items())[output_word][0])
return seed_phrase
def generate_rap(model, artists_bars, length_of_bar=10, length_of_rap=20, min_score_threshold=-0.2, max_score_threshold=0.2, tries=5):
artists_avg_readability = calc_readability(artists_bars)
artists_avg_rhyme_idx = calc_rhyme_density(artists_bars)
fire_rap = []
cur_tries = 0
candidate_bars = []
while len(fire_rap) < length_of_rap:
seed_phrase = markov_model.make_sentence(tries=500).split(" ")
seed_phrase = " ".join(seed_phrase[:3])
# CNN+LSTM
D = 512
T = train_X.shape[1]
i = Input(shape=(T,))
x = Embedding(V + 1, D)(i)
x = Dropout(0.2)(x)
x = Conv1D(filters=512, kernel_size=15)(x)
x = Dropout(0.2)(x)
x = Conv1D(filters=256, kernel_size=8)(x)
# LSTM
D = 768
T = train_X.shape[1]
i = Input(shape=(T,))
x = Embedding(V + 1, D)(i)
x = Dropout(0.1)(x)
x = LSTM(100, return_sequences=True)(x)
x = LSTM(100)(x)
x = Dropout(0.1)(x)
# GRU
D = 512
T = train_X.shape[1]
i = Input(shape=(T,))
x = Embedding(V + 1, D)(i)
x = Dropout(0.2)(x)
x = Bidirectional(GRU(150))(x)
x = Dense(V, activation="softmax")(x)
gru_model = Model(i, x)
D = 512
#Simple RNN
T = train_X.shape[1]
i = Input(shape=(T,))
x = Embedding(V, D)(i)
x = Dropout(0.2)(x)
x = SimpleRNN(150)(x)
x = Dense(V, activation="softmax")(x)
rnn_model = Model(i, x)
function csrfSafeMethod(method) {
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}
$.ajaxSetup({
beforeSend: function(xhr, settings) {
if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
xhr.setRequestHeader("X-CSRFToken", csrftoken);
}
}