derp
Browse files
app.py
CHANGED
@@ -36,16 +36,16 @@ if __name__ == "__main__":
|
|
36 |
outputs = model.generate(
|
37 |
**inputs, max_new_tokens=50, return_dict_in_generate=True, output_scores=True, do_sample=True
|
38 |
)
|
39 |
-
# Important
|
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 |
-
#
|
47 |
highlighted_out = [(tokenizer.decode(token), None) for token in inputs.input_ids]
|
48 |
-
#
|
49 |
for token, proba in zip(generated_tokens[0], transition_proba[0]):
|
50 |
this_label = None
|
51 |
assert 0. <= proba <= 1.0
|
@@ -70,7 +70,7 @@ if __name__ == "__main__":
|
|
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)
|
|
|
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
|
|
|
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)
|