jadehardouin
commited on
Commit
•
47a336c
1
Parent(s):
0a5ab63
Create results.py
Browse files- results.py +104 -0
results.py
ADDED
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import gradio as gr
|
3 |
+
|
4 |
+
shared_page1 = None
|
5 |
+
shared_page2 = None
|
6 |
+
|
7 |
+
def set_shared_pages(page1, page2):
|
8 |
+
global shared_page1, shared_page2
|
9 |
+
shared_page1 = page1
|
10 |
+
shared_page2 = page2
|
11 |
+
|
12 |
+
def compare_info(tco1, tco2, dropdown, dropdown2):
|
13 |
+
if error_occurred == False :
|
14 |
+
#Compute the cost/request ratio
|
15 |
+
r = tco1 / tco2
|
16 |
+
if r < 1:
|
17 |
+
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."""
|
18 |
+
elif r > 1:
|
19 |
+
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."""
|
20 |
+
else:
|
21 |
+
comparison_result = f"""Both solutions have the <b>same cost/request</b>."""
|
22 |
+
|
23 |
+
# Create a bar chart
|
24 |
+
services = [dropdown, dropdown2]
|
25 |
+
costs_to_compare = [tco1, tco2]
|
26 |
+
|
27 |
+
plt.figure(figsize=(6, 4))
|
28 |
+
plt.bar(services, costs_to_compare, color=['red', 'green'])
|
29 |
+
plt.xlabel('AI option services', fontsize=10)
|
30 |
+
plt.ylabel('($) Cost/Request', fontsize=10)
|
31 |
+
plt.title('Comparison of Cost/Request', fontsize=14)
|
32 |
+
|
33 |
+
plt.tight_layout()
|
34 |
+
plt.savefig('cost_comparison.png') # Save to a file
|
35 |
+
|
36 |
+
return gr.update(value='cost_comparison.png', visible=True), comparison_result
|
37 |
+
else:
|
38 |
+
return None, ""
|
39 |
+
|
40 |
+
def create_table(tco1, tco2, labor_cost1, labor_cost2, dropdown, dropdown2, latency, latency2):
|
41 |
+
if error_occurred == False:
|
42 |
+
if shared_page1 is None or shared_page2 is None:
|
43 |
+
raise ValueError("Shared instances not set.")
|
44 |
+
list_values = []
|
45 |
+
first_sol = [tco1, labor_cost1, latency]
|
46 |
+
second_sol = [tco2, labor_cost2, latency2]
|
47 |
+
list_values.append(first_sol)
|
48 |
+
list_values.append(second_sol)
|
49 |
+
|
50 |
+
data = pd.DataFrame(list_values, index=[dropdown, dropdown2], columns=["Cost/request ($) ", "Labor Cost ($/month)", "Average latency (s)"])
|
51 |
+
|
52 |
+
formatted_data = data.copy()
|
53 |
+
formatted_data["Cost/request ($) "] = formatted_data["Cost/request ($) "].apply('{:.5f}'.format)
|
54 |
+
formatted_data["Labor Cost ($/month)"] = formatted_data["Labor Cost ($/month)"].apply('{:.0f}'.format)
|
55 |
+
|
56 |
+
styled_data = formatted_data.style\
|
57 |
+
.set_properties(**{'background-color': '#ffffff', 'color': '#000000', 'border-color': '#e0e0e0', 'border-width': '1px', 'border-style': 'solid'})\
|
58 |
+
.to_html()
|
59 |
+
centered_styled_data = f"<center>{styled_data}</center>"
|
60 |
+
|
61 |
+
return gr.update(value=centered_styled_data)
|
62 |
+
else:
|
63 |
+
return ""
|
64 |
+
|
65 |
+
def compute_cost_per_request(*args):
|
66 |
+
dropdown_id = args[-4]
|
67 |
+
dropdown_id2 = args[-3]
|
68 |
+
input_tokens = args[-2]
|
69 |
+
output_tokens = args[-1]
|
70 |
+
global error_occurred
|
71 |
+
|
72 |
+
if dropdown_id!="" and dropdown_id2!="":
|
73 |
+
error_occurred = False
|
74 |
+
page1 = shared_page1
|
75 |
+
page2 = shared_page2
|
76 |
+
|
77 |
+
args_page1 = list(args) + [dropdown_id, input_tokens, output_tokens]
|
78 |
+
args_page2 = list(args) + [dropdown_id2, input_tokens, output_tokens]
|
79 |
+
result_page1 = page1.compute_cost_per_token(*args_page1)
|
80 |
+
result_page2 = page2.compute_cost_per_token(*args_page2)
|
81 |
+
tco1, latency, labor_cost1 = result_page1
|
82 |
+
tco2, latency2, labor_cost2 = result_page2
|
83 |
+
return tco1, latency, labor_cost1, tco2, latency2, labor_cost2
|
84 |
+
else:
|
85 |
+
error_occurred = True
|
86 |
+
raise gr.Error("Please select two AI service options.")
|
87 |
+
|
88 |
+
def update_plot(tco1, tco2, dropdown, dropdown2, labour_cost1, labour_cost2):
|
89 |
+
if error_occurred == False:
|
90 |
+
request_ranges = list(range(0, 1001, 100)) + list(range(1000, 10001, 500)) + list(range(10000, 100001, 1000)) + list(range(100000, 2000001, 100000))
|
91 |
+
costs_tco1 = [(tco1 * req + labour_cost1) for req in request_ranges]
|
92 |
+
costs_tco2 = [(tco2 * req + labour_cost2) for req in request_ranges]
|
93 |
+
|
94 |
+
data = pd.DataFrame({
|
95 |
+
"Number of requests": request_ranges * 2,
|
96 |
+
"Cost ($)": costs_tco1 + costs_tco2,
|
97 |
+
"AI model service": ["1)" + " " + dropdown] * len(request_ranges) + ["2)" + " " + dropdown2] * len(request_ranges)
|
98 |
+
}
|
99 |
+
)
|
100 |
+
return gr.LinePlot.update(data, visible=True, x="Number of requests", y="Cost ($)",color="AI model service",color_legend_position="bottom", title="Set-up TCO for one month", height=300, width=500, tooltip=["Number of requests", "Cost ($)", "AI model service"])
|
101 |
+
else:
|
102 |
+
return ""
|
103 |
+
|
104 |
+
error_occurred = False
|