Navyabhat commited on
Commit
f7c6887
1 Parent(s): 54176c1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -0
app.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from gpt import GPTLanguageModel
3
+ import torch
4
+ import config as cfg
5
+
6
+ torch.manual_seed(1337)
7
+
8
+
9
+ with open('input.txt', 'r', encoding='utf-8') as f:
10
+ text = f.read()
11
+
12
+
13
+ chars = sorted(list(set(text)))
14
+ vocab_size = len(chars)
15
+
16
+ stoi = { ch:i for i,ch in enumerate(chars) }
17
+ itos = { i:ch for i,ch in enumerate(chars) }
18
+ encode = lambda s: [stoi[c] for c in s]
19
+ decode = lambda l: ''.join([itos[i] for i in l])
20
+
21
+ model = GPTLanguageModel(vocab_size)
22
+ model.load_state_dict(torch.load('saved_model.pth', map_location=cfg.device))
23
+ m = model.to(cfg.device)
24
+
25
+ def inference(input_text, count):
26
+ encoded_text = [encode(input_text)]
27
+ count = int(count)
28
+ context = torch.tensor(encoded_text, dtype=torch.long, device=cfg.device)
29
+
30
+ out_text = decode(m.generate(context, max_new_tokens=count)[0].tolist())
31
+ return out_text
32
+
33
+ title = "ERAV1 Session 21: Training GPT from scratch"
34
+
35
+
36
+ demo = gr.Interface(
37
+ inference,
38
+ inputs = [gr.Textbox(label="Text", placeholder="Enter text"), gr.Textbox(label="Tokens", placeholder="Enter number of tokens to be generated")],
39
+ outputs = [gr.Textbox(label="Generated text")],
40
+ title = title
41
+ )
42
+
43
+ demo.launch()