import pandas as pd import gradio as gr 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 {1/r:.5f} times more expensive than the one of the first {dropdown} service.""" elif r > 1: comparison_result = f"""The cost/request of the second {dropdown2} service is {r:.5f} times cheaper than the one of the first {dropdown} service.""" else: comparison_result = f"""Both solutions have the same cost/request.""" # 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"