File size: 1,803 Bytes
7a202bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18f4fb0
7a202bc
 
 
 
 
 
 
18f4fb0
7a202bc
 
 
 
 
aeb86d8
 
 
 
7a202bc
 
 
 
 
 
 
aeb86d8
 
7a202bc
 
 
 
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
import gradio as gr
from gpt import GPTLanguageModel
import torch
import config as cfg

torch.manual_seed(1337)

# wget https://raw.githubusercontent.com/karpathy/char-rnn/master/data/tinyshakespeare/input.txt
with open('input.txt', 'r', encoding='utf-8') as f:
    text = f.read()

# here are all the unique characters that occur in this text
chars = sorted(list(set(text)))
vocab_size = len(chars)
# create a mapping from characters to integers
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] # encoder: take a string, output a list of integers
decode = lambda l: ''.join([itos[i] for i in l]) # decoder: take a list of integers, output a string

model = GPTLanguageModel(vocab_size)
model.load_state_dict(torch.load('saved_model.pth', map_location=cfg.device))
m = model.to(cfg.device)

def inference(input_context, count):
    encoded_text = [encode(input_context)]
    count = int(count)
    context = torch.tensor(encoded_text, dtype=torch.long, device=cfg.device)
     
    #print('--------------------context = ',context)
    out_text = decode(m.generate(context, max_new_tokens=count)[0].tolist())
    return out_text

title = "TSAI S21 Assignment: GPT training on mini shakespeare dataset"
description = "A simple Gradio interface that accepts a context and generates shakespere like text "
examples = [["Violets","200"],
            ["Julius","200"]
           ]
 
 

demo = gr.Interface(
    inference, 
    inputs = [gr.Textbox(placeholder="Enter starting characters"), gr.Textbox(placeholder="Enter number of characters you want to generate")], 
    outputs = [gr.Textbox(label="Shakespeare like generated text")],
    title = title,
    description = description,
    examples = examples
)

demo.launch()