Spaces:
Running
Running
token showing in highlights
Browse files
app.py
CHANGED
|
@@ -15,11 +15,11 @@ def landing():
|
|
| 15 |
st.markdown("*Note*: These services send data to a remote server for processing. The server logs requests. Don't use sensitive or identifiable information on this page.")
|
| 16 |
|
| 17 |
|
| 18 |
-
def show_token(token):
|
| 19 |
token_display = token.replace('\n', '↵').replace('\t', '⇥')
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
return token_display
|
| 24 |
|
| 25 |
|
|
@@ -155,7 +155,7 @@ def highlights_component(spans, show_alternatives, min_loss, show_all_on_hover=F
|
|
| 155 |
show = span['token'] != span['most_likely_token'] and span['loss_ratio'] >= min_loss
|
| 156 |
alternative_to_show = next(token for token in span['topk_tokens'] if token != span['token'])
|
| 157 |
show_alternative = show and show_alternatives
|
| 158 |
-
hover = f'<span class="alternative">{alternative_to_show}</span>'
|
| 159 |
html_out += '<span style="color: {color}" >{hover}{orig_token}</span>'.format(
|
| 160 |
color="grey" if show else "black",
|
| 161 |
orig_token=html.escape(span["token"]).replace('\n', '<br>'),
|
|
|
|
| 15 |
st.markdown("*Note*: These services send data to a remote server for processing. The server logs requests. Don't use sensitive or identifiable information on this page.")
|
| 16 |
|
| 17 |
|
| 18 |
+
def show_token(token: str, escape_markdown=True) -> str:
|
| 19 |
token_display = token.replace('\n', '↵').replace('\t', '⇥')
|
| 20 |
+
if escape_markdown:
|
| 21 |
+
for c in "\\`*_{}[]()#+-.!":
|
| 22 |
+
token_display = token_display.replace(c, "\\" + c)
|
| 23 |
return token_display
|
| 24 |
|
| 25 |
|
|
|
|
| 155 |
show = span['token'] != span['most_likely_token'] and span['loss_ratio'] >= min_loss
|
| 156 |
alternative_to_show = next(token for token in span['topk_tokens'] if token != span['token'])
|
| 157 |
show_alternative = show and show_alternatives
|
| 158 |
+
hover = f'<span class="alternative">{show_token(alternative_to_show, escape_markdown=False)}</span>'
|
| 159 |
html_out += '<span style="color: {color}" >{hover}{orig_token}</span>'.format(
|
| 160 |
color="grey" if show else "black",
|
| 161 |
orig_token=html.escape(span["token"]).replace('\n', '<br>'),
|