crismunoz commited on
Commit
a4eb35d
1 Parent(s): 47d88fd

first commit

Browse files
Files changed (2) hide show
  1. app.py +248 -0
  2. hai_logo.png +0 -0
app.py ADDED
@@ -0,0 +1,248 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import streamlit as st
3
+ import os
4
+ from huggingface_hub import HfApi, login
5
+ from streamlit_cookies_manager import EncryptedCookieManager
6
+ import re
7
+
8
+ st.set_page_config(
9
+ page_title="Holistic AI - LLM Risks",
10
+ page_icon="👋",
11
+ layout='wide'
12
+ )
13
+
14
+ def program():
15
+ dataset_name = "holistic-ai/LLM-Risks"
16
+ token = os.getenv("HF_TOKEN")
17
+
18
+ api = HfApi()
19
+ login(token)
20
+
21
+ repo_path = api.snapshot_download(repo_id=dataset_name, repo_type="dataset")
22
+
23
+ with open(f'{repo_path}/risk_annotation_consolidated.json') as file:
24
+ data = json.load(file)
25
+
26
+ task_names = list(set([item['task'] for item in data]))
27
+
28
+ def camel_to_whitespace(camel_str):
29
+ spaced_str = re.sub(r'([A-Z])', r' \1', camel_str).lower()
30
+ spaced_str = spaced_str.strip().title()
31
+ return spaced_str
32
+
33
+ task_2_task_string = {task: camel_to_whitespace(task) for task in task_names}
34
+ task_string_2_task = {task_string:task for task,task_string in task_2_task_string.items()}
35
+ task_strings = [task_2_task_string[t] for t in task_names]
36
+
37
+
38
+ # Sidebar filters
39
+ with st.sidebar:
40
+ st.sidebar.image("hai_logo.png", width=150, use_column_width=True)
41
+ st.header("Filters")
42
+ # Extract unique task names and groups
43
+
44
+ selected_task_string = st.selectbox("Select a Task", task_strings)
45
+ selected_task = task_string_2_task[selected_task_string]
46
+
47
+
48
+ # Filter data based on selected task
49
+ filtered_data_by_task = [item for item in data if item['task'] == selected_task]
50
+
51
+ groups = list(set([item['group'] for item in filtered_data_by_task]))
52
+ selected_group = st.selectbox("Select a Risk Group", groups)
53
+
54
+ # Filter data based on selected group
55
+ filtered_data_by_group = [item for item in filtered_data_by_task if item['group'] == selected_group]
56
+
57
+ st.divider()
58
+
59
+ st.sidebar.markdown(f"**Task**: {selected_task_string}")
60
+ st.sidebar.markdown(f"**Risk Group**: {selected_group}")
61
+ # CSS for reducing the vertical spacing between <p> tags, justifying text, and ensuring equal height cards
62
+ st.markdown("""
63
+ <style>
64
+ .card {
65
+ border: 1px solid #ddd;
66
+ border-radius: 10px;
67
+ padding: 10px;
68
+ margin: 10px;
69
+ height: 100%;
70
+ display: flex;
71
+ flex-direction: column;
72
+ justify-content: space-between;
73
+ box-sizing: border-box;
74
+ background-color: #e4e8f5;
75
+ }
76
+ .card h3 {
77
+ margin-top: 0;
78
+ background-color: #e4e8f5;
79
+ }
80
+ .card p {
81
+ margin: 2px 0;
82
+ padding: 0;
83
+ text-align: justify;
84
+ background-color: #e4e8f5;
85
+ }
86
+ .stApp {
87
+ max-width: 100%;
88
+ padding: 1rem;
89
+ }
90
+ .grid {
91
+ display: flex;
92
+ flex-wrap: wrap;
93
+ justify-content: space-between;
94
+ }
95
+ .grid-item {
96
+ flex: 1 0 23%; /* 4 items per row */
97
+ box-sizing: border-box;
98
+ margin: 1%;
99
+ display: flex;
100
+ }
101
+ .grid-item .card {
102
+ flex: 1;
103
+ display: flex;
104
+ flex-direction: column;
105
+ justify-content: space-between;
106
+ background-color: #e4e8f5;
107
+ }
108
+ @media (max-width: 1200px) {
109
+ .grid-item {
110
+ flex: 1 0 46%; /* 2 items per row */
111
+ }
112
+ }
113
+ @media (max-width: 768px) {
114
+ .grid-item {
115
+ flex: 1 0 96%; /* 1 item per row */
116
+ }
117
+ }
118
+ </style>
119
+ """, unsafe_allow_html=True)
120
+
121
+ sidebar_style = """
122
+ <style>
123
+ [data-testid="stSidebar"] {
124
+ background-color: white;
125
+ }
126
+ </style>
127
+ """
128
+
129
+ # Aplica el estilo al sidebar
130
+ st.markdown(sidebar_style, unsafe_allow_html=True)
131
+
132
+ #st.title("LLM Risks and Mitigators")
133
+
134
+
135
+ tabs = st.tabs(["Examples", "Mitigators"])
136
+
137
+ with tabs[0]:
138
+ # Display the filtered news as a grid of cards
139
+ if len(filtered_data_by_group) > 0:
140
+ for risk in set([item['risk'] for item in filtered_data_by_group]):
141
+ st.header(risk)
142
+ # Filter data based on current risk
143
+ filtered_data_by_risk = [item for item in filtered_data_by_group if item['risk'] == risk]
144
+
145
+ # Define the number of columns
146
+ num_columns = 3
147
+ col_index = 0
148
+
149
+ # Create an empty container for the grid
150
+ grid = st.container()
151
+
152
+ # Initialize an empty row
153
+ row = grid.columns(num_columns)
154
+
155
+ for item in filtered_data_by_risk:
156
+ for news in item['examples']:
157
+ with row[col_index]:
158
+ st.markdown(
159
+ f"""
160
+ <div class="grid-item">
161
+ <div class="card">
162
+ <h3>{news['title']}</h3>
163
+ <p>{news['incident']}</p>
164
+ <a href="{news['link']}" target="_blank">Read more</a>
165
+ </div>
166
+ </div>
167
+ """,
168
+ unsafe_allow_html=True
169
+ )
170
+ col_index = (col_index + 1) % num_columns
171
+ # Start a new row after the last column
172
+ if col_index == 0:
173
+ row = grid.columns(num_columns)
174
+
175
+ if len(filtered_data_by_group) == 0:
176
+ st.write("No news found for the selected task and group.")
177
+
178
+ with tabs[1]:
179
+ # Display the filtered news as a grid of cards
180
+ if len(filtered_data_by_group) > 0:
181
+ for risk in set([item['risk'] for item in filtered_data_by_group]):
182
+ st.header(risk)
183
+ # Filter data based on current risk
184
+ filtered_data_by_risk = [item for item in filtered_data_by_group if item['risk'] == risk]
185
+
186
+ # Define the number of columns
187
+ num_columns = 3
188
+ col_index = 0
189
+
190
+ # Create an empty container for the grid
191
+ grid = st.container()
192
+
193
+ # Initialize an empty row
194
+ row = grid.columns(num_columns)
195
+
196
+ for item in filtered_data_by_risk:
197
+ for news in item['mitigators']:
198
+ with row[col_index]:
199
+ st.markdown(
200
+ f"""
201
+ <div class="grid-item">
202
+ <div class="card">
203
+ <h3>{news['title']}</h3>
204
+ <p>{news['recommendation']}</p>
205
+ <p><b>Year:</b> {news['year']}</p>
206
+ <a href="{news['link']}" target="_blank">Read more</a>
207
+ </div>
208
+ </div>
209
+ """,
210
+ unsafe_allow_html=True
211
+ )
212
+ col_index = (col_index + 1) % num_columns
213
+ # Start a new row after the last column
214
+ if col_index == 0:
215
+ row = grid.columns(num_columns)
216
+
217
+ if len(filtered_data_by_group) == 0:
218
+ st.write("No news found for the selected task and group.")
219
+
220
+ SECRET_KEY = os.getenv('SECRET_KEY')
221
+
222
+ cookies = EncryptedCookieManager(
223
+ prefix="login",
224
+ password=os.getenv('COOKIES_PASSWORD')
225
+ )
226
+
227
+ if not cookies.ready():
228
+ st.stop()
229
+
230
+ def main():
231
+ # Título de la aplicación
232
+ st.title("LLM Mitigation")
233
+
234
+ if not cookies.get("authenticated"):
235
+ # Entrada de la clave secreta
236
+ user_key = st.text_input("Password:", type="password")
237
+
238
+ if st.button("Login"):
239
+ # Verificar si la clave ingresada coincide con la clave secreta
240
+ if user_key == SECRET_KEY:
241
+ cookies.__setitem__("authenticated", "True")
242
+ st.experimental_rerun()
243
+ else:
244
+ st.error("Acceso denegado. Clave incorrecta.")
245
+ else:
246
+ program()
247
+ if __name__ == "__main__":
248
+ main()
hai_logo.png ADDED