/
app.py
139 lines (114 loc) · 4.39 KB
/
app.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
import flask
import json
import random
random.seed(42)
app = flask.Flask('real/fake')
app.secret_key = '9)P39f.a2C99d9+wH662[=*@'
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 60
app.jinja_env.globals.update(len=len)
app.jinja_env.globals.update(max=max)
app.jinja_env.globals.update(zip=zip)
def load_data(labels_file, sample_size=50, examples=3):
labels_map = json.load(open(labels_file))
items = list(labels_map.items())
real_items = [item for item in items if item[1]]
fake_items = [item for item in items if not item[1]]
random.shuffle(real_items)
random.shuffle(fake_items)
real_examples, real_items = real_items[:examples], real_items[examples:]
fake_examples, fake_items = fake_items[:examples], fake_items[examples:]
real_items = real_items[:sample_size]
fake_items = fake_items[:sample_size]
assert len(real_items) == len(fake_items), (len(real_items),
len(fake_items))
real_examples = [file for file, _ in real_examples]
fake_examples = [file for file, _ in fake_examples]
examples = (real_examples, fake_examples)
items = real_items + fake_items
random.shuffle(items)
transposed_items = zip(*items)
return examples, transposed_items
(REAL_EXAMPLES, FAKE_EXAMPLES), (IMAGES, LABELS) = load_data('labels.json')
def reset():
flask.session['real_predictions'] = []
flask.session['fake_predictions'] = []
flask.session['all_predictions'] = []
def get_image_url(image_index):
return flask.url_for('static', filename=IMAGES[image_index])
@app.route('/')
def index():
return flask.render_template(
'index.html',
sample_size=len(IMAGES) // 2,
real_examples=REAL_EXAMPLES,
fake_examples=FAKE_EXAMPLES)
@app.route('/images')
@app.route('/images/<int:image_index>')
def images(image_index=0):
if image_index == 0:
reset()
flask.session['image_index'] = image_index
return flask.render_template(
'images.html',
image_url=get_image_url(image_index),
image_count=image_index,
number_of_images=len(LABELS))
@app.route('/predict/', methods=['POST'])
def predict():
prediction = flask.request.json['prediction']
image_index = flask.session['image_index']
if prediction is not None and image_index < len(LABELS):
if LABELS[image_index]:
if prediction:
flask.session['real_predictions'].append(True)
else:
flask.session['real_predictions'].append(False)
else:
if prediction:
flask.session['fake_predictions'].append(False)
else:
flask.session['fake_predictions'].append(True)
flask.session['all_predictions'].append(prediction)
image_index += 1
if image_index < len(LABELS):
new_url = flask.url_for('images', image_index=image_index)
else:
new_url = flask.url_for('done')
response = json.dumps({
'image_index': image_index,
'prediction': prediction,
'new_url': new_url,
})
print(response)
flask.session['image_index'] = image_index
return response, 200, {'ContentType': 'application/json'}
@app.route('/done')
def done():
real_images = [image for image, label in zip(IMAGES, LABELS) if label]
fake_images = [image for image, label in zip(IMAGES, LABELS) if not label]
return flask.render_template(
'done.html',
real_correct=sum(flask.session['real_predictions']),
fake_correct=sum(flask.session['fake_predictions']),
real_predictions=flask.session['real_predictions'],
fake_predictions=flask.session['fake_predictions'],
real_images=real_images,
fake_images=fake_images)
@app.route('/report')
def report():
predictions = flask.session.get('all_predictions', [])
report_lines = ['path,label,prediction']
for image, label, prediction in zip(IMAGES, LABELS, predictions):
l = 1 if label else 0
p = 1 if prediction else 0
report_lines.append('{},{},{}'.format(image, l, p))
report_path = '/tmp/report-{}.csv'.format(str(random.random())[2:])
with open(report_path, 'w') as report_file:
report_file.write('\n'.join(report_lines) + '\n')
return flask.send_file(
report_path,
mimetype='text/csv',
attachment_filename='report.csv',
as_attachment=True)
if __name__ == '__main__':
app.run()