Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
from transformers import GPT2Tokenizer
|
4 |
+
|
5 |
+
# Load the tokenizer
|
6 |
+
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
|
7 |
+
|
8 |
+
|
9 |
+
# Updated rate prices with the accurate rates for each model
|
10 |
+
rate_prices = {
|
11 |
+
"gpt-4": {"input": 0.03, "output": 0.06},
|
12 |
+
"gpt-4-32k": {"input": 0.06, "output": 0.12},
|
13 |
+
"gpt-4-1106-preview": {"input": 0.01, "output": 0.03},
|
14 |
+
"gpt-4-1106-vision-preview": {"input": 0.01, "output": 0.03},
|
15 |
+
"gpt-3.5-turbo-1106": {"input": 0.0010, "output": 0.0020},
|
16 |
+
"gpt-3.5-turbo-instruct": {"input": 0.0015, "output": 0.0020},
|
17 |
+
"gpt-3.5-turbo": {"input": 0.008, "output": 0.003, "additional_output": 0.006},
|
18 |
+
"davinci-002": {"input": 0.006, "output": 0.012, "additional_output": 0.012},
|
19 |
+
"babbage-002": {"input": 0.0004, "output": 0.0016, "additional_output": 0.0016},
|
20 |
+
}
|
21 |
+
|
22 |
+
def count_tokens(text):
|
23 |
+
return len(tokenizer.encode(text))
|
24 |
+
|
25 |
+
|
26 |
+
def calculate_cost(model, input_tokens, output_tokens):
|
27 |
+
input_rate = rate_prices[model]["input"]
|
28 |
+
output_rate = rate_prices[model]["output"]
|
29 |
+
additional_output_rate = rate_prices[model].get("additional_output", output_rate)
|
30 |
+
|
31 |
+
input_cost = (input_tokens / 1000) * input_rate
|
32 |
+
output_cost = (output_tokens / 1000) * output_rate
|
33 |
+
additional_output_cost = (output_tokens / 1000) * additional_output_rate
|
34 |
+
|
35 |
+
return input_cost + output_cost + additional_output_cost
|
36 |
+
|
37 |
+
# Streamlit App
|
38 |
+
st.title("GPT Usage Cost Estimator")
|
39 |
+
|
40 |
+
# User input
|
41 |
+
user_input = st.text_area("Enter your prompt")
|
42 |
+
estimated_output_tokens = st.number_input("Estimated number of output tokens", min_value=0, value=50)
|
43 |
+
selected_model = st.selectbox("Select the GPT model", list(rate_prices.keys()))
|
44 |
+
|
45 |
+
if st.button("Calculate Cost"):
|
46 |
+
input_tokens = count_tokens(user_input)
|
47 |
+
total_cost = calculate_cost(selected_model, input_tokens, estimated_output_tokens)
|
48 |
+
|
49 |
+
# Create a DataFrame for displaying results
|
50 |
+
results_df = pd.DataFrame({
|
51 |
+
"Detail": ["Number of Input Tokens", "Estimated Number of Output Tokens", "Estimated Total Cost"],
|
52 |
+
"Value": [input_tokens, estimated_output_tokens, f"${total_cost:.2f}"]
|
53 |
+
})
|
54 |
+
|
55 |
+
# Display the results in a table
|
56 |
+
st.table(results_df)
|
57 |
+
|
58 |
+
|
59 |
+
# Note about the pricing source
|
60 |
+
st.markdown("""
|
61 |
+
---
|
62 |
+
<sup>**Note:** The pricing information is based on [OpenAI's pricing page](https://openai.com/pricing) as of 12/14/2023.</sup>
|
63 |
+
""", unsafe_allow_html=True)
|