m-ric HF staff commited on
Commit
55b6593
β€’
1 Parent(s): 1cd1264

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -21
app.py CHANGED
@@ -218,7 +218,9 @@ def generate_nodes(node, step):
218
  selected_class = "nonselected-sequence"
219
  return f"<li> <a href='#' class='end-of-text child {selected_class}'> <span> <b>{clean(token)}</b> <br>Total score: {node.total_score:.2f}</span> </a> </li>"
220
 
221
- html_content = f"<li> <a href='#' class='nonfinal child'> <span> <b>{clean(token)}</b> </span>"
 
 
222
  if node.table is not None:
223
  html_content += node.table
224
  html_content += "</a>"
@@ -288,7 +290,7 @@ def generate_beams(start_sentence, scores, length_penalty, decoded_sequences):
288
  top_token_indexes,
289
  top_cumulative_scores,
290
  beam_indexes,
291
- current_completions,
292
  top_tokens,
293
  ) = ([], [], [], [], [])
294
  for beam_ix in range(n_beams): # Get possible descendants for each beam
@@ -308,19 +310,20 @@ def generate_beams(start_sentence, scores, length_penalty, decoded_sequences):
308
  + current_beam.cumulative_score
309
  )
310
  beam_indexes += [beam_ix] * n_beams
311
- current_completions += [beam_trees[beam_ix].current_sequence] * n_beams
312
  top_tokens += [tokenizer.decode([el]) for el in current_top_token_indexes]
313
 
 
314
  top_df = pd.DataFrame.from_dict(
315
  {
316
  "token_index": top_token_indexes,
317
  "cumulative_score": top_cumulative_scores,
318
  "beam_index": beam_indexes,
319
- "current_completions": current_completions,
320
  "token": top_tokens,
321
  }
322
  )
323
- maxes = top_df.groupby(["token_index", "current_completions"])[
324
  "cumulative_score"
325
  ].idxmax()
326
 
@@ -331,20 +334,20 @@ def generate_beams(start_sentence, scores, length_penalty, decoded_sequences):
331
  :n_beams
332
  ]
333
 
334
- # Write the scores table - one per beam source?
335
- # Edge case: if several beam indexes are actually on the same beam, the selected tokens by beam_index for the second one will be empty. So we reverse
336
  for beam_ix in reversed(list(range(n_beams))):
337
  current_beam = beam_trees[beam_ix]
338
- selected_tokens = top_df_selected.loc[
339
- top_df_selected["beam_index"] == beam_ix
340
- ]
341
- markdown_table = generate_markdown_table(
342
- step_scores[beam_ix, :],
343
- current_beam.cumulative_score,
344
- current_beam.children_score_divider,
345
- chosen_tokens=list(selected_tokens["token"].values),
346
- )
347
- beam_trees[beam_ix].table = markdown_table
 
348
 
349
  # Add new children for each beam
350
  cumulative_scores = [beam.cumulative_score for beam in beam_trees]
@@ -375,7 +378,10 @@ def generate_beams(start_sentence, scores, length_penalty, decoded_sequences):
375
  step == len(scores) - 1
376
  or current_token_choice_ix == tokenizer.eos_token_id
377
  ),
378
- is_selected_sequence=(current_sequence.replace('<|endoftext|>', '') in [el.replace('<|endoftext|>', '') for el in decoded_sequences]),
 
 
 
379
  )
380
 
381
  # Reassign all beams at once
@@ -391,8 +397,11 @@ def generate_beams(start_sentence, scores, length_penalty, decoded_sequences):
391
 
392
  return original_tree
393
 
 
394
  @spaces.GPU
395
- def get_beam_search_html(input_text, number_steps, number_beams, length_penalty, num_return_sequences):
 
 
396
  inputs = tokenizer([input_text], return_tensors="pt")
397
 
398
  outputs = model.generate(
@@ -426,7 +435,9 @@ def get_beam_search_html(input_text, number_steps, number_beams, length_penalty,
426
 
427
 
428
  def change_num_return_sequences(n_beams):
429
- return gr.Slider(label="Number of sequences", minimum=1, maximum=n_beams, step=1, value=n_beams)
 
 
430
 
431
 
432
  with gr.Blocks(
@@ -468,7 +479,9 @@ This parameter will not impact the beam search paths, but only influence the cho
468
  label="Number of return sequences", minimum=1, maximum=3, step=1, value=2
469
  )
470
 
471
- n_beams.change(fn=change_num_return_sequences, inputs=n_beams, outputs=num_return_sequences)
 
 
472
  button = gr.Button()
473
  out_html = gr.Markdown()
474
  out_markdown = gr.Markdown()
 
218
  selected_class = "nonselected-sequence"
219
  return f"<li> <a href='#' class='end-of-text child {selected_class}'> <span> <b>{clean(token)}</b> <br>Total score: {node.total_score:.2f}</span> </a> </li>"
220
 
221
+ html_content = (
222
+ f"<li> <a href='#' class='nonfinal child'> <span> <b>{clean(token)}</b> </span>"
223
+ )
224
  if node.table is not None:
225
  html_content += node.table
226
  html_content += "</a>"
 
290
  top_token_indexes,
291
  top_cumulative_scores,
292
  beam_indexes,
293
+ current_sequence,
294
  top_tokens,
295
  ) = ([], [], [], [], [])
296
  for beam_ix in range(n_beams): # Get possible descendants for each beam
 
310
  + current_beam.cumulative_score
311
  )
312
  beam_indexes += [beam_ix] * n_beams
313
+ current_sequence += [beam_trees[beam_ix].current_sequence] * n_beams
314
  top_tokens += [tokenizer.decode([el]) for el in current_top_token_indexes]
315
 
316
+
317
  top_df = pd.DataFrame.from_dict(
318
  {
319
  "token_index": top_token_indexes,
320
  "cumulative_score": top_cumulative_scores,
321
  "beam_index": beam_indexes,
322
+ "current_sequence": current_sequence,
323
  "token": top_tokens,
324
  }
325
  )
326
+ maxes = top_df.groupby(["token_index", "current_sequence"])[
327
  "cumulative_score"
328
  ].idxmax()
329
 
 
334
  :n_beams
335
  ]
336
 
337
+ # Write the scores table - one per beam source
 
338
  for beam_ix in reversed(list(range(n_beams))):
339
  current_beam = beam_trees[beam_ix]
340
+ if current_beam.table is None:
341
+ selected_tokens = top_df_selected.loc[
342
+ top_df_selected["current_sequence"] == current_beam.current_sequence
343
+ ]
344
+ markdown_table = generate_markdown_table(
345
+ step_scores[beam_ix, :],
346
+ current_beam.cumulative_score,
347
+ current_beam.children_score_divider,
348
+ chosen_tokens=list(selected_tokens["token"].values),
349
+ )
350
+ beam_trees[beam_ix].table = markdown_table
351
 
352
  # Add new children for each beam
353
  cumulative_scores = [beam.cumulative_score for beam in beam_trees]
 
378
  step == len(scores) - 1
379
  or current_token_choice_ix == tokenizer.eos_token_id
380
  ),
381
+ is_selected_sequence=(
382
+ current_sequence.replace("<|endoftext|>", "")
383
+ in [el.replace("<|endoftext|>", "") for el in decoded_sequences]
384
+ ),
385
  )
386
 
387
  # Reassign all beams at once
 
397
 
398
  return original_tree
399
 
400
+
401
  @spaces.GPU
402
+ def get_beam_search_html(
403
+ input_text, number_steps, number_beams, length_penalty, num_return_sequences
404
+ ):
405
  inputs = tokenizer([input_text], return_tensors="pt")
406
 
407
  outputs = model.generate(
 
435
 
436
 
437
  def change_num_return_sequences(n_beams):
438
+ return gr.Slider(
439
+ label="Number of sequences", minimum=1, maximum=n_beams, step=1, value=n_beams
440
+ )
441
 
442
 
443
  with gr.Blocks(
 
479
  label="Number of return sequences", minimum=1, maximum=3, step=1, value=2
480
  )
481
 
482
+ n_beams.change(
483
+ fn=change_num_return_sequences, inputs=n_beams, outputs=num_return_sequences
484
+ )
485
  button = gr.Button()
486
  out_html = gr.Markdown()
487
  out_markdown = gr.Markdown()