stillerman commited on
Commit
8fd4e48
1 Parent(s): 08acba6

rough draft

Browse files
Files changed (1) hide show
  1. app.py +53 -4
app.py CHANGED
@@ -1,7 +1,56 @@
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
+ import transformers
3
+ import torch.nn.functional as F
4
+ import numpy as np
5
 
6
+ def generate(model_name="Salesforce/codegen-350M-mono", text="World"):
7
+ model = transformers.AutoModelForCausalLM.from_pretrained(model_name)
8
+ tokenizer = transformers.AutoTokenizer.from_pretrained(model_name)
9
+ input_ids = tokenizer.encode(text, return_tensors='pt')
10
+ output = model.generate(input_ids, max_length=100, do_sample=True)
11
+ return tokenizer.decode(output[0])
12
 
13
+ def get_token_likelyhoods(model_name="Salesforce/codegen-350M-mono", text="World"):
14
+ # get likelyhoods for each token
15
+ model = transformers.AutoModelForCausalLM.from_pretrained(model_name)
16
+ tokenizer = transformers.AutoTokenizer.from_pretrained(model_name)
17
+ input_ids = tokenizer.encode(text, return_tensors='pt')
18
+ out = model(input_ids)
19
+ probs = F.softmax(out.logits, dim=-1).squeeze()
20
+
21
+ output = []
22
+ for tok, logits in zip(input_ids.squeeze()[1:], probs):
23
+ output.append((
24
+ tokenizer.decode(tok),
25
+ str(round(logits[tok].item() * 100, 4)) + "%",
26
+ # tokenizer.decode(np.argmax(logits.detach()))
27
+ ))
28
+
29
+ return output
30
+
31
+ demo = gr.Interface(
32
+ fn=get_token_likelyhoods,
33
+ title="Per-token likelyhood GUI based on Giant Language model Test Room",
34
+
35
+ inputs = [
36
+ gr.Textbox(
37
+ label="Model name",
38
+ lines=1,
39
+ value="Salesforce/codegen-350M-mono",
40
+ ),
41
+ gr.Textbox(
42
+ label="Text",
43
+ lines=3,
44
+ value="def first_n_primes(n):\n primes = []\n i = 2\n while len(primes) < n:\n if is_prime(i):\n primes.append(i)\n i += 1\n return",
45
+ ),
46
+ ],
47
+ outputs = gr.HighlightedText(
48
+ label="Diff",
49
+ combine_adjacent=True,
50
+ ).style(color_map={"+": "red", "-": "green"}),
51
+ )
52
+ if __name__ == "__main__":
53
+ demo.launch()
54
+
55
+ # iface = gr.Interface(fn=generate, inputs=["text", "text"], outputs="text")
56
+ # iface.launch()