This repository has been archived by the owner on Jan 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
109 lines (88 loc) · 3 KB
/
server.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
#!/usr/bin/python3
from __future__ import unicode_literals
import json
from gender import compute_bias, compute_similar, upload_text, load_files, delete_file
from flask import Flask, make_response, render_template, request, abort, redirect, url_for
from kmeans import load_model
app = Flask(__name__)
@app.route('/', methods=['GET'])
def index():
"""
Action to render home page.
"""
return render_template('index.html')
@app.route('/database', methods=['GET'])
def database():
"""
Action to render the database page.
"""
files = load_files()
# clusterized = load_model()
# data = {file: { 'content': content, 'cluster': clusterized.get(file) } for file, content in files.items()}
data = {file: { 'content': content, 'cluster': 1 } for file, content in files.items()}
return render_template('database.html', data = data )
@app.route('/database/<string:name>', methods=['GET'])
def content(name):
"""
Action to render the database page.
"""
files = load_files()
# clusterized = load_model()
try:
lines = files[name].split("\n")
# cluster = clusterized.get(name)
#return render_template('content.html', lines = lines, name = name, cluster = cluster )
return render_template('content.html', lines = lines, name = name )
except KeyError:
abort(404)
@app.route('/analyze', methods=['POST'])
def analyze():
"""
This action accepts user-provided text and returns a JSON payload of
computed bias.
"""
text = request.form['text']
bias = compute_bias(text)
app.logger.debug('Input: %s', text)
app.logger.debug('Computed bias: %s', bias)
res = make_response(json.dumps(bias), 200)
res.headers['Content-Type'] = 'application/json'
return res
@app.route('/similar', methods=['POST'])
def similar():
"""
Action to find similar word to the one pressed
"""
word = request.form['word']
heOrShe = request.form['heOrShe']
similarWords = compute_similar(word, heOrShe)
app.logger.debug('Finding similar word for %s (%s)', word, heOrShe)
app.logger.debug('Found words: %s', ', '.join([word for word, _ in similarWords]))
res = make_response(json.dumps(similarWords), 200)
res.headers['Content-Type'] = 'application/json'
return res
@app.route('/upload', methods=['POST'])
def upload():
"""
Action to append and save user-provided text to a json file
"""
text = request.form['text']
uploadjson = upload_text(text)
app.logger.debug('uploading %s ', text)
res = make_response(json.dumps(text), 200)
res.headers['Content-Type'] = 'application/json'
return res
@app.route('/delete', methods=['POST'])
def delete():
name = request.form['name']
delete_file(name)
return redirect(url_for('database'))
@app.route('/about', methods=['GET'])
def about():
"""
Action to render the about page.
"""
return render_template('about.html')
if __name__ == '__main__':
app.debug = True
app.run(host="0.0.0.0")