jadehardouin commited on
Commit
eef299f
β€’
1 Parent(s): 2fd6e58

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -5
app.py CHANGED
@@ -7,8 +7,9 @@ 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 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
  """
13
  formula = r"""
14
  $CR = \frac{CIT_{1K} \times IT + COT_{1K} \times OT}{1000}$ <br>
@@ -19,7 +20,7 @@ $COT_{1K}$ = Cost per 1000 Output Tokens <br>
19
  $IT$ = Input Tokens <br>
20
  $OT$ = Output Tokens
21
  """
22
-
23
  def on_use_case_change(use_case):
24
  if use_case == "Summarize":
25
  return gr.update(value=500), gr.update(value=200)
@@ -73,6 +74,27 @@ def create_table(tco1, tco2, labor_cost1, labor_cost2, dropdown, dropdown2, late
73
 
74
  return gr.update(value=centered_styled_data)
75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
  def update_plot(tco1, tco2, dropdown, dropdown2, labour_cost1, labour_cost2):
77
 
78
  request_ranges = list(range(0, 1001, 100)) + list(range(1000, 10001, 500)) + list(range(10000, 100001, 1000)) + list(range(100000, 2000001, 100000))
@@ -90,7 +112,7 @@ def update_plot(tco1, tco2, dropdown, dropdown2, labour_cost1, labour_cost2):
90
  style = theme.Style()
91
 
92
  with gr.Blocks(theme=style) as demo:
93
- Models: list[models.BaseTCOModel] = [models.OpenAIModelGPT4, models.OpenAIModelGPT3_5, models.CohereModel, models.OpenSourceLlama2Model]
94
  model_names = [Model().get_name() for Model in Models]
95
  gr.Markdown(value=text)
96
  gr.Markdown(value=description)
@@ -149,6 +171,6 @@ with gr.Blocks(theme=style) as demo:
149
  with gr.Column(scale=2):
150
  plot = gr.LinePlot(visible=False)
151
 
152
- compute_tco_btn.click(page1.compute_cost_per_token, inputs=page1.get_all_components_for_cost_computing() + [dropdown, input_tokens, output_tokens], outputs=[tco1, latency, labor_cost1]).then(page2.compute_cost_per_token, inputs=page2.get_all_components_for_cost_computing() + [dropdown2, input_tokens, output_tokens], outputs=[tco2, latency2, labor_cost2]).then(create_table, inputs=[tco1, tco2, labor_cost1, labor_cost2, dropdown, dropdown2, latency, latency2], outputs=table).then(compare_info, inputs=[tco1, tco2, dropdown, dropdown2], outputs=[image, ratio]).then(update_plot, inputs=[tco1, tco2, dropdown, dropdown2, labor_cost1, labor_cost2], outputs=plot)
153
 
154
  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
  """
14
  formula = r"""
15
  $CR = \frac{CIT_{1K} \times IT + COT_{1K} \times OT}{1000}$ <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":
26
  return gr.update(value=500), gr.update(value=200)
 
74
 
75
  return gr.update(value=centered_styled_data)
76
 
77
+ def compute_cost_per_request(*args):
78
+ dropdown_id = args[-2]
79
+ dropdown_id2 = args[-1]
80
+ if dropdown_id!=None and dropdown_id2!=None:
81
+ # Separate the arguments for page1 and page2
82
+ args_list = list(args)
83
+ args_page1 = args_list[:len(page1.get_all_components_for_cost_computing())] + [dropdown_id, input_tokens, output_tokens]
84
+ args_page2 = args_list[len(page1.get_all_components_for_cost_computing()):] + [dropdown_id2, input_tokens, output_tokens]
85
+ # Compute and compare using both pages
86
+ result_page1 = page1.compute_cost_per_token(*args_page1)
87
+ result_page2 = page2.compute_cost_per_token(*args_page2)
88
+ # Unpack the results from the functions
89
+ tco1, latency, labor_cost1 = result_page1
90
+ tco2, latency2, labor_cost2 = result_page2
91
+
92
+ return tco1, latency, labor_cost1, tco2, latency2, labor_cost2
93
+ else:
94
+ raise gr.Error("Please select two AI service options.")
95
+
96
+
97
+
98
  def update_plot(tco1, tco2, dropdown, dropdown2, labour_cost1, labour_cost2):
99
 
100
  request_ranges = list(range(0, 1001, 100)) + list(range(1000, 10001, 500)) + list(range(10000, 100001, 1000)) + list(range(100000, 2000001, 100000))
 
112
  style = theme.Style()
113
 
114
  with gr.Blocks(theme=style) as demo:
115
+ Models: list[models.BaseTCOModel] = [models.OpenAIModelGPT4, models.OpenAIModelGPT3_5, models.CohereModel, models.DIYLlama2Model]
116
  model_names = [Model().get_name() for Model in Models]
117
  gr.Markdown(value=text)
118
  gr.Markdown(value=description)
 
171
  with gr.Column(scale=2):
172
  plot = gr.LinePlot(visible=False)
173
 
174
+ 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]).then(create_table, inputs=[tco1, tco2, labor_cost1, labor_cost2, dropdown, dropdown2, latency, latency2], outputs=table).then(compare_info, inputs=[tco1, tco2, dropdown, dropdown2], outputs=[image, ratio]).then(update_plot, inputs=[tco1, tco2, dropdown, dropdown2, labor_cost1, labor_cost2], outputs=plot)
175
 
176
  demo.launch(debug=True)