Corianas commited on
Commit
e90d33c
1 Parent(s): c83297f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -3
app.py CHANGED
@@ -1,7 +1,72 @@
1
  import gradio as gr
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  iface.launch()
 
1
  import gradio as gr
2
+ from model import GPTConfig, GPT
3
+ import torch
4
 
5
+ def remove_caseifer(text):
6
+ new_text = ""
7
+ i = 0
8
+ while i < len(text):
9
+ if text[i] == "^":
10
+ if i+1 < len(text):
11
+ new_text += text[i+1].upper()
12
+ i += 1
13
+ else:
14
+ pass # skip this index
15
+ else:
16
+ new_text += text[i]
17
+ i += 1
18
+ return new_text
19
+
20
+ def add_caseifer(text):
21
+ new_text = ""
22
+ for char in text:
23
+ if char.isupper():
24
+ new_text += "^" + char.lower()
25
+ else:
26
+ new_text += char
27
+ return new_text
28
 
29
+ max_new_tokens = 175 # number of tokens generated in each sample
30
+ temperature = 0.8 # 1.0 = no change, < 1.0 = less random, > 1.0 = more random, in predictions
31
+ top_k = 200 # retain only the top_k most likely tokens, clamp others to have 0 probability
32
+ device = 'cuda' # examples: 'cpu', 'cuda', 'cuda:0', 'cuda:1', etc.
33
+
34
+
35
+ torch.backends.cuda.matmul.allow_tf32 = True # allow tf32 on matmul
36
+ torch.backends.cudnn.allow_tf32 = True # allow tf32 on cudnn
37
+ device_type = 'cuda' if 'cuda' in device else 'cpu' # for later use in torch.autocast
38
+ ptdtype = {'float32': torch.float32, 'bfloat16': torch.bfloat16, 'float16': torch.float16}[dtype]
39
+ ctx = nullcontext() if device_type == 'cpu' else torch.amp.autocast(device_type=device_type, dtype=ptdtype)
40
+
41
+ # init from a model saved in a specific directory
42
+ ckpt_path = os.path.join(out_dir, 'ckpt.pt')
43
+ checkpoint = torch.load(ckpt_path, map_location=device)
44
+ gptconf = GPTConfig(**checkpoint['model_args'])
45
+ model = GPT(gptconf)
46
+ state_dict = checkpoint['model']
47
+ unwanted_prefix = '_orig_mod.'
48
+ for k,v in list(state_dict.items()):
49
+ if k.startswith(unwanted_prefix):
50
+ state_dict[k[len(unwanted_prefix):]] = state_dict.pop(k)
51
+ model.load_state_dict(state_dict)
52
+
53
+ model.eval()
54
+ model.to(device)
55
+
56
+ meta_path = os.path.join(out_dir, 'meta.pkl')
57
+ load_meta = os.path.exists(meta_path)
58
+
59
+ with open(meta_path, 'rb') as f:
60
+ meta = pickle.load(f)
61
+ # TODO want to make this more general to arbitrary encoder/decoder schemes
62
+ stoi, itos = meta['stoi'], meta['itos']
63
+ encode = lambda s: [stoi[c] for c in s]
64
+ decode = lambda l: ''.join([itos[i] for i in l])
65
+
66
+ def gen(input)
67
+ start_ids = encode(add_caseifer(input))
68
+ x = (torch.tensor(start_ids, dtype=torch.long, device=device)[None, ...])
69
+ y = model.generate(x, max_new_tokens, temperature=temperature, top_k=top_k)
70
+ return remove_caseifer(decode(y[0].tolist()))
71
+ iface = gr.Interface(fn=gen, inputs="text", outputs="text")
72
  iface.launch()