File size: 1,761 Bytes
9d1893a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import torch
import torch.nn as nn
import torch.nn.functional as F
from model import Transformer

# hyperparameters
batch_size = 16 # how many independent sequences will we process in parallel?
block_size = 64 # what is the maximum context length for predictions?
max_iters = 5000
eval_interval = 100
learning_rate = 1e-3
device = 'cuda' if torch.cuda.is_available() else 'cpu'
eval_iters = 200
n_embd = 128
n_head = 8
n_layer = 4
dropout = 0.0
vocab = 101
# ------------

with open('/Users/deepaksharma/Documents/Python/Kaggle/GenerateKanyeLyrics/Kanye West Lyrics.txt','r',encoding='utf-8') as f:
    text = f.read()

chars = sorted(list(set(text)))

stoi = {ch:i for i,ch in enumerate(chars)}
itos = {i:ch for i,ch in enumerate(chars)}

encode = lambda s: [stoi[c] for c in s]
decode = lambda l: ''.join([itos[c] for c in l])


model = Transformer(n_embd,n_layer)
model.load_state_dict(torch.load('model_weights.pth'))
model.eval()

def generate_kanye_lyrics(text, max_tokens=500):
    if len(text)<64:
        initial_text = ""
        padding = 64-len(text)
        initial_list = []
        for i in range(0, padding):
            initial_list.append(0)
        context = initial_list + encode(text)
    else:
        padding = 0
        initial_text = text[0:len(text)-block_size]
        context = text[-block_size:]
        context = encode(context)
    context = torch.tensor(context, dtype=torch.long)
    lyrics = torch.stack([context for _ in range(1)], dim=0)
    return initial_text + decode(model.generate(lyrics, max_tokens=int(max_tokens))[0].tolist())[padding:]

demo = gr.Interface(fn=generate_kanye_lyrics, inputs=[gr.Textbox(lines=2, placeholder="Enter Starting lyrics ..."),gr.Number()], outputs="text")

demo.launch()