import streamlit as st st.set_page_config( page_title="Holistic AI - LLM Risks", page_icon="👋", layout='wide' ) import json import os from huggingface_hub import HfApi, login from streamlit_cookies_manager import EncryptedCookieManager import re def program(): dataset_name = "holistic-ai/LLM-Risks" token = os.getenv("HF_TOKEN") api = HfApi() login(token) repo_path = api.snapshot_download(repo_id=dataset_name, repo_type="dataset") with open(f'{repo_path}/risk_annotation_consolidated.json') as file: data = json.load(file) task_names = list(set([item['task'] for item in data])) def camel_to_whitespace(camel_str): spaced_str = re.sub(r'([A-Z])', r' \1', camel_str).lower() spaced_str = spaced_str.strip().title() return spaced_str task_2_task_string = {task: camel_to_whitespace(task) for task in task_names} task_string_2_task = {task_string:task for task,task_string in task_2_task_string.items()} task_strings = [task_2_task_string[t] for t in task_names] # Sidebar filters with st.sidebar: st.sidebar.image("hai_logo.png", width=150, use_column_width=True) st.header("Filters") # Extract unique task names and groups selected_task_string = st.selectbox("Select a Task", task_strings) selected_task = task_string_2_task[selected_task_string] # Filter data based on selected task filtered_data_by_task = [item for item in data if item['task'] == selected_task] groups = list(set([item['group'] for item in filtered_data_by_task])) selected_group = st.selectbox("Select a Risk Group", groups) # Filter data based on selected group filtered_data_by_group = [item for item in filtered_data_by_task if item['group'] == selected_group] st.divider() st.sidebar.markdown(f"**Task**: {selected_task_string}") st.sidebar.markdown(f"**Risk Group**: {selected_group}") # CSS for reducing the vertical spacing between
tags, justifying text, and ensuring equal height cards st.markdown(""" """, unsafe_allow_html=True) sidebar_style = """ """ # Aplica el estilo al sidebar st.markdown(sidebar_style, unsafe_allow_html=True) #st.title("LLM Risks and Mitigators") tabs = st.tabs(["Examples", "Mitigators"]) with tabs[0]: # Display the filtered news as a grid of cards if len(filtered_data_by_group) > 0: for risk in set([item['risk'] for item in filtered_data_by_group]): item = [item for item in filtered_data_by_group if item['risk'] == risk][0] st.header(risk) st.write(f"Risk Description: {item['description']}") # Define the number of columns num_columns = 3 col_index = 0 # Create an empty container for the grid grid = st.container() # Initialize an empty row row = grid.columns(num_columns) for news in item['examples']: with row[col_index]: st.markdown( f"""