cafierom commited on
Commit
6ab2bc7
·
verified ·
1 Parent(s): 72ac605

Upload 2 files

Browse files
Files changed (3) hide show
  1. .gitattributes +1 -0
  2. Stochastic_parrot.JPG +3 -0
  3. app.py +88 -47
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ Stochastic_parrot.JPG filter=lfs diff=lfs merge=lfs -text
Stochastic_parrot.JPG ADDED

Git LFS Details

  • SHA256: 107e388adfb605aa2abce5c19ef0890bdfb8874c5a6579ff9a793e35e642c524
  • Pointer size: 131 Bytes
  • Size of remote file: 127 kB
app.py CHANGED
@@ -8,41 +8,26 @@ print("Loading model...")
8
  model, processor = start_model()
9
  print("Model loaded.")
10
 
11
- @spaces.GPU
12
- def predict(prompt, dark_mode):
13
  """
14
- Generates a response and returns a main highlighted HTML string, a detailed token list HTML string, and visibility for the toggle button.
15
  """
16
- # Generate beams
17
- generated_dicts, transcription = make_beams(model, processor, prompt)
18
-
19
- # Get tokens and scores for the first beam
20
- beam_tokens = get_beam_tokens(generated_dicts, processor)
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>", "", gr.update(visible=False)
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
  main_container_style = "border: 2px solid lightblue; padding: 15px; border-radius: 8px; font-family: sans-serif;"
32
  title_style = "font-weight: bold; margin-bottom: 10px; font-size: 1.1em;"
33
 
34
- html_output = f'<div style="{main_container_style}">'
35
- html_output += f'<div style="{title_style}">Beam 1 | Score: {first_beam_score:.4f}</div>'
36
- html_output += '<div style="font-family: monospace; white-space: pre-wrap; line-height: 1.5; overflow-wrap: break-word; word-wrap: break-word;">'
37
 
38
- # Adjust colors based on dark mode
39
  normal_color = "white" if dark_mode else "black"
40
 
41
- for token, score in zip(first_beam_tokens, first_beam_scores):
42
- # Robustly replace SentencePiece space characters, literal underscores,
43
- # and non-breaking spaces with regular spaces.
44
- display_token = token.replace('▁', ' ').replace('_', ' ').replace(' ', ' ')
45
-
46
  if score < 0.6:
47
  color = "red"
48
  bg_color = "#441111" if dark_mode else "#ffe6e6"
@@ -50,22 +35,17 @@ def predict(prompt, dark_mode):
50
  else:
51
  color = normal_color
52
  style = f"color: {color};"
53
-
54
- html_output += f'<span style="{style}" title="Score: {score:.4f}">{display_token}</span>'
55
-
56
- html_output += '</div></div>'
57
 
58
  # Construct Detailed HTML output
59
  detail_table_style = "border-collapse: collapse; width: 100%; max-width: 500px; font-family: monospace;"
60
  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;"
61
  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;"
62
 
63
- 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>'
64
- for token, score in zip(first_beam_tokens, first_beam_scores):
65
- # Robustly replace SentencePiece space characters, literal underscores,
66
- # and non-breaking spaces with regular spaces.
67
  display_token = token.replace(' ', ' ').replace('_', ' ').replace(' ', ' ')
68
-
69
  if score < 0.6:
70
  color = "red"
71
  bg_color = "#441111" if dark_mode else "#ffe6e6"
@@ -73,20 +53,61 @@ def predict(prompt, dark_mode):
73
  else:
74
  color = normal_color
75
  token_cell_style = f"{td_style} color: {color};"
 
 
76
 
77
- detail_output += f'<tr><td style="{token_cell_style}">{display_token}</td><td style="{td_style}">{score:.4f}</td></tr>'
78
- detail_output += '</tbody></table>'
79
 
80
- return html_output, detail_output, gr.update(visible=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
 
82
  # Gradio Interface
83
  with gr.Blocks() as demo:
84
- gr.Markdown("# InflectionLM: Beam Token Visualization")
85
- gr.Markdown('''Input a prompt. The model will:
86
- - Generate multiple beams (a beam is a possible output generated by the model).
87
- - Display the first output beam, highlighting any word or parts of words (tokens) with a probability score < 0.6 in red.
88
- - Provide a toggle button to view detailed word/token probabilities for the beam.
89
- ''')
 
 
 
 
90
 
91
  with gr.Column():
92
  prompt_input = gr.Textbox(label="Prompt", placeholder="Enter your prompt here...", lines=3)
@@ -95,20 +116,40 @@ with gr.Blocks() as demo:
95
 
96
  output_html = gr.HTML(label="Highlighted Beam")
97
 
98
- toggle_btn = gr.Button("Show Token Details", visible=False)
 
 
 
99
  detail_html = gr.HTML(label="Detailed Token Probabilities", visible=False)
100
 
101
- # To track visibility, we can use a gr.State
102
  visibility_state = gr.State(value=False)
 
 
103
 
104
  def toggle_view(visible):
105
  return not visible, gr.update(visible=not visible)
106
 
107
  toggle_btn.click(fn=toggle_view, inputs=visibility_state, outputs=[visibility_state, detail_html])
108
 
 
 
 
 
 
 
 
109
  # Trigger generation on both button click and Enter key in textbox
110
- submit_btn.click(fn=predict, inputs=[prompt_input, dark_mode_toggle], outputs=[output_html, detail_html, toggle_btn])
111
- prompt_input.submit(fn=predict, inputs=[prompt_input, dark_mode_toggle], outputs=[output_html, detail_html, toggle_btn])
 
 
 
 
 
 
 
 
112
 
113
  if __name__ == "__main__":
114
  demo.launch()
 
8
  model, processor = start_model()
9
  print("Model loaded.")
10
 
11
+ def generate_beam_html(index, all_beams_data, dark_mode):
 
12
  """
13
+ Helper to construct the main and detailed HTML for a specific beam index.
14
  """
15
+ beam_tokens = all_beams_data["beam_tokens"][index]
16
+ beam_scores = all_beams_data["score_vectors"][index]
17
+ beam_overall_score = exp(all_beams_data["sequences_scores"][index]).item()
 
 
 
 
 
 
 
 
 
 
18
 
19
  # Construct Main HTML output
20
  main_container_style = "border: 2px solid lightblue; padding: 15px; border-radius: 8px; font-family: sans-serif;"
21
  title_style = "font-weight: bold; margin-bottom: 10px; font-size: 1.1em;"
22
 
23
+ main_html = f'<div style="{main_container_style}">'
24
+ main_html += f'<div style="{title_style}">Beam {index + 1} | Score: {beam_overall_score:.4f}</div>'
25
+ main_html += '<div style="font-family: monospace; white-space: pre-wrap; line-height: 1.5; overflow-wrap: break-word; word-wrap: break-word;">'
26
 
 
27
  normal_color = "white" if dark_mode else "black"
28
 
29
+ for token, score in zip(beam_tokens, beam_scores):
30
+ display_token = token.replace(' ', ' ').replace('_', ' ').replace(' ', ' ')
 
 
 
31
  if score < 0.6:
32
  color = "red"
33
  bg_color = "#441111" if dark_mode else "#ffe6e6"
 
35
  else:
36
  color = normal_color
37
  style = f"color: {color};"
38
+ main_html += f'<span style="{style}" title="Score: {score:.4f}">{display_token}</span>'
39
+ main_html += '</div></div>'
 
 
40
 
41
  # Construct Detailed HTML output
42
  detail_table_style = "border-collapse: collapse; width: 100%; max-width: 500px; font-family: monospace;"
43
  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;"
44
  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;"
45
 
46
+ detail_html = f'<table style="{detail_table_style}"><thead><tr><th style="{th_style}">Token</th><th style="{th_style}">Score</th></tr></thead><tbody>'
47
+ for token, score in zip(beam_tokens, beam_scores):
 
 
48
  display_token = token.replace(' ', ' ').replace('_', ' ').replace(' ', ' ')
 
49
  if score < 0.6:
50
  color = "red"
51
  bg_color = "#441111" if dark_mode else "#ffe6e6"
 
53
  else:
54
  color = normal_color
55
  token_cell_style = f"{td_style} color: {color};"
56
+ detail_html += f'<tr><td style="{token_cell_style}">{display_token}</td><td style="{td_style}">{score:.4f}</td></tr>'
57
+ detail_html += '</tbody></table>'
58
 
59
+ return main_html, detail_html
 
60
 
61
+ @spaces.GPU
62
+ def predict(prompt, dark_mode):
63
+ """
64
+ Generates responses for 5 beams and returns the first beam's visualization and visibility for controls.
65
+ """
66
+ # Generate beams
67
+ generated_dicts, transcription = make_beams(model, processor, prompt)
68
+
69
+ # Get tokens and scores for all beams
70
+ beam_tokens = get_beam_tokens(generated_dicts, processor)
71
+ score_vectors = calculate_score_vectors(model, generated_dicts)
72
+
73
+ if not beam_tokens or not score_vectors:
74
+ return "<span style='color: grey'>No tokens generated.</span>", "", gr.update(visible=False), gr.update(visible=False), None, 0
75
+
76
+ # Initialize state with all beam data
77
+ all_beams_data = {
78
+ "beam_tokens": beam_tokens,
79
+ "score_vectors": score_vectors,
80
+ "sequences_scores": generated_dicts.sequences_scores
81
+ }
82
+
83
+ # Generate HTML for the first beam (index 0)
84
+ main_html, detail_html = generate_beam_html(0, all_beams_data, dark_mode)
85
+
86
+ return main_html, detail_html, gr.update(visible=True), gr.update(visible=True), all_beams_data, 0
87
+
88
+ def switch_beam(current_index, all_beams_data, dark_mode):
89
+ """
90
+ Increments the beam index and returns the updated HTML.
91
+ """
92
+ if all_beams_data is None:
93
+ return None, None, 0
94
+
95
+ new_index = (current_index + 1) % 5
96
+ main_html, detail_html = generate_beam_html(new_index, all_beams_data, dark_mode)
97
+ return main_html, detail_html, new_index
98
 
99
  # Gradio Interface
100
  with gr.Blocks() as demo:
101
+ with gr.Row():
102
+ with gr.Column(scale=1):
103
+ gr.Image("Stochastic_parrot.JPG")
104
+ with gr.Column(scale=3):
105
+ gr.Markdown("# InflectionLM: Beam Token Visualization")
106
+ gr.Markdown('''Input a prompt. The model will:
107
+ - Generate multiple beams (a beam is a possible output generated by the model).
108
+ - Display the first output beam, highlighting any word or parts of words (tokens) with a probability score < 0.6 in red.
109
+ - Provide a toggle button to view detailed word/token probabilities for the beam.
110
+ ''')
111
 
112
  with gr.Column():
113
  prompt_input = gr.Textbox(label="Prompt", placeholder="Enter your prompt here...", lines=3)
 
116
 
117
  output_html = gr.HTML(label="Highlighted Beam")
118
 
119
+ with gr.Row():
120
+ toggle_btn = gr.Button("Show Token Details", visible=False)
121
+ next_beam_btn = gr.Button("Next Beam", visible=False)
122
+
123
  detail_html = gr.HTML(label="Detailed Token Probabilities", visible=False)
124
 
125
+ # State components
126
  visibility_state = gr.State(value=False)
127
+ all_beams_state = gr.State(value=None)
128
+ current_beam_state = gr.State(value=0)
129
 
130
  def toggle_view(visible):
131
  return not visible, gr.update(visible=not visible)
132
 
133
  toggle_btn.click(fn=toggle_view, inputs=visibility_state, outputs=[visibility_state, detail_html])
134
 
135
+ # Switch beam logic
136
+ next_beam_btn.click(
137
+ fn=switch_beam,
138
+ inputs=[current_beam_state, all_beams_state, dark_mode_toggle],
139
+ outputs=[output_html, detail_html, current_beam_state]
140
+ )
141
+
142
  # Trigger generation on both button click and Enter key in textbox
143
+ submit_btn.click(
144
+ fn=predict,
145
+ inputs=[prompt_input, dark_mode_toggle],
146
+ outputs=[output_html, detail_html, toggle_btn, next_beam_btn, all_beams_state, current_beam_state]
147
+ )
148
+ prompt_input.submit(
149
+ fn=predict,
150
+ inputs=[prompt_input, dark_mode_toggle],
151
+ outputs=[output_html, detail_html, toggle_btn, next_beam_btn, all_beams_state, current_beam_state]
152
+ )
153
 
154
  if __name__ == "__main__":
155
  demo.launch()