Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -392,14 +392,14 @@ def generate_beams(start_sentence, scores, length_penalty, decoded_sequences):
|
|
| 392 |
return original_tree
|
| 393 |
|
| 394 |
@spaces.GPU
|
| 395 |
-
def get_beam_search_html(input_text, number_steps, number_beams, length_penalty):
|
| 396 |
inputs = tokenizer([input_text], return_tensors="pt")
|
| 397 |
|
| 398 |
outputs = model.generate(
|
| 399 |
**inputs,
|
| 400 |
max_new_tokens=number_steps,
|
| 401 |
num_beams=number_beams,
|
| 402 |
-
num_return_sequences=
|
| 403 |
return_dict_in_generate=True,
|
| 404 |
length_penalty=length_penalty,
|
| 405 |
output_scores=True,
|
|
@@ -443,6 +443,7 @@ Play with the parameters below to understand how beam search decoding works!
|
|
| 443 |
- **Number of beams** (`num_beams`): the number of beams to use
|
| 444 |
- **Length penalty** (`length_penalty`): the length penalty to apply to outputs. `length_penalty` > 0.0 promotes longer sequences, while `length_penalty` < 0.0 encourages shorter sequences.
|
| 445 |
This parameter will not impact the beam search paths, but only influence the choice of sequences in the end towards longer or shorter sequences.
|
|
|
|
| 446 |
"""
|
| 447 |
)
|
| 448 |
text = gr.Textbox(
|
|
@@ -450,21 +451,24 @@ This parameter will not impact the beam search paths, but only influence the cho
|
|
| 450 |
value="Conclusion: thanks a lot. This article was originally published on",
|
| 451 |
)
|
| 452 |
with gr.Row():
|
| 453 |
-
|
| 454 |
label="Number of steps", minimum=1, maximum=12, step=1, value=4
|
| 455 |
)
|
| 456 |
-
|
| 457 |
label="Number of beams", minimum=2, maximum=4, step=1, value=3
|
| 458 |
)
|
| 459 |
length_penalty = gr.Slider(
|
| 460 |
label="Length penalty", minimum=-3, maximum=3, step=0.5, value=1
|
| 461 |
)
|
|
|
|
|
|
|
|
|
|
| 462 |
button = gr.Button()
|
| 463 |
out_html = gr.Markdown()
|
| 464 |
out_markdown = gr.Markdown()
|
| 465 |
button.click(
|
| 466 |
get_beam_search_html,
|
| 467 |
-
inputs=[text, steps, beams, length_penalty],
|
| 468 |
outputs=[out_html, out_markdown],
|
| 469 |
)
|
| 470 |
|
|
|
|
| 392 |
return original_tree
|
| 393 |
|
| 394 |
@spaces.GPU
|
| 395 |
+
def get_beam_search_html(input_text, number_steps, number_beams, length_penalty, number_sequences):
|
| 396 |
inputs = tokenizer([input_text], return_tensors="pt")
|
| 397 |
|
| 398 |
outputs = model.generate(
|
| 399 |
**inputs,
|
| 400 |
max_new_tokens=number_steps,
|
| 401 |
num_beams=number_beams,
|
| 402 |
+
num_return_sequences=number_sequences,
|
| 403 |
return_dict_in_generate=True,
|
| 404 |
length_penalty=length_penalty,
|
| 405 |
output_scores=True,
|
|
|
|
| 443 |
- **Number of beams** (`num_beams`): the number of beams to use
|
| 444 |
- **Length penalty** (`length_penalty`): the length penalty to apply to outputs. `length_penalty` > 0.0 promotes longer sequences, while `length_penalty` < 0.0 encourages shorter sequences.
|
| 445 |
This parameter will not impact the beam search paths, but only influence the choice of sequences in the end towards longer or shorter sequences.
|
| 446 |
+
- **Number of sequences** (`num_return_sequences`): the number of sequences to be returned at the end of generation.
|
| 447 |
"""
|
| 448 |
)
|
| 449 |
text = gr.Textbox(
|
|
|
|
| 451 |
value="Conclusion: thanks a lot. This article was originally published on",
|
| 452 |
)
|
| 453 |
with gr.Row():
|
| 454 |
+
n_steps = gr.Slider(
|
| 455 |
label="Number of steps", minimum=1, maximum=12, step=1, value=4
|
| 456 |
)
|
| 457 |
+
n_beams = gr.Slider(
|
| 458 |
label="Number of beams", minimum=2, maximum=4, step=1, value=3
|
| 459 |
)
|
| 460 |
length_penalty = gr.Slider(
|
| 461 |
label="Length penalty", minimum=-3, maximum=3, step=0.5, value=1
|
| 462 |
)
|
| 463 |
+
n_sequences = gr.Slider(
|
| 464 |
+
label="Number of sequences", minimum=1, maximum=n_beams, step=1, value=n_beams
|
| 465 |
+
)
|
| 466 |
button = gr.Button()
|
| 467 |
out_html = gr.Markdown()
|
| 468 |
out_markdown = gr.Markdown()
|
| 469 |
button.click(
|
| 470 |
get_beam_search_html,
|
| 471 |
+
inputs=[text, steps, beams, length_penalty, n_sequences],
|
| 472 |
outputs=[out_html, out_markdown],
|
| 473 |
)
|
| 474 |
|