Spaces:
Running
Running
Style changes
Browse files
app.py
CHANGED
|
@@ -46,8 +46,8 @@ def calculate_total_cost(prompt_tokens: int, completion_tokens: int, model: str)
|
|
| 46 |
completion_cost = completion_tokens * model_data['output_cost_per_token']
|
| 47 |
return prompt_cost, completion_cost
|
| 48 |
|
| 49 |
-
def update_model_list(function_calling, litellm_provider, max_price, supports_vision):
|
| 50 |
-
filtered_models = TOKEN_COSTS
|
| 51 |
|
| 52 |
if litellm_provider != "Any":
|
| 53 |
filtered_models = filtered_models[filtered_models['litellm_provider'] == litellm_provider]
|
|
@@ -94,14 +94,24 @@ def compute_all(input_type, prompt_text, completion_text, prompt_tokens, complet
|
|
| 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 |
-
|
| 98 |
-
df = df.
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
return df
|
| 104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue=gr.themes.colors.yellow, secondary_hue=gr.themes.colors.orange)) as demo:
|
| 106 |
gr.Markdown("""
|
| 107 |
# Text-to-$$$: Calculate the price of your LLM runs
|
|
@@ -109,8 +119,8 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue=gr.themes.colors.yellow, seconda
|
|
| 109 |
""")
|
| 110 |
|
| 111 |
with gr.Row():
|
| 112 |
-
with gr.Column(
|
| 113 |
-
gr.Markdown("## Input type")
|
| 114 |
input_type = gr.Radio(["Text Input", "Token Count Input"], label="Input Type", value="Text Input")
|
| 115 |
|
| 116 |
with gr.Group() as text_input_group:
|
|
@@ -121,27 +131,23 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue=gr.themes.colors.yellow, seconda
|
|
| 121 |
prompt_tokens_input = gr.Number(label="Prompt Tokens (thousands)", value=1.5)
|
| 122 |
completion_tokens_input = gr.Number(label="Completion Tokens (thousands)", value=2)
|
| 123 |
|
|
|
|
| 124 |
gr.Markdown("## Model choice:")
|
| 125 |
with gr.Row():
|
| 126 |
with gr.Column():
|
| 127 |
function_calling = gr.Checkbox(label="Supports Tool Calling", value=False)
|
| 128 |
supports_vision = gr.Checkbox(label="Supports Vision", value=False)
|
|
|
|
|
|
|
|
|
|
| 129 |
litellm_provider = gr.Dropdown(label="Inference Provider", choices=["Any"] + TOKEN_COSTS['litellm_provider'].unique().tolist(), value="Any")
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
compute_button = gr.Button("Compute Costs ⚙️", variant="secondary")
|
| 136 |
-
|
| 137 |
-
with gr.Column(scale=2):
|
| 138 |
-
results_table = gr.Dataframe(label="Cost Results")
|
| 139 |
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
gr.Group(visible=(choice == "Text Input")),
|
| 143 |
-
gr.Group(visible=(choice == "Token Count Input"))
|
| 144 |
-
)
|
| 145 |
|
| 146 |
input_type.change(
|
| 147 |
toggle_input_visibility,
|
|
@@ -149,15 +155,42 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue=gr.themes.colors.yellow, seconda
|
|
| 149 |
outputs=[text_input_group, token_input_group]
|
| 150 |
)
|
| 151 |
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
-
#
|
| 159 |
-
|
| 160 |
-
compute_all,
|
| 161 |
inputs=[
|
| 162 |
input_type,
|
| 163 |
prompt_text,
|
|
@@ -166,7 +199,7 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue=gr.themes.colors.yellow, seconda
|
|
| 166 |
completion_tokens_input,
|
| 167 |
model
|
| 168 |
],
|
| 169 |
-
outputs=
|
| 170 |
)
|
| 171 |
|
| 172 |
if __name__ == "__main__":
|
|
|
|
| 46 |
completion_cost = completion_tokens * model_data['output_cost_per_token']
|
| 47 |
return prompt_cost, completion_cost
|
| 48 |
|
| 49 |
+
def update_model_list(function_calling, litellm_provider, max_price, supports_vision, supports_max_input_tokens):
|
| 50 |
+
filtered_models = TOKEN_COSTS.loc[TOKEN_COSTS["max_input_tokens"] >= supports_max_input_tokens*1000]
|
| 51 |
|
| 52 |
if litellm_provider != "Any":
|
| 53 |
filtered_models = filtered_models[filtered_models['litellm_provider'] == litellm_provider]
|
|
|
|
| 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 (
|
| 111 |
+
gr.Group(visible=(choice == "Text Input")),
|
| 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
|
|
|
|
| 119 |
""")
|
| 120 |
|
| 121 |
with gr.Row():
|
| 122 |
+
with gr.Column():
|
| 123 |
+
gr.Markdown("## Input type:")
|
| 124 |
input_type = gr.Radio(["Text Input", "Token Count Input"], label="Input Type", value="Text Input")
|
| 125 |
|
| 126 |
with gr.Group() as text_input_group:
|
|
|
|
| 131 |
prompt_tokens_input = gr.Number(label="Prompt Tokens (thousands)", value=1.5)
|
| 132 |
completion_tokens_input = gr.Number(label="Completion Tokens (thousands)", value=2)
|
| 133 |
|
| 134 |
+
with gr.Column():
|
| 135 |
gr.Markdown("## Model choice:")
|
| 136 |
with gr.Row():
|
| 137 |
with gr.Column():
|
| 138 |
function_calling = gr.Checkbox(label="Supports Tool Calling", value=False)
|
| 139 |
supports_vision = gr.Checkbox(label="Supports Vision", value=False)
|
| 140 |
+
with gr.Column():
|
| 141 |
+
supports_max_input_tokens = gr.Slider(label="Min Supported Input Length (thousands)", minimum=2, maximum=256, step=2, value=2)
|
| 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,
|
|
|
|
| 155 |
outputs=[text_input_group, token_input_group]
|
| 156 |
)
|
| 157 |
|
| 158 |
+
gr.on(
|
| 159 |
+
triggers=[function_calling.change, litellm_provider.change, max_price.change, supports_vision.change, supports_max_input_tokens.change],
|
| 160 |
+
fn=update_model_list,
|
| 161 |
+
inputs=[function_calling, litellm_provider, max_price, supports_vision, supports_max_input_tokens],
|
| 162 |
+
outputs=model,
|
| 163 |
+
)
|
| 164 |
+
|
| 165 |
+
gr.on(
|
| 166 |
+
triggers=[
|
| 167 |
+
input_type.change,
|
| 168 |
+
prompt_text.change,
|
| 169 |
+
completion_text.change,
|
| 170 |
+
prompt_tokens_input.change,
|
| 171 |
+
completion_tokens_input.change,
|
| 172 |
+
function_calling.change,
|
| 173 |
+
litellm_provider.change,
|
| 174 |
+
max_price.change,
|
| 175 |
+
supports_vision.change,
|
| 176 |
+
supports_max_input_tokens.change,
|
| 177 |
+
model.change
|
| 178 |
+
],
|
| 179 |
+
fn=compute_all,
|
| 180 |
+
inputs=[
|
| 181 |
+
input_type,
|
| 182 |
+
prompt_text,
|
| 183 |
+
completion_text,
|
| 184 |
+
prompt_tokens_input,
|
| 185 |
+
completion_tokens_input,
|
| 186 |
+
model
|
| 187 |
+
],
|
| 188 |
+
outputs=results_table
|
| 189 |
+
)
|
| 190 |
|
| 191 |
+
# Load results on page load
|
| 192 |
+
demo.load(
|
| 193 |
+
fn=compute_all,
|
| 194 |
inputs=[
|
| 195 |
input_type,
|
| 196 |
prompt_text,
|
|
|
|
| 199 |
completion_tokens_input,
|
| 200 |
model
|
| 201 |
],
|
| 202 |
+
outputs=results_table
|
| 203 |
)
|
| 204 |
|
| 205 |
if __name__ == "__main__":
|