cafierom commited on
Commit
afd2381
·
verified ·
1 Parent(s): c41c60e

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -16
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import gradio as gr
2
  import spaces
 
3
  from inflections_funcs import start_model, make_beams, get_beam_tokens, calculate_score_vectors
4
 
5
  # Global model initialization
@@ -10,7 +11,7 @@ print("Model loaded.")
10
  @spaces.GPU
11
  def predict(prompt, dark_mode):
12
  """
13
- Generates a response and returns an HTML string with tokens highlighted by probability.
14
  """
15
  # Generate beams
16
  generated_dicts, transcription = make_beams(model, processor, prompt)
@@ -20,17 +21,19 @@ def predict(prompt, dark_mode):
20
  score_vectors = calculate_score_vectors(model, generated_dicts)
21
 
22
  if not beam_tokens or not score_vectors:
23
- return "<span style='color: grey'>No tokens generated.</span>"
24
 
25
  first_beam_tokens = beam_tokens[0]
26
  first_beam_scores = score_vectors[0]
 
27
 
28
- # Construct HTML output
29
- html_output = '<div style="font-family: monospace; white-space: pre-wrap; line-height: 1.5;">'
 
 
30
 
31
  # Adjust colors based on dark mode
32
  normal_color = "white" if dark_mode else "black"
33
- border_color = "#444" if dark_mode else "#eee"
34
 
35
  for token, score in zip(first_beam_tokens, first_beam_scores):
36
  # Replace SentencePiece space character (U+2581) with a regular space
@@ -38,8 +41,7 @@ def predict(prompt, dark_mode):
38
 
39
  if score < 0.6:
40
  color = "red"
41
- # Light red background for low-probability tokens to make them stand out without breaking flow
42
- bg_color = "#ffe6e6" if not dark_mode else "#441111"
43
  style = f"color: {color}; background-color: {bg_color};"
44
  else:
45
  color = normal_color
@@ -48,23 +50,59 @@ def predict(prompt, dark_mode):
48
  html_output += f'<span style="{style}" title="Score: {score:.4f}">{display_token}</span>'
49
 
50
  html_output += '</div>'
51
- return html_output
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
  # Gradio Interface
54
  with gr.Blocks() as demo:
55
  gr.Markdown("# LLM Token Probability Visualizer")
56
  gr.Markdown("Input a prompt and see the first beam's tokens. Tokens with a probability score < 0.6 are highlighted in red.")
57
 
58
- with gr.Row():
59
- with gr.Column():
60
- prompt_input = gr.Textbox(label="Prompt", placeholder="Enter your prompt here...", lines=3)
61
- dark_mode_toggle = gr.Checkbox(label="Dark Mode", value=False)
62
- submit_btn = gr.Button("Generate")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
 
64
- with gr.Column():
65
- output_html = gr.HTML(label="Highlighted Beam")
66
 
67
- submit_btn.click(fn=predict, inputs=[prompt_input, dark_mode_toggle], outputs=output_html)
68
 
69
  if __name__ == "__main__":
70
  demo.launch()
 
1
  import gradio as gr
2
  import spaces
3
+ from torch import exp
4
  from inflections_funcs import start_model, make_beams, get_beam_tokens, calculate_score_vectors
5
 
6
  # Global model initialization
 
11
  @spaces.GPU
12
  def predict(prompt, dark_mode):
13
  """
14
+ Generates a response and returns a main highlighted HTML string and a detailed token list HTML string.
15
  """
16
  # Generate beams
17
  generated_dicts, transcription = make_beams(model, processor, prompt)
 
21
  score_vectors = calculate_score_vectors(model, generated_dicts)
22
 
23
  if not beam_tokens or not score_vectors:
24
+ return "<span style='color: grey'>No tokens generated.</span>", ""
25
 
26
  first_beam_tokens = beam_tokens[0]
27
  first_beam_scores = score_vectors[0]
28
+ first_beam_score = exp(generated_dicts.sequences_scores[0]).item()
29
 
30
+ # Construct Main HTML output
31
+ # We use a <h3> tag to simulate a Markdown header for the beam title and score
32
+ html_output = f'<h3 style="font-family: sans-serif;">Beam 1 | Score: {first_beam_score:.4f}</h3>'
33
+ html_output += '<div style="font-family: monospace; white-space: pre-wrap; line-height: 1.5;">'
34
 
35
  # Adjust colors based on dark mode
36
  normal_color = "white" if dark_mode else "black"
 
37
 
38
  for token, score in zip(first_beam_tokens, first_beam_scores):
39
  # Replace SentencePiece space character (U+2581) with a regular space
 
41
 
42
  if score < 0.6:
43
  color = "red"
44
+ bg_color = "#441111" if dark_mode else "#ffe6e6"
 
45
  style = f"color: {color}; background-color: {bg_color};"
46
  else:
47
  color = normal_color
 
50
  html_output += f'<span style="{style}" title="Score: {score:.4f}">{display_token}</span>'
51
 
52
  html_output += '</div>'
53
+
54
+ # Construct Detailed HTML output
55
+ # We create a simple table to list tokens and their scores
56
+ detail_table_style = "border-collapse: collapse; width: 100%; max-width: 500px; font-family: monospace;"
57
+ th_style = "border: 1px solid #ccc; padding: 8px; text-align: left; background-color: #f2f2f2;" if not dark_mode else "border: 1px solid #444; padding: 8px; text-align: left; background-color: #333;"
58
+ td_style = "border: 1px solid #ccc; padding: 8px; text-align: left;" if not dark_mode else "border: 1px solid #444; padding: 8px; text-align: left;"
59
+
60
+ detail_output = f'<table style="{detail_table_style}"><thead><tr><th style="{th_style}">Token</th><th style="{th_style}">Score</th></tr></thead><tbody>'
61
+ for token, score in zip(first_beam_tokens, first_beam_scores):
62
+ display_token = token.replace(' ', ' ')
63
+ detail_output += f'<tr><td style="{td_style}">{display_token}</td><td style="{td_style}">{score:.4f}</td></tr>'
64
+ detail_output += '</tbody></table>'
65
+
66
+ return html_output, detail_output
67
 
68
  # Gradio Interface
69
  with gr.Blocks() as demo:
70
  gr.Markdown("# LLM Token Probability Visualizer")
71
  gr.Markdown("Input a prompt and see the first beam's tokens. Tokens with a probability score < 0.6 are highlighted in red.")
72
 
73
+ with gr.Column():
74
+ prompt_input = gr.Textbox(label="Prompt", placeholder="Enter your prompt here...", lines=3)
75
+ dark_mode_toggle = gr.Checkbox(label="Dark Mode", value=True)
76
+ submit_btn = gr.Button("Generate")
77
+
78
+ output_html = gr.HTML(label="Highlighted Beam")
79
+
80
+ toggle_btn = gr.Button("Show Token Details")
81
+ detail_html = gr.HTML(label="Detailed Token Probabilities", visible=False)
82
+
83
+ def toggle_details(visible):
84
+ return gr.update(visible=not visible, value="") if visible else gr.update(visible=True)
85
+
86
+ # This is slightly wrong. toggle_details should just toggle visibility.
87
+ # Let's use a better way.
88
+ def update_visibility(visible):
89
+ return gr.update(visible=not visible)
90
+
91
+ submit_btn.click(fn=predict, inputs=[prompt_input, dark_mode_toggle], outputs=[output_html, detail_html])
92
+
93
+ # We need to track the visibility state. Since we can't easily,
94
+ # we can use a gr.State or just a simple toggle function.
95
+
96
+ def on_toggle_click(current_visible):
97
+ return gr.update(visible=not current_visible)
98
+
99
+ # To track visibility, we can use a gr.State
100
+ visibility_state = gr.State(value=False)
101
 
102
+ def toggle_view(visible):
103
+ return not visible, gr.update(visible=not visible)
104
 
105
+ toggle_btn.click(fn=toggle_view, inputs=visibility_state, outputs=[visibility_state, detail_html])
106
 
107
  if __name__ == "__main__":
108
  demo.launch()