File size: 1,968 Bytes
335b70b
 
 
 
 
 
 
 
939d308
335b70b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5c99c8b
 
335b70b
 
 
 
 
 
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
import tensorflow as tf

from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
import numpy as np

# Default files
path_to_file = 'amy-winehouse.txt'
path_to_model = 'amy_winehouse.h5'

# Open model and dataset 
model = tf.keras.models.load_model(path_to_model) 
 
#Get name of file 
name = path_to_file.split('.')[0]
#print(name)
data= open(path_to_file).read()
corpus = data.lower().split('\n')

#Tokenize data
tokenizer = Tokenizer()
tokenizer.fit_on_texts(corpus)
total_words = len(tokenizer.word_index) + 1
word_index = tokenizer.word_index 
index_word = {index:word for word, index in tokenizer.word_index.items()}

n_gram_sequences = []
for line in corpus:
  token_list = tokenizer.texts_to_sequences([line])[0]
  for i in range(1, len(token_list)):
    n_gram_sequences.append(token_list[:i+1])
#print(np.shape(n_gram_sequences))

#for seq in n_gram_sequences:
#  print ([index_word[w] for w in seq])

# pad sequences
max_len = max([len(seq) for seq in n_gram_sequences])

#print(max_sequence_len, total_words)
n_gram_sequences = np.array(pad_sequences(n_gram_sequences, padding='pre', maxlen=max_len))


# Generate next words with an initial prompt
def predict_n_words(prompt, n_words):

  for _ in range(n_words):
    token_list = tokenizer.texts_to_sequences([prompt])[0]
    token_list = pad_sequences([token_list], padding='pre', maxlen=max_len-1,)
    predicted = np.argmax( model.predict(token_list), axis = 1)
    prompt += " " + index_word[predicted[0]]
  return prompt


import gradio as gr

demo = gr.Interface(
    fn=predict_n_words,
    inputs=[gr.Textbox(lines=2, placeholder="Prompt text here..."), gr.Slider(0, 500)], 
    outputs="text",
    examples=[
        ["I'll go back to black", 200],
        ["To see your eyes,", 200],
    ],
    title="Amy Winehouse RNN",
    description="Simple word-based RNN text generator trained on Amy Winehouse's songs",
)

demo.launch()