ybelkada commited on
Commit
6afb4d1
·
1 Parent(s): e4d3f97
Files changed (2) hide show
  1. app.py +56 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import gradio as gr
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer
4
+
5
+ gpt_neo_125_id = "EleutherAI/gpt-neo-125M"
6
+ detoxified_gpt_neo_id = "ybelkada/gpt-neo-125m-detoxified-small-context"
7
+
8
+ gpt_neo = AutoModelForCausalLM.from_pretrained(gpt_neo_125_id).to(0)
9
+ detoxified_neo = AutoModelForCausalLM.from_pretrained(detoxified_gpt_neo_id).to(0)
10
+
11
+ gpt_neo_1b_id = "EleutherAI/gpt-neo-2.7B"
12
+ detoxified_gpt_neo_1b_id = "ybelkada/gpt-neo-2.7B-detoxified-20shdl"
13
+
14
+ gpt_neo_1b = AutoModelForCausalLM.from_pretrained(gpt_neo_1b_id, torch_dtype=torch.bfloat16).to(0)
15
+ detoxified_neo_1b = AutoModelForCausalLM.from_pretrained(detoxified_gpt_neo_1b_id, torch_dtype=torch.bfloat16).to(0)
16
+
17
+ tokenizer = AutoTokenizer.from_pretrained(gpt_neo_125_id)
18
+
19
+ def compare_generation(text, max_new_tokens, temperature):
20
+ input_ids = tokenizer(text, return_tensors="pt").input_ids.to(0)
21
+
22
+ text_neo = tokenizer.decode(gpt_neo.generate(input_ids, max_new_tokens=max_new_tokens, temperature=temperature, do_sample=True)[0])
23
+ text_detoxified = tokenizer.decode(detoxified_neo.generate(input_ids, max_new_tokens=max_new_tokens, temperature=temperature, do_sample=True)[0])
24
+
25
+ text_neo_1b = tokenizer.decode(gpt_neo_1b.generate(input_ids, max_new_tokens=max_new_tokens, temperature=temperature)[0])
26
+ text_detoxified_1b = tokenizer.decode(detoxified_neo_1b.generate(input_ids, max_new_tokens=max_new_tokens, temperature=temperature)[0])
27
+
28
+ return text_neo, text_detoxified, text_neo_1b, text_detoxified_1b
29
+
30
+ iface = gr.Interface(
31
+ fn=compare_generation,
32
+ inputs=[
33
+ gr.Textbox(lines=5, label="Input text"),
34
+ gr.inputs.Slider(
35
+ minimum=8,
36
+ maximum=1000,
37
+ step=1,
38
+ default=8,
39
+ label="Number of tokens to generate",
40
+ ),
41
+ gr.inputs.Slider(
42
+ minimum=0,
43
+ maximum=2.5,
44
+ step=0.1,
45
+ default=0.6,
46
+ label="Temperature",
47
+ ),
48
+ ],
49
+ outputs=[
50
+ gr.Textbox(label="Predicted tokens - gpt neo 125m :", lines=5),
51
+ gr.Textbox(label="Predicted detoxified tokens - gpt neo 125m:", lines=5),
52
+ gr.Textbox(label="Predicted tokens - gpt neo 2.7b:", lines=5),
53
+ gr.Textbox(label="Predicted detoxified tokens - gpt neo 2.7b:", lines=5),
54
+ ]
55
+ )
56
+ iface.launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ transformers
2
+ gradio