-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
80 lines (61 loc) · 2.37 KB
/
app.py
File metadata and controls
80 lines (61 loc) · 2.37 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
from flask import Flask, request, render_template
import pandas as pd
import os
from werkzeug.utils import secure_filename
from Mondrian import mondrian
# from glutton import glutton
from gluttonMulti import glutton
app = Flask(__name__)
UPLOAD_FOLDER = 'uploads'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
@app.route('/', methods=['GET'])
def index():
return render_template("index.html")
@app.route('/process', methods=['POST'])
def process():
k = int(request.form['k'])
algorithm = request.form['algorithm']
file = request.files['file']
outputPath = request.form['outpath']
if file.filename == '':
return "No file selected."
filename = secure_filename(file.filename)
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(filepath)
df = pd.read_csv(filepath)
if algorithm == 'mondrian':
partitions = mondrian(df.copy(), k, 0)
results = []
for part in partitions:
# Handle min and max val anonymization:
if part['age'].min() == part['age'].max():
age_range = part['age'].max()
else:
age_range = f"{part['age'].min()}-{part['age'].max()}"
if part['zip'].min() == part['zip'].max():
zip_range = part['zip'].max()
else:
zip_range = f"{part['zip'].min()}-{part['zip'].max()}"
if part['gender'].min() == part['gender'].max():
gen_range = part['gender'].max()
else:
gen_range = "discard" # If not uniform M or F, discard instead
# Update values for anonymization
part['age'] = age_range
part['zip'] = zip_range
part['gender'] = gen_range
results.append(part)
result_df = pd.concat(results, ignore_index=True)
elif algorithm == 'glutton':
# result_df = glutton(df.copy(), 'age', k)
result_df = glutton(df,k)
else:
return "Invalid algorithm selected."
result_df.drop(labels="name",axis=1,inplace=True)
result_df.sort_values("uid",inplace=True)
table_html = result_df.to_html(classes='styled-table', index=False)
result_df.to_csv(outputPath,index=False)
return render_template("index.html", table=table_html)
if __name__ == '__main__':
app.run(debug=True)