Spaces:
Running
Running
import gradio as gr | |
import pandas as pd | |
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(model_choice, vm_rental_choice, out_diy): | |
VM_cost_per_hour=3.6730 #at Azure for the basic pay as you go option | |
maxed_out = 0.8 #percentage of time the VM is maxed out | |
used = 0.5 #percentage of time the VM is used | |
tokens_per_request = 64 | |
if model_choice == "Llama-2-7B": | |
tokens_per_second=694.38 | |
elif model_choice == "Llama-2-13B": | |
tokens_per_second=1000 | |
elif model_choice == "Llama-2-70B": | |
tokens_per_second=10000 | |
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 * 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 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): | |
data = pd.DataFrame( | |
{ | |
"Solution": ["Home-made", "SaaS"], | |
"Cost/token ($)": [diy_value, saas_value], | |
} | |
) | |
return gr.BarPlot.update(data, x="Solution", y="Cost/token ($)") | |
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=f""" | |
<p>This interface provides you with the cost per request you get using the open-source solution, based on the model you choose to use and how long you're planning to use it.</p> | |
<p>The selected prices for a Virtual Machine rental come from Azure's VM rental plans, which can offer reductions for long-term reserved usage.</p> | |
<p>To compute this cost per requets, some adjustments were chosen: the VM is an A100 40GB, supposedly maxed out at 80% and utilized 50% of the time in a full day. Plus, the number of tokens per request was set to 64.</p> | |
<p>To see the formula used to compute the cost/request, check the box just below!</p> | |
""" | |
description2=f""" | |
<p>This interface provides you with the cost per request 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.</p> | |
<p>To compute this cost per request, some adjustments were chosen: the number of tokens per request was set to 64.</p> | |
<p>To see the formula used to compute the cost/request, check the box just below!</p> | |
""" | |
description3=f""" | |
<p>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.</p> | |
""" | |
models = ["Llama-2-7B", "Llama-2-13B", "Llama-2-70B"] | |
vm_rental_choice = ["pay as you go", "1 year reserved", "3 years reserved"] | |
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) | |
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.Row(visible=False) as input_diy_column: | |
model_inp = gr.Dropdown(models, label="Select an AI Model") | |
rental_plan_inp = gr.Dropdown(vm_rental_choice, label="Select a VM Rental Plan") | |
rental_plan_inp.change(fn=calculate_tco, inputs=[model_inp, rental_plan_inp, out_diy], outputs=out_diy) | |
with gr.Row(visible=False) as text_saas_column: | |
gr.Markdown(description2) | |
with gr.Row(visible=False) as input_saas_column: | |
model_provider_inp = gr.Dropdown(model_provider, label="Model Provider") | |
context_inp = gr.Dropdown(context, label="Context") | |
context_inp.change(fn=calculate_tco_2, inputs=[model_provider_inp, context_inp, out_saas], outputs=out_saas) | |
def submit(solution_selection): | |
if solution_selection == "Open-source": | |
return { | |
title_column: gr.update(visible=True), | |
text_diy_column: gr.update(visible=True), | |
input_diy_column: gr.update(visible=True), | |
text_saas_column: gr.update(visible=False), | |
input_saas_column: gr.update(visible=False), | |
} | |
else: | |
return { | |
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, | |
[out_saas, text_diy_column, 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.Row(visible=False) as input_diy_column2: | |
model_inp2 = gr.Dropdown(models, label="Select an AI Model") | |
rental_plan_inp2 = gr.Dropdown(vm_rental_choice, label="Select a VM Rental Plan") | |
rental_plan_inp2.change(fn=calculate_tco, inputs=[model_inp2, rental_plan_inp2, out_diy2], outputs=out_diy2) | |
with gr.Row(visible=False) as text_saas_column2: | |
gr.Markdown(description2) | |
with gr.Row(visible=False) as input_saas_column2: | |
model_provider_inp2 = gr.Dropdown(['OpenAI'], label="Model Provider") | |
context_inp2 = gr.Dropdown(['4K context', '16K context'], label="Context") | |
context_inp2.change(fn=calculate_tco_2, inputs=[model_provider_inp2, context_inp2, out_saas2], outputs=out_saas2) | |
def submit2(solution_selection2): | |
if solution_selection2 == "Open-source": | |
return { | |
title_column2: gr.update(visible=True), | |
text_diy_column2: gr.update(visible=True), | |
input_diy_column2: gr.update(visible=True), | |
text_saas_column2: gr.update(visible=False), | |
input_saas_column2: gr.update(visible=False), | |
} | |
else: | |
return { | |
text_diy_column2: gr.update(visible=False), | |
input_diy_column2: gr.update(visible=False), | |
title_column2: gr.update(visible=True), | |
text_saas_column2: gr.update(visible=True), | |
input_saas_column2: gr.update(visible=True), | |
} | |
solution_selection2.change( | |
submit2, | |
solution_selection2, | |
[out_diy2, out_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(title="Comparison", x_title="Solution", y_title="Cost/token ($)", interactive=True, width=500) | |
if solution_selection=="Open-source": | |
context_inp2.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) | |
else: | |
rental_plan_inp2.change(fn=update_plot, inputs=[out_diy2, out_saas], outputs=plot) | |
context_inp.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) | |
model_inp2.change(fn=update_plot, inputs=[out_diy2, out_saas], outputs=plot) | |
demo.launch() |