File size: 22,006 Bytes
e0e93c4 2d9906b 2b78da8 e0e93c4 73d3fc4 2d9906b 73d3fc4 2d9906b b3b6d77 e0e93c4 b3b6d77 e0e93c4 73d3fc4 e0e93c4 73d3fc4 e0e93c4 73d3fc4 e0e93c4 b3b6d77 73d3fc4 2d9906b e0e93c4 2d9906b 73d3fc4 2d9906b 73d3fc4 b3b6d77 73d3fc4 2d9906b b3b6d77 2d9906b 4ff2b6d 2d9906b b3b6d77 2d9906b 2b78da8 b3b6d77 73d3fc4 2d9906b 73d3fc4 2b78da8 437efc5 b3b6d77 e0e93c4 73d3fc4 b3b6d77 73d3fc4 2d9906b b3b6d77 2d9906b 73d3fc4 2d9906b 73d3fc4 b3b6d77 73d3fc4 2b78da8 73d3fc4 b3b6d77 73d3fc4 2b78da8 73d3fc4 b3b6d77 73d3fc4 2d9906b 73d3fc4 2b78da8 73d3fc4 2b78da8 73d3fc4 2b78da8 73d3fc4 2d9906b 73d3fc4 b3b6d77 2d9906b 73d3fc4 2d9906b 73d3fc4 b3b6d77 73d3fc4 2b78da8 73d3fc4 b3b6d77 2d9906b 73d3fc4 2b78da8 73d3fc4 2b78da8 b3b6d77 73d3fc4 2d9906b 73d3fc4 2b78da8 73d3fc4 2b78da8 73d3fc4 2b78da8 73d3fc4 2b78da8 73d3fc4 2d9906b b3b6d77 2d9906b 73d3fc4 2d9906b 2b78da8 b3b6d77 2b78da8 b3b6d77 2b78da8 2d9906b 73d3fc4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 |
import gradio as gr
import pandas as pd
import matplotlib.pyplot as plt
import io
import base64
text = "<h1 style='text-align: center; color: blue; font-size: 30px;'>TCO Comparison Calculator"
text1 = "<h1 style='text-align: center; color: blue; font-size: 20px;'>First solution"
text2 = "<h1 style='text-align: center; color: blue; font-size: 20px;'>Second solution"
text3 = "<h1 style='text-align: center; color: blue; font-size: 25px;'>Comparison"
text4 = "<h1 style='text-align: center; color: blue; font-size: 25px;'>Results"
diy_value = 0
saas_value = 0
def calculate_tco(maxed_out, used, tokens_per_second_inp, vm_cost_per_hour, vm_rental_choice, out_diy):
tokens_per_request = 64
maxed_out = maxed_out / 100
used = used / 100
if vm_rental_choice == "pay as you go":
reduction = 0
elif vm_rental_choice == "1 year reserved":
reduction = 0.34
elif vm_rental_choice == "3 years reserved":
reduction = 0.62
homemade_cost_per_token = vm_cost_per_hour * (1 - reduction) / (tokens_per_second_inp * 3600 * maxed_out * used)
homemade_cost_per_request = tokens_per_request * homemade_cost_per_token
out_diy = homemade_cost_per_token
return out_diy
def calculate_tco_2(model_provider, context, out_saas):
tokens_per_request = 64
if model_provider == "OpenAI":
if context == "4K context":
saas_cost_per_token = 0.00035
saas_cost_per_request = saas_cost_per_token * tokens_per_request
elif context == "16K context" :
saas_cost_per_token = 0.0007
saas_cost_per_request = saas_cost_per_token * tokens_per_request
out_saas = saas_cost_per_token
return out_saas
def update_tco(maxed_out, used, tokens_per_second_inp, vm_cost_per_hour_inp, rental_plan_inp, out_diy):
if maxed_out!=None and used!=None and tokens_per_second_inp!=None and vm_cost_per_hour_inp!=None and rental_plan_inp!=None:
return calculate_tco(maxed_out, used, tokens_per_second_inp, vm_cost_per_hour_inp, rental_plan_inp, out_diy)
return None
def update_tco2(model_provider_inp, context_inp, out_saas):
if model_provider_inp!=None and context_inp!=None:
return calculate_tco_2(model_provider_inp, context_inp, out_saas)
return None
def extract_cost_from_text(text):
try:
cost = float(text)
return cost
except ValueError as e:
raise ValueError("Invalid cost text format")
def compare(cost_text1, cost_text2):
try:
# Extract the costs from the input strings
cost1 = extract_cost_from_text(cost_text1)
cost2 = extract_cost_from_text(cost_text2)
r = cost1 / cost2
if r < 1:
comparison_result = f"First solution is cheaper, with a ratio of {r:.2f}."
elif r > 1:
comparison_result = f"Second solution is cheaper, with a ratio of {r:.2f}."
else:
comparison_result = "Both solutions will cost the same."
return comparison_result
except ValueError as e:
return f"Error: {str(e)}"
def update_plot(diy_value, saas_value):
# if maxed_out and used and tokens_per_second_inp and vm_cost_per_hour:
# diy_value = calculate_tco(maxed_out.value, used.value, tokens_per_second_inp, vm_cost_per_hour, vm_rental_choice, out_diy)
# else :
# diy_value = 0
# if model_provider_inp2 and context_inp2:
# saas_value = calculate_tco_2(model_provider_inp2, context_inp2, out_saas2)
# else:
# saas_value = 0
data = pd.DataFrame(
{
"Solution": ["Open-source", "SaaS"],
"Cost/token ($)": [diy_value, saas_value],
}
)
return gr.BarPlot.update(data, x="Solution", y="Cost/token ($)")
def update_plot2(diy_value, saas_value):
# if maxed_out2!=None and used2!=None and tokens_per_second_inp2!=None and vm_cost_per_hour2!=None and vm_rental_choice!=None:
# diy_value = calculate_tco(maxed_out2.value, used2.value, tokens_per_second_inp2, vm_cost_per_hour2, vm_rental_choice, out_diy2)
# else:
# diy_value = 0
# if model_provider_inp2 and context_inp2:
# saas_value = calculate_tco_2(model_provider_inp, context_inp, out_saas)
# else:
# saas_value = 0
data = pd.DataFrame(
{
"Solution": ["Open-source", "SaaS"],
"Cost/token ($)": [diy_value, saas_value],
}
)
return gr.BarPlot.update(data, x="Solution", y="Cost/token ($)")
def render_latex(latex_str):
fig, ax = plt.subplots(figsize=(1, 1))
ax.text(0.5, 0.5, f"${latex_str}$", size=12, usetex=True, va="center", ha="center")
ax.axis("off")
buf = io.BytesIO()
plt.savefig(buf, format="png")
plt.close(fig)
base64_str = base64.b64encode(buf.getvalue()).decode("utf-8")
return f"<img src='data:image/png;base64,{base64_str}'>"
def update_vm_choice(model_inp):
if model_inp == "Llama-2-7B" or "Llama-2-13B" or "Llama-2-70B":
new_options = ["A100 40GB"]
return gr.Dropdown.update(choices=new_options)
def token_per_s_and_cost(vm_inp):
if vm_inp == "A100 40GB":
return [694.38, 3.6730, 694.38, 3.6730]
def submit_diy(rental_plan):
calculate_tco(maxed_out.value, used.value, tokens_per_second_inp.value, vm_cost_per_hour_inp.value, rental_plan, out_diy)
def submit_saas(context_inp):
calculate_tco_2(model_provider_inp, context_inp, out_saas)
description=f"""
<p>In this demo application, we help you compare different solutions for your AI incorporation plans, such as open-source or SaaS.</p>
<p>First, you'll have to choose the two solutions you'd like to compare. Then, follow the instructions to select your configurations for each solution and we will compute the cost/request accordingly to them. Eventually, you can compare both solutions to evaluate which one best suits your needs, in the short or long term.</p>
"""
description1="This interface provides you with the cost per token you get using the open-source solution, based on the model you choose to use and how long you're planning to use it. The selected prices for a Virtual Machine rental come from Azure's VM rental plans, which can offer reductions for long-term reserved usage."
description2="This interface provides you with the cost per token resulting from the AI model provider you choose and the number of tokens you select for context, which the model will take into account when processing input texts."
description3="This interface compares the cost per request for the two solutions you selected and gives you an insight of whether a solution is more valuable in the long term."
test_list = []
models = ["Llama-2-7B", "Llama-2-13B", "Llama-2-70B"]
vm_rental_choice = ["pay as you go", "1 year reserved", "3 years reserved"]
vm_choice = ["A100 40GB"]
model_provider = ["OpenAI"]
context = ["4K context", "16K context"]
error_box = gr.Textbox(label="Error", visible=False)
with gr.Blocks(theme=gr.themes.Soft()) as demo:
gr.Markdown(value=text)
gr.Markdown(value=description)
out_diy = gr.State(value=0)
out_saas = gr.State(value=0)
out_diy2 = gr.State(value=0)
out_saas2 = gr.State(value=0)
tokens_per_second_inp = gr.State()
vm_cost_per_hour_inp = gr.State()
tokens_per_second_inp2 = gr.State()
vm_cost_per_hour_inp2 = gr.State()
with gr.Row():
with gr.Column():
solution_selection = gr.Dropdown(["SaaS", "Open-source"], label="Select a Solution")
with gr.Row(visible=False) as title_column:
gr.Markdown(value=text1)
with gr.Row(visible=False) as text_diy_column:
gr.Markdown(description1)
with gr.Accordion("Open to see the formula", visible=False, open=False) as formula_diy:
gr.Markdown(
r"$ opensource\_cost\_per\_request = \frac{tokens\_per\_request \times VM\_cost\_per\_hour \times (1 - reduction)}{tokens\_per\_second \times 3600 \times maxed\_out \times used}$"
)
with gr.Row(visible=False) as input_diy_column:
with gr.Column():
with gr.Row():
model_inp = gr.Dropdown(models, label="Select an AI Model", info="Open-source AI model used for your application")
with gr.Row() as vm:
with gr.Column():
with gr.Row():
vm_inp = gr.Dropdown(vm_choice, label="Select a Virtual Machine", info="Your options for this choice depend on the model you previously chose")
with gr.Row(visible=False) as vm_info:
token_per_seconds = gr.Textbox(interactive=False, label="Token/s", info="To compute this value based on your model and VM choice, we chose an input length of 233 tokens.")
vm_cost_per_hour = gr.Textbox(interactive=False, label="Cost/h ($) for the VM")
with gr.Row() as use_case:
maxed_out = gr.Slider(minimum=0.01, value=80, label="% maxed out", info="percentage of how much your machine is maxed out")
used = gr.Slider(minimum=0.01, value=50, label="% used", info="percentage of time your machine is used")
rental_plan_inp = gr.Dropdown(vm_rental_choice, label="Select a VM Rental Plan", info="These options are from Azure's VM rental plans. By default, the cost taken into account are from the pay as you go plan.")
model_inp.change(fn=update_vm_choice, inputs=model_inp, outputs=vm_inp)
vm_inp.change(fn=token_per_s_and_cost, inputs=vm_inp, outputs=[tokens_per_second_inp, vm_cost_per_hour_inp, token_per_seconds, vm_cost_per_hour])
maxed_out.change(fn=update_tco, inputs=[maxed_out, used, tokens_per_second_inp, vm_cost_per_hour_inp, rental_plan_inp, out_diy], outputs=out_diy)
used.change(fn=update_tco, inputs=[maxed_out, used, tokens_per_second_inp, vm_cost_per_hour_inp, rental_plan_inp, out_diy], outputs=out_diy)
model_inp.change(fn=update_tco, inputs=[maxed_out, used, tokens_per_second_inp, vm_cost_per_hour_inp, rental_plan_inp, out_diy], outputs=out_diy)
vm_inp.change(fn=update_tco, inputs=[maxed_out, used, tokens_per_second_inp, vm_cost_per_hour_inp, rental_plan_inp, out_diy], outputs=out_diy)
rental_plan_inp.change(fn=update_tco, inputs=[maxed_out, used, tokens_per_second_inp, vm_cost_per_hour_inp, rental_plan_inp, out_diy], outputs=out_diy)
with gr.Row(visible=False) as text_saas_column:
gr.Markdown(description2)
with gr.Accordion("Open to see the formula", visible=False, open=False) as formula_saas:
gr.Markdown(
r"$ saas\_cost\_per\_request = saas\_cost\_per\_token \times tokens\_per\_request$"
)
with gr.Row(visible=False) as input_saas_column:
model_provider_inp = gr.Dropdown(model_provider, label="Model Provider", value="OpenAI", info="Choose an AI model provider you want to work with")
context_inp = gr.Dropdown(context, label="Context", info="Number of tokens the model considers when processing text")
model_provider_inp.change(fn=update_tco2, inputs=[model_provider_inp, context_inp, out_saas], outputs=out_saas)
context_inp.change(fn=update_tco2, inputs=[model_provider_inp, context_inp, out_saas], outputs=out_saas)
def show_vm_info():
return {
vm_info: gr.update(visible=True),
}
vm_inp.change(show_vm_info, outputs=vm_info)
def submit(solution_selection):
if solution_selection == "Open-source":
return {
formula_diy: gr.update(visible=True),
title_column: gr.update(visible=True),
text_diy_column: gr.update(visible=True),
input_diy_column: gr.update(visible=True),
formula_saas: gr.update(visible=False),
text_saas_column: gr.update(visible=False),
input_saas_column: gr.update(visible=False),
}
else:
return {
formula_saas: gr.update(visible=True),
formula_diy: gr.update(visible=False),
text_diy_column: gr.update(visible=False),
input_diy_column: gr.update(visible=False),
title_column: gr.update(visible=True),
text_saas_column: gr.update(visible=True),
input_saas_column: gr.update(visible=True),
}
solution_selection.change(
submit,
solution_selection,
[model_inp, vm, vm_info, vm_inp, maxed_out, used, out_saas, text_diy_column, formula_diy, formula_saas, title_column, text_saas_column, model_inp, rental_plan_inp, model_provider_inp, context_inp, input_diy_column, input_saas_column],
)
# gr.Divider(style="vertical", thickness=2, color="blue")
with gr.Column():
solution_selection2 = gr.Dropdown(["SaaS", "Open-source"], label="Select a solution")
with gr.Row(visible=False) as title_column2:
gr.Markdown(value=text2)
with gr.Row(visible=False) as text_diy_column2:
gr.Markdown(description1)
with gr.Accordion("Open to see the formula", visible=False, open=False) as formula_diy2:
gr.Markdown(
r"$ homemade\_cost\_per\_request = \frac{tokens\_per\_request \times VM\_cost\_per\_hour \times (1 - reduction)}{tokens\_per\_second \times 3600 \times maxed\_out \times used}$"
)
with gr.Row(visible=False) as input_diy_column2:
with gr.Column():
with gr.Row():
model_inp2 = gr.Dropdown(models, label="Select an AI Model", info="Open-source AI model used for your application")
with gr.Row() as vm2:
with gr.Column():
with gr.Row():
vm_inp2 = gr.Dropdown(vm_choice, label="Select a Virtual Machine", info="Your options for this choice depend on the model you previously chose")
with gr.Row(visible=False) as vm_info2:
tokens_per_second2 = gr.Textbox(interactive=False, label="Token/s", info="To compute this value based on your model and VM choice, we chose an input length of 233 tokens.")
vm_cost_per_hour2 = gr.Textbox(interactive=False, label="Cost/h ($) for the VM")
with gr.Row() as use_case2:
maxed_out2 = gr.Slider(minimum=0.01, value=80, label="% maxed out", info="percentage of how much your machine is maxed out")
used2 = gr.Slider(minimum=0.01, value=50, label="% used", info="percentage of time your machine is used")
rental_plan_inp2 = gr.Dropdown(vm_rental_choice, label="Select a VM Rental Plan", info="These options are from Azure's VM rental plans")
model_inp2.change(fn=update_vm_choice, inputs=model_inp2, outputs=vm_inp2)
vm_inp2.change(fn=token_per_s_and_cost, inputs=vm_inp2, outputs=[tokens_per_second_inp2, vm_cost_per_hour_inp2, tokens_per_second2, vm_cost_per_hour2])
maxed_out2.change(fn=update_tco, inputs=[maxed_out2, used2, tokens_per_second_inp2, vm_cost_per_hour_inp2, rental_plan_inp2, out_diy2], outputs=out_diy2)
used2.change(fn=update_tco, inputs=[maxed_out2, used2, tokens_per_second_inp2, vm_cost_per_hour_inp2, rental_plan_inp2, out_diy2], outputs=out_diy2)
model_inp2.change(fn=update_tco, inputs=[maxed_out2, used2, tokens_per_second_inp2, vm_cost_per_hour_inp2, rental_plan_inp2, out_diy2], outputs=out_diy2)
vm_inp2.change(fn=update_tco, inputs=[maxed_out2, used2, tokens_per_second_inp2, vm_cost_per_hour_inp2, rental_plan_inp2, out_diy2], outputs=out_diy2)
rental_plan_inp2.change(fn=update_tco, inputs=[maxed_out2, used2, tokens_per_second_inp2, vm_cost_per_hour_inp2, rental_plan_inp2, out_diy2], outputs=out_diy2)
with gr.Row(visible=False) as text_saas_column2:
gr.Markdown(description2)
with gr.Accordion("Open to see the formula", visible=False, open=False) as formula_saas2:
gr.Markdown(
r"$ saas\_cost\_per\_request = saas\_cost\_per\_token \times tokens\_per\_request$"
)
with gr.Row(visible=False) as input_saas_column2:
model_provider_inp2 = gr.Dropdown(['OpenAI'], label="Model Provider", value="OpenAI", info="Choose an AI model provider you want to work with")
context_inp2 = gr.Dropdown(['4K context', '16K context'], label="Context", info="Number of tokens the model considers when processing text")
model_provider_inp2.change(fn=update_tco2, inputs=[model_provider_inp2, context_inp2, out_saas2], outputs=out_saas2)
context_inp2.change(fn=update_tco2, inputs=[model_provider_inp2, context_inp2, out_saas2], outputs=out_saas2)
def show_vm_info():
return {
vm_info2: gr.update(visible=True),
}
vm_inp2.change(show_vm_info, outputs=vm_info2)
def submit2(solution_selection2):
if solution_selection2 == "Open-source":
return {
formula_diy2: gr.update(visible=True),
title_column2: gr.update(visible=True),
text_diy_column2: gr.update(visible=True),
input_diy_column2: gr.update(visible=True),
formula_saas2: gr.update(visible=False),
text_saas_column2: gr.update(visible=False),
input_saas_column2: gr.update(visible=False),
}
else:
return {
formula_diy2: gr.update(visible=False),
text_diy_column2: gr.update(visible=False),
input_diy_column2: gr.update(visible=False),
title_column2: gr.update(visible=True),
formula_saas2: gr.update(visible=True),
text_saas_column2: gr.update(visible=True),
input_saas_column2: gr.update(visible=True),
}
solution_selection2.change(
submit2,
solution_selection2,
[vm2, vm_info2, vm_inp2, maxed_out2, used2, out_diy2, out_saas2, formula_diy2, formula_saas2, title_column2, text_diy_column2, text_saas_column2, model_inp2, rental_plan_inp2, model_provider_inp2, context_inp2, input_diy_column2, input_saas_column2],
)
with gr.Row():
with gr.Column():
with gr.Row():
gr.Markdown(text3)
with gr.Row():
plot = gr.BarPlot(vertical=False, title="Comparison", y_title="Cost/token ($)", width=500, interactive=True)
context_inp.change(fn=update_plot, inputs=[out_diy2, out_saas], outputs=plot)
maxed_out2.change(fn=update_plot, inputs=[out_diy2, out_saas], outputs=plot)
used2.change(fn=update_plot, inputs=[out_diy2, out_saas], outputs=plot)
vm_inp2.change(fn=update_plot, inputs=[out_diy2, out_saas], outputs=plot)
model_provider_inp.change(fn=update_plot, inputs=[out_diy2, out_saas], outputs=plot)
rental_plan_inp2.change(fn=update_plot, inputs=[out_diy2, out_saas], outputs=plot)
model_inp2.change(fn=update_plot, inputs=[out_diy2, out_saas], outputs=plot)
context_inp2.change(fn=update_plot, inputs=[out_diy, out_saas2], outputs=plot)
vm_inp.change(fn=update_plot, inputs=[out_diy, out_saas2], outputs=plot)
maxed_out.change(fn=update_plot, inputs=[out_diy, out_saas2], outputs=plot)
used.change(fn=update_plot, inputs=[out_diy, out_saas2], outputs=plot)
model_provider_inp2.change(fn=update_plot, inputs=[out_diy, out_saas2], outputs=plot)
rental_plan_inp.change(fn=update_plot, inputs=[out_diy, out_saas2], outputs=plot)
model_inp.change(fn=update_plot, inputs=[out_diy, out_saas2], outputs=plot)
demo.launch() |