import gradio as gr text = "
In this demo application, we help you compare different solutions for your AI incorporation plans, such as open-source or SaaS.
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.
""" description1=f"""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.
The selected prices for a Virtual Machine rental come from Azure's VM rental plans, which can offer reductions for long-term reserved usage.
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.
To see the formula used to compute the cost/request, check the box just below!
""" description2=f"""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.
To compute this cost per token, some adjustments were chosen: the number of tokensper request was set to 64.
To see the formula used to compute the cost/request, check the box just below!
""" description3=f"""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.
""" models = ["Llama-2-7B", "Llama-2-13B", "Llama-2-70B"] vm_rental_choice = ["pay as you go", "1 year reserved", "3 years reserved"] model_provider = ["OpenAI"] context = ["4K context", "16K context"] error_box = gr.Textbox(label="Error", visible=False) bool_diy = False bool_saas = False with gr.Blocks(theme=gr.themes.Soft()) as demo: gr.Markdown(value=text) gr.Markdown(value=description) with gr.Row(): with gr.Column(scale=2): solution_selection = gr.Dropdown(["SaaS", "Home-made"], label="Select a Solution") submit_btn = gr.Button("Submit") with gr.Row(visible=False) as title_column: gr.Markdown(value=text1) with gr.Row(visible=False) as text_diy_column: gr.Markdown(description1) with gr.Row(visible=False) as see_formula_diy: formula_diy_btn = gr.Checkbox(label="See formula", value=False) with gr.Row(visible=False) as formula_diy: gr.Markdown( 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}$" ) def submit_formula_diy(formula_diy_btn): if formula_diy_btn: return{ formula_diy: gr.update(visible=True), } else: return{ formula_diy: gr.update(visible=False), } formula_diy_btn.select( submit_formula_diy, [formula_diy_btn], [formula_diy] ) with gr.Row(visible=False) as input_diy_column: inp = gr.Dropdown(models, label="Select an AI Model") inp2 = gr.Dropdown(vm_rental_choice, label="Select a VM Rental Plan") with gr.Row(visible=False) as output_diy_column: out = gr.Textbox(label="Cost/request") btn = gr.Button("Compute the cost/request") btn.click(fn=calculate_tco, inputs=[inp, inp2], outputs=out) with gr.Row(visible=False) as text_saas_column: gr.Markdown(description2) with gr.Row(visible=False) as see_formula_saas: formula_saas_btn = gr.Checkbox(label="See formula", value=False) with gr.Row(visible=False) as formula_saas: gr.Markdown( r"$ saas\_cost\_per\_request = saas\_cost\_per\_token \times tokens\_per\_request$" ) def submit_formula_diy(formula_saas_btn): if formula_saas_btn: return{ formula_saas: gr.update(visible=True), } else: return{ formula_saas: gr.update(visible=False), } formula_saas_btn.select( submit_formula_diy, [formula_saas_btn], [formula_saas] ) with gr.Row(visible=False) as input_saas_column: model_provider_inp = gr.Dropdown(model_provider, label="Model Provider") context_inp = gr.Dropdown(context, label="Context") with gr.Row(visible=False) as output_saas_column: out_2 = gr.Textbox(label="Cost/request") btn_2 = gr.Button("Compute the cost/request") btn_2.click(fn=calculate_tco_2, inputs=[model_provider_inp, context_inp], outputs=out_2) def submit(solution_selection): if solution_selection == "Home-made": return { see_formula_diy: gr.update(visible=True), title_column: gr.update(visible=True), text_diy_column: gr.update(visible=True), input_diy_column: gr.update(visible=True), output_diy_column: gr.update(visible=True), see_formula_saas: gr.update(visible=False), text_saas_column: gr.update(visible=False), input_saas_column: gr.update(visible=False), output_saas_column: gr.update(visible=False), } else: return { see_formula_diy: gr.update(visible=False), text_diy_column: gr.update(visible=False), input_diy_column: gr.update(visible=False), output_diy_column: gr.update(visible=False), see_formula_saas: gr.update(visible=True), title_column: gr.update(visible=True), text_saas_column: gr.update(visible=True), input_saas_column: gr.update(visible=True), output_saas_column: gr.update(visible=True), } submit_btn.click( submit, solution_selection, [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], ) # gr.Divider(style="vertical", thickness=2, color="blue") with gr.Column(scale=2): solution_selection2 = gr.Dropdown(["SaaS", "Home-made"], label="Select a Solution") submit_btn2 = gr.Button("Submit") with gr.Row(visible=False) as title_column2: gr.Markdown(value=text2) with gr.Row(visible=False) as text_diy_column2: gr.Markdown(description1) with gr.Row(visible=False) as see_formula_diy2: formula_diy_btn2 = gr.Checkbox(label="See formula", value=False) with gr.Row(visible=False) as formula_diy2: gr.Markdown( 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}$" ) def submit_formula_diy(formula_diy_btn2): if formula_diy_btn2: return{ formula_diy2: gr.update(visible=True), } else: return{ formula_diy2: gr.update(visible=False), } formula_diy_btn2.select( submit_formula_diy, [formula_diy_btn2], [formula_diy2] ) with gr.Row(visible=False) as input_diy_column2: inp_2 = gr.Dropdown(models, label="Select an AI Model") inp2_2 = gr.Dropdown(vm_rental_choice, label="Select a VM Rental Plan") with gr.Row(visible=False) as output_diy_column2: out2 = gr.Textbox(label="Cost/request") btn2 = gr.Button("Compute the cost/request") btn2.click(fn=calculate_tco, inputs=[inp_2, inp2_2], outputs=out2) with gr.Row(visible=False) as text_saas_column2: gr.Markdown(description2) with gr.Row(visible=False) as see_formula_saas2: formula_saas_btn2 = gr.Checkbox(label="See formula", value=False) with gr.Row(visible=False) as formula_saas2: gr.Markdown( r"$ saas\_cost\_per\_request = saas\_cost\_per\_token \times tokens\_per\_request$" ) def submit_formula_diy2(formula_saas_btn2): if formula_saas_btn2: return{ formula_saas2: gr.update(visible=True), } else: return{ formula_saas2: gr.update(visible=False), } formula_saas_btn2.select( submit_formula_diy2, [formula_saas_btn2], [formula_saas2] ) with gr.Row(visible=False) as input_saas_column2: model_provider_inp2 = gr.Dropdown(['OpenAI'], label="Model Provider") context_inp2 = gr.Dropdown(['4K context', '16K context'], label="Context") with gr.Row(visible=False) as output_saas_column2: out_2_2 = gr.Textbox(label="Cost/request") btn_2_2 = gr.Button("Compute the cost/request") btn_2_2.click(fn=calculate_tco_2, inputs=[model_provider_inp2, context_inp2], outputs=out_2_2) def submit(solution_selection2): if solution_selection2 == "Home-made": return { see_formula_diy2: gr.update(visible=True), title_column2: gr.update(visible=True), text_diy_column2: gr.update(visible=True), input_diy_column2: gr.update(visible=True), output_diy_column2: gr.update(visible=True), see_formula_saas2: gr.update(visible=False), text_saas_column2: gr.update(visible=False), input_saas_column2: gr.update(visible=False), output_saas_column2: gr.update(visible=False), } else: return { see_formula_diy2: gr.update(visible=False), text_diy_column2: gr.update(visible=False), input_diy_column2: gr.update(visible=False), output_diy_column2: gr.update(visible=False), see_formula_saas2: gr.update(visible=True), title_column2: gr.update(visible=True), text_saas_column2: gr.update(visible=True), input_saas_column2: gr.update(visible=True), output_saas_column2: gr.update(visible=True), } submit_btn2.click( submit, solution_selection2, [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], ) gr.Markdown(value=text3) gr.Markdown(description3) error_box = gr.Textbox(label="Error", visible=False) with gr.Row(): cost_text1_inp = gr.Textbox(label="Cost/request for the first solution", placeholder="Enter cost/request for the first solution") cost_text2_inp = gr.Textbox(label="Cost/request for the second solution",placeholder="Enter cost/request for the second solution") btn_compare = gr.Button("Compare") with gr.Row(visible=False) as output: out_compare = gr.Textbox() def submit2(cost_text1_inp, cost_text2_inp): if len(cost_text1_inp) == 0: return {error_box: gr.update(value="Enter cost of solution 1", visible=True)} if len(cost_text2_inp) == 0: return {error_box: gr.update(value="Enter cost of solution 2", visible=True)} return { output: gr.update(visible=True), out_compare: compare(cost_text1_inp, cost_text2_inp), } btn_compare.click( submit2, inputs=[cost_text1_inp, cost_text2_inp], outputs=[out_compare, output] ) demo.launch()