File size: 4,671 Bytes
47a336c
 
e9b6cd8
47a336c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c01e0e
47a336c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c01e0e
47a336c
 
4c01e0e
 
47a336c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1e8f0e
47a336c
 
 
 
 
90c34ac
47a336c
 
 
2804169
47a336c
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import pandas as pd
import gradio as gr 
import matplotlib.pyplot as plt

shared_page1 = None
shared_page2 = None

def set_shared_pages(page1, page2):
    global shared_page1, shared_page2
    shared_page1 = page1
    shared_page2 = page2

def compare_info(tco1, tco2, dropdown, dropdown2):
    if error_occurred == False :
        #Compute the cost/request ratio
        r = tco1 / tco2   
        if r < 1:
            comparison_result = f"""The cost/request of the second {dropdown2} service is <b>{1/r:.5f} times more expensive</b> than the one of the first {dropdown} service."""   
        elif r > 1:
            comparison_result = f"""The cost/request of the second {dropdown2} service is <b>{r:.5f} times cheaper</b> than the one of the first {dropdown} service."""
        else:
            comparison_result = f"""Both solutions have the <b>same cost/request</b>."""
            
        # Create a bar chart
        services = [dropdown, dropdown2]
        costs_to_compare = [tco1, tco2]
        
        plt.figure(figsize=(6, 4))
        plt.bar(services, costs_to_compare, color=['red', 'green'])
        plt.xlabel('AI option services', fontsize=10)
        plt.ylabel('(€) Cost/Request', fontsize=10)
        plt.title('Comparison of Cost/Request', fontsize=14)

        plt.tight_layout()
        plt.savefig('cost_comparison.png')  # Save to a file

        return gr.update(value='cost_comparison.png', visible=True), comparison_result
    else:
        return None, ""

def create_table(tco1, tco2, labor_cost1, labor_cost2, dropdown, dropdown2, latency, latency2):
    if error_occurred == False:
        if shared_page1 is None or shared_page2 is None:
            raise ValueError("Shared instances not set.")
        list_values = []
        first_sol = [tco1, labor_cost1, latency]
        second_sol = [tco2, labor_cost2, latency2]
        list_values.append(first_sol)
        list_values.append(second_sol)

        data = pd.DataFrame(list_values, index=[dropdown, dropdown2], columns=["Cost/request (€) ", "Labor Cost (€/month)", "Average latency (s)"])
        
        formatted_data = data.copy()
        formatted_data["Cost/request (€) "] = formatted_data["Cost/request (€) "].apply('{:.5f}'.format)
        formatted_data["Labor Cost (€/month)"] = formatted_data["Labor Cost (€/month)"].apply('{:.0f}'.format)

        styled_data = formatted_data.style\
            .set_properties(**{'background-color': '#ffffff', 'color': '#000000', 'border-color': '#e0e0e0', 'border-width': '1px', 'border-style': 'solid'})\
            .to_html()
        centered_styled_data = f"<center>{styled_data}</center>"
        
        return gr.update(value=centered_styled_data)
    else:
        return ""

def compute_cost_per_request(*args):
    dropdown_id = args[-4]
    dropdown_id2 = args[-3]
    input_tokens = args[-2]
    output_tokens = args[-1]
    global error_occurred 

    if dropdown_id!="" and dropdown_id2!="":
        error_occurred = False
        page1 = shared_page1
        page2 = shared_page2

        args_page1 = list(args) + [dropdown_id, input_tokens, output_tokens]
        args_page2 = list(args) + [dropdown_id2, input_tokens, output_tokens]
        result_page1 = page1.compute_cost_per_token(*args_page1)
        result_page2 = page2.compute_cost_per_token(*args_page2)
        tco1, latency, labor_cost1 = result_page1
        tco2, latency2, labor_cost2 = result_page2   
        return tco1, latency, labor_cost1, tco2, latency2, labor_cost2
    else: 
        error_occurred = True
        raise gr.Error("Please select two AI service options.")

def update_plot(tco1, tco2, dropdown, dropdown2, labour_cost1, labour_cost2):
    if error_occurred == False:
        request_ranges = list(range(0, 1001, 100)) + list(range(1000, 10001, 500)) + list(range(10000, 100001, 1000)) + list(range(100000, 1600001, 100000))
        costs_tco1 = [(tco1 * req + labour_cost1) for req in request_ranges]
        costs_tco2 = [(tco2 * req + labour_cost2) for req in request_ranges]

        data = pd.DataFrame({
            "Number of requests": request_ranges * 2,
            "Cost (€)": costs_tco1 + costs_tco2,
            "AI model service": ["1)" + " " + dropdown] * len(request_ranges) + ["2)" + " " + dropdown2] * len(request_ranges)
            }
        )
        return gr.LinePlot.update(data, visible=True, x="Number of requests", y="Cost ($)", color="AI model service", color_legend_title=" ", color_legend_position="right", title="TCO for one month", height=300, width=500, tooltip=["Number of requests", "Cost ($)", "AI model service"])
    else:
        return ""

error_occurred = False