m-ric HF staff commited on
Commit
62a5f44
·
1 Parent(s): 955d640

Clean state

Browse files
Files changed (1) hide show
  1. app.py +60 -23
app.py CHANGED
@@ -35,9 +35,12 @@ cmap = plt.get_cmap('RdYlGn_r') # Red-Yellow-Green colormap, reversed
35
  def count_string_tokens(string: str, model: str) -> int:
36
  try:
37
  encoding = tiktoken.encoding_for_model(model.split('/')[-1])
38
- except KeyError:
39
- print(f"Model {model} not found. Using cl100k_base encoding.")
40
- encoding = tiktoken.get_encoding("cl100k_base")
 
 
 
41
  return len(encoding.encode(string))
42
 
43
  def calculate_total_cost(prompt_tokens: int, completion_tokens: int, model: str) -> float:
@@ -85,26 +88,42 @@ def compute_all(input_type, prompt_text, completion_text, prompt_tokens, complet
85
  df[col] = df[col].str.replace('$', '').astype(float)
86
 
87
  if len(df) > 1:
88
- def apply_color(val, min, max):
89
- norm = plt.Normalize(min, max)
 
90
  color = cmap(norm(val))
91
- rgba = tuple(int(x * 255) for x in color[:3]) + (0.5,)
92
  rgba = tuple(int(x * 255) for x in color[:3]) + (0.5,) # 0.5 for 50% opacity
93
  return f'background-color: rgba{rgba}'
94
-
95
- min, max = df["Total Cost"].min(), df["Total Cost"].max()
96
- df = df.style.applymap(lambda x: apply_color(x, min, max), subset=["Total Cost"])
97
  else:
98
- df = df.style.applymap(lambda x: 'background-color: rgba(0, 0, 0, 0)', subset=["Total Cost"])
99
-
100
- df = df.format({"Prompt Cost": "${:.6f}", "Completion Cost": "${:.6f}", "Total Cost": "${:.6f}"})
101
- df = df.set_properties(**{
102
- 'font-family': 'Arial, sans-serif',
103
- 'white-space': 'pre-wrap',
104
- 'vertical-align': 'middle'
105
- })
106
- df = df.set_properties(**{'font-weight': 'bold'}, subset=['Total Cost'])
107
- return df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
 
109
  def toggle_input_visibility(choice):
110
  return (
@@ -112,7 +131,25 @@ def toggle_input_visibility(choice):
112
  gr.Group(visible=(choice == "Token Count Input"))
113
  )
114
 
115
- with gr.Blocks(theme=gr.themes.Soft(primary_hue=gr.themes.colors.yellow, secondary_hue=gr.themes.colors.orange)) as demo:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
  gr.Markdown("""
117
  # Text-to-$$$: Calculate the price of your LLM runs
118
  Based on prices data from [BerriAI's litellm](https://github.com/BerriAI/litellm/blob/main/model_prices_and_context_window.json).
@@ -142,12 +179,12 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue=gr.themes.colors.yellow, seconda
142
  max_price = gr.Slider(label="Max Price per Input Token", minimum=0, maximum=0.001, step=0.00001, value=0.001)
143
  litellm_provider = gr.Dropdown(label="Inference Provider", choices=["Any"] + TOKEN_COSTS['litellm_provider'].unique().tolist(), value="Any")
144
 
145
- model = gr.Dropdown(label="Models (can select multiple)", choices=TOKEN_COSTS['model'].tolist(), value="anyscale/meta-llama/Meta-Llama-3-8B-Instruct", multiselect=True)
146
 
147
- gr.Markdown("## ➡️ Resulting Costs:")
148
 
149
  with gr.Row():
150
- results_table = gr.Dataframe()
151
 
152
  input_type.change(
153
  toggle_input_visibility,
 
35
  def count_string_tokens(string: str, model: str) -> int:
36
  try:
37
  encoding = tiktoken.encoding_for_model(model.split('/')[-1])
38
+ except:
39
+ try:
40
+ encoding = tiktoken.encoding_for_model(model.split('/')[-2] + '/' + model.split('/')[-1])
41
+ except KeyError:
42
+ print(f"Model {model} not found. Using cl100k_base encoding.")
43
+ encoding = tiktoken.get_encoding("cl100k_base")
44
  return len(encoding.encode(string))
45
 
46
  def calculate_total_cost(prompt_tokens: int, completion_tokens: int, model: str) -> float:
 
88
  df[col] = df[col].str.replace('$', '').astype(float)
89
 
90
  if len(df) > 1:
91
+ norm = plt.Normalize(df['Total Cost'].min(), df['Total Cost'].max())
92
+
93
+ def apply_color(val):
94
  color = cmap(norm(val))
 
95
  rgba = tuple(int(x * 255) for x in color[:3]) + (0.5,) # 0.5 for 50% opacity
96
  return f'background-color: rgba{rgba}'
97
+
 
 
98
  else:
99
+ def apply_color(val):
100
+ return "background-color: rgba(0, 0, 0, 0)"
101
+
102
+
103
+ # Apply colors and formatting
104
+ def style_cell(val, column):
105
+ style = ''
106
+ if column == 'Total Cost':
107
+ style += 'font-weight: bold; '
108
+ if column in ['Prompt Cost', 'Completion Cost', 'Total Cost']:
109
+ val = f'${val:.6f}'
110
+ if column == 'Total Cost':
111
+ style += apply_color(float(val.replace('$', '')))
112
+ return f'<td style="{style}">{val}</td>'
113
+
114
+ html_table = '<table class="styled-table">'
115
+ html_table += '<thead><tr>'
116
+ for col in df.columns:
117
+ html_table += f'<th>{col}</th>'
118
+ html_table += '</tr></thead><tbody>'
119
+ for _, row in df.iterrows():
120
+ html_table += '<tr>'
121
+ for col in df.columns:
122
+ html_table += style_cell(row[col], col)
123
+ html_table += '</tr>'
124
+ html_table += '</tbody></table>'
125
+
126
+ return html_table
127
 
128
  def toggle_input_visibility(choice):
129
  return (
 
131
  gr.Group(visible=(choice == "Token Count Input"))
132
  )
133
 
134
+ with gr.Blocks(css="""
135
+ .styled-table {
136
+ border-collapse: collapse;
137
+ margin: 25px 0;
138
+ font-family: Arial, sans-serif;
139
+ width: 100%;
140
+ }
141
+ .styled-table th, .styled-table td {
142
+ padding: 12px 15px;
143
+ text-align: left;
144
+ vertical-align: middle;
145
+ }
146
+ .styled-table tbody tr {
147
+ border-bottom: 1px solid #dddddd;
148
+ }
149
+ .styled-table tbody tr:nth-of-type(even) {
150
+ background-color: #f3f3f3;
151
+ }
152
+ """, theme=gr.themes.Soft(primary_hue=gr.themes.colors.yellow, secondary_hue=gr.themes.colors.orange)) as demo:
153
  gr.Markdown("""
154
  # Text-to-$$$: Calculate the price of your LLM runs
155
  Based on prices data from [BerriAI's litellm](https://github.com/BerriAI/litellm/blob/main/model_prices_and_context_window.json).
 
179
  max_price = gr.Slider(label="Max Price per Input Token", minimum=0, maximum=0.001, step=0.00001, value=0.001)
180
  litellm_provider = gr.Dropdown(label="Inference Provider", choices=["Any"] + TOKEN_COSTS['litellm_provider'].unique().tolist(), value="Any")
181
 
182
+ model = gr.Dropdown(label="Models (at least 1)", choices=TOKEN_COSTS['model'].tolist(), value="anyscale/meta-llama/Meta-Llama-3-8B-Instruct", multiselect=True)
183
 
184
+ gr.Markdown("## Resulting Costs 👇")
185
 
186
  with gr.Row():
187
+ results_table = gr.HTML()
188
 
189
  input_type.change(
190
  toggle_input_visibility,