-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerator.py
68 lines (54 loc) · 1.78 KB
/
generator.py
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
from keras.models import load_model
import sys
import io
import numpy as np
maxlen = 140
start_text = sys.argv[1]
try:
diversity = int(sys.argv[2])
except Exception as e:
diversity = 0.3
try:
length = int(sys.argv[2])
except Exception as e:
length = 400
model = load_model('dropout_cnn_model.hd5')
#some dependencies
path = 'lyrics.txt'
with io.open(path, encoding='utf-8') as f:
text = f.read().lower()
chars = sorted(list(set(text)))
char_indices = dict((c, i) for i, c in enumerate(chars))
indices_char = dict((i, c) for i, c in enumerate(chars))
def sample(preds, temperature=1.0):
# helper function to sample an index from a probability array
preds = np.asarray(preds).astype('float64')
preds = np.log(preds) / temperature
exp_preds = np.exp(preds)
preds = exp_preds / np.sum(exp_preds)
probas = np.random.multinomial(1, preds, 1)
return np.argmax(probas)
def generate_rap(sentence, diversity=0.3, length=400):
len_sentence = len(sentence)
if len_sentence > 40:
sentence = sentence[:41]
elif len_sentence < 40:
diff = maxlen - len_sentence
extra = ' ' * diff
sentence = extra + sentence
generated = ''
print('----- Generating with seed: "' + sentence + '"')
for i in range(length):
x_pred = np.zeros((1, maxlen, len(chars)))
for t, char in enumerate(sentence):
x_pred[0, t, char_indices[char]] = 1.
preds = model.predict(x_pred, verbose=0)[0]
next_index = sample(preds, diversity)
next_char = indices_char[next_index]
generated += next_char
sentence = sentence[1:] + next_char
sys.stdout.write(next_char)
sys.stdout.flush()
print('\n')
if __name__ == "__main__":
generate_rap(start_text, diversity, length)