Spaces:
Running
Running
Upload app.py
Browse files
app.py
CHANGED
|
@@ -115,6 +115,10 @@ def create_probability_chart(prob_before: float, prob_after: float) -> go.Figure
|
|
| 115 |
"""Create a bar chart showing probability change."""
|
| 116 |
fig = go.Figure()
|
| 117 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
fig.add_trace(go.Bar(
|
| 119 |
x=['Before Token', 'After Token'],
|
| 120 |
y=[prob_before, prob_after],
|
|
@@ -171,13 +175,15 @@ def create_pivotal_token_flow(df: pd.DataFrame, selected_query: str = None) -> g
|
|
| 171 |
f"Query: {str(row.get('query', ''))[:50]}..."
|
| 172 |
for _, row in positive_df.iterrows()
|
| 173 |
]
|
|
|
|
|
|
|
| 174 |
fig.add_trace(go.Scatter(
|
| 175 |
x=list(range(len(positive_df))),
|
| 176 |
-
y=
|
| 177 |
mode='markers',
|
| 178 |
name='Positive Impact',
|
| 179 |
marker=dict(
|
| 180 |
-
size=
|
| 181 |
color='#22c55e',
|
| 182 |
opacity=0.7
|
| 183 |
),
|
|
@@ -195,13 +201,15 @@ def create_pivotal_token_flow(df: pd.DataFrame, selected_query: str = None) -> g
|
|
| 195 |
f"Query: {str(row.get('query', ''))[:50]}..."
|
| 196 |
for _, row in negative_df.iterrows()
|
| 197 |
]
|
|
|
|
|
|
|
| 198 |
fig.add_trace(go.Scatter(
|
| 199 |
x=list(range(len(negative_df))),
|
| 200 |
-
y=
|
| 201 |
mode='markers',
|
| 202 |
name='Negative Impact',
|
| 203 |
marker=dict(
|
| 204 |
-
size=
|
| 205 |
color='#ef4444',
|
| 206 |
opacity=0.7
|
| 207 |
),
|
|
@@ -290,8 +298,8 @@ def create_thought_anchor_graph(df: pd.DataFrame, selected_query: str = None) ->
|
|
| 290 |
for edge in G.edges():
|
| 291 |
x0, y0 = pos[edge[0]]
|
| 292 |
x1, y1 = pos[edge[1]]
|
| 293 |
-
edge_x.extend([x0, x1, None])
|
| 294 |
-
edge_y.extend([y0, y1, None])
|
| 295 |
|
| 296 |
edge_trace = go.Scatter(
|
| 297 |
x=edge_x, y=edge_y,
|
|
@@ -309,12 +317,12 @@ def create_thought_anchor_graph(df: pd.DataFrame, selected_query: str = None) ->
|
|
| 309 |
|
| 310 |
for node in G.nodes():
|
| 311 |
x, y = pos[node]
|
| 312 |
-
node_x.append(x)
|
| 313 |
-
node_y.append(y)
|
| 314 |
|
| 315 |
node_data = G.nodes[node]
|
| 316 |
is_positive = node_data.get('is_positive', True)
|
| 317 |
-
importance = node_data.get('importance', 0.3)
|
| 318 |
|
| 319 |
node_colors.append('#22c55e' if is_positive else '#ef4444')
|
| 320 |
node_sizes.append(20 + importance * 50)
|
|
@@ -401,8 +409,8 @@ def create_probability_space_visualization(df: pd.DataFrame, color_by: str = 'is
|
|
| 401 |
hover_texts.append(text)
|
| 402 |
|
| 403 |
fig.add_trace(go.Scatter(
|
| 404 |
-
x=df['prob_before'],
|
| 405 |
-
y=df['prob_after'],
|
| 406 |
mode='markers',
|
| 407 |
marker=dict(
|
| 408 |
size=8,
|
|
@@ -519,8 +527,8 @@ def create_embedding_visualization(df: pd.DataFrame, color_by: str = 'is_positiv
|
|
| 519 |
|
| 520 |
# Create dataframe for plotting
|
| 521 |
plot_df = df.iloc[valid_indices].copy()
|
| 522 |
-
plot_df['x'] = coords[:, 0]
|
| 523 |
-
plot_df['y'] = coords[:, 1]
|
| 524 |
|
| 525 |
# Handle color column
|
| 526 |
if color_by not in plot_df.columns:
|
|
@@ -618,6 +626,8 @@ def create_pivotal_token_trace(df: pd.DataFrame, selected_query: str) -> Tuple[s
|
|
| 618 |
# Create probability delta chart
|
| 619 |
fig = go.Figure()
|
| 620 |
|
|
|
|
|
|
|
| 621 |
colors = ['#22c55e' if d > 0 else '#ef4444' for d in prob_deltas]
|
| 622 |
|
| 623 |
fig.add_trace(go.Bar(
|
|
@@ -746,8 +756,8 @@ def create_circuit_visualization(df: pd.DataFrame, query_idx: int = 0) -> Tuple[
|
|
| 746 |
colors = ['#22c55e' if p > 0.5 else '#ef4444' for p in prob_values]
|
| 747 |
|
| 748 |
fig.add_trace(go.Scatter(
|
| 749 |
-
x=sentence_ids,
|
| 750 |
-
y=prob_values,
|
| 751 |
mode='lines+markers',
|
| 752 |
name='Success Probability',
|
| 753 |
line=dict(color='#6366f1', width=2),
|
|
@@ -830,21 +840,27 @@ def create_statistics_dashboard(df: pd.DataFrame) -> Tuple[str, go.Figure]:
|
|
| 830 |
fig = make_subplots(rows=1, cols=2,
|
| 831 |
subplot_titles=("Probability Delta Distribution", second_chart_title))
|
| 832 |
|
| 833 |
-
# First chart: Probability Delta histogram
|
| 834 |
if 'prob_delta' in df.columns and len(df['prob_delta'].dropna()) > 0:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 835 |
fig.add_trace(
|
| 836 |
-
go.
|
| 837 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 838 |
row=1, col=1
|
| 839 |
)
|
| 840 |
-
else:
|
| 841 |
-
# Fallback: show prob_before/prob_after if available
|
| 842 |
-
if 'prob_after' in df.columns:
|
| 843 |
-
fig.add_trace(
|
| 844 |
-
go.Histogram(x=df['prob_after'].dropna(), nbinsx=30, name="Prob After",
|
| 845 |
-
marker_color='#6366f1'),
|
| 846 |
-
row=1, col=1
|
| 847 |
-
)
|
| 848 |
|
| 849 |
# Second chart: Categories, patterns, or task types
|
| 850 |
if 'sentence_category' in df.columns:
|
|
|
|
| 115 |
"""Create a bar chart showing probability change."""
|
| 116 |
fig = go.Figure()
|
| 117 |
|
| 118 |
+
# Ensure values are Python floats
|
| 119 |
+
prob_before = float(prob_before) if prob_before is not None else 0.0
|
| 120 |
+
prob_after = float(prob_after) if prob_after is not None else 0.0
|
| 121 |
+
|
| 122 |
fig.add_trace(go.Bar(
|
| 123 |
x=['Before Token', 'After Token'],
|
| 124 |
y=[prob_before, prob_after],
|
|
|
|
| 175 |
f"Query: {str(row.get('query', ''))[:50]}..."
|
| 176 |
for _, row in positive_df.iterrows()
|
| 177 |
]
|
| 178 |
+
y_vals = positive_df['prob_delta'].tolist()
|
| 179 |
+
sizes = [10 + abs(v) * 30 for v in y_vals]
|
| 180 |
fig.add_trace(go.Scatter(
|
| 181 |
x=list(range(len(positive_df))),
|
| 182 |
+
y=y_vals,
|
| 183 |
mode='markers',
|
| 184 |
name='Positive Impact',
|
| 185 |
marker=dict(
|
| 186 |
+
size=sizes,
|
| 187 |
color='#22c55e',
|
| 188 |
opacity=0.7
|
| 189 |
),
|
|
|
|
| 201 |
f"Query: {str(row.get('query', ''))[:50]}..."
|
| 202 |
for _, row in negative_df.iterrows()
|
| 203 |
]
|
| 204 |
+
y_vals = negative_df['prob_delta'].tolist()
|
| 205 |
+
sizes = [10 + abs(v) * 30 for v in y_vals]
|
| 206 |
fig.add_trace(go.Scatter(
|
| 207 |
x=list(range(len(negative_df))),
|
| 208 |
+
y=y_vals,
|
| 209 |
mode='markers',
|
| 210 |
name='Negative Impact',
|
| 211 |
marker=dict(
|
| 212 |
+
size=sizes,
|
| 213 |
color='#ef4444',
|
| 214 |
opacity=0.7
|
| 215 |
),
|
|
|
|
| 298 |
for edge in G.edges():
|
| 299 |
x0, y0 = pos[edge[0]]
|
| 300 |
x1, y1 = pos[edge[1]]
|
| 301 |
+
edge_x.extend([float(x0), float(x1), None])
|
| 302 |
+
edge_y.extend([float(y0), float(y1), None])
|
| 303 |
|
| 304 |
edge_trace = go.Scatter(
|
| 305 |
x=edge_x, y=edge_y,
|
|
|
|
| 317 |
|
| 318 |
for node in G.nodes():
|
| 319 |
x, y = pos[node]
|
| 320 |
+
node_x.append(float(x))
|
| 321 |
+
node_y.append(float(y))
|
| 322 |
|
| 323 |
node_data = G.nodes[node]
|
| 324 |
is_positive = node_data.get('is_positive', True)
|
| 325 |
+
importance = float(node_data.get('importance', 0.3))
|
| 326 |
|
| 327 |
node_colors.append('#22c55e' if is_positive else '#ef4444')
|
| 328 |
node_sizes.append(20 + importance * 50)
|
|
|
|
| 409 |
hover_texts.append(text)
|
| 410 |
|
| 411 |
fig.add_trace(go.Scatter(
|
| 412 |
+
x=df['prob_before'].tolist(),
|
| 413 |
+
y=df['prob_after'].tolist(),
|
| 414 |
mode='markers',
|
| 415 |
marker=dict(
|
| 416 |
size=8,
|
|
|
|
| 527 |
|
| 528 |
# Create dataframe for plotting
|
| 529 |
plot_df = df.iloc[valid_indices].copy()
|
| 530 |
+
plot_df['x'] = coords[:, 0].tolist()
|
| 531 |
+
plot_df['y'] = coords[:, 1].tolist()
|
| 532 |
|
| 533 |
# Handle color column
|
| 534 |
if color_by not in plot_df.columns:
|
|
|
|
| 626 |
# Create probability delta chart
|
| 627 |
fig = go.Figure()
|
| 628 |
|
| 629 |
+
# Ensure all values are Python native types
|
| 630 |
+
prob_deltas = [float(d) for d in prob_deltas]
|
| 631 |
colors = ['#22c55e' if d > 0 else '#ef4444' for d in prob_deltas]
|
| 632 |
|
| 633 |
fig.add_trace(go.Bar(
|
|
|
|
| 756 |
colors = ['#22c55e' if p > 0.5 else '#ef4444' for p in prob_values]
|
| 757 |
|
| 758 |
fig.add_trace(go.Scatter(
|
| 759 |
+
x=[int(s) if isinstance(s, (int, np.integer)) else s for s in sentence_ids],
|
| 760 |
+
y=[float(p) for p in prob_values],
|
| 761 |
mode='lines+markers',
|
| 762 |
name='Success Probability',
|
| 763 |
line=dict(color='#6366f1', width=2),
|
|
|
|
| 840 |
fig = make_subplots(rows=1, cols=2,
|
| 841 |
subplot_titles=("Probability Delta Distribution", second_chart_title))
|
| 842 |
|
| 843 |
+
# First chart: Probability Delta histogram (using numpy for binning)
|
| 844 |
if 'prob_delta' in df.columns and len(df['prob_delta'].dropna()) > 0:
|
| 845 |
+
prob_data = df['prob_delta'].dropna().values
|
| 846 |
+
# Create histogram manually using numpy
|
| 847 |
+
counts, bin_edges = np.histogram(prob_data, bins=30)
|
| 848 |
+
bin_centers = [(bin_edges[i] + bin_edges[i+1]) / 2 for i in range(len(bin_edges)-1)]
|
| 849 |
fig.add_trace(
|
| 850 |
+
go.Bar(x=bin_centers, y=counts.tolist(), name="Prob Delta",
|
| 851 |
+
marker_color='#6366f1', width=(bin_edges[1]-bin_edges[0])*0.9),
|
| 852 |
+
row=1, col=1
|
| 853 |
+
)
|
| 854 |
+
elif 'prob_after' in df.columns and len(df['prob_after'].dropna()) > 0:
|
| 855 |
+
# Fallback: show prob_after distribution
|
| 856 |
+
prob_data = df['prob_after'].dropna().values
|
| 857 |
+
counts, bin_edges = np.histogram(prob_data, bins=30)
|
| 858 |
+
bin_centers = [(bin_edges[i] + bin_edges[i+1]) / 2 for i in range(len(bin_edges)-1)]
|
| 859 |
+
fig.add_trace(
|
| 860 |
+
go.Bar(x=bin_centers, y=counts.tolist(), name="Prob After",
|
| 861 |
+
marker_color='#6366f1', width=(bin_edges[1]-bin_edges[0])*0.9),
|
| 862 |
row=1, col=1
|
| 863 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 864 |
|
| 865 |
# Second chart: Categories, patterns, or task types
|
| 866 |
if 'sentence_category' in df.columns:
|