|
import pandas as pd |
|
import gradio as gr |
|
import matplotlib.pyplot as plt |
|
|
|
shared_page1 = None |
|
shared_page2 = None |
|
|
|
def set_shared_pages(page1, page2): |
|
global shared_page1, shared_page2 |
|
shared_page1 = page1 |
|
shared_page2 = page2 |
|
|
|
def compare_info(tco1, tco2, dropdown, dropdown2): |
|
if error_occurred == False : |
|
|
|
r = tco1 / tco2 |
|
if r < 1: |
|
comparison_result = f"""The cost/request of the second {dropdown2} service is <b>{1/r:.5f} times more expensive</b> than the one of the first {dropdown} service.""" |
|
elif r > 1: |
|
comparison_result = f"""The cost/request of the second {dropdown2} service is <b>{r:.5f} times cheaper</b> than the one of the first {dropdown} service.""" |
|
else: |
|
comparison_result = f"""Both solutions have the <b>same cost/request</b>.""" |
|
|
|
|
|
services = [dropdown, dropdown2] |
|
costs_to_compare = [tco1, tco2] |
|
|
|
plt.figure(figsize=(6, 4)) |
|
plt.bar(services, costs_to_compare, color=['red', 'green']) |
|
plt.xlabel('AI option services', fontsize=10) |
|
plt.ylabel('($) Cost/Request', fontsize=10) |
|
plt.title('Comparison of Cost/Request', fontsize=14) |
|
|
|
plt.tight_layout() |
|
plt.savefig('cost_comparison.png') |
|
|
|
return gr.update(value='cost_comparison.png', visible=True), comparison_result |
|
else: |
|
return None, "" |
|
|
|
def create_table(tco1, tco2, labor_cost1, labor_cost2, dropdown, dropdown2, latency, latency2): |
|
if error_occurred == False: |
|
if shared_page1 is None or shared_page2 is None: |
|
raise ValueError("Shared instances not set.") |
|
list_values = [] |
|
first_sol = [tco1, labor_cost1, latency] |
|
second_sol = [tco2, labor_cost2, latency2] |
|
list_values.append(first_sol) |
|
list_values.append(second_sol) |
|
|
|
data = pd.DataFrame(list_values, index=[dropdown, dropdown2], columns=["Cost/request ($) ", "Labor Cost ($/month)", "Average latency (s)"]) |
|
|
|
formatted_data = data.copy() |
|
formatted_data["Cost/request ($) "] = formatted_data["Cost/request ($) "].apply('{:.5f}'.format) |
|
formatted_data["Labor Cost ($/month)"] = formatted_data["Labor Cost ($/month)"].apply('{:.0f}'.format) |
|
|
|
styled_data = formatted_data.style\ |
|
.set_properties(**{'background-color': '#ffffff', 'color': '#000000', 'border-color': '#e0e0e0', 'border-width': '1px', 'border-style': 'solid'})\ |
|
.to_html() |
|
centered_styled_data = f"<center>{styled_data}</center>" |
|
|
|
return gr.update(value=centered_styled_data) |
|
else: |
|
return "" |
|
|
|
def compute_cost_per_request(*args): |
|
dropdown_id = args[-4] |
|
dropdown_id2 = args[-3] |
|
input_tokens = args[-2] |
|
output_tokens = args[-1] |
|
global error_occurred |
|
|
|
if dropdown_id!="" and dropdown_id2!="": |
|
error_occurred = False |
|
page1 = shared_page1 |
|
page2 = shared_page2 |
|
|
|
args_page1 = list(args) + [dropdown_id, input_tokens, output_tokens] |
|
args_page2 = list(args) + [dropdown_id2, input_tokens, output_tokens] |
|
result_page1 = page1.compute_cost_per_token(*args_page1) |
|
result_page2 = page2.compute_cost_per_token(*args_page2) |
|
tco1, latency, labor_cost1 = result_page1 |
|
tco2, latency2, labor_cost2 = result_page2 |
|
return tco1, latency, labor_cost1, tco2, latency2, labor_cost2 |
|
else: |
|
error_occurred = True |
|
raise gr.Error("Please select two AI service options.") |
|
|
|
def update_plot(tco1, tco2, dropdown, dropdown2, labour_cost1, labour_cost2): |
|
if error_occurred == False: |
|
request_ranges = list(range(0, 1001, 100)) + list(range(1000, 10001, 500)) + list(range(10000, 100001, 1000)) + list(range(100000, 1600001, 100000)) |
|
costs_tco1 = [(tco1 * req + labour_cost1) for req in request_ranges] |
|
costs_tco2 = [(tco2 * req + labour_cost2) for req in request_ranges] |
|
|
|
data = pd.DataFrame({ |
|
"Number of requests": request_ranges * 2, |
|
"Cost ($)": costs_tco1 + costs_tco2, |
|
"AI model service": ["1)" + " " + dropdown] * len(request_ranges) + ["2)" + " " + dropdown2] * len(request_ranges) |
|
} |
|
) |
|
return gr.LinePlot.update(data, visible=True, x="Number of requests", y="Cost ($)", color="AI model service", color_legend_title=" ", color_legend_position="right", title="TCO for one month", height=300, width=500, tooltip=["Number of requests", "Cost ($)", "AI model service"]) |
|
else: |
|
return "" |
|
|
|
error_occurred = False |