MaxiiMin commited on
Commit
ac52ed4
·
verified ·
1 Parent(s): 6575dea

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -0
app.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer
4
+
5
+ model_name = "Qwen/Qwen2.5-0.5B"
6
+ model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto")
7
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
8
+
9
+ def highlight_probabilities(text):
10
+ inputs = tokenizer([text], return_tensors="pt").input_ids.to(model.device)
11
+ inp, outp = inputs[:, :-1], inputs[:, 1:].unsqueeze(-1)
12
+
13
+ with torch.no_grad():
14
+ logits = model(inp).logits
15
+
16
+ probs = torch.softmax(logits, dim=-1)
17
+ chosen = torch.gather(probs, dim=2, index=outp).squeeze(-1).cpu().numpy()[0]
18
+
19
+ tokens = tokenizer.convert_ids_to_tokens(inp[0].cpu().tolist())
20
+ highlights = [
21
+ (tok.replace("Ġ", ""), float(p)) for tok, p in zip(tokens, chosen)
22
+ ]
23
+ return highlights
24
+
25
+ with gr.Blocks() as demo:
26
+ gr.Markdown("## Token-by-Token Probability Highlighter")
27
+ txt = gr.Textbox(
28
+ label="Input Text",
29
+ placeholder="Type or paste any text here…" ,
30
+ lines=4
31
+ )
32
+ highlighted = gr.HighlightedText(
33
+ label="Token Probabilities",
34
+ combine_adjacent=True,
35
+ show_legend=True,
36
+ )
37
+ txt.change(
38
+ fn=highlight_probabilities,
39
+ inputs=txt,
40
+ outputs=highlighted
41
+ )
42
+
43
+ if __name__ == "__main__":
44
+ demo.launch()