Commit
Β·
51fc1ab
1
Parent(s):
dd3c0f8
Fix routing UI bugs and unify chart formats
Browse files- Fix double-click bug on Let's ROUTE button (remove unnecessary yield)
- Disable Let's ROUTE button until routing model is selected
- Unify token charts: all values in millions (M)
- Unify legends: horizontal, positioned at top-right
- Fix model_name mismatch in calculated tokens
app.py
CHANGED
|
@@ -686,26 +686,26 @@ def create_token_charts(df: pd.DataFrame, input_price: float, cache_read_price:
|
|
| 686 |
|
| 687 |
token_data = pd.DataFrame({
|
| 688 |
"Token Type": ["Uncached Input", "Cache Read", "Cache Creation", "Completion"],
|
| 689 |
-
"Total Tokens": [total_uncached_input, total_cache_read, total_cache_creation, total_completion],
|
| 690 |
})
|
| 691 |
|
| 692 |
fig_tokens = px.bar(
|
| 693 |
token_data,
|
| 694 |
x="Token Type",
|
| 695 |
-
y="Total Tokens",
|
| 696 |
title="Total Tokens by Type",
|
| 697 |
color="Token Type",
|
| 698 |
color_discrete_sequence=["#EF553B", "#19D3F3", "#FFA15A", "#AB63FA"],
|
| 699 |
)
|
| 700 |
fig_tokens.update_layout(
|
| 701 |
xaxis_title="Token Type",
|
| 702 |
-
yaxis_title="
|
| 703 |
showlegend=False,
|
| 704 |
margin=dict(l=40, r=20, t=40, b=40),
|
| 705 |
)
|
| 706 |
-
total_all =
|
| 707 |
fig_tokens.add_annotation(
|
| 708 |
-
text=f"Total: {total_all
|
| 709 |
xref="paper", yref="paper",
|
| 710 |
x=0.95, y=0.95, showarrow=False,
|
| 711 |
font=dict(size=12),
|
|
@@ -722,26 +722,26 @@ def create_token_charts(df: pd.DataFrame, input_price: float, cache_read_price:
|
|
| 722 |
|
| 723 |
fig_stacked = go.Figure()
|
| 724 |
fig_stacked.add_trace(go.Bar(
|
| 725 |
-
name="Uncached Input", x=df_sorted["trajectory_idx"], y=df_sorted["uncached_input_tokens"],
|
| 726 |
-
marker_color="#EF553B", hovertemplate="Trajectory: %{x}<br>Uncached Input: %{y
|
| 727 |
))
|
| 728 |
fig_stacked.add_trace(go.Bar(
|
| 729 |
-
name="Cache Read", x=df_sorted["trajectory_idx"], y=df_sorted["cache_read_tokens"],
|
| 730 |
-
marker_color="#19D3F3", hovertemplate="Trajectory: %{x}<br>Cache Read: %{y
|
| 731 |
))
|
| 732 |
fig_stacked.add_trace(go.Bar(
|
| 733 |
-
name="Cache Creation", x=df_sorted["trajectory_idx"], y=df_sorted["cache_creation_tokens"],
|
| 734 |
-
marker_color="#FFA15A", hovertemplate="Trajectory: %{x}<br>Cache Creation: %{y
|
| 735 |
))
|
| 736 |
fig_stacked.add_trace(go.Bar(
|
| 737 |
-
name="Completion", x=df_sorted["trajectory_idx"], y=df_sorted["completion_tokens"],
|
| 738 |
-
marker_color="#AB63FA", hovertemplate="Trajectory: %{x}<br>Completion: %{y
|
| 739 |
))
|
| 740 |
fig_stacked.update_layout(
|
| 741 |
barmode="stack",
|
| 742 |
title="Tokens per Trajectory (stacked)",
|
| 743 |
xaxis_title="Trajectory (sorted by total tokens)",
|
| 744 |
-
yaxis_title="Tokens",
|
| 745 |
legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1),
|
| 746 |
margin=dict(l=50, r=20, t=60, b=40),
|
| 747 |
)
|
|
@@ -1367,7 +1367,7 @@ def build_app():
|
|
| 1367 |
end_step_3 = gr.Number(label="End (int=step; 0,0-1,0=ratio)", value=0.5, minimum=0, precision=2, interactive=True)
|
| 1368 |
|
| 1369 |
gr.Markdown("---")
|
| 1370 |
-
route_btn = gr.Button("π Let's ROUTE!!", variant="primary", size="lg")
|
| 1371 |
routing_result = gr.Markdown(visible=False)
|
| 1372 |
|
| 1373 |
|
|
@@ -1458,7 +1458,7 @@ def build_app():
|
|
| 1458 |
def on_routing_model_1_select(model_name):
|
| 1459 |
prices = get_routing_prices_with_labels(model_name)
|
| 1460 |
show_btn = bool(model_name)
|
| 1461 |
-
return *prices, gr.update(visible=show_btn)
|
| 1462 |
|
| 1463 |
def on_routing_model_2_select(model_name):
|
| 1464 |
prices = get_routing_prices_with_labels(model_name)
|
|
@@ -1471,7 +1471,7 @@ def build_app():
|
|
| 1471 |
routing_model_1.change(
|
| 1472 |
fn=on_routing_model_1_select,
|
| 1473 |
inputs=[routing_model_1],
|
| 1474 |
-
outputs=[routing_price_1_input, routing_price_1_cache_read, routing_price_1_cache_creation, routing_price_1_completion, add_model_2_btn],
|
| 1475 |
)
|
| 1476 |
|
| 1477 |
add_model_2_btn.click(
|
|
@@ -1645,13 +1645,6 @@ def build_app():
|
|
| 1645 |
additional_token_models = [(routing_model_1_val, total_routing_tokens)]
|
| 1646 |
additional_cost_models = [(routing_model_1_val, routing_costs)]
|
| 1647 |
|
| 1648 |
-
yield (
|
| 1649 |
-
gr.update(visible=True, value="β³ Creating charts..."),
|
| 1650 |
-
gr.update(visible=True),
|
| 1651 |
-
None,
|
| 1652 |
-
None,
|
| 1653 |
-
)
|
| 1654 |
-
|
| 1655 |
tokens_chart = create_routed_token_chart(total_base_tokens, additional_token_models)
|
| 1656 |
cost_chart = create_routed_cost_chart(base_costs, additional_cost_models)
|
| 1657 |
|
|
|
|
| 686 |
|
| 687 |
token_data = pd.DataFrame({
|
| 688 |
"Token Type": ["Uncached Input", "Cache Read", "Cache Creation", "Completion"],
|
| 689 |
+
"Total Tokens (M)": [total_uncached_input / 1e6, total_cache_read / 1e6, total_cache_creation / 1e6, total_completion / 1e6],
|
| 690 |
})
|
| 691 |
|
| 692 |
fig_tokens = px.bar(
|
| 693 |
token_data,
|
| 694 |
x="Token Type",
|
| 695 |
+
y="Total Tokens (M)",
|
| 696 |
title="Total Tokens by Type",
|
| 697 |
color="Token Type",
|
| 698 |
color_discrete_sequence=["#EF553B", "#19D3F3", "#FFA15A", "#AB63FA"],
|
| 699 |
)
|
| 700 |
fig_tokens.update_layout(
|
| 701 |
xaxis_title="Token Type",
|
| 702 |
+
yaxis_title="Tokens (M)",
|
| 703 |
showlegend=False,
|
| 704 |
margin=dict(l=40, r=20, t=40, b=40),
|
| 705 |
)
|
| 706 |
+
total_all = total_uncached_input + total_cache_read + total_cache_creation + total_completion
|
| 707 |
fig_tokens.add_annotation(
|
| 708 |
+
text=f"Total: {total_all/1e6:.2f}M",
|
| 709 |
xref="paper", yref="paper",
|
| 710 |
x=0.95, y=0.95, showarrow=False,
|
| 711 |
font=dict(size=12),
|
|
|
|
| 722 |
|
| 723 |
fig_stacked = go.Figure()
|
| 724 |
fig_stacked.add_trace(go.Bar(
|
| 725 |
+
name="Uncached Input", x=df_sorted["trajectory_idx"], y=df_sorted["uncached_input_tokens"] / 1e6,
|
| 726 |
+
marker_color="#EF553B", hovertemplate="Trajectory: %{x}<br>Uncached Input: %{y:.2f}M<extra></extra>",
|
| 727 |
))
|
| 728 |
fig_stacked.add_trace(go.Bar(
|
| 729 |
+
name="Cache Read", x=df_sorted["trajectory_idx"], y=df_sorted["cache_read_tokens"] / 1e6,
|
| 730 |
+
marker_color="#19D3F3", hovertemplate="Trajectory: %{x}<br>Cache Read: %{y:.2f}M<extra></extra>",
|
| 731 |
))
|
| 732 |
fig_stacked.add_trace(go.Bar(
|
| 733 |
+
name="Cache Creation", x=df_sorted["trajectory_idx"], y=df_sorted["cache_creation_tokens"] / 1e6,
|
| 734 |
+
marker_color="#FFA15A", hovertemplate="Trajectory: %{x}<br>Cache Creation: %{y:.2f}M<extra></extra>",
|
| 735 |
))
|
| 736 |
fig_stacked.add_trace(go.Bar(
|
| 737 |
+
name="Completion", x=df_sorted["trajectory_idx"], y=df_sorted["completion_tokens"] / 1e6,
|
| 738 |
+
marker_color="#AB63FA", hovertemplate="Trajectory: %{x}<br>Completion: %{y:.2f}M<extra></extra>",
|
| 739 |
))
|
| 740 |
fig_stacked.update_layout(
|
| 741 |
barmode="stack",
|
| 742 |
title="Tokens per Trajectory (stacked)",
|
| 743 |
xaxis_title="Trajectory (sorted by total tokens)",
|
| 744 |
+
yaxis_title="Tokens (M)",
|
| 745 |
legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1),
|
| 746 |
margin=dict(l=50, r=20, t=60, b=40),
|
| 747 |
)
|
|
|
|
| 1367 |
end_step_3 = gr.Number(label="End (int=step; 0,0-1,0=ratio)", value=0.5, minimum=0, precision=2, interactive=True)
|
| 1368 |
|
| 1369 |
gr.Markdown("---")
|
| 1370 |
+
route_btn = gr.Button("π Let's ROUTE!!", variant="primary", size="lg", interactive=False)
|
| 1371 |
routing_result = gr.Markdown(visible=False)
|
| 1372 |
|
| 1373 |
|
|
|
|
| 1458 |
def on_routing_model_1_select(model_name):
|
| 1459 |
prices = get_routing_prices_with_labels(model_name)
|
| 1460 |
show_btn = bool(model_name)
|
| 1461 |
+
return *prices, gr.update(visible=show_btn), gr.update(interactive=show_btn)
|
| 1462 |
|
| 1463 |
def on_routing_model_2_select(model_name):
|
| 1464 |
prices = get_routing_prices_with_labels(model_name)
|
|
|
|
| 1471 |
routing_model_1.change(
|
| 1472 |
fn=on_routing_model_1_select,
|
| 1473 |
inputs=[routing_model_1],
|
| 1474 |
+
outputs=[routing_price_1_input, routing_price_1_cache_read, routing_price_1_cache_creation, routing_price_1_completion, add_model_2_btn, route_btn],
|
| 1475 |
)
|
| 1476 |
|
| 1477 |
add_model_2_btn.click(
|
|
|
|
| 1645 |
additional_token_models = [(routing_model_1_val, total_routing_tokens)]
|
| 1646 |
additional_cost_models = [(routing_model_1_val, routing_costs)]
|
| 1647 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1648 |
tokens_chart = create_routed_token_chart(total_base_tokens, additional_token_models)
|
| 1649 |
cost_chart = create_routed_cost_chart(base_costs, additional_cost_models)
|
| 1650 |
|