add markdown
Browse files
app.py
CHANGED
@@ -3,60 +3,78 @@ import gradio as gr
|
|
3 |
from transformers import GPT2Tokenizer, AutoModelForCausalLM
|
4 |
import numpy as np
|
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 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
if __name__ == "__main__":
|
62 |
demo.launch()
|
|
|
3 |
from transformers import GPT2Tokenizer, AutoModelForCausalLM
|
4 |
import numpy as np
|
5 |
|
6 |
+
|
7 |
+
MODEL_NAME = "gpt2"
|
8 |
+
|
9 |
+
|
10 |
+
if __name__ == "__main__":
|
11 |
+
# Define your model and your tokenizer
|
12 |
+
tokenizer = GPT2Tokenizer.from_pretrained(MODEL_NAME)
|
13 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME)
|
14 |
+
if tokenizer.pad_token_id is None:
|
15 |
+
tokenizer.pad_token_id = tokenizer.eos_token_id
|
16 |
+
model.config.pad_token_id = model.config.eos_token_id
|
17 |
+
|
18 |
+
# Define your color-coding labels; if prob > x, then label = y; Sorted in descending probability order!
|
19 |
+
probs_to_label = [
|
20 |
+
(0.1, "p >= 10%"),
|
21 |
+
(0.01, "p >= 1%"),
|
22 |
+
(1e-20, "p < 1%"),
|
23 |
+
]
|
24 |
+
|
25 |
+
label_to_color = {
|
26 |
+
"p >= 10%": "green",
|
27 |
+
"p >= 1%": "yellow",
|
28 |
+
"p < 1%": "red"
|
29 |
+
}
|
30 |
+
|
31 |
+
def get_tokens_and_labels(prompt):
|
32 |
+
"""
|
33 |
+
Given the prompt (text), return a list of tuples (decoded_token, label)
|
34 |
+
"""
|
35 |
+
inputs = tokenizer([prompt], return_tensors="pt")
|
36 |
+
outputs = model.generate(
|
37 |
+
**inputs, max_new_tokens=50, return_dict_in_generate=True, output_scores=True, do_sample=True
|
38 |
+
)
|
39 |
+
# Important, don't forget to set `normalize_logits=True` to obtain normalized probabilities (i.e. sum(p) = 1)
|
40 |
+
transition_scores = model.compute_transition_scores(outputs.sequences, outputs.scores, normalize_logits=True)
|
41 |
+
transition_proba = np.exp(transition_scores)
|
42 |
+
# We only have scores for the generated tokens, so pop out the prompt tokens
|
43 |
+
input_length = 1 if model.config.is_encoder_decoder else inputs.input_ids.shape[1]
|
44 |
+
generated_tokens = outputs.sequences[:, input_length:]
|
45 |
+
|
46 |
+
# initialize the highlighted output with the prompt, which will have no color label
|
47 |
+
highlighted_out = [(tokenizer.decode(token), None) for token in inputs.input_ids]
|
48 |
+
# get the (decoded_token, label) pairs for the generated tokens
|
49 |
+
for token, proba in zip(generated_tokens[0], transition_proba[0]):
|
50 |
+
this_label = None
|
51 |
+
assert 0. <= proba <= 1.0
|
52 |
+
for min_proba, label in probs_to_label:
|
53 |
+
if proba >= min_proba:
|
54 |
+
this_label = label
|
55 |
+
break
|
56 |
+
highlighted_out.append((tokenizer.decode(token), this_label))
|
57 |
+
|
58 |
+
return highlighted_out
|
59 |
+
|
60 |
+
demo = gr.Blocks()
|
61 |
+
with demo:
|
62 |
+
gr.Markdown(
|
63 |
+
"""
|
64 |
+
# Foo Bar
|
65 |
+
"""
|
66 |
+
)
|
67 |
+
|
68 |
+
prompt = gr.Textbox(label="Prompt", lines=3, value="Today is")
|
69 |
+
highlighted_text = gr.HighlightedText(
|
70 |
+
label="Highlighted generation",
|
71 |
+
combine_adjacent=True,
|
72 |
+
show_legend=True,
|
73 |
+
).style(color_map=label_to_color),
|
74 |
+
button = gr.Button(f"Generate with {MODEL_NAME}")
|
75 |
+
|
76 |
+
button.click(get_tokens_and_labels, inputs=prompt, outputs=highlighted_text)
|
77 |
+
|
78 |
+
|
79 |
if __name__ == "__main__":
|
80 |
demo.launch()
|