jadehardouin commited on
Commit
73d3fc4
1 Parent(s): e0e93c4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +329 -17
app.py CHANGED
@@ -1,7 +1,14 @@
1
  import gradio as gr
2
 
 
 
 
 
 
3
  def calculate_tco(model_choice, vm_rental_choice):
4
- VM_cost_per_hour=3.6730
 
 
5
  tokens_per_request = 64
6
 
7
  if model_choice == "Llama-2-7B":
@@ -13,31 +20,336 @@ def calculate_tco(model_choice, vm_rental_choice):
13
  elif model_choice == "Llama-2-70B":
14
  tokens_per_second=10000
15
 
16
- if vm_rental_option == "1 month" or "3 months" or "6 months":
17
  reduction = 0
18
 
19
- elif vm_rental_option == "1 year":
20
  reduction = 0.34
21
 
22
- elif vm_rental_option == "3 years":
23
  reduction = 0.62
24
 
25
- homemade_cost_per_request = VM_cost_per_hour * (1 - reduction) * tokens_per_request / (tokens_per_second * 3600) #* 0.8 * 0.5
26
- saas_cost_per_request = 0.018 * tokens_per_request
27
 
28
- output = f"Cost/request with a plan for {vm_rental_choice} using:\n"
29
- output += f"- SaaS solution from OpenAI: ${saas_cost_per_request:.5f}\n"
30
- output += f"- Home-made solution with the model {model_choice}: ${homemade_cost_per_request:.5f}"
31
  return output
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  models = ["Llama-2-7B", "Llama-2-13B", "Llama-2-70B"]
34
- vm_rental_option = ["1 month", "3 months", "6 months", "1 year", "3 years"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
- iface = gr.Interface(
37
- fn=calculate_tco,
38
- inputs=[gr.inputs.Dropdown(models, label="Select AI Model"),
39
- gr.inputs.Dropdown(vm_rental_option, label="Select VM Rental Duration")],
40
- outputs=gr.outputs.Textbox()
41
- )
42
 
43
- iface.launch()
 
1
  import gradio as gr
2
 
3
+ text = "<h1 style='text-align: center; color: blue; font-size: 30px;'>TCO Comparison Calculator"
4
+ text1 = "<h1 style='text-align: center; color: blue; font-size: 20px;'>First solution"
5
+ text2 = "<h1 style='text-align: center; color: blue; font-size: 20px;'>Second solution"
6
+ text3 = "<h1 style='text-align: center; color: blue; font-size: 25px;'>Comparison"
7
+
8
  def calculate_tco(model_choice, vm_rental_choice):
9
+ VM_cost_per_hour=3.6730 #at Azure for the basic pay as you go option
10
+ maxed_out = 0.8 #percentage of time the VM is maxed out
11
+ used = 0.5 #percentage of time the VM is used
12
  tokens_per_request = 64
13
 
14
  if model_choice == "Llama-2-7B":
 
20
  elif model_choice == "Llama-2-70B":
21
  tokens_per_second=10000
22
 
23
+ if vm_rental_choice == "pay as you go":
24
  reduction = 0
25
 
26
+ elif vm_rental_choice == "1 year reserved":
27
  reduction = 0.34
28
 
29
+ elif vm_rental_choice == "3 years reserved":
30
  reduction = 0.62
31
 
32
+ homemade_cost_per_token = VM_cost_per_hour * (1 - reduction) / (tokens_per_second * 3600 * maxed_out * used)
33
+ homemade_cost_per_request = tokens_per_request * homemade_cost_per_token
34
 
35
+ output = f"In a situation of a {vm_rental_choice} plan, using an Home-made solution with the open-source model {model_choice}: \ncost per request = ${homemade_cost_per_request:.7f}"
 
 
36
  return output
37
 
38
+ def calculate_tco_2(model_provider, context):
39
+ tokens_per_request = 64
40
+
41
+ if model_provider == "OpenAI":
42
+ if context == "4K context":
43
+ saas_cost_per_token = 0.00035
44
+ saas_cost_per_request = saas_cost_per_token * tokens_per_request
45
+ elif context == "16K context" :
46
+ saas_cost_per_token = 0.0007
47
+ saas_cost_per_request = saas_cost_per_token * tokens_per_request
48
+ return f"When renting services from {model_provider} for a SaaS solution, specifying a {context}, you'll get: \ncost per request = ${saas_cost_per_request:.7f}"
49
+
50
+ def extract_cost_from_text(text):
51
+ try:
52
+ cost = float(text)
53
+ return cost
54
+ except ValueError as e:
55
+ raise ValueError("Invalid cost text format")
56
+
57
+ def compare(cost_text1, cost_text2):
58
+ try:
59
+ # Extract the costs from the input strings
60
+ cost1 = extract_cost_from_text(cost_text1)
61
+ cost2 = extract_cost_from_text(cost_text2)
62
+
63
+ r = cost1 / cost2
64
+
65
+ if r < 1:
66
+ comparison_result = f"First solution is cheaper, with a ratio of {r:.2f}."
67
+ elif r > 1:
68
+ comparison_result = f"Second solution is cheaper, with a ratio of {r:.2f}."
69
+ else:
70
+ comparison_result = "Both solutions will cost the same."
71
+
72
+ return comparison_result
73
+
74
+ except ValueError as e:
75
+ return f"Error: {str(e)}"
76
+
77
+ description=f"""
78
+ <p>In this demo application, we help you compare different solutions for your AI incorporation plans, such as open-source or SaaS.</p>
79
+ <p>First, you'll have to choose the two options you'd like to compare. Then, follow the instructions to select your configurations for each option and we will compute the cost/request accordingly to them. Eventually, you can compare both solutions to evaluate which one best suits your needs, in the short or long term.</p>
80
+ """
81
+ description1=f"""
82
+ <p>This interface provides you with the cost per token you get using the open-source solution, based on the model you choose to use and how long you're planning to use it.</p>
83
+ <p>The selected prices for a Virtual Machine rental come from Azure's VM rental plans, which can offer reductions for long-term reserved usage.</p>
84
+ <p>To compute this cost per token, some adjustments were chosen: the VM is an A100 40GB, supposedly maxed out at 80% and utilized 50% of the time in a full day. Plus, the number of tokens per request was set to 64.</p>
85
+ <p>To see the formula used to compute the cost/request, check the box just below!</p>
86
+ """
87
+ description2=f"""
88
+ <p>This interface provides you with the cost per token resulting from the AI model provider you choose and the number of tokens you select for context, which the model will take into account when processing input texts.</p>
89
+ <p>To compute this cost per token, some adjustments were chosen: the number of tokensper request was set to 64.</p>
90
+ <p>To see the formula used to compute the cost/request, check the box just below!</p>
91
+ """
92
+ description3=f"""
93
+ <p>This interface compares the cost per token for the two solutions you selected and gives you an insight of whether a solution is more valuable in the long term.</p>
94
+ """
95
+
96
  models = ["Llama-2-7B", "Llama-2-13B", "Llama-2-70B"]
97
+ vm_rental_choice = ["pay as you go", "1 year reserved", "3 years reserved"]
98
+ model_provider = ["OpenAI"]
99
+ context = ["4K context", "16K context"]
100
+ error_box = gr.Textbox(label="Error", visible=False)
101
+ bool_diy = False
102
+ bool_saas = False
103
+
104
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
105
+ gr.Markdown(value=text)
106
+ gr.Markdown(value=description)
107
+
108
+ with gr.Row():
109
+ with gr.Column(scale=2):
110
+
111
+ solution_selection = gr.Dropdown(["SaaS", "Home-made"], label="Select a Solution")
112
+ submit_btn = gr.Button("Submit")
113
+
114
+ with gr.Row(visible=False) as title_column:
115
+ gr.Markdown(value=text1)
116
+
117
+ with gr.Row(visible=False) as text_diy_column:
118
+ gr.Markdown(description1)
119
+
120
+ with gr.Row(visible=False) as see_formula_diy:
121
+ formula_diy_btn = gr.Checkbox(label="See formula", value=False)
122
+ with gr.Row(visible=False) as formula_diy:
123
+ gr.Markdown(
124
+ r"$ homemade\_cost\_per\_request = \frac{tokens\_per\_request \times VM\_cost\_per\_hour \times (1 - reduction)}{tokens\_per\_second \times 3600 \times maxed\_out \times used}$"
125
+ )
126
+ def submit_formula_diy(formula_diy_btn):
127
+ if formula_diy_btn:
128
+ return{
129
+ formula_diy: gr.update(visible=True),
130
+ }
131
+ else:
132
+ return{
133
+ formula_diy: gr.update(visible=False),
134
+ }
135
+
136
+ formula_diy_btn.select(
137
+ submit_formula_diy,
138
+ [formula_diy_btn],
139
+ [formula_diy]
140
+ )
141
+
142
+ with gr.Row(visible=False) as input_diy_column:
143
+ inp = gr.Dropdown(models, label="Select an AI Model")
144
+ inp2 = gr.Dropdown(vm_rental_choice, label="Select a VM Rental Plan")
145
+
146
+ with gr.Row(visible=False) as output_diy_column:
147
+ out = gr.Textbox(label="Cost/request")
148
+ btn = gr.Button("Compute the cost/request")
149
+ btn.click(fn=calculate_tco, inputs=[inp, inp2], outputs=out)
150
+
151
+ with gr.Row(visible=False) as text_saas_column:
152
+ gr.Markdown(description2)
153
+
154
+ with gr.Row(visible=False) as see_formula_saas:
155
+ formula_saas_btn = gr.Checkbox(label="See formula", value=False)
156
+ with gr.Row(visible=False) as formula_saas:
157
+ gr.Markdown(
158
+ r"$ saas\_cost\_per\_request = saas\_cost\_per\_token \times tokens\_per\_request$"
159
+ )
160
+ def submit_formula_diy(formula_saas_btn):
161
+ if formula_saas_btn:
162
+ return{
163
+ formula_saas: gr.update(visible=True),
164
+ }
165
+ else:
166
+ return{
167
+ formula_saas: gr.update(visible=False),
168
+ }
169
+
170
+ formula_saas_btn.select(
171
+ submit_formula_diy,
172
+ [formula_saas_btn],
173
+ [formula_saas]
174
+ )
175
+
176
+ with gr.Row(visible=False) as input_saas_column:
177
+ model_provider_inp = gr.Dropdown(model_provider, label="Model Provider")
178
+ context_inp = gr.Dropdown(context, label="Context")
179
+
180
+ with gr.Row(visible=False) as output_saas_column:
181
+ out_2 = gr.Textbox(label="Cost/request")
182
+ btn_2 = gr.Button("Compute the cost/request")
183
+ btn_2.click(fn=calculate_tco_2, inputs=[model_provider_inp, context_inp], outputs=out_2)
184
+
185
+ def submit(solution_selection):
186
+ if solution_selection == "Home-made":
187
+ return {
188
+ see_formula_diy: gr.update(visible=True),
189
+ title_column: gr.update(visible=True),
190
+ text_diy_column: gr.update(visible=True),
191
+ input_diy_column: gr.update(visible=True),
192
+ output_diy_column: gr.update(visible=True),
193
+ see_formula_saas: gr.update(visible=False),
194
+ text_saas_column: gr.update(visible=False),
195
+ input_saas_column: gr.update(visible=False),
196
+ output_saas_column: gr.update(visible=False),
197
+ }
198
+ else:
199
+ return {
200
+ see_formula_diy: gr.update(visible=False),
201
+ text_diy_column: gr.update(visible=False),
202
+ input_diy_column: gr.update(visible=False),
203
+ output_diy_column: gr.update(visible=False),
204
+ see_formula_saas: gr.update(visible=True),
205
+ title_column: gr.update(visible=True),
206
+ text_saas_column: gr.update(visible=True),
207
+ input_saas_column: gr.update(visible=True),
208
+ output_saas_column: gr.update(visible=True),
209
+ }
210
+ submit_btn.click(
211
+ submit,
212
+ solution_selection,
213
+ [see_formula_diy, see_formula_saas, text_diy_column, title_column, text_saas_column, inp, inp2, out, btn, model_provider_inp, context_inp, out_2, btn_2, input_diy_column, input_saas_column, output_diy_column, output_saas_column],
214
+ )
215
+
216
+ # gr.Divider(style="vertical", thickness=2, color="blue")
217
+
218
+ with gr.Column(scale=2):
219
+
220
+ solution_selection2 = gr.Dropdown(["SaaS", "Home-made"], label="Select a Solution")
221
+ submit_btn2 = gr.Button("Submit")
222
+
223
+ with gr.Row(visible=False) as title_column2:
224
+ gr.Markdown(value=text2)
225
+
226
+ with gr.Row(visible=False) as text_diy_column2:
227
+ gr.Markdown(description1)
228
+
229
+ with gr.Row(visible=False) as see_formula_diy2:
230
+ formula_diy_btn2 = gr.Checkbox(label="See formula", value=False)
231
+ with gr.Row(visible=False) as formula_diy2:
232
+ gr.Markdown(
233
+ r"$ homemade\_cost\_per\_request = \frac{tokens\_per\_request \times VM\_cost\_per\_hour \times (1 - reduction)}{tokens\_per\_second \times 3600 \times maxed\_out \times used}$"
234
+ )
235
+
236
+ def submit_formula_diy(formula_diy_btn2):
237
+ if formula_diy_btn2:
238
+ return{
239
+ formula_diy2: gr.update(visible=True),
240
+ }
241
+ else:
242
+ return{
243
+ formula_diy2: gr.update(visible=False),
244
+ }
245
+
246
+ formula_diy_btn2.select(
247
+ submit_formula_diy,
248
+ [formula_diy_btn2],
249
+ [formula_diy2]
250
+ )
251
+
252
+ with gr.Row(visible=False) as input_diy_column2:
253
+ inp_2 = gr.Dropdown(models, label="Select an AI Model")
254
+ inp2_2 = gr.Dropdown(vm_rental_choice, label="Select a VM Rental Plan")
255
+
256
+ with gr.Row(visible=False) as output_diy_column2:
257
+ out2 = gr.Textbox(label="Cost/request")
258
+ btn2 = gr.Button("Compute the cost/request")
259
+ btn2.click(fn=calculate_tco, inputs=[inp_2, inp2_2], outputs=out2)
260
+
261
+ with gr.Row(visible=False) as text_saas_column2:
262
+ gr.Markdown(description2)
263
+
264
+ with gr.Row(visible=False) as see_formula_saas2:
265
+ formula_saas_btn2 = gr.Checkbox(label="See formula", value=False)
266
+ with gr.Row(visible=False) as formula_saas2:
267
+ gr.Markdown(
268
+ r"$ saas\_cost\_per\_request = saas\_cost\_per\_token \times tokens\_per\_request$"
269
+ )
270
+
271
+ def submit_formula_diy2(formula_saas_btn2):
272
+ if formula_saas_btn2:
273
+ return{
274
+ formula_saas2: gr.update(visible=True),
275
+ }
276
+ else:
277
+ return{
278
+ formula_saas2: gr.update(visible=False),
279
+ }
280
+
281
+ formula_saas_btn2.select(
282
+ submit_formula_diy2,
283
+ [formula_saas_btn2],
284
+ [formula_saas2]
285
+ )
286
+
287
+ with gr.Row(visible=False) as input_saas_column2:
288
+ model_provider_inp2 = gr.Dropdown(['OpenAI'], label="Model Provider")
289
+ context_inp2 = gr.Dropdown(['4K context', '16K context'], label="Context")
290
+
291
+ with gr.Row(visible=False) as output_saas_column2:
292
+ out_2_2 = gr.Textbox(label="Cost/request")
293
+ btn_2_2 = gr.Button("Compute the cost/request")
294
+ btn_2_2.click(fn=calculate_tco_2, inputs=[model_provider_inp2, context_inp2], outputs=out_2_2)
295
+
296
+ def submit(solution_selection2):
297
+ if solution_selection2 == "Home-made":
298
+ return {
299
+ see_formula_diy2: gr.update(visible=True),
300
+ title_column2: gr.update(visible=True),
301
+ text_diy_column2: gr.update(visible=True),
302
+ input_diy_column2: gr.update(visible=True),
303
+ output_diy_column2: gr.update(visible=True),
304
+ see_formula_saas2: gr.update(visible=False),
305
+ text_saas_column2: gr.update(visible=False),
306
+ input_saas_column2: gr.update(visible=False),
307
+ output_saas_column2: gr.update(visible=False),
308
+ }
309
+ else:
310
+ return {
311
+ see_formula_diy2: gr.update(visible=False),
312
+ text_diy_column2: gr.update(visible=False),
313
+ input_diy_column2: gr.update(visible=False),
314
+ output_diy_column2: gr.update(visible=False),
315
+ see_formula_saas2: gr.update(visible=True),
316
+ title_column2: gr.update(visible=True),
317
+ text_saas_column2: gr.update(visible=True),
318
+ input_saas_column2: gr.update(visible=True),
319
+ output_saas_column2: gr.update(visible=True),
320
+ }
321
+ submit_btn2.click(
322
+ submit,
323
+ solution_selection2,
324
+ [see_formula_diy2, see_formula_saas2, title_column2, text_diy_column2, text_saas_column2, inp_2, inp2_2, out2, btn2, model_provider_inp2, context_inp2, out_2_2, btn_2_2, input_diy_column2, input_saas_column2, output_diy_column2, output_saas_column2],
325
+ )
326
+
327
+ gr.Markdown(value=text3)
328
+ gr.Markdown(description3)
329
+ error_box = gr.Textbox(label="Error", visible=False)
330
+ with gr.Row():
331
+ cost_text1_inp = gr.Textbox(label="Cost/request for the first solution", placeholder="Enter cost/request for the first solution")
332
+ cost_text2_inp = gr.Textbox(label="Cost/request for the second solution",placeholder="Enter cost/request for the second solution")
333
+
334
+ btn_compare = gr.Button("Compare")
335
+
336
+ with gr.Row(visible=False) as output:
337
+ out_compare = gr.Textbox()
338
+
339
+ def submit2(cost_text1_inp, cost_text2_inp):
340
+ if len(cost_text1_inp) == 0:
341
+ return {error_box: gr.update(value="Enter cost of solution 1", visible=True)}
342
+ if len(cost_text2_inp) == 0:
343
+ return {error_box: gr.update(value="Enter cost of solution 2", visible=True)}
344
+ return {
345
+ output: gr.update(visible=True),
346
+ out_compare: compare(cost_text1_inp, cost_text2_inp),
347
+ }
348
 
349
+ btn_compare.click(
350
+ submit2,
351
+ inputs=[cost_text1_inp, cost_text2_inp],
352
+ outputs=[out_compare, output]
353
+ )
 
354
 
355
+ demo.launch()