jadehardouin commited on
Commit
0a5ab63
1 Parent(s): f630ea0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -98
app.py CHANGED
@@ -1,8 +1,7 @@
1
  import gradio as gr
2
  import models
3
- import pandas as pd
4
  import theme
5
- import matplotlib.pyplot as plt
6
 
7
  text = "<h1 style='text-align: center; color: #333333; font-size: 40px;'>AI 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."
@@ -20,6 +19,8 @@ $COT_{1K}$ = Cost per 1000 Output Tokens <br>
20
  $IT$ = Input Tokens <br>
21
  $OT$ = Output Tokens
22
  """
 
 
23
 
24
  def on_use_case_change(use_case):
25
  if use_case == "Summarize":
@@ -29,91 +30,6 @@ def on_use_case_change(use_case):
29
  else:
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)
92
- result_page2 = page2.compute_cost_per_token(*args_page2)
93
- tco1, latency, labor_cost1 = result_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:
@@ -147,17 +63,15 @@ with gr.Blocks(theme=style) as demo:
147
  dropdown2 = gr.Dropdown(model_names, interactive=True, label=" Second AI service option ")
148
  with gr.Accordion("Click here for more information on the computation parameters for your second AI service option", open=False):
149
  page2.render()
150
-
 
 
 
151
  dropdown.change(page1.make_model_visible, inputs=[dropdown, use_case], outputs=page1.get_all_components())
152
  dropdown2.change(page2.make_model_visible, inputs=[dropdown2, use_case], outputs=page2.get_all_components())
153
 
154
  compute_tco_btn = gr.Button("Compute & Compare", size="lg", variant="primary", scale=1)
155
- tco1 = gr.State()
156
- tco2 = gr.State()
157
- labor_cost1 = gr.State()
158
- labor_cost2 = gr.State()
159
- latency = gr.State()
160
- latency2 = gr.State()
161
 
162
  with gr.Row():
163
  with gr.Accordion("Click here to see the cost/request computation formula", open=False):
@@ -176,9 +90,9 @@ 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)
 
1
  import gradio as gr
2
  import models
3
+ import results
4
  import theme
 
5
 
6
  text = "<h1 style='text-align: center; color: #333333; font-size: 40px;'>AI TCO Comparison Calculator"
7
  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."
 
19
  $IT$ = Input Tokens <br>
20
  $OT$ = Output Tokens
21
  """
22
+ def set_shared_data(page1, page2):
23
+ return page1, page2
24
 
25
  def on_use_case_change(use_case):
26
  if use_case == "Summarize":
 
30
  else:
31
  return gr.update(value=50), gr.update(value=10)
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  style = theme.Style()
34
 
35
  with gr.Blocks(theme=style) as demo:
 
63
  dropdown2 = gr.Dropdown(model_names, interactive=True, label=" Second AI service option ")
64
  with gr.Accordion("Click here for more information on the computation parameters for your second AI service option", open=False):
65
  page2.render()
66
+
67
+ shared_page1, shared_page2 = set_shared_data(page1, page2)
68
+ results.set_shared_pages(shared_page1, shared_page2)
69
+
70
  dropdown.change(page1.make_model_visible, inputs=[dropdown, use_case], outputs=page1.get_all_components())
71
  dropdown2.change(page2.make_model_visible, inputs=[dropdown2, use_case], outputs=page2.get_all_components())
72
 
73
  compute_tco_btn = gr.Button("Compute & Compare", size="lg", variant="primary", scale=1)
74
+ tco1, tco2, labor_cost1, labor_cost2, latency, latency2 = [gr.State() for _ in range(6)]
 
 
 
 
 
75
 
76
  with gr.Row():
77
  with gr.Accordion("Click here to see the cost/request computation formula", open=False):
 
90
  with gr.Column(scale=2):
91
  plot = gr.LinePlot(visible=False)
92
 
93
+ compute_tco_btn.click(results.compute_cost_per_request, inputs=page1.get_all_components_for_cost_computing() + page2.get_all_components_for_cost_computing() + [dropdown, dropdown2, input_tokens, output_tokens], outputs=[tco1, latency, labor_cost1, tco2, latency2, labor_cost2])\
94
+ .then(results.create_table, inputs=[tco1, tco2, labor_cost1, labor_cost2, dropdown, dropdown2, latency, latency2], outputs=table)\
95
+ .then(results.compare_info, inputs=[tco1, tco2, dropdown, dropdown2], outputs=[image, ratio])\
96
+ .then(results.update_plot, inputs=[tco1, tco2, dropdown, dropdown2, labor_cost1, labor_cost2], outputs=plot)
97
 
98
  demo.launch(debug=True)