ehagey commited on
Commit
81497d1
·
verified ·
1 Parent(s): 1a78080

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. 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 sidebar for inputs
35
- st.sidebar.header("Configuration")
36
 
37
- # Token input section
38
- st.sidebar.subheader("Token Settings")
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
- total_input_tokens = input_tokens * total_runs
83
- total_output_tokens = output_tokens * total_runs
 
 
84
 
85
- input_cost = (total_input_tokens / 1_000_000) * llm_data[llm]["input_cost_per_m"]
86
- output_cost = (total_output_tokens / 1_000_000) * llm_data[llm]["output_cost_per_m"]
87
- total_cost = input_cost + output_cost
88
 
89
- results.append({
90
- "Model": llm,
91
- "Base Runs": base_runs,
92
- "Stability Test Iterations": stability_iterations.get(llm, 0),
93
- "Total Runs": total_runs,
94
- "Total Input Tokens": total_input_tokens,
95
- "Total Output Tokens": total_output_tokens,
96
- "Input Cost ($)": input_cost,
97
- "Output Cost ($)": output_cost,
98
- "Total Cost ($)": total_cost
99
- })
100
-
101
- # Create DataFrame from results
102
- results_df = pd.DataFrame(results)
103
-
104
- # Main content
105
- st.header("Cost Summary")
106
- st.dataframe(results_df, use_container_width=True)
107
-
108
- # Calculate overall totals
109
- total_input_cost = results_df["Input Cost ($)"].sum()
110
- total_output_cost = results_df["Output Cost ($)"].sum()
111
- total_cost = results_df["Total Cost ($)"].sum()
112
-
113
- # Display totals
114
- col1, col2, col3 = st.columns(3)
115
- col1.metric("Total Input Cost", f"${total_input_cost:.2f}")
116
- col2.metric("Total Output Cost", f"${total_output_cost:.2f}")
117
- col3.metric("Total API Cost", f"${total_cost:.2f}")
118
-
119
- # Data visualization
120
- st.header("Cost Visualization")
121
-
122
- # Cost breakdown by model
123
- fig1, ax1 = plt.subplots(figsize=(10, 6))
124
- models = results_df["Model"]
125
- input_costs = results_df["Input Cost ($)"]
126
- output_costs = results_df["Output Cost ($)"]
127
-
128
- x = np.arange(len(models))
129
- width = 0.35
130
-
131
- ax1.bar(x - width/2, input_costs, width, label='Input Cost')
132
- ax1.bar(x + width/2, output_costs, width, label='Output Cost')
133
-
134
- ax1.set_ylabel('Cost ($)')
135
- ax1.set_title('Cost Breakdown by Model')
136
- ax1.set_xticks(x)
137
- ax1.set_xticklabels(models, rotation=45, ha='right')
138
- ax1.legend()
139
-
140
- fig1.tight_layout()
141
- st.pyplot(fig1)
142
-
143
- # Percentage of total cost by model
144
- fig2, ax2 = plt.subplots(figsize=(8, 8))
145
- ax2.pie(results_df["Total Cost ($)"], labels=results_df["Model"], autopct='%1.1f%%', startangle=90)
146
- ax2.axis('equal')
147
- ax2.set_title('Percentage of Total Cost by Model')
148
- st.pyplot(fig2)
149
-
150
- # Export options
151
- st.header("Export Options")
152
- csv = results_df.to_csv(index=False).encode('utf-8')
153
- st.download_button(
154
- label="Download Results as CSV",
155
- data=csv,
156
- file_name='llm_budget_results.csv',
157
- mime='text/csv',
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("---")