Spaces:
Running on Zero
Running on Zero
Upload app.py
Browse files
app.py
CHANGED
|
@@ -28,16 +28,20 @@ def predict(prompt, dark_mode):
|
|
| 28 |
first_beam_score = exp(generated_dicts.sequences_scores[0]).item()
|
| 29 |
|
| 30 |
# Construct Main HTML output
|
| 31 |
-
# We use a
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
# Adjust colors based on dark mode
|
| 36 |
normal_color = "white" if dark_mode else "black"
|
| 37 |
|
| 38 |
for token, score in zip(first_beam_tokens, first_beam_scores):
|
| 39 |
-
# Replace SentencePiece space character (U+2581)
|
| 40 |
-
display_token = token.replace(' ', ' ')
|
| 41 |
|
| 42 |
if score < 0.6:
|
| 43 |
color = "red"
|
|
@@ -49,7 +53,7 @@ def predict(prompt, dark_mode):
|
|
| 49 |
|
| 50 |
html_output += f'<span style="{style}" title="Score: {score:.4f}">{display_token}</span>'
|
| 51 |
|
| 52 |
-
html_output += '</div>'
|
| 53 |
|
| 54 |
# Construct Detailed HTML output
|
| 55 |
# We create a simple table to list tokens and their scores
|
|
@@ -59,8 +63,19 @@ def predict(prompt, dark_mode):
|
|
| 59 |
|
| 60 |
detail_output = f'<table style="{detail_table_style}"><thead><tr><th style="{th_style}">Token</th><th style="{th_style}">Score</th></tr></thead><tbody>'
|
| 61 |
for token, score in zip(first_beam_tokens, first_beam_scores):
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
detail_output += '</tbody></table>'
|
| 65 |
|
| 66 |
return html_output, detail_output
|
|
|
|
| 28 |
first_beam_score = exp(generated_dicts.sequences_scores[0]).item()
|
| 29 |
|
| 30 |
# Construct Main HTML output
|
| 31 |
+
# We use a simple div for the title instead of a markdown-style header
|
| 32 |
+
main_container_style = "border: 2px solid lightblue; padding: 15px; border-radius: 8px; font-family: sans-serif;"
|
| 33 |
+
title_style = "font-weight: bold; margin-bottom: 10px; font-size: 1.1em;"
|
| 34 |
+
|
| 35 |
+
html_output = f'<div style="{main_container_style}">'
|
| 36 |
+
html_output += f'<div style="{title_style}">Beam 1 | Score: {first_beam_score:.4f}</div>'
|
| 37 |
+
html_output += '<div style="font-family: monospace; white-space: pre-wrap; line-height: 1.5; overflow-wrap: break-word; word-wrap: break-word;">'
|
| 38 |
|
| 39 |
# Adjust colors based on dark mode
|
| 40 |
normal_color = "white" if dark_mode else "black"
|
| 41 |
|
| 42 |
for token, score in zip(first_beam_tokens, first_beam_scores):
|
| 43 |
+
# Replace SentencePiece space character (U+2581) and any underscores used for spaces
|
| 44 |
+
display_token = token.replace(' ', ' ').replace('_', ' ')
|
| 45 |
|
| 46 |
if score < 0.6:
|
| 47 |
color = "red"
|
|
|
|
| 53 |
|
| 54 |
html_output += f'<span style="{style}" title="Score: {score:.4f}">{display_token}</span>'
|
| 55 |
|
| 56 |
+
html_output += '</div></div>'
|
| 57 |
|
| 58 |
# Construct Detailed HTML output
|
| 59 |
# We create a simple table to list tokens and their scores
|
|
|
|
| 63 |
|
| 64 |
detail_output = f'<table style="{detail_table_style}"><thead><tr><th style="{th_style}">Token</th><th style="{th_style}">Score</th></tr></thead><tbody>'
|
| 65 |
for token, score in zip(first_beam_tokens, first_beam_scores):
|
| 66 |
+
# Replace SentencePiece space character (U+2581) and any underscores used for spaces
|
| 67 |
+
display_token = token.replace(' ', ' ').replace('_', ' ')
|
| 68 |
+
|
| 69 |
+
# Apply red highlighting for low-probability tokens
|
| 70 |
+
if score < 0.6:
|
| 71 |
+
color = "red"
|
| 72 |
+
bg_color = "#441111" if dark_mode else "#ffe6e6"
|
| 73 |
+
token_cell_style = f"{td_style} color: {color}; background-color: {bg_color};"
|
| 74 |
+
else:
|
| 75 |
+
color = normal_color
|
| 76 |
+
token_cell_style = f"{td_style} color: {color};"
|
| 77 |
+
|
| 78 |
+
detail_output += f'<tr><td style="{token_cell_style}">{display_token}</td><td style="{td_style}">{score:.4f}</td></tr>'
|
| 79 |
detail_output += '</tbody></table>'
|
| 80 |
|
| 81 |
return html_output, detail_output
|