Spaces:
Running
Running
add option to show additional alternatives, even those that aren't the most-likely-next-token
Browse files
app.py
CHANGED
@@ -129,29 +129,35 @@ def highlight_edits():
|
|
129 |
with st.expander("Controls"):
|
130 |
num_to_show = st.slider("Number of edits to show", 1, num_different, value=num_different // 2)
|
131 |
show_alternatives = st.checkbox("Show alternatives", value=True)
|
|
|
|
|
|
|
|
|
132 |
min_loss = loss_ratios_for_different[num_to_show - 1]
|
133 |
|
134 |
with output_container:
|
135 |
-
highlights_component(spans, show_alternatives, min_loss)
|
136 |
|
137 |
if st.checkbox("Show details"):
|
138 |
import pandas as pd
|
139 |
st.write(pd.DataFrame(spans)[['token', 'token_loss', 'most_likely_token', 'loss_ratio']])
|
140 |
st.write("Token loss is the difference between the original token and the most likely token. The loss ratio is the token loss divided by the highest token loss in the document.")
|
141 |
|
142 |
-
def highlights_component(spans, show_alternatives, min_loss):
|
143 |
import streamlit.components.v1 as components
|
144 |
import html
|
145 |
|
146 |
html_out = ''
|
147 |
for span in spans:
|
148 |
show = span['token'] != span['most_likely_token'] and span['loss_ratio'] >= min_loss
|
|
|
|
|
149 |
show_alternative = show and show_alternatives
|
150 |
-
hover = f'<span class="alternative">{
|
151 |
html_out += '<span style="color: {color}" >{hover}{orig_token}</span>'.format(
|
152 |
color="grey" if show else "black",
|
153 |
orig_token=html.escape(span["token"]).replace('\n', '<br>'),
|
154 |
-
hover=hover if show_alternative else ''
|
155 |
)
|
156 |
html_out = f"""
|
157 |
<style>
|
|
|
129 |
with st.expander("Controls"):
|
130 |
num_to_show = st.slider("Number of edits to show", 1, num_different, value=num_different // 2)
|
131 |
show_alternatives = st.checkbox("Show alternatives", value=True)
|
132 |
+
if show_alternatives:
|
133 |
+
show_all_on_hover = st.checkbox("Show all alternatives on hover", value=False)
|
134 |
+
else:
|
135 |
+
show_all_on_hover = False
|
136 |
min_loss = loss_ratios_for_different[num_to_show - 1]
|
137 |
|
138 |
with output_container:
|
139 |
+
highlights_component(spans, show_alternatives, min_loss, show_all_on_hover=show_all_on_hover)
|
140 |
|
141 |
if st.checkbox("Show details"):
|
142 |
import pandas as pd
|
143 |
st.write(pd.DataFrame(spans)[['token', 'token_loss', 'most_likely_token', 'loss_ratio']])
|
144 |
st.write("Token loss is the difference between the original token and the most likely token. The loss ratio is the token loss divided by the highest token loss in the document.")
|
145 |
|
146 |
+
def highlights_component(spans, show_alternatives, min_loss, show_all_on_hover=False):
|
147 |
import streamlit.components.v1 as components
|
148 |
import html
|
149 |
|
150 |
html_out = ''
|
151 |
for span in spans:
|
152 |
show = span['token'] != span['most_likely_token'] and span['loss_ratio'] >= min_loss
|
153 |
+
alternative_to_show = next(token for token in span['topk_tokens'] if token != span['token'])
|
154 |
+
print(span['topk_tokens'])
|
155 |
show_alternative = show and show_alternatives
|
156 |
+
hover = f'<span class="alternative">{alternative_to_show}</span>'
|
157 |
html_out += '<span style="color: {color}" >{hover}{orig_token}</span>'.format(
|
158 |
color="grey" if show else "black",
|
159 |
orig_token=html.escape(span["token"]).replace('\n', '<br>'),
|
160 |
+
hover=hover if show_all_on_hover or show_alternative else ''
|
161 |
)
|
162 |
html_out = f"""
|
163 |
<style>
|