prakharg24 commited on
Commit
1d6b210
·
verified ·
1 Parent(s): f5bf857

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +169 -105
app.py CHANGED
@@ -2,29 +2,88 @@ import streamlit as st
2
 
3
  st.set_page_config(layout="wide")
4
 
5
- st.title("AI Model Fairness Auditing")
 
 
6
 
7
- # -------------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  # Strategy Definitions
9
- # -------------------------
10
 
11
  model_owner_actions = {
12
- "Fine-tuning / overfitting on auditing test": {"cheating": 25, "cost": 0, "openness": -5},
13
- "Fine-tuning on auditing-like data distribution": {"cheating": 20, "cost": 0, "openness": -3},
14
- "Training a special audit-behavior mode": {"cheating": 30, "cost": 5, "openness": -10},
15
- "Withholding training details": {"cheating": 15, "cost": 0, "openness": -15}
16
  }
17
 
18
  auditor_actions = {
19
- "Audit on one dataset": {"cheating": 20, "cost": 10, "openness": 5},
20
- "Audit on multiple datasets": {"cheating": -15, "cost": 25, "openness": 10},
21
- "Request internal model access": {"cheating": -25, "cost": 30, "openness": 30},
22
- "Request training data documentation": {"cheating": -10, "cost": 5, "openness": 20}
23
  }
24
 
25
- # -------------------------
26
  # Session State
27
- # -------------------------
28
 
29
  if "owner_selected" not in st.session_state:
30
  st.session_state.owner_selected = []
@@ -32,133 +91,138 @@ if "owner_selected" not in st.session_state:
32
  if "auditor_selected" not in st.session_state:
33
  st.session_state.auditor_selected = []
34
 
35
- # -------------------------
36
- # Toggle functions
37
- # -------------------------
38
 
39
- def toggle_owner(action):
40
- if action in st.session_state.owner_selected:
41
- st.session_state.owner_selected.remove(action)
42
  else:
43
- st.session_state.owner_selected.append(action)
44
 
45
- def toggle_auditor(action):
46
- if action in st.session_state.auditor_selected:
47
- st.session_state.auditor_selected.remove(action)
48
  else:
49
- st.session_state.auditor_selected.append(action)
50
 
51
- # -------------------------
52
  # Layout
53
- # -------------------------
54
 
55
  left, right = st.columns(2)
56
 
57
- # -------------------------
58
- # Model Owner Panel
59
- # -------------------------
60
 
61
  with left:
62
- st.header("Model Owner")
63
-
64
- st.subheader("Selected Actions")
65
 
66
- selected_box = st.container(border=True)
67
-
68
- with selected_box:
69
- for action in st.session_state.owner_selected:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  st.button(
71
- action,
72
- key=f"owner_selected_{action}",
73
  on_click=toggle_owner,
74
  args=(action,),
75
- use_container_width=True
76
  )
77
 
78
- st.subheader("Available Actions")
79
-
80
- available_box = st.container(border=True)
81
-
82
- with available_box:
83
- for action in model_owner_actions:
84
- if action not in st.session_state.owner_selected:
85
- st.button(
86
- action,
87
- key=f"owner_available_{action}",
88
- on_click=toggle_owner,
89
- args=(action,),
90
- use_container_width=True
91
- )
92
-
93
- # -------------------------
94
- # Auditor Panel
95
- # -------------------------
96
 
97
  with right:
98
- st.header("Auditor")
99
-
100
- st.subheader("Selected Actions")
101
 
102
- selected_box = st.container(border=True)
103
-
104
- with selected_box:
105
- for action in st.session_state.auditor_selected:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
  st.button(
107
- action,
108
- key=f"auditor_selected_{action}",
109
  on_click=toggle_auditor,
110
  args=(action,),
111
- use_container_width=True
112
  )
113
 
114
- st.subheader("Available Actions")
115
-
116
- available_box = st.container(border=True)
117
-
118
- with available_box:
119
- for action in auditor_actions:
120
- if action not in st.session_state.auditor_selected:
121
- st.button(
122
- action,
123
- key=f"auditor_available_{action}",
124
- on_click=toggle_auditor,
125
- args=(action,),
126
- use_container_width=True
127
- )
128
-
129
- # -------------------------
130
  # Score Calculation
131
- # -------------------------
132
 
133
- cheating_score = 50
134
- audit_cost = 0
135
- openness_required = 0
136
 
137
- for action in st.session_state.owner_selected:
138
- cheating_score += model_owner_actions[action]["cheating"]
139
- audit_cost += model_owner_actions[action]["cost"]
140
- openness_required += model_owner_actions[action]["openness"]
141
 
142
- for action in st.session_state.auditor_selected:
143
- cheating_score += auditor_actions[action]["cheating"]
144
- audit_cost += auditor_actions[action]["cost"]
145
- openness_required += auditor_actions[action]["openness"]
146
 
147
- cheating_score = max(0, min(100, cheating_score))
148
- openness_required = max(0, openness_required)
149
 
150
- # -------------------------
151
  # Dashboard
152
- # -------------------------
153
 
154
  st.divider()
155
 
156
- st.subheader("Audit Outcome Dashboard")
 
 
 
157
 
158
- col1, col2, col3 = st.columns(3)
 
159
 
160
- col1.metric("Chance of Cheating", f"{cheating_score}%")
161
- col2.metric("Audit Cost", audit_cost)
162
- col3.metric("Openness Required", openness_required)
163
 
164
- st.progress(cheating_score / 100)
 
2
 
3
  st.set_page_config(layout="wide")
4
 
5
+ # --------------------------------------------------
6
+ # Custom Styling
7
+ # --------------------------------------------------
8
 
9
+ st.markdown("""
10
+ <style>
11
+
12
+ @import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600&display=swap');
13
+
14
+ html, body, [class*="css"] {
15
+ font-family: 'Inter', sans-serif;
16
+ }
17
+
18
+ /* title */
19
+ .title {
20
+ font-size:40px;
21
+ font-weight:600;
22
+ margin-bottom:25px;
23
+ }
24
+
25
+ /* card containers */
26
+ .card-box {
27
+ padding:10px;
28
+ border-radius:12px;
29
+ margin-bottom:10px;
30
+ }
31
+
32
+ /* available actions */
33
+ .available {
34
+ background:#f4f6fb;
35
+ border:1px solid #d8dbea;
36
+ }
37
+
38
+ /* selected actions */
39
+ .selected {
40
+ background:#e8f7ee;
41
+ border:1px solid #8bd6a3;
42
+ }
43
+
44
+ /* card text */
45
+ .card-text {
46
+ font-size:16px;
47
+ padding:8px;
48
+ }
49
+
50
+ .metric-card {
51
+ background:#f9fafc;
52
+ border-radius:12px;
53
+ padding:20px;
54
+ text-align:center;
55
+ }
56
+
57
+ </style>
58
+ """, unsafe_allow_html=True)
59
+
60
+ # --------------------------------------------------
61
+ # Title
62
+ # --------------------------------------------------
63
+
64
+ st.markdown('<div class="title">AI Model Fairness Auditing</div>', unsafe_allow_html=True)
65
+
66
+ # --------------------------------------------------
67
  # Strategy Definitions
68
+ # --------------------------------------------------
69
 
70
  model_owner_actions = {
71
+ "Fine-tuning / overfitting on auditing test": {"cheating": 25, "cost": 0, "open": -5},
72
+ "Fine-tuning on auditing-like distribution": {"cheating": 20, "cost": 0, "open": -3},
73
+ "Training special audit behaviour": {"cheating": 30, "cost": 5, "open": -10},
74
+ "Withholding training details": {"cheating": 15, "cost": 0, "open": -15},
75
  }
76
 
77
  auditor_actions = {
78
+ "Audit on one dataset": {"cheating": 20, "cost": 10, "open": 5},
79
+ "Audit on multiple datasets": {"cheating": -15, "cost": 25, "open": 10},
80
+ "Request internal model access": {"cheating": -25, "cost": 30, "open": 30},
81
+ "Request training documentation": {"cheating": -10, "cost": 5, "open": 20},
82
  }
83
 
84
+ # --------------------------------------------------
85
  # Session State
86
+ # --------------------------------------------------
87
 
88
  if "owner_selected" not in st.session_state:
89
  st.session_state.owner_selected = []
 
91
  if "auditor_selected" not in st.session_state:
92
  st.session_state.auditor_selected = []
93
 
94
+ # --------------------------------------------------
95
+ # Toggle Functions
96
+ # --------------------------------------------------
97
 
98
+ def toggle_owner(a):
99
+ if a in st.session_state.owner_selected:
100
+ st.session_state.owner_selected.remove(a)
101
  else:
102
+ st.session_state.owner_selected.append(a)
103
 
104
+ def toggle_auditor(a):
105
+ if a in st.session_state.auditor_selected:
106
+ st.session_state.auditor_selected.remove(a)
107
  else:
108
+ st.session_state.auditor_selected.append(a)
109
 
110
+ # --------------------------------------------------
111
  # Layout
112
+ # --------------------------------------------------
113
 
114
  left, right = st.columns(2)
115
 
116
+ # --------------------------------------------------
117
+ # MODEL OWNER
118
+ # --------------------------------------------------
119
 
120
  with left:
 
 
 
121
 
122
+ st.markdown("### Model Owner")
123
+
124
+ st.markdown("**Chosen**")
125
+
126
+ for action in st.session_state.owner_selected:
127
+ st.markdown(
128
+ f'<div class="card-box selected"><div class="card-text">{action}</div></div>',
129
+ unsafe_allow_html=True,
130
+ )
131
+ st.button(
132
+ "toggle",
133
+ key=f"owner_sel_{action}",
134
+ on_click=toggle_owner,
135
+ args=(action,),
136
+ )
137
+
138
+ st.markdown("**Available**")
139
+
140
+ for action in model_owner_actions:
141
+ if action not in st.session_state.owner_selected:
142
+ st.markdown(
143
+ f'<div class="card-box available"><div class="card-text">{action}</div></div>',
144
+ unsafe_allow_html=True,
145
+ )
146
  st.button(
147
+ "choose",
148
+ key=f"owner_av_{action}",
149
  on_click=toggle_owner,
150
  args=(action,),
 
151
  )
152
 
153
+ # --------------------------------------------------
154
+ # AUDITOR
155
+ # --------------------------------------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
156
 
157
  with right:
 
 
 
158
 
159
+ st.markdown("### Auditor")
160
+
161
+ st.markdown("**Chosen**")
162
+
163
+ for action in st.session_state.auditor_selected:
164
+ st.markdown(
165
+ f'<div class="card-box selected"><div class="card-text">{action}</div></div>',
166
+ unsafe_allow_html=True,
167
+ )
168
+ st.button(
169
+ "toggle",
170
+ key=f"aud_sel_{action}",
171
+ on_click=toggle_auditor,
172
+ args=(action,),
173
+ )
174
+
175
+ st.markdown("**Available**")
176
+
177
+ for action in auditor_actions:
178
+ if action not in st.session_state.auditor_selected:
179
+ st.markdown(
180
+ f'<div class="card-box available"><div class="card-text">{action}</div></div>',
181
+ unsafe_allow_html=True,
182
+ )
183
  st.button(
184
+ "choose",
185
+ key=f"aud_av_{action}",
186
  on_click=toggle_auditor,
187
  args=(action,),
 
188
  )
189
 
190
+ # --------------------------------------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
191
  # Score Calculation
192
+ # --------------------------------------------------
193
 
194
+ cheating = 50
195
+ cost = 0
196
+ open_req = 0
197
 
198
+ for a in st.session_state.owner_selected:
199
+ cheating += model_owner_actions[a]["cheating"]
200
+ cost += model_owner_actions[a]["cost"]
201
+ open_req += model_owner_actions[a]["open"]
202
 
203
+ for a in st.session_state.auditor_selected:
204
+ cheating += auditor_actions[a]["cheating"]
205
+ cost += auditor_actions[a]["cost"]
206
+ open_req += auditor_actions[a]["open"]
207
 
208
+ cheating = max(0, min(100, cheating))
209
+ open_req = max(0, open_req)
210
 
211
+ # --------------------------------------------------
212
  # Dashboard
213
+ # --------------------------------------------------
214
 
215
  st.divider()
216
 
217
+ c1, c2, c3 = st.columns(3)
218
+
219
+ with c1:
220
+ st.metric("Cheating Chance", f"{cheating}%")
221
 
222
+ with c2:
223
+ st.metric("Audit Cost", cost)
224
 
225
+ with c3:
226
+ st.metric("Openness Required", open_req)
 
227
 
228
+ st.progress(cheating / 100)