Spaces:
Running
Running
jadehardouin
commited on
Commit
β’
a0c59c8
1
Parent(s):
2e06873
Update app.py
Browse files
app.py
CHANGED
@@ -7,7 +7,7 @@ import matplotlib.pyplot as plt
|
|
7 |
text = "<h1 style='text-align: center; color: #333333; font-size: 40px;'>TCO Comparison Calculator"
|
8 |
text2 = "Please note that the cost/request only defines the infrastructure cost for deployment. The labor cost must be added for the whole AI model service deployment TCO."
|
9 |
description=f"""
|
10 |
-
<p>In this demo application, we help you compare different AI model services, such as Open source or SaaS solutions, based on the Total Cost of Ownership
|
11 |
<p>Please note that we focus on getting the service up and running, but not the maintenance that follows.π</p>
|
12 |
<p>If you want to <strong>contribute to the calculator</strong> by adding your own AI service option, follow this <a href="https://huggingface.co/spaces/mithril-security/TCO_calculator/blob/main/How_to_contribute.md">tutorial</a> π. </p>
|
13 |
"""
|
@@ -30,54 +30,62 @@ def on_use_case_change(use_case):
|
|
30 |
return gr.update(value=50), gr.update(value=10)
|
31 |
|
32 |
def compare_info(tco1, tco2, dropdown, dropdown2):
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
plt.bar(services, costs_to_compare, color=['red', 'green'])
|
48 |
-
plt.xlabel('AI option services', fontsize=10)
|
49 |
-
plt.ylabel('($) Cost/Request', fontsize=10)
|
50 |
-
plt.title('Comparison of Cost/Request', fontsize=14)
|
51 |
|
52 |
-
|
53 |
-
|
54 |
|
55 |
-
|
|
|
|
|
56 |
|
57 |
def create_table(tco1, tco2, labor_cost1, labor_cost2, dropdown, dropdown2, latency, latency2):
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
|
|
63 |
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
|
|
|
|
76 |
|
77 |
def compute_cost_per_request(*args):
|
78 |
dropdown_id = args[-2]
|
79 |
dropdown_id2 = args[-1]
|
|
|
80 |
if dropdown_id!="" and dropdown_id2!="":
|
|
|
81 |
args_page1 = list(args) + [dropdown_id, input_tokens, output_tokens]
|
82 |
args_page2 = list(args) + [dropdown_id2, input_tokens, output_tokens]
|
83 |
result_page1 = page1.compute_cost_per_token(*args_page1)
|
@@ -86,22 +94,26 @@ def compute_cost_per_request(*args):
|
|
86 |
tco2, latency2, labor_cost2 = result_page2
|
87 |
return tco1, latency, labor_cost1, tco2, latency2, labor_cost2
|
88 |
else:
|
|
|
89 |
raise gr.Error("Please select two AI service options.")
|
90 |
|
91 |
def update_plot(tco1, tco2, dropdown, dropdown2, labour_cost1, labour_cost2):
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
data = pd.DataFrame({
|
98 |
-
"Number of requests": request_ranges * 2,
|
99 |
-
"Cost ($)": costs_tco1 + costs_tco2,
|
100 |
-
"AI model service": ["1)" + " " + dropdown] * len(request_ranges) + ["2)" + " " + dropdown2] * len(request_ranges)
|
101 |
-
}
|
102 |
-
)
|
103 |
-
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"])
|
104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
style = theme.Style()
|
106 |
|
107 |
with gr.Blocks(theme=style) as demo:
|
@@ -164,6 +176,9 @@ with gr.Blocks(theme=style) as demo:
|
|
164 |
with gr.Column(scale=2):
|
165 |
plot = gr.LinePlot(visible=False)
|
166 |
|
167 |
-
compute_tco_btn.click(compute_cost_per_request, inputs=page1.get_all_components_for_cost_computing() + page2.get_all_components_for_cost_computing() + [dropdown, dropdown2], outputs=[tco1, latency, labor_cost1, tco2, latency2, labor_cost2])
|
|
|
|
|
|
|
168 |
|
169 |
demo.launch(debug=True)
|
|
|
7 |
text = "<h1 style='text-align: center; color: #333333; font-size: 40px;'>TCO Comparison Calculator"
|
8 |
text2 = "Please note that the cost/request only defines the infrastructure cost for deployment. The labor cost must be added for the whole AI model service deployment TCO."
|
9 |
description=f"""
|
10 |
+
<p>In this demo application, we help you compare different AI model services, such as Open source or SaaS solutions, based on the Total Cost of Ownership for their deployment. π</p>
|
11 |
<p>Please note that we focus on getting the service up and running, but not the maintenance that follows.π</p>
|
12 |
<p>If you want to <strong>contribute to the calculator</strong> by adding your own AI service option, follow this <a href="https://huggingface.co/spaces/mithril-security/TCO_calculator/blob/main/How_to_contribute.md">tutorial</a> π. </p>
|
13 |
"""
|
|
|
30 |
return gr.update(value=50), gr.update(value=10)
|
31 |
|
32 |
def compare_info(tco1, tco2, dropdown, dropdown2):
|
33 |
+
if error_occurred == False :
|
34 |
+
#Compute the cost/request ratio
|
35 |
+
r = tco1 / tco2
|
36 |
+
if r < 1:
|
37 |
+
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."""
|
38 |
+
elif r > 1:
|
39 |
+
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."""
|
40 |
+
else:
|
41 |
+
comparison_result = f"""Both solutions have the <b>same cost/request</b>."""
|
42 |
+
|
43 |
+
# Create a bar chart
|
44 |
+
services = [dropdown, dropdown2]
|
45 |
+
costs_to_compare = [tco1, tco2]
|
46 |
|
47 |
+
plt.figure(figsize=(6, 4))
|
48 |
+
plt.bar(services, costs_to_compare, color=['red', 'green'])
|
49 |
+
plt.xlabel('AI option services', fontsize=10)
|
50 |
+
plt.ylabel('($) Cost/Request', fontsize=10)
|
51 |
+
plt.title('Comparison of Cost/Request', fontsize=14)
|
|
|
|
|
|
|
|
|
52 |
|
53 |
+
plt.tight_layout()
|
54 |
+
plt.savefig('cost_comparison.png') # Save to a file
|
55 |
|
56 |
+
return gr.update(value='cost_comparison.png', visible=True), comparison_result
|
57 |
+
else:
|
58 |
+
return None, ""
|
59 |
|
60 |
def create_table(tco1, tco2, labor_cost1, labor_cost2, dropdown, dropdown2, latency, latency2):
|
61 |
+
if error_occurred == False:
|
62 |
+
list_values = []
|
63 |
+
first_sol = [tco1, labor_cost1, latency]
|
64 |
+
second_sol = [tco2, labor_cost2, latency2]
|
65 |
+
list_values.append(first_sol)
|
66 |
+
list_values.append(second_sol)
|
67 |
|
68 |
+
data = pd.DataFrame(list_values, index=[dropdown, dropdown2], columns=["Cost/request ($) ", "Labor Cost ($/month)", "Average latency (s)"])
|
69 |
+
|
70 |
+
formatted_data = data.copy()
|
71 |
+
formatted_data["Cost/request ($) "] = formatted_data["Cost/request ($) "].apply('{:.5f}'.format)
|
72 |
+
formatted_data["Labor Cost ($/month)"] = formatted_data["Labor Cost ($/month)"].apply('{:.0f}'.format)
|
73 |
|
74 |
+
styled_data = formatted_data.style\
|
75 |
+
.set_properties(**{'background-color': '#ffffff', 'color': '#000000', 'border-color': '#e0e0e0', 'border-width': '1px', 'border-style': 'solid'})\
|
76 |
+
.to_html()
|
77 |
+
centered_styled_data = f"<center>{styled_data}</center>"
|
78 |
+
|
79 |
+
return gr.update(value=centered_styled_data)
|
80 |
+
else:
|
81 |
+
return ""
|
82 |
|
83 |
def compute_cost_per_request(*args):
|
84 |
dropdown_id = args[-2]
|
85 |
dropdown_id2 = args[-1]
|
86 |
+
global error_occurred
|
87 |
if dropdown_id!="" and dropdown_id2!="":
|
88 |
+
error_occurred = False
|
89 |
args_page1 = list(args) + [dropdown_id, input_tokens, output_tokens]
|
90 |
args_page2 = list(args) + [dropdown_id2, input_tokens, output_tokens]
|
91 |
result_page1 = page1.compute_cost_per_token(*args_page1)
|
|
|
94 |
tco2, latency2, labor_cost2 = result_page2
|
95 |
return tco1, latency, labor_cost1, tco2, latency2, labor_cost2
|
96 |
else:
|
97 |
+
error_occurred = True
|
98 |
raise gr.Error("Please select two AI service options.")
|
99 |
|
100 |
def update_plot(tco1, tco2, dropdown, dropdown2, labour_cost1, labour_cost2):
|
101 |
+
if error_occurred == False:
|
102 |
+
request_ranges = list(range(0, 1001, 100)) + list(range(1000, 10001, 500)) + list(range(10000, 100001, 1000)) + list(range(100000, 2000001, 100000))
|
103 |
+
costs_tco1 = [(tco1 * req + labour_cost1) for req in request_ranges]
|
104 |
+
costs_tco2 = [(tco2 * req + labour_cost2) for req in request_ranges]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
|
106 |
+
data = pd.DataFrame({
|
107 |
+
"Number of requests": request_ranges * 2,
|
108 |
+
"Cost ($)": costs_tco1 + costs_tco2,
|
109 |
+
"AI model service": ["1)" + " " + dropdown] * len(request_ranges) + ["2)" + " " + dropdown2] * len(request_ranges)
|
110 |
+
}
|
111 |
+
)
|
112 |
+
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"])
|
113 |
+
else:
|
114 |
+
return ""
|
115 |
+
|
116 |
+
error_occurred = False
|
117 |
style = theme.Style()
|
118 |
|
119 |
with gr.Blocks(theme=style) as demo:
|
|
|
176 |
with gr.Column(scale=2):
|
177 |
plot = gr.LinePlot(visible=False)
|
178 |
|
179 |
+
compute_tco_btn.click(compute_cost_per_request, inputs=page1.get_all_components_for_cost_computing() + page2.get_all_components_for_cost_computing() + [dropdown, dropdown2], outputs=[tco1, latency, labor_cost1, tco2, latency2, labor_cost2])\
|
180 |
+
.then(create_table, inputs=[tco1, tco2, labor_cost1, labor_cost2, dropdown, dropdown2, latency, latency2], outputs=table)\
|
181 |
+
.then(compare_info, inputs=[tco1, tco2, dropdown, dropdown2], outputs=[image, ratio])\
|
182 |
+
.then(update_plot, inputs=[tco1, tco2, dropdown, dropdown2, labor_cost1, labor_cost2], outputs=plot)
|
183 |
|
184 |
demo.launch(debug=True)
|