forked from kaaanishk/deepfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredict.py
More file actions
142 lines (112 loc) Β· 4.28 KB
/
predict.py
File metadata and controls
142 lines (112 loc) Β· 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import os
import random
import argparse
from keras.models import load_model
from core.preprocess import *
parser = argparse.ArgumentParser()
parser.add_argument("--data_path", type=str, help='Path to training data')
parser.add_argument("--artist", type=str, help='Artist name')
args = parser.parse_args()
def generate_lyrics(text):
bars = []
last_words = []
lyrics_length = len(text.split('\n'))
count = 0
markov_model = markov(text)
while len(bars) < lyrics_length / 9 and count < lyrics_length * 2:
bar = markov_model.make_sentence()
if bar is not None and syllables(bar) < 1:
last_word = re.sub(r"\W+", '', bar.split(" ")[-1]).lower()
if bar not in bars and last_words.count(last_word) < 3:
bars.append(bar)
last_words.append(last_word)
count += 1
return bars
def compose_rap(random_number, lyrics, rhymes_list, model):
rap_vectors = []
initial_lines = lyrics[random_number:random_number + 8]
starting_input = []
for line in initial_lines:
starting_input.append([syllables(line), rhyme(line, rhymes_list)])
starting_vectors = model.predict(
np.array([starting_input]).flatten().reshape(4, 2, 2))
rap_vectors.append(starting_vectors)
for i in range(49):
rap_vectors.append(model.predict(
np.array([rap_vectors[-1]]).flatten().reshape(4, 2, 2)))
return rap_vectors
def vectors_into_song(vectors, generated_lyrics, rhyme_list):
def last_word_compare(rap, line2):
penalty = 0
for line1 in rap:
word1 = line1.split(" ")[-1]
word2 = line2.split(" ")[-1]
while word1[-1] in "?!,. ":
word1 = word1[:-1]
while word2[-1] in "?!,. ":
word2 = word2[:-1]
if word1 == word2:
penalty += 0.2
return penalty
def calculate_score(vector_half, syllables, rhyme, penalty,
max_syllables=16):
desired_syllables = vector_half[0]
desired_rhyme = vector_half[1]
desired_syllables = desired_syllables * max_syllables
desired_rhyme = desired_rhyme * len(rhyme_list)
score = 1.0 - (abs((float(desired_syllables) - float(syllables))) + abs(
(float(desired_rhyme) - float(rhyme)))) - penalty
return score
dataset = []
for line in generated_lyrics:
line_list = [line, syllables(line), rhyme(line, rhyme_list)]
dataset.append(line_list)
rap = []
vector_halves = []
for vector in vectors:
vector_halves.append(list(vector[0][0]))
vector_halves.append(list(vector[0][1]))
for vector in vector_halves:
scorelist = []
for item in dataset:
line = item[0]
if len(rap) != 0:
penalty = last_word_compare(rap, line)
else:
penalty = 0
total_score = calculate_score(vector, item[1], item[2], penalty)
score_entry = [line, total_score]
scorelist.append(score_entry)
fixed_score_list = []
for score in scorelist:
fixed_score_list.append(float(score[1]))
max_score = max(fixed_score_list)
for item in scorelist:
if item[1] == max_score:
rap.append(item[0])
for i in dataset:
if item[0] == i[0]:
dataset.remove(i)
break
break
return rap
def predict(path, artist):
path = os.path.join(path, artist)
model = load_model(os.path.join(path, 'model.h5'))
with open(os.path.join(path, 'lyrics.txt'), mode='r') as f:
text = f.read()
lyrics = split_lyrics(text)
initial_index = random.choice(range(len(lyrics) - 1))
bars = generate_lyrics(text)
with open(os.path.join(path, 'rhymes.txt'), mode='r') as f:
rhymes_list = f.read()
vectors = compose_rap(initial_index, lyrics, rhymes_list, model)
rap = vectors_into_song(vectors, bars, rhymes_list)
with open(os.path.join(path, 'rap.txt'), "w") as f:
for bar in rap:
f.write(bar)
f.write("\n")
if __name__ == "__main__":
data_path = args.data_path
artist = args.artist
predict(data_path, artist)