import gradio as gr import pandas as pd from datasets import load_dataset # Initialize total counts total_yes_count = 0 total_count = 0 def calculate_percentage(*answers): """ Calculate the percentage of 'yes' answers. :param answers: Iterable containing the answers. :returns: Percentage of 'yes' answers. """ global total_yes_count, total_count yes_count = sum(answers) total_yes_count += yes_count total_count += len(answers) percentage = yes_count / len(answers) * 100 return f"{percentage}%" def calculate_overall_percentage(): """ Calculate the overall percentage of 'yes' answers. :returns: Overall percentage of 'yes' answers. """ global total_yes_count, total_count if total_count != 100: return "Make sure you have submitted your answers in all the tabs." overall_percentage = total_yes_count return f"{overall_percentage}%" # Load data dataset = load_dataset("mariagrandury/fmti-indicators") df = pd.DataFrame(dataset["train"]) grouped = df.groupby(["Domain", "Subdomain"]) # Create an interface per group of indicators interfaces = [] tab_names = [] for (domain, subdomain), group in grouped: questions = group["Definition"].tolist() inputs = [gr.Checkbox(label=question) for question in questions] output = gr.Textbox(label="Subdomain Percentage") iface = gr.Interface( fn=calculate_percentage, inputs=inputs, outputs=output, title=f"{domain} - {subdomain}", allow_flagging="never", ) interfaces.append(iface) tab_names.append(subdomain) # Add overall percentage button overall_button = gr.Interface( fn=calculate_overall_percentage, inputs=[], outputs=gr.Textbox(label="Overall Percentage"), title="Transparency Score", allow_flagging="never", ) interfaces.append(overall_button) tab_names.append("Total Transparency Score") # Create the tabbed interface tabbed_interface = gr.TabbedInterface( interface_list=interfaces, tab_names=tab_names, title="The Foundation Model Transparency Index", ) tabbed_interface.launch()