Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +103 -128
src/streamlit_app.py
CHANGED
@@ -1,7 +1,5 @@
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
-
import matplotlib.pyplot as plt
|
4 |
-
import numpy as np
|
5 |
|
6 |
st.set_page_config(page_title="LLM API Budget Dashboard", layout="wide")
|
7 |
|
@@ -11,10 +9,10 @@ st.markdown("This dashboard helps you budget your API calls to various LLMs base
|
|
11 |
|
12 |
# Define LLM models and their costs
|
13 |
llm_data = {
|
14 |
-
"GPT-4o": {"input_cost_per_m": 2.50, "output_cost_per_m": 10.00},
|
15 |
-
"Claude 3.7 Sonnet": {"input_cost_per_m": 3.00, "output_cost_per_m": 15.00},
|
16 |
-
"Gemini Flash 1.5-8b": {"input_cost_per_m": 0.038, "output_cost_per_m": 0.15},
|
17 |
-
"o3-mini": {"input_cost_per_m": 1.10, "output_cost_per_m": 4.40}
|
18 |
}
|
19 |
|
20 |
# Convert the LLM data to a DataFrame for displaying in a table
|
@@ -22,7 +20,8 @@ llm_df = pd.DataFrame([
|
|
22 |
{
|
23 |
"Model": model,
|
24 |
"Input Cost ($/M tokens)": data["input_cost_per_m"],
|
25 |
-
"Output Cost ($/M tokens)": data["output_cost_per_m"]
|
|
|
26 |
}
|
27 |
for model, data in llm_data.items()
|
28 |
])
|
@@ -31,131 +30,107 @@ llm_df = pd.DataFrame([
|
|
31 |
st.subheader("LLM Cost Information")
|
32 |
st.dataframe(llm_df, use_container_width=True)
|
33 |
|
34 |
-
# Create
|
35 |
-
st.
|
36 |
|
37 |
-
|
38 |
-
st.
|
39 |
-
input_tokens = st.sidebar.number_input("Input Tokens", min_value=1, value=1000, step=100)
|
40 |
-
output_tokens = st.sidebar.number_input("Output Tokens", min_value=1, value=500, step=100)
|
41 |
-
|
42 |
-
# LLM selection
|
43 |
-
st.sidebar.subheader("Select LLMs")
|
44 |
-
selected_llms = st.sidebar.multiselect("Choose LLMs", options=list(llm_data.keys()), default=list(llm_data.keys()))
|
45 |
-
|
46 |
-
# Run count settings
|
47 |
-
st.sidebar.subheader("Run Count Settings")
|
48 |
-
uniform_runs = st.sidebar.checkbox("Run all LLMs the same number of times", value=True)
|
49 |
-
|
50 |
-
if uniform_runs:
|
51 |
-
uniform_run_count = st.sidebar.number_input("Number of runs for all LLMs", min_value=1, value=1, step=1)
|
52 |
-
run_counts = {llm: uniform_run_count for llm in selected_llms}
|
53 |
-
else:
|
54 |
-
st.sidebar.write("Set individual run counts for each LLM:")
|
55 |
-
run_counts = {}
|
56 |
-
for llm in selected_llms:
|
57 |
-
run_counts[llm] = st.sidebar.number_input(f"Runs for {llm}", min_value=1, value=1, step=1)
|
58 |
-
|
59 |
-
# Stability test settings
|
60 |
-
st.sidebar.subheader("Stability Test Settings")
|
61 |
-
stability_test = st.sidebar.checkbox("Enable stability testing", value=False)
|
62 |
-
|
63 |
-
if stability_test:
|
64 |
-
st.sidebar.write("Set stability iterations for selected LLMs:")
|
65 |
-
stability_iterations = {}
|
66 |
-
for llm in selected_llms:
|
67 |
-
stability_enabled = st.sidebar.checkbox(f"Test stability for {llm}", value=False)
|
68 |
-
if stability_enabled:
|
69 |
-
iterations = st.sidebar.number_input(f"Iterations for {llm}", min_value=2, value=10, step=1)
|
70 |
-
stability_iterations[llm] = iterations
|
71 |
-
else:
|
72 |
-
stability_iterations = {}
|
73 |
-
|
74 |
-
# Calculate costs
|
75 |
-
results = []
|
76 |
-
|
77 |
-
for llm in selected_llms:
|
78 |
-
base_runs = run_counts[llm]
|
79 |
-
stability_runs = stability_iterations.get(llm, 0)
|
80 |
-
total_runs = base_runs * (1 if stability_runs == 0 else stability_runs)
|
81 |
|
82 |
-
|
83 |
-
|
|
|
|
|
84 |
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
"
|
95 |
-
|
96 |
-
|
97 |
-
"
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
st.
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
st.
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
159 |
|
160 |
# Footer
|
161 |
st.markdown("---")
|
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
|
|
|
|
3 |
|
4 |
st.set_page_config(page_title="LLM API Budget Dashboard", layout="wide")
|
5 |
|
|
|
9 |
|
10 |
# Define LLM models and their costs
|
11 |
llm_data = {
|
12 |
+
"GPT-4o": {"input_cost_per_m": 2.50, "output_cost_per_m": 10.00, "description": "OpenAI's GPT-4o model"},
|
13 |
+
"Claude 3.7 Sonnet": {"input_cost_per_m": 3.00, "output_cost_per_m": 15.00, "description": "Anthropic's Claude 3.7 Sonnet model"},
|
14 |
+
"Gemini Flash 1.5-8b": {"input_cost_per_m": 0.038, "output_cost_per_m": 0.15, "description": "Google's Gemini Flash 1.5-8b model"},
|
15 |
+
"o3-mini": {"input_cost_per_m": 1.10, "output_cost_per_m": 4.40, "description": "Created Jan 31, 2025, 200k context"}
|
16 |
}
|
17 |
|
18 |
# Convert the LLM data to a DataFrame for displaying in a table
|
|
|
20 |
{
|
21 |
"Model": model,
|
22 |
"Input Cost ($/M tokens)": data["input_cost_per_m"],
|
23 |
+
"Output Cost ($/M tokens)": data["output_cost_per_m"],
|
24 |
+
"Description": data["description"]
|
25 |
}
|
26 |
for model, data in llm_data.items()
|
27 |
])
|
|
|
30 |
st.subheader("LLM Cost Information")
|
31 |
st.dataframe(llm_df, use_container_width=True)
|
32 |
|
33 |
+
# Create columns for main layout
|
34 |
+
left_col, right_col = st.columns([1, 3])
|
35 |
|
36 |
+
with left_col:
|
37 |
+
st.header("Configuration")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
+
# Token input section
|
40 |
+
st.subheader("Token Settings")
|
41 |
+
input_tokens = st.number_input("Input Tokens", min_value=1, value=1000, step=100)
|
42 |
+
output_tokens = st.number_input("Output Tokens", min_value=1, value=500, step=100)
|
43 |
|
44 |
+
# LLM selection
|
45 |
+
st.subheader("Select LLMs")
|
46 |
+
selected_llms = st.multiselect("Choose LLMs", options=list(llm_data.keys()), default=list(llm_data.keys()))
|
47 |
|
48 |
+
# Run count settings
|
49 |
+
st.subheader("Run Count Settings")
|
50 |
+
uniform_runs = st.checkbox("Run all LLMs the same number of times", value=True)
|
51 |
+
|
52 |
+
if uniform_runs:
|
53 |
+
uniform_run_count = st.number_input("Number of runs for all LLMs", min_value=1, value=1, step=1)
|
54 |
+
run_counts = {llm: uniform_run_count for llm in selected_llms}
|
55 |
+
else:
|
56 |
+
st.write("Set individual run counts for each LLM:")
|
57 |
+
run_counts = {}
|
58 |
+
for llm in selected_llms:
|
59 |
+
run_counts[llm] = st.number_input(f"Runs for {llm}", min_value=1, value=1, step=1)
|
60 |
+
|
61 |
+
# Stability test settings
|
62 |
+
st.subheader("Stability Test Settings")
|
63 |
+
stability_test = st.checkbox("Enable stability testing", value=False)
|
64 |
+
|
65 |
+
stability_iterations = {}
|
66 |
+
if stability_test:
|
67 |
+
st.write("Set stability iterations for selected LLMs:")
|
68 |
+
for llm in selected_llms:
|
69 |
+
stability_enabled = st.checkbox(f"Test stability for {llm}", value=False, key=f"stability_{llm}")
|
70 |
+
if stability_enabled:
|
71 |
+
iterations = st.number_input(f"Iterations for {llm}", min_value=2, value=10, step=1, key=f"iterations_{llm}")
|
72 |
+
stability_iterations[llm] = iterations
|
73 |
+
|
74 |
+
with right_col:
|
75 |
+
# Calculate costs
|
76 |
+
st.header("Cost Results")
|
77 |
+
|
78 |
+
if not selected_llms:
|
79 |
+
st.warning("Please select at least one LLM model.")
|
80 |
+
else:
|
81 |
+
results = []
|
82 |
+
|
83 |
+
for llm in selected_llms:
|
84 |
+
base_runs = run_counts[llm]
|
85 |
+
stability_runs = stability_iterations.get(llm, 0)
|
86 |
+
total_runs = base_runs * (1 if stability_runs == 0 else stability_runs)
|
87 |
+
|
88 |
+
total_input_tokens = input_tokens * total_runs
|
89 |
+
total_output_tokens = output_tokens * total_runs
|
90 |
+
|
91 |
+
input_cost = (total_input_tokens / 1_000_000) * llm_data[llm]["input_cost_per_m"]
|
92 |
+
output_cost = (total_output_tokens / 1_000_000) * llm_data[llm]["output_cost_per_m"]
|
93 |
+
total_cost = input_cost + output_cost
|
94 |
+
|
95 |
+
results.append({
|
96 |
+
"Model": llm,
|
97 |
+
"Base Runs": base_runs,
|
98 |
+
"Stability Test Iterations": stability_iterations.get(llm, 0),
|
99 |
+
"Total Runs": total_runs,
|
100 |
+
"Total Input Tokens": total_input_tokens,
|
101 |
+
"Total Output Tokens": total_output_tokens,
|
102 |
+
"Input Cost ($)": input_cost,
|
103 |
+
"Output Cost ($)": output_cost,
|
104 |
+
"Total Cost ($)": total_cost
|
105 |
+
})
|
106 |
+
|
107 |
+
# Create DataFrame from results
|
108 |
+
results_df = pd.DataFrame(results)
|
109 |
+
|
110 |
+
# Display results
|
111 |
+
st.subheader("Cost Breakdown")
|
112 |
+
st.dataframe(results_df, use_container_width=True)
|
113 |
+
|
114 |
+
# Calculate overall totals
|
115 |
+
total_input_cost = results_df["Input Cost ($)"].sum()
|
116 |
+
total_output_cost = results_df["Output Cost ($)"].sum()
|
117 |
+
total_cost = results_df["Total Cost ($)"].sum()
|
118 |
+
|
119 |
+
# Display totals
|
120 |
+
col1, col2, col3 = st.columns(3)
|
121 |
+
col1.metric("Total Input Cost", f"${total_input_cost:.2f}")
|
122 |
+
col2.metric("Total Output Cost", f"${total_output_cost:.2f}")
|
123 |
+
col3.metric("Total API Cost", f"${total_cost:.2f}")
|
124 |
+
|
125 |
+
# Export options
|
126 |
+
st.subheader("Export Options")
|
127 |
+
csv = results_df.to_csv(index=False).encode('utf-8')
|
128 |
+
st.download_button(
|
129 |
+
label="Download Results as CSV",
|
130 |
+
data=csv,
|
131 |
+
file_name='llm_budget_results.csv',
|
132 |
+
mime='text/csv',
|
133 |
+
)
|
134 |
|
135 |
# Footer
|
136 |
st.markdown("---")
|