File size: 1,891 Bytes
a4d575b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c71dae9
 
 
a4d575b
 
 
 
 
 
b9454e8
a4d575b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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) }
# encoder: take a string, output a list of integers
encode = lambda s: [stoi[c] for c in s] 
# decoder: take a list of integers, output a string
decode = lambda l: ''.join([itos[i] for i in l]) 

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

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

title = "GPT Application : GPT built from scratch and trained on mini Shakespeare dataset"
description = "A simple Gradio interface based application that accepts a context, and character count and generates Shakespeare data like text "
examples = [["Edward","200"],
            ["Buckingham","200"],
            ["Margaret", "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 data like generated text")],
    title = title,
    description = description,
    examples = examples
)

demo.launch()