m-ric HF staff commited on
Commit
9ae565a
1 Parent(s): 47b44cb

Multiple style fixes, remove price slider

Browse files
Files changed (1) hide show
  1. app.py +20 -15
app.py CHANGED
@@ -30,6 +30,12 @@ TOKEN_COSTS = TOKEN_COSTS.loc[
30
  ]
31
  TOKEN_COSTS["supports_vision"] = TOKEN_COSTS["supports_vision"].fillna(False)
32
 
 
 
 
 
 
 
33
  cmap = plt.get_cmap('RdYlGn_r') # Red-Yellow-Green colormap, reversed
34
 
35
  def count_string_tokens(string: str, model: str) -> int:
@@ -75,21 +81,19 @@ def compute_all(input_type, prompt_text, completion_text, prompt_tokens, complet
75
  prompt_tokens = int(prompt_tokens * 1000)
76
  completion_tokens = int(completion_tokens * 1000)
77
 
 
78
  prompt_cost, completion_cost = calculate_total_cost(prompt_tokens, completion_tokens, model)
79
  total_cost = prompt_cost + completion_cost
80
 
81
  results.append({
82
  "Model": model,
83
- "Prompt Cost": f"${prompt_cost:.6f}",
84
- "Completion Cost": f"${completion_cost:.6f}",
85
- "Total Cost": f"${total_cost:.6f}"
 
86
  })
87
 
88
  df = pd.DataFrame(results)
89
-
90
- # Convert cost columns to numeric, removing the '$' sign
91
- for col in ["Prompt Cost", "Completion Cost", "Total Cost"]:
92
- df[col] = df[col].str.replace('$', '').astype(float)
93
 
94
  if len(df) > 1:
95
  norm = plt.Normalize(df['Total Cost'].min(), df['Total Cost'].max())
@@ -109,10 +113,11 @@ def compute_all(input_type, prompt_text, completion_text, prompt_tokens, complet
109
  style = ''
110
  if column == 'Total Cost':
111
  style += 'font-weight: bold; '
112
- if column in ['Prompt Cost', 'Completion Cost', 'Total Cost']:
 
113
  val = f'${val:.6f}'
114
- if column == 'Total Cost':
115
- style += apply_color(float(val.replace('$', '')))
116
  return f'<td style="{style}">{val}</td>'
117
 
118
  html_table = '<table class="styled-table">'
@@ -158,7 +163,7 @@ with gr.Blocks(css="""
158
  }
159
  """, theme=gr.themes.Soft(primary_hue=gr.themes.colors.yellow, secondary_hue=gr.themes.colors.orange)) as demo:
160
  gr.Markdown("""
161
- # Text-to-Dollars: Get the price of your LLM runs
162
  Based on prices data from [BerriAI's litellm](https://github.com/BerriAI/litellm/blob/main/model_prices_and_context_window.json).
163
  """)
164
 
@@ -168,8 +173,8 @@ with gr.Blocks(css="""
168
  input_type = gr.Radio(["Text Input", "Token Count Input"], label="Input Type", value="Text Input")
169
 
170
  with gr.Group() as text_input_group:
171
- prompt_text = gr.Textbox(label="Prompt", value="Tell me a joke about AI. Here's an example: Why did the neural network go to therapy? It had too many deep issues!", lines=3)
172
- completion_text = gr.Textbox(label="Completion", value="", lines=3)
173
 
174
  with gr.Group(visible=False) as token_input_group:
175
  prompt_tokens_input = gr.Number(label="Prompt Tokens (thousands)", value=1.5)
@@ -183,7 +188,7 @@ with gr.Blocks(css="""
183
  supports_vision = gr.Checkbox(label="Supports Vision", value=False)
184
  with gr.Column():
185
  supports_max_input_tokens = gr.Slider(label="Min Supported Input Length (thousands)", minimum=2, maximum=256, step=2, value=2)
186
- max_price = gr.Slider(label="Max Price per Input Token", minimum=0, maximum=0.001, step=0.00001, value=0.001)
187
  litellm_provider = gr.Dropdown(label="Inference Provider", choices=["Any"] + TOKEN_COSTS['litellm_provider'].unique().tolist(), value="Any")
188
 
189
  model = gr.Dropdown(label="Models (at least 1)", choices=TOKEN_COSTS['model'].tolist(), value=["anyscale/meta-llama/Meta-Llama-3-8B-Instruct", "gpt-4o", "claude-3-sonnet-20240229"], multiselect=True)
@@ -215,7 +220,7 @@ with gr.Blocks(css="""
215
  completion_tokens_input.change,
216
  function_calling.change,
217
  litellm_provider.change,
218
- max_price.change,
219
  supports_vision.change,
220
  supports_max_input_tokens.change,
221
  model.change
 
30
  ]
31
  TOKEN_COSTS["supports_vision"] = TOKEN_COSTS["supports_vision"].fillna(False)
32
 
33
+ def clean_names(s):
34
+ s = s.replace("_", " ").replace("ai", "AI")
35
+ return s[0].upper() + s[1:]
36
+
37
+ TOKEN_COSTS["litellm_provider"] = TOKEN_COSTS["litellm_provider"].apply(clean_names)
38
+
39
  cmap = plt.get_cmap('RdYlGn_r') # Red-Yellow-Green colormap, reversed
40
 
41
  def count_string_tokens(string: str, model: str) -> int:
 
81
  prompt_tokens = int(prompt_tokens * 1000)
82
  completion_tokens = int(completion_tokens * 1000)
83
 
84
+ model_data = TOKEN_COSTS[TOKEN_COSTS['model'] == model].iloc[0]
85
  prompt_cost, completion_cost = calculate_total_cost(prompt_tokens, completion_tokens, model)
86
  total_cost = prompt_cost + completion_cost
87
 
88
  results.append({
89
  "Model": model,
90
+ "Provider": model_data['litellm_provider'],
91
+ "Input Cost / M tokens": model_data['input_cost_per_token']*1e6,
92
+ "Output Cost / M tokens": model_data['output_cost_per_token']*1e6,
93
+ "Total Cost": round(total_cost, 6),
94
  })
95
 
96
  df = pd.DataFrame(results)
 
 
 
 
97
 
98
  if len(df) > 1:
99
  norm = plt.Normalize(df['Total Cost'].min(), df['Total Cost'].max())
 
113
  style = ''
114
  if column == 'Total Cost':
115
  style += 'font-weight: bold; '
116
+ style += apply_color(val)
117
+ if column in ['Total Cost']:
118
  val = f'${val:.6f}'
119
+ if column in ["Input Cost / M tokens", "Output Cost / M tokens"]:
120
+ val = f'${val}'
121
  return f'<td style="{style}">{val}</td>'
122
 
123
  html_table = '<table class="styled-table">'
 
163
  }
164
  """, theme=gr.themes.Soft(primary_hue=gr.themes.colors.yellow, secondary_hue=gr.themes.colors.orange)) as demo:
165
  gr.Markdown("""
166
+ # Text-to-Dollars: Get the price of your LLM API calls!
167
  Based on prices data from [BerriAI's litellm](https://github.com/BerriAI/litellm/blob/main/model_prices_and_context_window.json).
168
  """)
169
 
 
173
  input_type = gr.Radio(["Text Input", "Token Count Input"], label="Input Type", value="Text Input")
174
 
175
  with gr.Group() as text_input_group:
176
+ prompt_text = gr.Textbox(label="Prompt", value="Tell me a joke about AI.", lines=3)
177
+ completion_text = gr.Textbox(label="Completion", value="Certainly: Why did the neural network go to therapy? It had too many deep issues!", lines=3)
178
 
179
  with gr.Group(visible=False) as token_input_group:
180
  prompt_tokens_input = gr.Number(label="Prompt Tokens (thousands)", value=1.5)
 
188
  supports_vision = gr.Checkbox(label="Supports Vision", value=False)
189
  with gr.Column():
190
  supports_max_input_tokens = gr.Slider(label="Min Supported Input Length (thousands)", minimum=2, maximum=256, step=2, value=2)
191
+ max_price = gr.Slider(label="Max Price per Input Token", minimum=0, maximum=0.001, step=0.00001, value=0.001, visible=False, interactive=False)
192
  litellm_provider = gr.Dropdown(label="Inference Provider", choices=["Any"] + TOKEN_COSTS['litellm_provider'].unique().tolist(), value="Any")
193
 
194
  model = gr.Dropdown(label="Models (at least 1)", choices=TOKEN_COSTS['model'].tolist(), value=["anyscale/meta-llama/Meta-Llama-3-8B-Instruct", "gpt-4o", "claude-3-sonnet-20240229"], multiselect=True)
 
220
  completion_tokens_input.change,
221
  function_calling.change,
222
  litellm_provider.change,
223
+ # max_price.change,
224
  supports_vision.change,
225
  supports_max_input_tokens.change,
226
  model.change