codelion commited on
Commit
cf7578d
·
verified ·
1 Parent(s): 177bb00

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -11
app.py CHANGED
@@ -232,13 +232,25 @@ def visualize_logprobs(json_input, prob_filter=-1e9, page_size=50, page=0):
232
  # Top 3 Token Log Probabilities (paginated)
233
  alt_viz_html = ""
234
  if paginated_logprobs and paginated_alternatives:
235
- alt_viz_html = "<h3>Top 3 Token Log Probabilities (Paginated)</h3><ul>"
236
  for i, (token, probs) in enumerate(zip(paginated_tokens, paginated_alternatives)):
237
- alt_viz_html += f"<li>Position {i+start_idx} (Token: {token}):<br>"
238
- for tok, prob in probs:
239
- alt_viz_html += f"{tok}: {prob:.4f}<br>"
240
- alt_viz_html += "</li>"
241
- alt_viz_html += "</ul>"
 
 
 
 
 
 
 
 
 
 
 
 
242
 
243
  return (main_fig, df, colored_text_html, alt_viz_html, drops_fig, anomaly_fig, total_pages, page)
244
 
@@ -281,15 +293,15 @@ with gr.Blocks(title="Log Probability Visualizer") as app:
281
  btn.click(
282
  fn=visualize_logprobs,
283
  inputs=[json_input, prob_filter, page_size, page],
284
- outputs=[plot_output, table_output, text_output, alt_viz_output, drops_output, anomaly_output, gr.State(visible=False), gr.State(visible=False)],
285
  )
286
 
287
  # Pagination controls
288
  with gr.Row():
289
  prev_btn = gr.Button("Previous Page")
290
  next_btn = gr.Button("Next Page")
291
- total_pages_output = gr.Number(label="Total Pages", interactive=False, visible=False)
292
- current_page_output = gr.Number(label="Current Page", interactive=False, visible=False)
293
 
294
  def update_page(json_input, prob_filter, page_size, current_page, action):
295
  if action == "prev" and current_page > 0:
@@ -301,13 +313,13 @@ with gr.Blocks(title="Log Probability Visualizer") as app:
301
  return gr.update(value=current_page), gr.update(value=total_pages)
302
 
303
  prev_btn.click(
304
- fn=lambda *args: update_page(*args, "prev"),
305
  inputs=[json_input, prob_filter, page_size, page, gr.State()],
306
  outputs=[page, total_pages_output]
307
  )
308
 
309
  next_btn.click(
310
- fn=lambda *args: update_page(*args, "next"),
311
  inputs=[json_input, prob_filter, page_size, page, gr.State()],
312
  outputs=[page, total_pages_output]
313
  )
 
232
  # Top 3 Token Log Probabilities (paginated)
233
  alt_viz_html = ""
234
  if paginated_logprobs and paginated_alternatives:
235
+ alt_viz_fig = go.Figure()
236
  for i, (token, probs) in enumerate(zip(paginated_tokens, paginated_alternatives)):
237
+ for j, (alt_tok, prob) in enumerate(probs):
238
+ 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]))
239
+ alt_viz_fig.update_layout(
240
+ title="Top 3 Token Log Probabilities (Paginated)",
241
+ xaxis_title="Token (Position)",
242
+ yaxis_title="Log Probability",
243
+ barmode='stack',
244
+ hovermode="closest",
245
+ clickmode='event+select'
246
+ )
247
+ alt_viz_fig.update_traces(
248
+ 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],
249
+ hovertemplate='<b>%{customdata}</b><extra></extra>'
250
+ )
251
+ alt_viz_html = alt_viz_fig.to_html(include_plotlyjs='cdn', full_html=False)
252
+ else:
253
+ alt_viz_html = "No finite log probabilities to display."
254
 
255
  return (main_fig, df, colored_text_html, alt_viz_html, drops_fig, anomaly_fig, total_pages, page)
256
 
 
293
  btn.click(
294
  fn=visualize_logprobs,
295
  inputs=[json_input, prob_filter, page_size, page],
296
+ outputs=[plot_output, table_output, text_output, alt_viz_output, drops_output, anomaly_output, gr.State(), gr.State()],
297
  )
298
 
299
  # Pagination controls
300
  with gr.Row():
301
  prev_btn = gr.Button("Previous Page")
302
  next_btn = gr.Button("Next Page")
303
+ total_pages_output = gr.Number(label="Total Pages", interactive=False)
304
+ current_page_output = gr.Number(label="Current Page", interactive=False)
305
 
306
  def update_page(json_input, prob_filter, page_size, current_page, action):
307
  if action == "prev" and current_page > 0:
 
313
  return gr.update(value=current_page), gr.update(value=total_pages)
314
 
315
  prev_btn.click(
316
+ fn=update_page,
317
  inputs=[json_input, prob_filter, page_size, page, gr.State()],
318
  outputs=[page, total_pages_output]
319
  )
320
 
321
  next_btn.click(
322
+ fn=update_page,
323
  inputs=[json_input, prob_filter, page_size, page, gr.State()],
324
  outputs=[page, total_pages_output]
325
  )