import gradio as gr import pandas as pd 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 solutions you'd like to compare. Then, follow the instructions to select your configurations for each solution 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_intro="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." description2_intro="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." description3="This interface compares the cost per request for the two solutions you selected and gives you an insight of whether a solution is more valuable in the long term." latex_formula = r"$ saas\_cost\_per\_request = saas\_cost\_per\_token \times tokens\_per\_request$" latex_formula2 = r"$ open\-source\_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}$" tooltip_html = "Hover here to see formula" tooltip_html2 = "Hover here to see formula" description1 = f"{description1_intro} {tooltip_html2}" description2 = f"{description2_intro} {tooltip_html}" 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) with gr.Blocks(theme=gr.themes.Soft()) as demo: gr.Markdown(value=text) gr.Markdown(value=description) out_diy = gr.State(value=0) out_saas = gr.State(value=0) out_diy2 = gr.State(value=0) out_saas2 = gr.State(value=0) with gr.Row(): with gr.Column(): solution_selection = gr.Dropdown(["SaaS", "Open-source"], label="Select a Solution") 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 input_diy_column: model_inp = gr.Dropdown(models, label="Select an AI Model") rental_plan_inp = gr.Dropdown(vm_rental_choice, label="Select a VM Rental Plan") rental_plan_inp.change(fn=calculate_tco, inputs=[model_inp, rental_plan_inp, out_diy], outputs=out_diy) with gr.Row(visible=False) as text_saas_column: gr.Markdown(description2) 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") context_inp.change(fn=calculate_tco_2, inputs=[model_provider_inp, context_inp, out_saas], outputs=out_saas) def submit(solution_selection): if solution_selection == "Open-source": return { title_column: gr.update(visible=True), text_diy_column: gr.update(visible=True), input_diy_column: gr.update(visible=True), text_saas_column: gr.update(visible=False), input_saas_column: gr.update(visible=False), } else: return { text_diy_column: gr.update(visible=False), input_diy_column: gr.update(visible=False), title_column: gr.update(visible=True), text_saas_column: gr.update(visible=True), input_saas_column: gr.update(visible=True), } solution_selection.change( submit, solution_selection, [out_saas, text_diy_column, title_column, text_saas_column, model_inp, rental_plan_inp, model_provider_inp, context_inp, input_diy_column, input_saas_column], ) # gr.Divider(style="vertical", thickness=2, color="blue") with gr.Column(): solution_selection2 = gr.Dropdown(["SaaS", "Open-source"], label="Select a Solution") 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 input_diy_column2: model_inp2 = gr.Dropdown(models, label="Select an AI Model") rental_plan_inp2 = gr.Dropdown(vm_rental_choice, label="Select a VM Rental Plan") rental_plan_inp2.change(fn=calculate_tco, inputs=[model_inp2, rental_plan_inp2, out_diy2], outputs=out_diy2) with gr.Row(visible=False) as text_saas_column2: gr.Markdown(description2) 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") context_inp2.change(fn=calculate_tco_2, inputs=[model_provider_inp2, context_inp2, out_saas2], outputs=out_saas2) def submit2(solution_selection2): if solution_selection2 == "Open-source": return { title_column2: gr.update(visible=True), text_diy_column2: gr.update(visible=True), input_diy_column2: gr.update(visible=True), text_saas_column2: gr.update(visible=False), input_saas_column2: gr.update(visible=False), } else: return { text_diy_column2: gr.update(visible=False), input_diy_column2: gr.update(visible=False), title_column2: gr.update(visible=True), text_saas_column2: gr.update(visible=True), input_saas_column2: gr.update(visible=True), } solution_selection2.change( submit2, solution_selection2, [out_diy2, out_saas2, title_column2, text_diy_column2, text_saas_column2, model_inp2, rental_plan_inp2, model_provider_inp2, context_inp2, input_diy_column2, input_saas_column2], ) with gr.Row(): with gr.Column(): with gr.Row(): gr.Markdown(text3) with gr.Row(): plot = gr.BarPlot(title="Comparison", y_title="Cost/token ($)", interactive=True, width=500) if solution_selection=="Open-source": context_inp2.change(fn=update_plot, inputs=[out_diy, out_saas2], outputs=plot) model_provider_inp2.change(fn=update_plot, inputs=[out_diy, out_saas2], outputs=plot) rental_plan_inp.change(fn=update_plot, inputs=[out_diy, out_saas2], outputs=plot) model_inp.change(fn=update_plot, inputs=[out_diy, out_saas2], outputs=plot) else: rental_plan_inp2.change(fn=update_plot, inputs=[out_diy2, out_saas], outputs=plot) context_inp.change(fn=update_plot, inputs=[out_diy2, out_saas], outputs=plot) model_provider_inp.change(fn=update_plot, inputs=[out_diy2, out_saas], outputs=plot) model_inp2.change(fn=update_plot, inputs=[out_diy2, out_saas], outputs=plot) demo.launch()