Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,7 @@ import matplotlib.pyplot as plt
|
|
| 4 |
import pandas as pd
|
| 5 |
import io
|
| 6 |
import base64
|
| 7 |
-
import ast
|
| 8 |
|
| 9 |
# Function to process and visualize log probs
|
| 10 |
def visualize_logprobs(json_input):
|
|
@@ -64,10 +64,34 @@ def visualize_logprobs(json_input):
|
|
| 64 |
columns=["Token", "Log Prob", "Top 1 Alternative", "Top 2 Alternative", "Top 3 Alternative"]
|
| 65 |
)
|
| 66 |
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
except Exception as e:
|
| 70 |
-
return f"Error: {str(e)}", None
|
| 71 |
|
| 72 |
# Gradio interface
|
| 73 |
with gr.Blocks(title="Log Probability Visualizer") as app:
|
|
@@ -80,13 +104,14 @@ with gr.Blocks(title="Log Probability Visualizer") as app:
|
|
| 80 |
# Outputs
|
| 81 |
plot_output = gr.HTML(label="Log Probability Plot")
|
| 82 |
table_output = gr.Dataframe(label="Token Log Probabilities and Top Alternatives")
|
|
|
|
| 83 |
|
| 84 |
# Button to trigger visualization
|
| 85 |
btn = gr.Button("Visualize")
|
| 86 |
btn.click(
|
| 87 |
fn=visualize_logprobs,
|
| 88 |
inputs=json_input,
|
| 89 |
-
outputs=[plot_output, table_output]
|
| 90 |
)
|
| 91 |
|
| 92 |
# Launch the app
|
|
|
|
| 4 |
import pandas as pd
|
| 5 |
import io
|
| 6 |
import base64
|
| 7 |
+
import ast
|
| 8 |
|
| 9 |
# Function to process and visualize log probs
|
| 10 |
def visualize_logprobs(json_input):
|
|
|
|
| 64 |
columns=["Token", "Log Prob", "Top 1 Alternative", "Top 2 Alternative", "Top 3 Alternative"]
|
| 65 |
)
|
| 66 |
|
| 67 |
+
# Generate colored text based on log probabilities
|
| 68 |
+
# Normalize log probs to [0, 1] for color scaling (0 = most uncertain, 1 = most confident)
|
| 69 |
+
min_logprob = min(logprobs)
|
| 70 |
+
max_logprob = max(logprobs)
|
| 71 |
+
if max_logprob == min_logprob:
|
| 72 |
+
normalized_probs = [0.5] * len(logprobs) # Avoid division by zero
|
| 73 |
+
else:
|
| 74 |
+
normalized_probs = [(lp - min_logprob) / (max_logprob - min_logprob) for lp in logprobs]
|
| 75 |
+
|
| 76 |
+
# Create HTML for colored text
|
| 77 |
+
colored_text = ""
|
| 78 |
+
for i, (token, norm_prob) in enumerate(zip(tokens, normalized_probs)):
|
| 79 |
+
# Map normalized probability to RGB color (green for high confidence, red for low)
|
| 80 |
+
# Use a simple linear interpolation: green (0, 255, 0) to red (255, 0, 0)
|
| 81 |
+
r = int(255 * (1 - norm_prob)) # Red increases as uncertainty increases
|
| 82 |
+
g = int(255 * norm_prob) # Green decreases as uncertainty increases
|
| 83 |
+
b = 0 # Blue stays 0 for simplicity
|
| 84 |
+
color = f'rgb({r}, {g}, {b})'
|
| 85 |
+
colored_text += f'<span style="color: {color}; font-weight: bold;">{token}</span>'
|
| 86 |
+
if i < len(tokens) - 1:
|
| 87 |
+
colored_text += " " # Add space between tokens
|
| 88 |
+
|
| 89 |
+
colored_text_html = f'<p>{colored_text}</p>'
|
| 90 |
+
|
| 91 |
+
return img_html, df, colored_text_html
|
| 92 |
|
| 93 |
except Exception as e:
|
| 94 |
+
return f"Error: {str(e)}", None, None
|
| 95 |
|
| 96 |
# Gradio interface
|
| 97 |
with gr.Blocks(title="Log Probability Visualizer") as app:
|
|
|
|
| 104 |
# Outputs
|
| 105 |
plot_output = gr.HTML(label="Log Probability Plot")
|
| 106 |
table_output = gr.Dataframe(label="Token Log Probabilities and Top Alternatives")
|
| 107 |
+
text_output = gr.HTML(label="Colored Text (Confidence Visualization)")
|
| 108 |
|
| 109 |
# Button to trigger visualization
|
| 110 |
btn = gr.Button("Visualize")
|
| 111 |
btn.click(
|
| 112 |
fn=visualize_logprobs,
|
| 113 |
inputs=json_input,
|
| 114 |
+
outputs=[plot_output, table_output, text_output]
|
| 115 |
)
|
| 116 |
|
| 117 |
# Launch the app
|