-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAIA-consumer-rights.py
289 lines (243 loc) · 10.6 KB
/
AIA-consumer-rights.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import glob
import yaml
import random
import pandas as pd
import abstra.forms as af
import plotly.graph_objects as go
from plotly.subplots import make_subplots
# Get a list of all the file paths that ends with .yaml from in specified directory
# These are the questions that will be used in the survey
fileList = glob.glob('./AIA/consumer-rights/**.yaml')
# Sort the list of filepaths by number
fileList.sort(key=lambda x: int(x.split('question')[-1].split('.')[0]))
# Initialize a pandas.DataFrame
df = pd.DataFrame(
columns=['question', 'multiple', 'options', 'scores', 'accountability', 'transparency', 'truthfulness', "information"]
)
# Iterate over the list of filepaths
for filePath in fileList:
with open(filePath) as file:
# Read the current yaml file
documents = yaml.full_load(file)
# Concatenate the current yaml file to the DataFrame
df = pd.concat([df, pd.DataFrame.from_dict(documents, orient='index').T], ignore_index=True)
# Create a new column with the maximum impact increase and decrease
# These values represent the maximum impact that the application can have on society,
# together with the maximal decrease in impact that the application can receive
df["max_impact_increase"] = df.scores.apply(lambda x: sum(num for num in x if num > 0))
df["max_impact_decrease"] = df.scores.apply(lambda x: sum(num for num in x if num < 0))
# We filter the `max_impact_increase` and `max_impact_decrease` to get only the
# correct scores, in relation to the `multiple` column
mask = df['multiple'] == False
df.loc[mask, 'max_impact_increase'] = df[mask]['scores'].apply(max)
df.loc[mask, 'max_impact_decrease'] = df[mask]['scores'].apply(min)
# We initialize the scores for each principle
score_accountability = 0
score_transparency = 0
score_truthfulness = 0
# We initialize the maximum scores for each principle
max_accountability = df[df['accountability'] == True].max_impact_increase.sum()
max_transparency = df[df['transparency'] == True].max_impact_increase.sum()
max_truthfulness = df[df['truthfulness'] == True].max_impact_increase.sum()
# Render Intro page
intro = (
af.Page()
.display_html(
"""
<style>
h1, h2 {
text-align: center;
}
p {
text-align: justify;
}
</style>
<h1>Algorithmic Impact Assessment</h1>
<h2><em>Consumer Rights</em></h2>
<p>This <strong>Algorithmic Impact Assessment (AIA)</strong> is a tool that seeks to attribute the impact that the \
application could have on consumers, taking as a point of reference the ethical principles of <em>Accountability</em>, \
<em>Transparency</em>, and <em>Truthfulness</em> as points of reference.</p>
<ul>
<li><strong>Accountability:</strong> "<em>Accountability refers to the idea that AI technology developers and \
deployers should comply with regulatory bodies. These actors should also be accountable for their actions and \
the impacts caused by their technologies</em>."</li><br>
<li><strong>Transparency:</strong> "<em>This principle supports the idea that the use and development of AI \
technologies should be transparent for all interested stakeholders. Transparency can be related to \
'the transparency of an organization' or 'the transparency of an algorithm.' Transparency is also \
related to the idea that such information should be understandable to nonexperts and, when necessary, \
subject to auditing</em>."</li><br>
<li><strong>Truthfulness:</strong> "<em>This principle upholds the idea that AI technologies must provide truthful\
information. It is also related to the idea that people should not be deceived when interacting\
with AI systems</em>."</li><br>
</ul>
<p>To access more definitions, visit <a href="https://nkluge-correa.github.io/worldwide_AI-ethics/" \
target="_blank">Worldwide AI Ethics</a>.</p>"""
).run("Next")
)
# Loop through the questions and render the pages
pages = [
af.Page()
.display_html(
f"""
<style>
h2, p {"{text-align: center;}"}
</style>
<h2>{df.question[i]}</h2>""")
.read_cards(
label=f"",
options=[{"description": x} for x in df.options[i]],
multiple=True if df.multiple[i] else False,
required=True,
initial_value=random.choice([{"description": x} for x in df.options[i]]),
hint=df.information[i] if df.information[i] else None,
)
.display_progress(i+1, len(df), text=F"")
.run("Next")
for i in range(len(df))
]
# Loop through the pages and calculate the scores
for i, page in enumerate(pages):
# Get the row from the DataFrame that matches the question
temp_df = df[df['question'] == df.question[i]]
# Create a hash map from the options and scores
hash_map = dict(zip(temp_df.options.iloc[0], temp_df.scores.iloc[0]))
try:
# For questions that are not multiple choice, we don't need to iterate
# over the list, since it only contains one dictionary
if temp_df.accountability.iloc[0]:
score_accountability += hash_map[page['']['description']]
if temp_df.transparency.iloc[0]:
score_transparency += hash_map[page['']['description']]
if temp_df.truthfulness.iloc[0]:
score_truthfulness += hash_map[page['']['description']]
except:
# For a list, we iterate ovver every dictionary in the list and
# associate the score to the description, adding it to the total score
for d in page['']:
if temp_df.accountability.iloc[0]:
score_accountability += hash_map[d['description']]
if temp_df.transparency.iloc[0]:
score_transparency += hash_map[d['description']]
if temp_df.truthfulness.iloc[0]:
score_truthfulness += hash_map[d['description']]
# We make sure that the scores are not negative
score_accountability = score_accountability if score_accountability > 0 else 0
score_transparency = score_transparency if score_transparency > 0 else 0
score_truthfulness = score_truthfulness if score_truthfulness > 0 else 0
# We create the indicator plots for each principle
fig = make_subplots(
rows=1, cols=3,
specs=[[{"type": "indicator"}, {"type": "indicator"}, {"type": "indicator"}]],
subplot_titles=("<b>Impact on Accountability</b>","<b>Impact on Transparency</b>", "<b>Impact on Truthfulness</b>"),
horizontal_spacing = 0.10,
)
fig.add_trace(go.Indicator(
mode = "gauge+number",
value = (score_accountability/max_accountability) * 100,
domain = {'x': [0, 1], 'y': [0, 1]},
gauge = {
'axis': {'range': [None, 100], 'tickwidth': 1, 'tickcolor': "black"},
'bar': {'color': "slategray"},
'bgcolor': "white",
'borderwidth': 2,
'bordercolor': "gray",
'steps': [
{'range': [0, 30], 'color': 'yellowgreen'},
{'range': [30, 70], 'color': 'yellow'},
{'range': [70, 100], 'color': 'tomato'}],
'threshold': {
'line': {'color': "black", 'width': 4},
'thickness': 0.75,
'value': (score_accountability/max_accountability) * 100}}),
row=1, col=1)
fig.add_trace(go.Indicator(
mode = "gauge+number",
value = (score_transparency/max_transparency) * 100,
domain = {'x': [0, 1], 'y': [0, 1]},
gauge = {
'axis': {'range': [None, 100], 'tickwidth': 1, 'tickcolor': "black"},
'bar': {'color': "slategray"},
'bgcolor': "white",
'borderwidth': 2,
'bordercolor': "gray",
'steps': [
{'range': [0, 30], 'color': 'yellowgreen'},
{'range': [30, 70], 'color': 'yellow'},
{'range': [70, 100], 'color': 'tomato'}],
'threshold': {
'line': {'color': "black", 'width': 4},
'thickness': 0.75,
'value': (score_transparency/max_transparency) * 100}}),
row=1, col=2)
fig.add_trace(go.Indicator(
mode = "gauge+number",
value = (score_truthfulness/max_truthfulness) * 100,
domain = {'x': [0, 1], 'y': [0, 1]},
gauge = {
'axis': {'range': [None, 100], 'tickwidth': 1, 'tickcolor': "black"},
'bar': {'color': "slategray"},
'bgcolor': "white",
'borderwidth': 2,
'bordercolor': "gray",
'steps': [
{'range': [0, 30], 'color': 'yellowgreen'},
{'range': [30, 70], 'color': 'yellow'},
{'range': [70, 100], 'color': 'tomato'}],
'threshold': {
'line': {'color': "black", 'width': 4},
'thickness': 0.75,
'value': (score_truthfulness/max_truthfulness) * 100}}),
row=1, col=3)
fig.update_layout(
paper_bgcolor='rgba(0,0,0,0)',
plot_bgcolor='rgba(0,0,0,0)',
template='plotly_white',
font = {'color': "black", 'family': "Arial"})
fig_total = go.Figure(go.Indicator(
mode = "gauge+number",
value = (((score_accountability/max_accountability) +
(score_transparency/max_transparency) +
(score_truthfulness/max_truthfulness))/ 3) * 100,
domain = {'x': [0, 1], 'y': [0, 1]},
gauge = {
'axis': {'range': [None, 100], 'tickwidth': 1, 'tickcolor': "black"},
'bar': {'color': "slategray"},
'bgcolor': "white",
'borderwidth': 2,
'bordercolor': "gray",
'steps': [
{'range': [0, 30], 'color': 'yellowgreen'},
{'range': [30, 70], 'color': 'yellow'},
{'range': [70, 100], 'color': 'tomato'}],
'threshold': {
'line': {'color': "black", 'width': 4},
'thickness': 0.75,
'value': (((score_accountability/max_accountability) +
(score_transparency/max_transparency) +
(score_truthfulness/max_truthfulness))/ 3) * 100}}))
fig_total.update_layout(
paper_bgcolor='rgba(0,0,0,0)',
plot_bgcolor='rgba(0,0,0,0)',
template='plotly_white',
title=dict(text='<b>Total Impact Score (Consumer Rights)</b>', x=0.5, y=0.9, font={'size': 24}),
font = {'color': "black", 'family': "Arial"})
# Render the results page ...
result = (
af.Page()
.display_html(
f"""
<style>
h1, h2, p {"{text-align: center;}"}
</style>
<h1>Algorithmic Impact Assessment</h1>
<h2><em>Results</em></h2>
<hr>
<p>This report contains the risk assessment carried out by our tool. Our assessment consists of a set of \
risk indicators relating to the three principles assessed in the survey: <strong>Accountability</strong>, \
<strong>Transparency</strong>, and <strong>Truthfulness</strong>.</p>
<p>The result below is an estimate of the relative risk of your application (Scores range from 0 to 100).</p>"""
)
.display_plotly(fig, full_width=True)
.display_plotly(fig_total, full_width=False)
.run("Finish")
)