codelion commited on
Commit
f292ecf
·
verified ·
1 Parent(s): 2d02771

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -116
app.py CHANGED
@@ -46,23 +46,24 @@ def parse_input(json_input):
46
  # Function to ensure a value is a float, converting from string if necessary
47
  def ensure_float(value):
48
  if value is None:
49
- return None
 
50
  if isinstance(value, str):
51
  try:
52
  return float(value)
53
  except ValueError:
54
  logger.error("Failed to convert string '%s' to float", value)
55
- return None
56
  if isinstance(value, (int, float)):
57
  return float(value)
58
- return None
59
 
60
  # Function to create an empty Plotly figure
61
  def create_empty_figure(title):
62
  return go.Figure().update_layout(title=title, xaxis_title="", yaxis_title="", showlegend=False)
63
 
64
- # Function to process and visualize log probs with interactive Plotly plots and pagination
65
- def visualize_logprobs(json_input, page_size=100, page=0):
66
  try:
67
  # Parse the input (handles both JSON and Python dictionaries)
68
  data = parse_input(json_input)
@@ -75,47 +76,36 @@ def visualize_logprobs(json_input, page_size=100, page=0):
75
  else:
76
  raise ValueError("Input must be a list or dictionary with 'content' key")
77
 
78
- # Extract tokens, log probs, and top alternatives, skipping None or non-finite values with fixed filter of -100000
79
  tokens = []
80
  logprobs = []
81
- top_alternatives = [] # List to store top 3 log probs (selected token + 2 alternatives)
82
  for entry in content:
83
  logprob = ensure_float(entry.get("logprob", None))
84
- if logprob is not None and math.isfinite(logprob) and logprob >= -100000:
85
  tokens.append(entry["token"])
86
  logprobs.append(logprob)
87
  # Get top_logprobs, default to empty dict if None
88
  top_probs = entry.get("top_logprobs", {})
89
- # Ensure all values in top_logprobs are floats
90
- finite_top_probs = {}
91
  for key, value in top_probs.items():
92
  float_value = ensure_float(value)
93
  if float_value is not None and math.isfinite(float_value):
94
- finite_top_probs[key] = float_value
95
- # Get the top 3 log probs (including the selected token)
96
- all_probs = {entry["token"]: logprob} # Add the selected token's logprob
97
- all_probs.update(finite_top_probs) # Add alternatives
98
- sorted_probs = sorted(all_probs.items(), key=lambda x: x[1], reverse=True)
99
- top_3 = sorted_probs[:3] # Top 3 log probs (highest to lowest)
100
- top_alternatives.append(top_3)
101
  else:
102
  logger.debug("Skipping entry with logprob: %s (type: %s)", entry.get("logprob"), type(entry.get("logprob", None)))
103
 
104
  # Check if there's valid data after filtering
105
  if not logprobs or not tokens:
106
- return (create_empty_figure("Log Probabilities of Generated Tokens"), None, "No finite log probabilities to display.", create_empty_figure("Top 3 Token Log Probabilities"), create_empty_figure("Significant Probability Drops"), 1, 0)
107
-
108
- # Paginate data for large inputs
109
- total_pages = max(1, (len(logprobs) + page_size - 1) // page_size)
110
- start_idx = page * page_size
111
- end_idx = min((page + 1) * page_size, len(logprobs))
112
- paginated_tokens = tokens[start_idx:end_idx]
113
- paginated_logprobs = logprobs[start_idx:end_idx]
114
- paginated_alternatives = top_alternatives[start_idx:end_idx] if top_alternatives else []
115
 
116
  # 1. Main Log Probability Plot (Interactive Plotly)
117
  main_fig = go.Figure()
118
- main_fig.add_trace(go.Scatter(x=list(range(len(paginated_logprobs))), y=paginated_logprobs, mode='markers+lines', name='Log Prob', marker=dict(color='blue')))
119
  main_fig.update_layout(
120
  title="Log Probabilities of Generated Tokens",
121
  xaxis_title="Token Position",
@@ -124,15 +114,15 @@ def visualize_logprobs(json_input, page_size=100, page=0):
124
  clickmode='event+select'
125
  )
126
  main_fig.update_traces(
127
- customdata=[f"Token: {tok}, Log Prob: {prob:.4f}, Position: {i+start_idx}" for i, (tok, prob) in enumerate(zip(paginated_tokens, paginated_logprobs))],
128
  hovertemplate='<b>%{customdata}</b><extra></extra>'
129
  )
130
 
131
  # 2. Probability Drop Analysis (Interactive Plotly)
132
- if len(paginated_logprobs) < 2:
133
  drops_fig = create_empty_figure("Significant Probability Drops")
134
  else:
135
- drops = [paginated_logprobs[i+1] - paginated_logprobs[i] for i in range(len(paginated_logprobs)-1)]
136
  drops_fig = go.Figure()
137
  drops_fig.add_trace(go.Bar(x=list(range(len(drops))), y=drops, name='Drop', marker_color='red'))
138
  drops_fig.update_layout(
@@ -143,79 +133,75 @@ def visualize_logprobs(json_input, page_size=100, page=0):
143
  clickmode='event+select'
144
  )
145
  drops_fig.update_traces(
146
- customdata=[f"Drop: {drop:.4f}, From: {paginated_tokens[i]} to {paginated_tokens[i+1]}, Position: {i+start_idx}" for i, drop in enumerate(drops)],
147
  hovertemplate='<b>%{customdata}</b><extra></extra>'
148
  )
149
 
150
- # Create DataFrame for the table (paginated)
151
  table_data = []
152
- for i, entry in enumerate(content[start_idx:end_idx]):
 
153
  logprob = ensure_float(entry.get("logprob", None))
154
- if logprob is not None and math.isfinite(logprob) and logprob >= -100000 and "top_logprobs" in entry and entry["top_logprobs"] is not None:
155
  token = entry["token"]
156
  top_logprobs = entry["top_logprobs"]
157
  # Ensure all values in top_logprobs are floats
158
- finite_top_logprobs = {}
159
  for key, value in top_logprobs.items():
160
  float_value = ensure_float(value)
161
  if float_value is not None and math.isfinite(float_value):
162
- finite_top_logprobs[key] = float_value
163
- # Extract top 3 alternatives from top_logprobs
164
- top_3 = sorted(finite_top_logprobs.items(), key=lambda x: x[1], reverse=True)[:3]
165
  row = [token, f"{logprob:.4f}"]
166
- for alt_token, alt_logprob in top_3:
167
  row.append(f"{alt_token}: {alt_logprob:.4f}")
168
- while len(row) < 5:
 
169
  row.append("")
170
  table_data.append(row)
171
 
172
  df = (
173
  pd.DataFrame(
174
  table_data,
175
- columns=[
176
- "Token",
177
- "Log Prob",
178
- "Top 1 Alternative",
179
- "Top 2 Alternative",
180
- "Top 3 Alternative",
181
- ],
182
  )
183
  if table_data
184
  else None
185
  )
186
 
187
- # Generate colored text (paginated)
188
- if paginated_logprobs:
189
- min_logprob = min(paginated_logprobs)
190
- max_logprob = max(paginated_logprobs)
191
  if max_logprob == min_logprob:
192
- normalized_probs = [0.5] * len(paginated_logprobs)
193
  else:
194
  normalized_probs = [
195
- (lp - min_logprob) / (max_logprob - min_logprob) for lp in paginated_logprobs
196
  ]
197
 
198
  colored_text = ""
199
- for i, (token, norm_prob) in enumerate(zip(paginated_tokens, normalized_probs)):
200
  r = int(255 * (1 - norm_prob)) # Red for low confidence
201
  g = int(255 * norm_prob) # Green for high confidence
202
  b = 0
203
  color = f"rgb({r}, {g}, {b})"
204
  colored_text += f'<span style="color: {color}; font-weight: bold;">{token}</span>'
205
- if i < len(paginated_tokens) - 1:
206
  colored_text += " "
207
  colored_text_html = f"<p>{colored_text}</p>"
208
  else:
209
  colored_text_html = "No finite log probabilities to display."
210
 
211
- # Top 3 Token Log Probabilities (Interactive Plotly, paginated)
212
- alt_viz_fig = create_empty_figure("Top 3 Token Log Probabilities") if not paginated_logprobs or not paginated_alternatives else go.Figure()
213
- if paginated_logprobs and paginated_alternatives:
214
- for i, (token, probs) in enumerate(zip(paginated_tokens, paginated_alternatives)):
215
  for j, (alt_tok, prob) in enumerate(probs):
216
- alt_viz_fig.add_trace(go.Bar(x=[f"{token} (Pos {i+start_idx})"], y=[prob], name=f"{alt_tok}", marker_color=['blue', 'green', 'red'][j]))
217
  alt_viz_fig.update_layout(
218
- title="Top 3 Token Log Probabilities (Paginated)",
219
  xaxis_title="Token (Position)",
220
  yaxis_title="Log Probability",
221
  barmode='stack',
@@ -223,33 +209,29 @@ def visualize_logprobs(json_input, page_size=100, page=0):
223
  clickmode='event+select'
224
  )
225
  alt_viz_fig.update_traces(
226
- customdata=[f"Token: {tok}, Alt: {alt}, Log Prob: {prob:.4f}, Position: {i+start_idx}" for i, (tok, alts) in enumerate(zip(paginated_tokens, paginated_alternatives)) for alt, prob in alts],
227
  hovertemplate='<b>%{customdata}</b><extra></extra>'
228
  )
229
 
230
- return (main_fig, df, colored_text_html, alt_viz_fig, drops_fig, total_pages, page)
231
 
232
  except Exception as e:
233
  logger.error("Visualization failed: %s", str(e))
234
- return (create_empty_figure("Log Probabilities of Generated Tokens"), None, "No finite log probabilities to display.", create_empty_figure("Top 3 Token Log Probabilities"), create_empty_figure("Significant Probability Drops"), 1, 0)
235
 
236
- # Gradio interface with interactive layout and pagination
237
  with gr.Blocks(title="Log Probability Visualizer") as app:
238
  gr.Markdown("# Log Probability Visualizer")
239
  gr.Markdown(
240
- "Paste your JSON or Python dictionary log prob data below to visualize the tokens and their probabilities. Fixed filter ≥ -100000, 100 tokens per page."
241
  )
242
 
243
  with gr.Row():
244
- with gr.Column(scale=1):
245
- json_input = gr.Textbox(
246
- label="JSON Input",
247
- lines=10,
248
- placeholder="Paste your JSON (e.g., {\"content\": [...]}) or Python dict (e.g., {'content': [...]}) here...",
249
- )
250
- with gr.Column(scale=1):
251
- page = gr.Number(value=0, label="Page Number", precision=0, minimum=0)
252
- page_size = gr.Number(value=100, label="Page Size", precision=0, minimum=10, maximum=1000, interactive=False) # Fixed at 100, non-interactive
253
 
254
  with gr.Row():
255
  plot_output = gr.Plot(label="Log Probability Plot (Click for Tokens)")
@@ -257,7 +239,7 @@ with gr.Blocks(title="Log Probability Visualizer") as app:
257
 
258
  with gr.Row():
259
  table_output = gr.Dataframe(label="Token Log Probabilities and Top Alternatives")
260
- alt_viz_output = gr.Plot(label="Top 3 Token Log Probabilities (Click for Details)")
261
 
262
  with gr.Row():
263
  text_output = gr.HTML(label="Colored Text (Confidence Visualization)")
@@ -265,46 +247,8 @@ with gr.Blocks(title="Log Probability Visualizer") as app:
265
  btn = gr.Button("Visualize")
266
  btn.click(
267
  fn=visualize_logprobs,
268
- inputs=[json_input, page_size, page],
269
- outputs=[plot_output, table_output, text_output, alt_viz_output, drops_output, gr.State(), gr.State()],
270
- )
271
-
272
- # Pagination controls
273
- with gr.Row():
274
- prev_btn = gr.Button("Previous Page")
275
- next_btn = gr.Button("Next Page")
276
- total_pages_output = gr.Number(label="Total Pages", interactive=False)
277
- current_page_output = gr.Number(label="Current Page", interactive=False)
278
-
279
- def update_page(json_input, current_page, action):
280
- try:
281
- # Safely get total_pages by trying to process the data
282
- result = visualize_logprobs(json_input, 100, 0) # Use fixed page size and page 0
283
- if isinstance(result[0], str) or result[0] is None: # Check if it's an error message or empty figure
284
- total_pages = 1 # Default to 1 page if no data
285
- else:
286
- total_pages = result[5] # Extract total_pages from the result (index 5)
287
- except Exception as e:
288
- logger.error("Failed to calculate total pages: %s", str(e))
289
- total_pages = 1 # Default to 1 page on error
290
-
291
- if action == "prev" and current_page > 0:
292
- current_page -= 1
293
- elif action == "next":
294
- if current_page < total_pages - 1:
295
- current_page += 1
296
- return gr.update(value=current_page), gr.update(value=total_pages)
297
-
298
- prev_btn.click(
299
- fn=update_page,
300
- inputs=[json_input, page, gr.State()],
301
- outputs=[page, total_pages_output]
302
- )
303
-
304
- next_btn.click(
305
- fn=update_page,
306
- inputs=[json_input, page, gr.State()],
307
- outputs=[page, total_pages_output]
308
  )
309
 
310
  app.launch()
 
46
  # Function to ensure a value is a float, converting from string if necessary
47
  def ensure_float(value):
48
  if value is None:
49
+ logger.debug("Replacing None logprob with 0.0")
50
+ return 0.0 # Default to 0.0 for None to ensure visualization
51
  if isinstance(value, str):
52
  try:
53
  return float(value)
54
  except ValueError:
55
  logger.error("Failed to convert string '%s' to float", value)
56
+ return 0.0 # Default to 0.0 for invalid strings
57
  if isinstance(value, (int, float)):
58
  return float(value)
59
+ return 0.0 # Default for any other type
60
 
61
  # Function to create an empty Plotly figure
62
  def create_empty_figure(title):
63
  return go.Figure().update_layout(title=title, xaxis_title="", yaxis_title="", showlegend=False)
64
 
65
+ # Function to process and visualize the full log probs with dynamic top_logprobs
66
+ def visualize_logprobs(json_input):
67
  try:
68
  # Parse the input (handles both JSON and Python dictionaries)
69
  data = parse_input(json_input)
 
76
  else:
77
  raise ValueError("Input must be a list or dictionary with 'content' key")
78
 
79
+ # Extract tokens, log probs, and top alternatives, skipping non-finite values with fixed filter of -100000
80
  tokens = []
81
  logprobs = []
82
+ top_alternatives = [] # List to store all top_logprobs (dynamic length)
83
  for entry in content:
84
  logprob = ensure_float(entry.get("logprob", None))
85
+ if math.isfinite(logprob) and logprob >= -100000:
86
  tokens.append(entry["token"])
87
  logprobs.append(logprob)
88
  # Get top_logprobs, default to empty dict if None
89
  top_probs = entry.get("top_logprobs", {})
90
+ # Ensure all values in top_logprobs are floats and create a list of tuples
91
+ finite_top_probs = []
92
  for key, value in top_probs.items():
93
  float_value = ensure_float(value)
94
  if float_value is not None and math.isfinite(float_value):
95
+ finite_top_probs.append((key, float_value))
96
+ # Sort by log probability (descending) to get all alternatives
97
+ sorted_probs = sorted(finite_top_probs, key=lambda x: x[1], reverse=True)
98
+ top_alternatives.append(sorted_probs) # Store all alternatives, dynamic length
 
 
 
99
  else:
100
  logger.debug("Skipping entry with logprob: %s (type: %s)", entry.get("logprob"), type(entry.get("logprob", None)))
101
 
102
  # Check if there's valid data after filtering
103
  if not logprobs or not tokens:
104
+ return (create_empty_figure("Log Probabilities of Generated Tokens"), None, "No finite log probabilities to display.", create_empty_figure("Top Token Log Probabilities"), create_empty_figure("Significant Probability Drops"))
 
 
 
 
 
 
 
 
105
 
106
  # 1. Main Log Probability Plot (Interactive Plotly)
107
  main_fig = go.Figure()
108
+ main_fig.add_trace(go.Scatter(x=list(range(len(logprobs))), y=logprobs, mode='markers+lines', name='Log Prob', marker=dict(color='blue')))
109
  main_fig.update_layout(
110
  title="Log Probabilities of Generated Tokens",
111
  xaxis_title="Token Position",
 
114
  clickmode='event+select'
115
  )
116
  main_fig.update_traces(
117
+ customdata=[f"Token: {tok}, Log Prob: {prob:.4f}, Position: {i}" for i, (tok, prob) in enumerate(zip(tokens, logprobs))],
118
  hovertemplate='<b>%{customdata}</b><extra></extra>'
119
  )
120
 
121
  # 2. Probability Drop Analysis (Interactive Plotly)
122
+ if len(logprobs) < 2:
123
  drops_fig = create_empty_figure("Significant Probability Drops")
124
  else:
125
+ drops = [logprobs[i+1] - logprobs[i] for i in range(len(logprobs)-1)]
126
  drops_fig = go.Figure()
127
  drops_fig.add_trace(go.Bar(x=list(range(len(drops))), y=drops, name='Drop', marker_color='red'))
128
  drops_fig.update_layout(
 
133
  clickmode='event+select'
134
  )
135
  drops_fig.update_traces(
136
+ customdata=[f"Drop: {drop:.4f}, From: {tokens[i]} to {tokens[i+1]}, Position: {i}" for i, drop in enumerate(drops)],
137
  hovertemplate='<b>%{customdata}</b><extra></extra>'
138
  )
139
 
140
+ # Create DataFrame for the table with dynamic top_logprobs
141
  table_data = []
142
+ max_alternatives = max(len(alts) for alts in top_alternatives) if top_alternatives else 0
143
+ for i, entry in enumerate(content):
144
  logprob = ensure_float(entry.get("logprob", None))
145
+ if math.isfinite(logprob) and logprob >= -100000 and "top_logprobs" in entry and entry["top_logprobs"] is not None:
146
  token = entry["token"]
147
  top_logprobs = entry["top_logprobs"]
148
  # Ensure all values in top_logprobs are floats
149
+ finite_top_logprobs = []
150
  for key, value in top_logprobs.items():
151
  float_value = ensure_float(value)
152
  if float_value is not None and math.isfinite(float_value):
153
+ finite_top_logprobs.append((key, float_value))
154
+ # Sort by log probability (descending)
155
+ sorted_probs = sorted(finite_top_logprobs, key=lambda x: x[1], reverse=True)
156
  row = [token, f"{logprob:.4f}"]
157
+ for alt_token, alt_logprob in sorted_probs[:max_alternatives]: # Use max number of alternatives
158
  row.append(f"{alt_token}: {alt_logprob:.4f}")
159
+ # Pad with empty strings if fewer alternatives than max
160
+ while len(row) < 2 + max_alternatives:
161
  row.append("")
162
  table_data.append(row)
163
 
164
  df = (
165
  pd.DataFrame(
166
  table_data,
167
+ columns=["Token", "Log Prob"] + [f"Alt {i+1}" for i in range(max_alternatives)],
 
 
 
 
 
 
168
  )
169
  if table_data
170
  else None
171
  )
172
 
173
+ # Generate colored text
174
+ if logprobs:
175
+ min_logprob = min(logprobs)
176
+ max_logprob = max(logprobs)
177
  if max_logprob == min_logprob:
178
+ normalized_probs = [0.5] * len(logprobs)
179
  else:
180
  normalized_probs = [
181
+ (lp - min_logprob) / (max_logprob - min_logprob) for lp in logprobs
182
  ]
183
 
184
  colored_text = ""
185
+ for i, (token, norm_prob) in enumerate(zip(tokens, normalized_probs)):
186
  r = int(255 * (1 - norm_prob)) # Red for low confidence
187
  g = int(255 * norm_prob) # Green for high confidence
188
  b = 0
189
  color = f"rgb({r}, {g}, {b})"
190
  colored_text += f'<span style="color: {color}; font-weight: bold;">{token}</span>'
191
+ if i < len(tokens) - 1:
192
  colored_text += " "
193
  colored_text_html = f"<p>{colored_text}</p>"
194
  else:
195
  colored_text_html = "No finite log probabilities to display."
196
 
197
+ # Top Token Log Probabilities (Interactive Plotly, dynamic length)
198
+ alt_viz_fig = create_empty_figure("Top Token Log Probabilities") if not logprobs or not top_alternatives else go.Figure()
199
+ if logprobs and top_alternatives:
200
+ for i, (token, probs) in enumerate(zip(tokens, top_alternatives)):
201
  for j, (alt_tok, prob) in enumerate(probs):
202
+ alt_viz_fig.add_trace(go.Bar(x=[f"{token} (Pos {i})"], y=[prob], name=f"{alt_tok}", marker_color=['blue', 'green', 'red', 'purple', 'orange'][:len(probs)]))
203
  alt_viz_fig.update_layout(
204
+ title="Top Token Log Probabilities",
205
  xaxis_title="Token (Position)",
206
  yaxis_title="Log Probability",
207
  barmode='stack',
 
209
  clickmode='event+select'
210
  )
211
  alt_viz_fig.update_traces(
212
+ customdata=[f"Token: {tok}, Alt: {alt}, Log Prob: {prob:.4f}, Position: {i}" for i, (tok, alts) in enumerate(zip(tokens, top_alternatives)) for alt, prob in alts],
213
  hovertemplate='<b>%{customdata}</b><extra></extra>'
214
  )
215
 
216
+ return (main_fig, df, colored_text_html, alt_viz_fig, drops_fig)
217
 
218
  except Exception as e:
219
  logger.error("Visualization failed: %s", str(e))
220
+ return (create_empty_figure("Log Probabilities of Generated Tokens"), None, "No finite log probabilities to display.", create_empty_figure("Top Token Log Probabilities"), create_empty_figure("Significant Probability Drops"))
221
 
222
+ # Gradio interface with full dataset visualization and dynamic top_logprobs
223
  with gr.Blocks(title="Log Probability Visualizer") as app:
224
  gr.Markdown("# Log Probability Visualizer")
225
  gr.Markdown(
226
+ "Paste your JSON or Python dictionary log prob data below to visualize all tokens at once. Fixed filter ≥ -100000, dynamic number of top_logprobs."
227
  )
228
 
229
  with gr.Row():
230
+ json_input = gr.Textbox(
231
+ label="JSON Input",
232
+ lines=10,
233
+ placeholder="Paste your JSON (e.g., {\"content\": [...]}) or Python dict (e.g., {'content': [...]}) here...",
234
+ )
 
 
 
 
235
 
236
  with gr.Row():
237
  plot_output = gr.Plot(label="Log Probability Plot (Click for Tokens)")
 
239
 
240
  with gr.Row():
241
  table_output = gr.Dataframe(label="Token Log Probabilities and Top Alternatives")
242
+ alt_viz_output = gr.Plot(label="Top Token Log Probabilities (Click for Details)")
243
 
244
  with gr.Row():
245
  text_output = gr.HTML(label="Colored Text (Confidence Visualization)")
 
247
  btn = gr.Button("Visualize")
248
  btn.click(
249
  fn=visualize_logprobs,
250
+ inputs=[json_input],
251
+ outputs=[plot_output, table_output, text_output, alt_viz_output, drops_output],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
252
  )
253
 
254
  app.launch()