|
import gradio as gr |
|
|
|
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM |
|
import numpy as np |
|
|
|
|
|
MODEL_NAME = "google/flan-t5-base" |
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME) |
|
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME) |
|
if tokenizer.pad_token_id is None: |
|
tokenizer.pad_token_id = tokenizer.eos_token_id |
|
model.config.pad_token_id = model.config.eos_token_id |
|
|
|
|
|
probs_to_label = [ |
|
(0.1, "p >= 10%"), |
|
(0.01, "p >= 1%"), |
|
(1e-20, "p < 1%"), |
|
] |
|
|
|
label_to_color = { |
|
"p >= 10%": "green", |
|
"p >= 1%": "yellow", |
|
"p < 1%": "red" |
|
} |
|
|
|
def get_tokens_and_labels(prompt): |
|
""" |
|
Given the prompt (text), return a list of tuples (decoded_token, label) |
|
""" |
|
inputs = tokenizer([prompt], return_tensors="pt") |
|
outputs = model.generate( |
|
**inputs, max_new_tokens=50, return_dict_in_generate=True, output_scores=True |
|
) |
|
|
|
transition_scores = model.compute_transition_scores(outputs.sequences, outputs.scores, normalize_logits=True) |
|
transition_proba = np.exp(transition_scores) |
|
|
|
input_length = 1 if model.config.is_encoder_decoder else inputs.input_ids.shape[1] |
|
generated_ids = outputs.sequences[:, input_length:] |
|
generated_tokens = tokenizer.convert_ids_to_tokens(generated_ids[0]) |
|
|
|
|
|
|
|
if model.config.is_encoder_decoder: |
|
highlighted_out = [] |
|
else: |
|
input_tokens = tokenizer.convert_ids_to_tokens(inputs.input_ids) |
|
highlighted_out = [(token.replace("β", " "), None) for token in input_tokens] |
|
|
|
for token, proba in zip(generated_tokens, transition_proba[0]): |
|
this_label = None |
|
assert 0. <= proba <= 1.0 |
|
for min_proba, label in probs_to_label: |
|
if proba >= min_proba: |
|
this_label = label |
|
break |
|
highlighted_out.append((token.replace("β", " "), this_label)) |
|
|
|
return highlighted_out |
|
|
|
demo = gr.Blocks() |
|
with demo: |
|
gr.Markdown( |
|
""" |
|
# π Color-Coded Text Generation π |
|
|
|
This is a demo of how you can obtain the probabilities of each generated token, and use them to |
|
color code the model output. Internally, it relies on |
|
[`compute_transition_scores`](https://huggingface.co/docs/transformers/main/en/main_classes/text_generation#transformers.GenerationMixin.compute_transition_scores), |
|
which was added in `transformers` v4.26.0. |
|
|
|
β οΈ For instance, with the pre-populated input and its color-coded output, you can see that |
|
`google/flan-t5-base` struggles with arithmetics. |
|
|
|
π€ Feel free to clone this demo and modify it to your needs π€ |
|
""" |
|
) |
|
|
|
with gr.Row(): |
|
with gr.Column(): |
|
prompt = gr.Textbox( |
|
label="Prompt", |
|
lines=3, |
|
value=( |
|
"Answer the following question by reasoning step-by-step. The cafeteria had 23 apples. " |
|
"If they used 20 for lunch and bought 6 more, how many apples do they have?" |
|
), |
|
) |
|
button = gr.Button(f"Generate with {MODEL_NAME}") |
|
with gr.Column(): |
|
highlighted_text = gr.HighlightedText( |
|
label="Highlighted generation", |
|
combine_adjacent=True, |
|
show_legend=True, |
|
color_map=label_to_color, |
|
) |
|
|
|
button.click(get_tokens_and_labels, inputs=prompt, outputs=highlighted_text) |
|
|
|
|
|
if __name__ == "__main__": |
|
demo.launch() |
|
|