-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
357 lines (292 loc) · 11.7 KB
/
app.py
File metadata and controls
357 lines (292 loc) · 11.7 KB
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
import os
import numpy as np
from flask import Flask, render_template, request, jsonify, session, send_from_directory
from flask_cors import CORS
from werkzeug.utils import secure_filename
from services.parser_service import parse_shortlist_request
from services.shortlist_service import run_shortlist
from utils.file_loader import load_and_clean_csv
app = Flask(__name__)
CORS(app)
app.secret_key = "fairness-audit-key-2024"
app.config["UPLOAD_FOLDER"] = os.path.join(os.path.dirname(__file__), "uploads")
app.config["MAX_CONTENT_LENGTH"] = 50 * 1024 * 1024 # 50MB
os.makedirs(app.config["UPLOAD_FOLDER"], exist_ok=True)
# ───────────── ROUTES ─────────────
@app.route("/")
def index():
return render_template("index.html")
@app.route("/sample/<filename>")
def sample_file(filename):
return send_from_directory(os.path.dirname(__file__), filename)
@app.route("/api/upload", methods=["POST"])
def upload():
if "file" not in request.files:
return jsonify({"error": "No file uploaded"}), 400
f = request.files["file"]
if f.filename == "":
return jsonify({"error": "Empty filename"}), 400
filename = secure_filename(f.filename)
filepath = os.path.join(app.config["UPLOAD_FOLDER"], filename)
f.save(filepath)
df = load_and_clean_csv(filepath)
columns = df.columns.tolist()
dtypes = {c: str(df[c].dtype) for c in columns}
preview = df.head(10).fillna("").to_dict(orient="records")
# Auto-detect dataset
dataset_name = "Unknown"
suggestions = {}
if "education.num" in columns:
dataset_name = "UCI Adult Income"
suggestions = {
"gender_col": "sex",
"target_col": "income",
"positive_val": ">50K",
"occupation_col": "occupation",
}
elif "Attrition" in columns:
dataset_name = "IBM HR Analytics"
suggestions = {
"gender_col": "Gender",
"target_col": "HighIncome",
"positive_val": "Yes",
"occupation_col": "JobRole",
}
return jsonify(
{
"filename": filename,
"filepath": filepath,
"columns": columns,
"dtypes": dtypes,
"rows": len(df),
"preview": preview,
"dataset_name": dataset_name,
"suggestions": suggestions,
}
)
@app.route("/api/unique_values", methods=["POST"])
def unique_values():
data = request.json
df = load_and_clean_csv(data["filepath"])
col = data["column"]
values = df[col].dropna().unique().tolist()
return jsonify({"values": values})
# ───────────── BIAS ANALYSIS (solution.py) ─────────────
@app.route("/api/bias_analysis", methods=["POST"])
def bias_analysis():
data = request.json
df = load_and_clean_csv(data["filepath"])
gender_col = data["gender_col"]
target_col = data["target_col"]
occupation_col = data["occupation_col"]
positive_outcome = data["positive_val"]
audit_df = df[[gender_col, target_col, occupation_col]].copy()
audit_df[occupation_col] = audit_df[occupation_col].replace(
"?", "Private/Undisclosed"
)
# Overall bias
groups = audit_df[gender_col].unique().tolist()
group_rates = {}
for group in groups:
subset = audit_df[audit_df[gender_col] == group]
rate = float((subset[target_col] == positive_outcome).mean())
group_rates[group] = round(rate, 4)
rates = list(group_rates.values())
di_score = round(min(rates) / max(rates), 4) if max(rates) > 0 else 0
parity = round(abs(rates[0] - rates[1]), 4) if len(rates) >= 2 else 0
if di_score >= 0.8:
di_verdict = "PASSES"
elif di_score >= 0.6:
di_verdict = "WARNING"
else:
di_verdict = "FAILS"
# Within-occupation bias
occupations = audit_df[occupation_col].unique().tolist()
biased_jobs = []
fair_jobs = []
skipped_jobs = []
for occ in occupations:
occ_subset = audit_df[audit_df[occupation_col] == occ]
occ_rates = {}
valid = True
for group in groups:
grp_subset = occ_subset[occ_subset[gender_col] == group]
if len(grp_subset) < 5:
valid = False
break
rate = float((grp_subset[target_col] == positive_outcome).mean())
occ_rates[group] = round(rate, 4)
if not valid:
skipped_jobs.append(occ)
continue
occ_rate_values = list(occ_rates.values())
occ_di = (
round(min(occ_rate_values) / max(occ_rate_values), 4)
if max(occ_rate_values) > 0
else 0
)
if occ_di < 0.8:
biased_jobs.append({"name": occ, "di_score": occ_di, "rates": occ_rates})
else:
fair_jobs.append({"name": occ, "di_score": occ_di, "rates": occ_rates})
# Conclusion
if di_score < 0.8 and len(biased_jobs) > 0:
conclusion = "SYSTEMIC GENDER BIAS DETECTED"
conclusion_detail = "Bias exists both overall AND within specific job roles. This is pure gender discrimination, not just occupational difference."
elif di_score < 0.8 and len(biased_jobs) == 0:
conclusion = "OCCUPATIONAL SEGREGATION"
conclusion_detail = "Overall bias exists BUT jobs individually are fair. Women may be concentrated in lower-paying job categories."
elif di_score >= 0.8 and len(biased_jobs) > 0:
conclusion = "HIDDEN BIAS IN SPECIFIC ROLES"
conclusion_detail = "Overall numbers look fair BUT specific jobs show bias. Needs targeted investigation."
else:
conclusion = "NO SIGNIFICANT BIAS DETECTED"
conclusion_detail = "System appears fair both overall and within occupations."
return jsonify(
{
"groups": groups,
"group_rates": group_rates,
"di_score": di_score,
"di_verdict": di_verdict,
"parity": parity,
"parity_pass": abs(parity) < 0.1,
"biased_jobs": sorted(biased_jobs, key=lambda x: x["di_score"]),
"fair_jobs": sorted(fair_jobs, key=lambda x: x["di_score"]),
"skipped_jobs": skipped_jobs,
"conclusion": conclusion,
"conclusion_detail": conclusion_detail,
"total_occupations": len(occupations),
}
)
# ───────────── REWEIGHTING (reweight.py) ─────────────
@app.route("/api/reweight", methods=["POST"])
def reweight():
data = request.json
df = load_and_clean_csv(data["filepath"])
gender_col = data["gender_col"]
income_col = data["target_col"]
positive_val = data["positive_val"]
scoring_weights = data.get("scoring_weights", {})
n = len(df)
p_gender = df[gender_col].value_counts() / n
p_income = df[income_col].value_counts() / n
p_joint = df.groupby([gender_col, income_col]).size() / n
def compute_fairness_weight(row):
g = row[gender_col]
y = row[income_col]
joint = p_joint.get((g, y), 1e-9)
return (p_gender[g] * p_income[y]) / joint
df["fairness_weight"] = df.apply(compute_fairness_weight, axis=1)
weight_stats = {
"mean": round(float(df["fairness_weight"].mean()), 4),
"std": round(float(df["fairness_weight"].std()), 4),
"min": round(float(df["fairness_weight"].min()), 4),
"max": round(float(df["fairness_weight"].max()), 4),
}
weight_by_group = {}
for g, grp in df.groupby(gender_col):
weight_by_group[g] = round(float(grp["fairness_weight"].mean()), 4)
weight_by_combo = {}
for (g, y), grp in df.groupby([gender_col, income_col]):
weight_by_combo[f"{g} + {y}"] = round(float(grp["fairness_weight"].mean()), 4)
# Merit scoring
numeric_cols = df.select_dtypes(include=[np.number]).columns.tolist()
numeric_cols = [c for c in numeric_cols if c not in ["fairness_weight"]]
if not scoring_weights:
if "education.num" in numeric_cols:
scoring_weights = {
"education.num": 40,
"hours.per.week": 35,
"age": 25,
}
else:
scoring_weights = {numeric_cols[0]: 100} if numeric_cols else {}
total_w = sum(scoring_weights.values())
if total_w > 0:
scoring_weights = {k: v / total_w for k, v in scoring_weights.items()}
for feat in scoring_weights:
if feat in df.columns:
min_v = df[feat].min()
max_v = df[feat].max()
df[f"_norm_{feat}"] = (df[feat] - min_v) / (max_v - min_v + 1e-9)
df["merit_score"] = sum(
df[f"_norm_{feat}"] * w
for feat, w in scoring_weights.items()
if f"_norm_{feat}" in df.columns
)
df["final_score"] = df["merit_score"] * df["fairness_weight"]
drop_cols = [c for c in df.columns if c.startswith("_norm_")]
df_out = df.drop(columns=drop_cols)
out_path = os.path.join(
app.config["UPLOAD_FOLDER"], "reweighted_" + os.path.basename(data["filepath"])
)
df_out.to_csv(out_path, index=False)
return jsonify(
{
"weight_stats": weight_stats,
"weight_by_group": weight_by_group,
"weight_by_combo": weight_by_combo,
"numeric_cols": numeric_cols,
"scoring_weights": {
k: round(v * 100, 1) for k, v in scoring_weights.items()
},
"merit_range": [
round(float(df["merit_score"].min()), 4),
round(float(df["merit_score"].max()), 4),
],
"final_range": [
round(float(df["final_score"].min()), 4),
round(float(df["final_score"].max()), 4),
],
"output_path": out_path,
"rows": len(df_out),
}
)
# ───────────── SHORTLIST (shortlist.py — Random Forest) ─────────────
@app.route("/api/shortlist", methods=["POST"])
def shortlist():
params = parse_shortlist_request(request.json)
if not params["filepath"]:
return jsonify({"success": False, "error": "Missing filepath"}), 400
if not os.path.exists(params["filepath"]):
return jsonify({"success": False, "error": "File not found"}), 400
try:
result = run_shortlist(**params)
return jsonify(result)
except ValueError as exc:
return jsonify({"success": False, "error": str(exc)}), 400
except Exception as exc:
return jsonify({"success": False, "error": str(exc)}), 500
# ───────────── PROXY DETECTION ─────────────
@app.route("/api/detect_proxies", methods=["POST"])
def detect_proxies():
from sklearn.preprocessing import LabelEncoder
data = request.json
df = load_and_clean_csv(data["filepath"])
gender_col = data["gender_col"]
threshold = data.get("threshold", 0.3)
df_encoded = df.copy()
le = LabelEncoder()
for col in df_encoded.columns:
if df_encoded[col].dtype == "object":
df_encoded[col] = le.fit_transform(df_encoded[col].astype(str))
gender_encoded = df_encoded[gender_col]
correlations = {}
for col in df_encoded.columns:
if col == gender_col:
continue
corr = abs(float(df_encoded[col].corr(gender_encoded)))
correlations[col] = round(corr, 4)
correlations = dict(
sorted(correlations.items(), key=lambda x: x[1], reverse=True)
)
flagged = [col for col, corr in correlations.items() if corr >= threshold]
return jsonify(
{
"correlations": correlations,
"flagged": flagged,
"threshold": threshold,
}
)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 5000)))