Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -24,15 +24,16 @@ STYLE = """
|
|
| 24 |
overflow: scroll;
|
| 25 |
}
|
| 26 |
.prose ul ul {
|
| 27 |
-
margin: 0!important;
|
| 28 |
font-size: 10px!important;
|
| 29 |
}
|
|
|
|
|
|
|
|
|
|
| 30 |
.prose td, th {
|
| 31 |
padding-left: 2px;
|
| 32 |
padding-right: 2px;
|
| 33 |
padding-top: 0;
|
| 34 |
padding-bottom: 0;
|
| 35 |
-
text-wrap: nowrap;
|
| 36 |
}
|
| 37 |
|
| 38 |
.tree {
|
|
@@ -42,69 +43,77 @@ STYLE = """
|
|
| 42 |
font-size: 10px;
|
| 43 |
width: 100%;
|
| 44 |
height: auto;
|
|
|
|
|
|
|
| 45 |
}
|
| 46 |
.tree ul {
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
margin: 0!important;
|
| 51 |
display: flex;
|
| 52 |
-
flex-direction:
|
| 53 |
-
|
| 54 |
-
|
| 55 |
}
|
| 56 |
.tree li {
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
| 63 |
}
|
|
|
|
| 64 |
.tree li::before, .tree li::after {
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
width: 55%;
|
| 71 |
-
min-width: 30px;
|
| 72 |
-
height: 10px;
|
| 73 |
}
|
| 74 |
-
.tree li::
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
border-left: 2px solid var(--body-text-color);
|
| 78 |
}
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
| 82 |
}
|
| 83 |
-
.tree
|
| 84 |
-
|
| 85 |
}
|
|
|
|
| 86 |
.tree li:first-child::before, .tree li:last-child::after {
|
| 87 |
-
|
| 88 |
}
|
| 89 |
.tree li:last-child::before {
|
| 90 |
-
border-
|
| 91 |
-
border-radius:
|
| 92 |
-
-webkit-border-radius:
|
| 93 |
-
-moz-border-radius:
|
| 94 |
}
|
| 95 |
.tree li:first-child::after {
|
| 96 |
border-radius: 5px 0 0 0;
|
| 97 |
-webkit-border-radius: 5px 0 0 0;
|
| 98 |
-moz-border-radius: 5px 0 0 0;
|
| 99 |
}
|
|
|
|
| 100 |
.tree ul ul::before {
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
| 108 |
}
|
| 109 |
.tree li a {
|
| 110 |
border: 1px solid var(--body-text-color);
|
|
@@ -114,6 +123,7 @@ STYLE = """
|
|
| 114 |
text-decoration-line: none;
|
| 115 |
border-radius: 5px;
|
| 116 |
transition: .5s;
|
|
|
|
| 117 |
}
|
| 118 |
.tree li a span {
|
| 119 |
padding: 5px;
|
|
@@ -127,8 +137,7 @@ STYLE = """
|
|
| 127 |
background: #ffedd5;
|
| 128 |
}
|
| 129 |
.tree li a:hover+ul li::after, .tree li a:hover+ul li::before, .tree li a:hover+ul::before, .tree li a:hover+ul ul::before {
|
| 130 |
-
border-color: #
|
| 131 |
-
color= #CCC;
|
| 132 |
}
|
| 133 |
.chosen {
|
| 134 |
background-color: #ea580c;
|
|
@@ -176,6 +185,7 @@ def generate_markdown_table(scores, sequence_prob, top_k=4, chosen_tokens=None):
|
|
| 176 |
|
| 177 |
|
| 178 |
def generate_html(start_sentence, original_tree):
|
|
|
|
| 179 |
html_output = """<div class="custom-container">
|
| 180 |
<div class="tree">
|
| 181 |
<ul>"""
|
|
@@ -203,6 +213,7 @@ class BeamNode:
|
|
| 203 |
|
| 204 |
|
| 205 |
def generate_beams(start_sentence, scores, sequences, beam_indices):
|
|
|
|
| 206 |
sequences = sequences.cpu().numpy()
|
| 207 |
original_tree = BeamNode(
|
| 208 |
cumulative_score=0, table=None, current_sentence=start_sentence, children={}
|
|
@@ -230,9 +241,7 @@ def generate_beams(start_sentence, scores, sequences, beam_indices):
|
|
| 230 |
)
|
| 231 |
beam_indexes += [beam_ix] * n_beams
|
| 232 |
current_completions += [beam_trees[beam_ix].current_sentence] * n_beams
|
| 233 |
-
top_tokens += [
|
| 234 |
-
tokenizer.decode([el]) for el in current_top_token_indexes
|
| 235 |
-
]
|
| 236 |
|
| 237 |
top_df = pd.DataFrame.from_dict(
|
| 238 |
{
|
|
@@ -258,7 +267,9 @@ def generate_beams(start_sentence, scores, sequences, beam_indices):
|
|
| 258 |
# 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
|
| 259 |
for beam_ix in reversed(list(range(n_beams))):
|
| 260 |
current_beam = beam_trees[beam_ix]
|
| 261 |
-
selected_tokens = top_df_selected.loc[
|
|
|
|
|
|
|
| 262 |
markdown_table = generate_markdown_table(
|
| 263 |
step_scores[beam_ix, :],
|
| 264 |
current_beam.cumulative_score,
|
|
@@ -323,6 +334,7 @@ def get_beam_search_html(input_text, number_steps, number_beams):
|
|
| 323 |
outputs.beam_indices[:, :],
|
| 324 |
)
|
| 325 |
html = generate_html(input_text, original_tree)
|
|
|
|
| 326 |
return html
|
| 327 |
|
| 328 |
|
|
@@ -333,7 +345,7 @@ with gr.Blocks(
|
|
| 333 |
css=STYLE,
|
| 334 |
) as demo:
|
| 335 |
text = gr.Textbox(label="Sentence to decode from", value="Today is")
|
| 336 |
-
steps = gr.Slider(label="Number of steps", minimum=1, maximum=
|
| 337 |
beams = gr.Slider(label="Number of beams", minimum=2, maximum=4, step=1, value=3)
|
| 338 |
button = gr.Button()
|
| 339 |
out = gr.Markdown(label="Output")
|
|
|
|
| 24 |
overflow: scroll;
|
| 25 |
}
|
| 26 |
.prose ul ul {
|
|
|
|
| 27 |
font-size: 10px!important;
|
| 28 |
}
|
| 29 |
+
.prose li {
|
| 30 |
+
margin-bottom: 0!important;
|
| 31 |
+
}
|
| 32 |
.prose td, th {
|
| 33 |
padding-left: 2px;
|
| 34 |
padding-right: 2px;
|
| 35 |
padding-top: 0;
|
| 36 |
padding-bottom: 0;
|
|
|
|
| 37 |
}
|
| 38 |
|
| 39 |
.tree {
|
|
|
|
| 43 |
font-size: 10px;
|
| 44 |
width: 100%;
|
| 45 |
height: auto;
|
| 46 |
+
text-align: center;
|
| 47 |
+
display:inline-block;
|
| 48 |
}
|
| 49 |
.tree ul {
|
| 50 |
+
padding-left: 20px;
|
| 51 |
+
position: relative;
|
| 52 |
+
transition: all 0.5s ease 0s;
|
|
|
|
| 53 |
display: flex;
|
| 54 |
+
flex-direction: column;
|
| 55 |
+
gap: 10px;
|
| 56 |
+
margin: 0px !important;
|
| 57 |
}
|
| 58 |
.tree li {
|
| 59 |
+
display: flex;
|
| 60 |
+
text-align: center;
|
| 61 |
+
list-style-type: none;
|
| 62 |
+
position: relative;
|
| 63 |
+
padding-left: 20px;
|
| 64 |
+
transition: all 0.5s ease 0s;
|
| 65 |
+
flex-direction: row;
|
| 66 |
+
justify-content: start;
|
| 67 |
+
align-items: center;
|
| 68 |
}
|
| 69 |
+
|
| 70 |
.tree li::before, .tree li::after {
|
| 71 |
+
content: "";
|
| 72 |
+
position: absolute;
|
| 73 |
+
left: 0px;
|
| 74 |
+
border-left: 1px solid var(--body-text-color);
|
| 75 |
+
width: 20px;
|
|
|
|
|
|
|
|
|
|
| 76 |
}
|
| 77 |
+
.tree li::before {
|
| 78 |
+
top: 0;
|
| 79 |
+
height:50%;
|
|
|
|
| 80 |
}
|
| 81 |
+
.tree li::after {
|
| 82 |
+
top: 50%;
|
| 83 |
+
height: 70%;
|
| 84 |
+
bottom: auto;
|
| 85 |
+
border-top: 1px solid var(--body-text-color);
|
| 86 |
}
|
| 87 |
+
.tree li:only-child::after, li:only-child::before {
|
| 88 |
+
display: none;
|
| 89 |
}
|
| 90 |
+
|
| 91 |
.tree li:first-child::before, .tree li:last-child::after {
|
| 92 |
+
border: 0 none;
|
| 93 |
}
|
| 94 |
.tree li:last-child::before {
|
| 95 |
+
border-bottom: 1px solid var(--body-text-color);
|
| 96 |
+
border-radius: 0px 0px 0px 5px;
|
| 97 |
+
-webkit-border-radius: 0px 0px 0px 5px;
|
| 98 |
+
-moz-border-radius: 0px 0px 0px 5px;
|
| 99 |
}
|
| 100 |
.tree li:first-child::after {
|
| 101 |
border-radius: 5px 0 0 0;
|
| 102 |
-webkit-border-radius: 5px 0 0 0;
|
| 103 |
-moz-border-radius: 5px 0 0 0;
|
| 104 |
}
|
| 105 |
+
|
| 106 |
.tree ul ul::before {
|
| 107 |
+
content: "";
|
| 108 |
+
position: absolute;
|
| 109 |
+
left: 0;
|
| 110 |
+
top: 50%;
|
| 111 |
+
border-top: 1px solid var(--body-text-color);
|
| 112 |
+
width: 20px;
|
| 113 |
+
height: 0;
|
| 114 |
+
}
|
| 115 |
+
.tree ul:has(> li:only-child)::before {
|
| 116 |
+
width:40px;
|
| 117 |
}
|
| 118 |
.tree li a {
|
| 119 |
border: 1px solid var(--body-text-color);
|
|
|
|
| 123 |
text-decoration-line: none;
|
| 124 |
border-radius: 5px;
|
| 125 |
transition: .5s;
|
| 126 |
+
width: 200px;
|
| 127 |
}
|
| 128 |
.tree li a span {
|
| 129 |
padding: 5px;
|
|
|
|
| 137 |
background: #ffedd5;
|
| 138 |
}
|
| 139 |
.tree li a:hover+ul li::after, .tree li a:hover+ul li::before, .tree li a:hover+ul::before, .tree li a:hover+ul ul::before {
|
| 140 |
+
border-color: #7c2d12;
|
|
|
|
| 141 |
}
|
| 142 |
.chosen {
|
| 143 |
background-color: #ea580c;
|
|
|
|
| 185 |
|
| 186 |
|
| 187 |
def generate_html(start_sentence, original_tree):
|
| 188 |
+
|
| 189 |
html_output = """<div class="custom-container">
|
| 190 |
<div class="tree">
|
| 191 |
<ul>"""
|
|
|
|
| 213 |
|
| 214 |
|
| 215 |
def generate_beams(start_sentence, scores, sequences, beam_indices):
|
| 216 |
+
print(tokenizer.batch_decode(sequences))
|
| 217 |
sequences = sequences.cpu().numpy()
|
| 218 |
original_tree = BeamNode(
|
| 219 |
cumulative_score=0, table=None, current_sentence=start_sentence, children={}
|
|
|
|
| 241 |
)
|
| 242 |
beam_indexes += [beam_ix] * n_beams
|
| 243 |
current_completions += [beam_trees[beam_ix].current_sentence] * n_beams
|
| 244 |
+
top_tokens += [tokenizer.decode([el]) for el in current_top_token_indexes]
|
|
|
|
|
|
|
| 245 |
|
| 246 |
top_df = pd.DataFrame.from_dict(
|
| 247 |
{
|
|
|
|
| 267 |
# 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
|
| 268 |
for beam_ix in reversed(list(range(n_beams))):
|
| 269 |
current_beam = beam_trees[beam_ix]
|
| 270 |
+
selected_tokens = top_df_selected.loc[
|
| 271 |
+
top_df_selected["beam_index"] == beam_ix
|
| 272 |
+
]
|
| 273 |
markdown_table = generate_markdown_table(
|
| 274 |
step_scores[beam_ix, :],
|
| 275 |
current_beam.cumulative_score,
|
|
|
|
| 334 |
outputs.beam_indices[:, :],
|
| 335 |
)
|
| 336 |
html = generate_html(input_text, original_tree)
|
| 337 |
+
print(html)
|
| 338 |
return html
|
| 339 |
|
| 340 |
|
|
|
|
| 345 |
css=STYLE,
|
| 346 |
) as demo:
|
| 347 |
text = gr.Textbox(label="Sentence to decode from", value="Today is")
|
| 348 |
+
steps = gr.Slider(label="Number of steps", minimum=1, maximum=8, step=1, value=4)
|
| 349 |
beams = gr.Slider(label="Number of beams", minimum=2, maximum=4, step=1, value=3)
|
| 350 |
button = gr.Button()
|
| 351 |
out = gr.Markdown(label="Output")
|