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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
import plotly.graph_objects as go
|
5 |
+
import plotly.express as px
|
6 |
+
|
7 |
+
# Set page config
|
8 |
+
st.set_page_config(
|
9 |
+
page_title="GPT-4o Cost Calculator",
|
10 |
+
page_icon="๐ฐ",
|
11 |
+
layout="wide"
|
12 |
+
)
|
13 |
+
|
14 |
+
# Custom CSS
|
15 |
+
st.markdown("""
|
16 |
+
<style>
|
17 |
+
.main {
|
18 |
+
padding: 2rem;
|
19 |
+
}
|
20 |
+
.stAlert {
|
21 |
+
padding: 1rem;
|
22 |
+
margin: 1rem 0;
|
23 |
+
}
|
24 |
+
</style>
|
25 |
+
""", unsafe_allow_html=True)
|
26 |
+
|
27 |
+
# Define the pricing data
|
28 |
+
pricing_data = {
|
29 |
+
"gpt-4o-audio-preview": {
|
30 |
+
"text_input": 2.50,
|
31 |
+
"text_output": 10.00,
|
32 |
+
"audio_input": 100.00,
|
33 |
+
"audio_output": 200.00,
|
34 |
+
"description": "Full-featured model with highest quality"
|
35 |
+
},
|
36 |
+
"gpt-4o-audio-preview-2024-12-17": {
|
37 |
+
"text_input": 2.50,
|
38 |
+
"text_output": 10.00,
|
39 |
+
"audio_input": 40.00,
|
40 |
+
"audio_output": 80.00,
|
41 |
+
"description": "Updated model with optimized audio pricing"
|
42 |
+
},
|
43 |
+
"gpt-4o-audio-preview-2024-10-01": {
|
44 |
+
"text_input": 2.50,
|
45 |
+
"text_output": 10.00,
|
46 |
+
"audio_input": 100.00,
|
47 |
+
"audio_output": 200.00,
|
48 |
+
"description": "Legacy model with standard pricing"
|
49 |
+
},
|
50 |
+
"gpt-4o-mini-audio-preview": {
|
51 |
+
"text_input": 0.150,
|
52 |
+
"text_output": 0.600,
|
53 |
+
"audio_input": 10.000,
|
54 |
+
"audio_output": 20.000,
|
55 |
+
"description": "Cost-effective model for lighter workloads"
|
56 |
+
},
|
57 |
+
"gpt-4o-mini-audio-preview-2024-12-17": {
|
58 |
+
"text_input": 0.150,
|
59 |
+
"text_output": 0.600,
|
60 |
+
"audio_input": 10.000,
|
61 |
+
"audio_output": 20.000,
|
62 |
+
"description": "Updated mini model with optimized performance"
|
63 |
+
}
|
64 |
+
}
|
65 |
+
|
66 |
+
# Constants for calculations
|
67 |
+
TOKENS_PER_MINUTE_AUDIO = 1000 # Approximate tokens per minute of audio
|
68 |
+
WORDS_PER_MINUTE = 150 # Average speaking rate
|
69 |
+
TOKENS_PER_WORD = 1.3 # Approximate tokens per word
|
70 |
+
|
71 |
+
def calculate_costs(model, input_type, minutes):
|
72 |
+
"""Calculate costs based on input parameters"""
|
73 |
+
pricing = pricing_data[model]
|
74 |
+
|
75 |
+
if input_type == "Audio":
|
76 |
+
tokens = minutes * TOKENS_PER_MINUTE_AUDIO
|
77 |
+
input_cost = (tokens * pricing["audio_input"]) / 1000000
|
78 |
+
output_cost = (tokens * pricing["audio_output"]) / 1000000
|
79 |
+
else: # Text
|
80 |
+
words = minutes * WORDS_PER_MINUTE
|
81 |
+
tokens = words * TOKENS_PER_WORD
|
82 |
+
input_cost = (tokens * pricing["text_input"]) / 1000000
|
83 |
+
output_cost = (tokens * pricing["text_output"]) / 1000000
|
84 |
+
|
85 |
+
return {
|
86 |
+
"tokens": tokens,
|
87 |
+
"words": words if input_type == "Text" else None,
|
88 |
+
"input_cost": input_cost,
|
89 |
+
"output_cost": output_cost,
|
90 |
+
"total_cost": input_cost + output_cost
|
91 |
+
}
|
92 |
+
|
93 |
+
# Header
|
94 |
+
st.title("GPT-4o Cost Calculator ๐ฐ")
|
95 |
+
st.markdown("Estimate your GPT-4o API costs based on usage")
|
96 |
+
|
97 |
+
# Create tabs
|
98 |
+
tab1, tab2, tab3 = st.tabs(["๐ Pricing Reference", "๐งฎ Calculator", "๐ Guide"])
|
99 |
+
|
100 |
+
# Tab 1: Pricing Reference
|
101 |
+
with tab1:
|
102 |
+
st.header("Original GPT-4o Pricing")
|
103 |
+
|
104 |
+
# Create pricing table
|
105 |
+
pricing_df = pd.DataFrame([
|
106 |
+
{
|
107 |
+
"Model": model,
|
108 |
+
"Description": data["description"],
|
109 |
+
"Text Input": f"${data['text_input']:.3f}",
|
110 |
+
"Text Output": f"${data['text_output']:.3f}",
|
111 |
+
"Audio Input": f"${data['audio_input']:.3f}",
|
112 |
+
"Audio Output": f"${data['audio_output']:.3f}"
|
113 |
+
}
|
114 |
+
for model, data in pricing_data.items()
|
115 |
+
])
|
116 |
+
|
117 |
+
st.dataframe(
|
118 |
+
pricing_df,
|
119 |
+
hide_index=True,
|
120 |
+
use_container_width=True
|
121 |
+
)
|
122 |
+
|
123 |
+
st.caption("All prices are per 1 million tokens")
|
124 |
+
|
125 |
+
# Tab 2: Calculator
|
126 |
+
with tab2:
|
127 |
+
st.header("Cost Calculator")
|
128 |
+
|
129 |
+
# Create two columns
|
130 |
+
col1, col2 = st.columns([1, 1])
|
131 |
+
|
132 |
+
with col1:
|
133 |
+
st.subheader("Input Parameters")
|
134 |
+
|
135 |
+
# Model selection
|
136 |
+
selected_model = st.selectbox(
|
137 |
+
"Select Model",
|
138 |
+
options=list(pricing_data.keys()),
|
139 |
+
help="Choose the GPT-4o model you want to use"
|
140 |
+
)
|
141 |
+
|
142 |
+
# Input type selection
|
143 |
+
input_type = st.radio(
|
144 |
+
"Select Input Type",
|
145 |
+
options=["Text", "Audio"],
|
146 |
+
help="Choose whether you're processing text or audio",
|
147 |
+
horizontal=True
|
148 |
+
)
|
149 |
+
|
150 |
+
# Duration input
|
151 |
+
minutes = st.number_input(
|
152 |
+
"Duration (minutes)",
|
153 |
+
min_value=0.0,
|
154 |
+
value=1.0,
|
155 |
+
step=0.5,
|
156 |
+
help="Enter the duration of your content in minutes"
|
157 |
+
)
|
158 |
+
|
159 |
+
# Show relevant examples
|
160 |
+
if input_type == "Text":
|
161 |
+
st.info(
|
162 |
+
f"๐ก For {minutes:.1f} minutes of text:\n"
|
163 |
+
f"- Approximately {int(minutes * WORDS_PER_MINUTE):,} words\n"
|
164 |
+
f"- Based on average speaking rate ({WORDS_PER_MINUTE} words/minute)"
|
165 |
+
)
|
166 |
+
else:
|
167 |
+
st.info(
|
168 |
+
f"๐ก For {minutes:.1f} minutes of audio:\n"
|
169 |
+
f"- Approximately {int(minutes * TOKENS_PER_MINUTE_AUDIO):,} tokens\n"
|
170 |
+
f"- Based on audio processing requirements"
|
171 |
+
)
|
172 |
+
|
173 |
+
# Calculate costs
|
174 |
+
costs = calculate_costs(selected_model, input_type, minutes)
|
175 |
+
|
176 |
+
with col2:
|
177 |
+
st.subheader("Cost Breakdown")
|
178 |
+
|
179 |
+
# Create metrics
|
180 |
+
col_a, col_b = st.columns(2)
|
181 |
+
with col_a:
|
182 |
+
st.metric(
|
183 |
+
"Processing Cost",
|
184 |
+
f"${costs['input_cost']:.2f}",
|
185 |
+
f"{costs['tokens']:,.0f} tokens"
|
186 |
+
)
|
187 |
+
with col_b:
|
188 |
+
st.metric(
|
189 |
+
"Response Cost",
|
190 |
+
f"${costs['output_cost']:.2f}",
|
191 |
+
f"{costs['tokens']:,.0f} tokens"
|
192 |
+
)
|
193 |
+
|
194 |
+
# Total cost
|
195 |
+
st.metric(
|
196 |
+
"Total Estimated Cost",
|
197 |
+
f"${costs['total_cost']:.2f}",
|
198 |
+
f"For {minutes} minute{'s' if minutes != 1 else ''}"
|
199 |
+
)
|
200 |
+
|
201 |
+
# Create a pie chart for cost distribution
|
202 |
+
fig = px.pie(
|
203 |
+
values=[costs['input_cost'], costs['output_cost']],
|
204 |
+
names=['Input Processing', 'Output Processing'],
|
205 |
+
title='Cost Distribution'
|
206 |
+
)
|
207 |
+
st.plotly_chart(fig, use_container_width=True)
|
208 |
+
|
209 |
+
# Tab 3: Guide
|
210 |
+
with tab3:
|
211 |
+
st.header("Understanding Tokens and Costs")
|
212 |
+
|
213 |
+
# What are tokens?
|
214 |
+
st.subheader("What are tokens?")
|
215 |
+
st.markdown("""
|
216 |
+
Tokens are the basic units that GPT-4o processes:
|
217 |
+
- For text: ~4 characters or ยพ of a word
|
218 |
+
- For audio: ~1 second of speech
|
219 |
+
""")
|
220 |
+
|
221 |
+
# Examples
|
222 |
+
col_a, col_b = st.columns(2)
|
223 |
+
|
224 |
+
with col_a:
|
225 |
+
st.subheader("Text Examples")
|
226 |
+
st.markdown("""
|
227 |
+
| Content | Words | Tokens |
|
228 |
+
|---------|-------|---------|
|
229 |
+
| Short message | 20 | ~26 |
|
230 |
+
| Email | 200 | ~260 |
|
231 |
+
| Document page | 500 | ~650 |
|
232 |
+
""")
|
233 |
+
|
234 |
+
with col_b:
|
235 |
+
st.subheader("Audio Examples")
|
236 |
+
st.markdown("""
|
237 |
+
| Content | Duration | Tokens |
|
238 |
+
|---------|----------|---------|
|
239 |
+
| Short clip | 1 min | ~1,000 |
|
240 |
+
| Conversation | 5 min | ~5,000 |
|
241 |
+
| Meeting | 30 min | ~30,000 |
|
242 |
+
""")
|
243 |
+
|
244 |
+
# Cost optimization tips
|
245 |
+
st.subheader("Cost Optimization Tips")
|
246 |
+
st.markdown("""
|
247 |
+
1. **Choose the right model:**
|
248 |
+
- Use mini models for development
|
249 |
+
- Use full models for production
|
250 |
+
|
251 |
+
2. **Optimize content length:**
|
252 |
+
- Keep text concise
|
253 |
+
- Trim unnecessary audio
|
254 |
+
|
255 |
+
3. **Batch processing:**
|
256 |
+
- Combine related requests
|
257 |
+
- Process in optimal chunks
|
258 |
+
""")
|
259 |
+
|
260 |
+
# Footer
|
261 |
+
st.markdown("---")
|
262 |
+
st.caption("""
|
263 |
+
Note: These calculations are estimates based on average usage patterns.
|
264 |
+
Actual token usage and costs may vary depending on the specific content and use case.
|
265 |
+
Prices are based on the official OpenAI GPT-4o pricing.
|
266 |
+
""")
|