jadehardouin commited on
Commit
044dd38
1 Parent(s): 406259e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -31
app.py CHANGED
@@ -1,38 +1,74 @@
1
  import gradio as gr
2
  import models
3
- import time
4
 
5
- text = "<h1 style='text-align: center; color: midnightblue; font-size: 30px;'>TCO Comparison Calculator"
6
- text1 = "<h1 style='text-align: center; color: midnightblue; font-size: 20px;'>First solution"
7
- text2 = "<h1 style='text-align: center; color: midnightblue; font-size: 20px;'>Second solution"
8
- text3 = "<h1 style='text-align: center; color: midnightblue; font-size: 20px;'>Compute and compare cost/token for both solutions"
 
9
  description=f"""
10
- <p>In this demo application, we help you compare different solutions for your AI integration plans, such as Open source or SaaS.</p>
11
- <p>First, you'll have to choose the two solutions you'd like to compare. Then, follow the instructions to select your configurations for each solution and we will compute the cost/token accordingly to them. Eventually, both solutions are compared to evaluate which one best suits your needs.</p>
12
  """
13
 
14
- def compute_ratio(tco1, tco2):
15
- time.sleep(2)
16
- try:
17
- r = tco1 / tco2
18
-
19
- if r < 1:
20
- comparison_result = f"Second solution is {1/r:.5f} times more expensive than the first"
21
- elif r > 1:
22
- comparison_result = f"Second solution is {r:.5f} times cheaper than the first"
23
- else:
24
- comparison_result = "Both solutions will cost the same."
25
- return comparison_result
26
-
27
- except ValueError as e:
28
- return f"Error: {str(e)}"
 
 
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
31
- Models: list[models.BaseTCOModel] = [models.OpenAIModel, models.CohereModel, models.OpenSourceLlama2Model,models.OpenSourceDIY]
32
  model_names = [Model().get_name() for Model in Models]
33
  gr.Markdown(value=text)
34
  gr.Markdown(value=description)
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  with gr.Row():
37
  with gr.Column():
38
  gr.Markdown(value=text1)
@@ -46,27 +82,33 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
46
  dropdown2 = gr.Dropdown(model_names, interactive=True, label="AI service options")
47
  page2.render()
48
 
49
- dropdown.change(page1.make_model_visible, inputs=dropdown, outputs=page1.get_all_components())
50
- dropdown2.change(page2.make_model_visible, inputs=dropdown2, outputs=page2.get_all_components())
51
 
52
  gr.Markdown(value=text3)
53
- compute_tco_btn = gr.Button("Compute cost/token", size="lg")
54
  tco1 = gr.State()
55
  tco2 = gr.State()
56
 
57
  with gr.Row():
58
  with gr.Column():
59
- tco_output = gr.Text("Output 1: ", label="Cost/token for the first solution")
 
60
  with gr.Accordion("Open to see the formula", open=False):
61
  tco_formula = gr.Markdown()
62
 
63
  with gr.Column():
64
- tco_output2 = gr.Text("Output 2: ", label="Cost/token for the second solution")
 
65
  with gr.Accordion("Open to see the formula", open=False):
66
  tco_formula2 = gr.Markdown()
 
 
 
 
 
 
67
 
68
- ratio = gr.Text("Ratio: ", label="Ratio of cost/token for both solutions")
69
-
70
- compute_tco_btn.click(page1.compute_cost_per_token, inputs=page1.get_all_components_for_cost_computing() + [dropdown], outputs=[tco_output, tco1, tco_formula]).then(page2.compute_cost_per_token, inputs=page2.get_all_components_for_cost_computing() + [dropdown2], outputs=[tco_output2, tco2, tco_formula2]).then(compute_ratio, inputs=[tco1, tco2], outputs=ratio)
71
 
72
  demo.launch(debug=True)
 
1
  import gradio as gr
2
  import models
3
+ import pandas as pd
4
 
5
+ text = "<h1 style='text-align: center; color: midnightblue; font-size: 40px;'>TCO Comparison Calculator"
6
+ text0 = "<h1 style='text-align: center; color: midnightblue; font-size: 30px;'>Describe your use case"
7
+ text1 = "<h1 style='text-align: center; color: midnightblue; font-size: 30px;'>First solution"
8
+ text2 = "<h1 style='text-align: center; color: midnightblue; font-size: 30px;'>Second solution"
9
+ text3 = "<h1 style='text-align: center; color: midnightblue; font-size: 30px;'>Compute and compare TCOs"
10
  description=f"""
11
+ <p>In this demo application, we help you compare different AI model services, such as Open source or SaaS solutions.</p>
12
+ <p>First, you'll have to choose how you want to use the AI model service based on your use case. Then, select the two model service solutions you'd like to compare. Depending on the solution you chose, you could be able to modify some parameters of the set-up. Eventually, we will provide you with the cost of deployment for the selected model services, as a function of the number of requests. You can compare both solutions to evaluate which one best suits your needs.</p>
13
  """
14
 
15
+ def on_use_case_change(use_case):
16
+ if use_case == "Summarize":
17
+ return gr.update(value=500), gr.update(value=200)
18
+ elif use_case == "Question-Answering":
19
+ return gr.update(value=300), gr.update(value=300)
20
+ else:
21
+ return gr.update(value=50), gr.update(value=10)
22
+
23
+ def compare(tco1, tco2):
24
+ r = tco1 / tco2
25
+ if r < 1:
26
+ comparison_result = f"Second solution is {1/r:.5f} times more expensive than the first"
27
+ elif r > 1:
28
+ comparison_result = f"Second solution is {r:.5f} times cheaper than the first"
29
+ else:
30
+ comparison_result = "Both solutions will cost the same."
31
+ return comparison_result
32
 
33
+ def update_plot(tco1, tco2, dropdown, dropdown2):
34
+ if dropdown == "(Open source) Llama 2" or dropdown == "(Open source) DIY":
35
+ salary = 1000
36
+ salary2 = 0
37
+ elif dropdown2 == "(Open source) Llama 2" or dropdown2 == "(Open source) DIY":
38
+ salary = 0
39
+ salary2 = 1000
40
+ else:
41
+ salary = 0
42
+ salary2 = 0
43
+ data = pd.DataFrame(
44
+ {
45
+ "Number of requests": [100, 200, 300, 400, 500, 1000, 10000, 100, 200, 300, 400, 500, 1000, 10000],
46
+ "Cost ($)": [(tco1 * req + salary) for req in [100, 200, 300, 400, 500, 1000, 10000]] + [(tco2 * req + salary2) for req in [100, 200, 300, 400, 500, 1000, 10000]],
47
+ "AI model service": [dropdown] * 7 + [dropdown2] * 7
48
+ }
49
+ )
50
+ return gr.LinePlot.update(data, x="Number of requests", y="Cost ($)",color="AI model service",color_legend_position="bottom", title="Total Cost of Model Serving for one month", height=300, width=500, tooltip=["Number of requests", "Cost ($)", "AI model service"])
51
+
52
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
53
+ Models: list[models.BaseTCOModel] = [models.OpenAIModel, models.CohereModel, models.OpenSourceLlama2Model, models.OpenSourceDIY]
54
  model_names = [Model().get_name() for Model in Models]
55
  gr.Markdown(value=text)
56
  gr.Markdown(value=description)
57
 
58
+ with gr.Row():
59
+ with gr.Column():
60
+ with gr.Row():
61
+ gr.Markdown(value=text0)
62
+ with gr.Row():
63
+ use_case = gr.Dropdown(["Summarize", "Question-Answering", "Classification"], value="Question-Answering", label="End goal of your AI model service")
64
+ with gr.Row():
65
+ input_tokens = gr.Slider(minimum=1, maximum=1000, value=300, step=1, label="Number of input token", info="We put a value that we find best suit your use case choice but feel free to adjust", interactive=True)
66
+ output_tokens = gr.Slider(minimum=1, maximum=1000, value=300, step=1, label="Number of output token", info="We put a value that we find best suit your use case choice but feel free to adjust", interactive=True)
67
+ with gr.Row(visible=False):
68
+ num_users = gr.Number(value="1000", interactive = True, label="Number of users for your service")
69
+
70
+ use_case.change(on_use_case_change, inputs=use_case, outputs=[input_tokens, output_tokens])
71
+
72
  with gr.Row():
73
  with gr.Column():
74
  gr.Markdown(value=text1)
 
82
  dropdown2 = gr.Dropdown(model_names, interactive=True, label="AI service options")
83
  page2.render()
84
 
85
+ dropdown.change(page1.make_model_visible, inputs=[dropdown, use_case, num_users, input_tokens, output_tokens], outputs=page1.get_all_components())
86
+ dropdown2.change(page2.make_model_visible, inputs=[dropdown2, use_case, num_users, input_tokens, output_tokens], outputs=page2.get_all_components())
87
 
88
  gr.Markdown(value=text3)
89
+ compute_tco_btn = gr.Button("Compute cost/request", size="lg")
90
  tco1 = gr.State()
91
  tco2 = gr.State()
92
 
93
  with gr.Row():
94
  with gr.Column():
95
+ tco_output = gr.Text("Output 1: ", label="Cost/request for the first solution")
96
+ latency_info = gr.Markdown()
97
  with gr.Accordion("Open to see the formula", open=False):
98
  tco_formula = gr.Markdown()
99
 
100
  with gr.Column():
101
+ tco_output2 = gr.Text("Output 2: ", label="Cost/request for the second solution")
102
+ latency_info2 = gr.Markdown()
103
  with gr.Accordion("Open to see the formula", open=False):
104
  tco_formula2 = gr.Markdown()
105
+
106
+ with gr.Row():
107
+ with gr.Column(scale=1):
108
+ ratio = gr.Text("Ratio: ", label="Ratio of cost/request for both solutions")
109
+ with gr.Column(scale=3):
110
+ plot = gr.LinePlot()
111
 
112
+ compute_tco_btn.click(page1.compute_cost_per_token, inputs=page1.get_all_components_for_cost_computing() + [dropdown], outputs=[tco_output, tco1, tco_formula, latency_info]).then(page2.compute_cost_per_token, inputs=page2.get_all_components_for_cost_computing() + [dropdown2], outputs=[tco_output2, tco2, tco_formula2, latency_info2]).then(compare, inputs=[tco1, tco2], outputs=ratio).then(update_plot, inputs=[tco1, tco2, dropdown, dropdown2], outputs=plot)
 
 
113
 
114
  demo.launch(debug=True)