-
Notifications
You must be signed in to change notification settings - Fork 0
/
flask_rest_api.py
executable file
·78 lines (60 loc) · 2.13 KB
/
flask_rest_api.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
#!flask/bin/python
import logging
from bokeh.plotting import figure
from bokeh.resources import CDN
from bokeh.embed import file_html, components
from flask import Flask, jsonify, abort, make_response, request
from flask_cors import CORS, cross_origin
from fringiness import *
from data_getter import *
import traceback
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)
app = Flask(__name__)
CORS(app)
@app.errorhandler(404)
def not_found(error):
return make_response(jsonify({'error': 'Not found'}), 404)
"""@app.route('/fringe-default/v1.0', methods=['POST'])
def frindge():
if not request.json or not 'input' in request.json:
abort(400)
current_article_text = request.json['input']
logging.info(current_article_text)
res = run(current_article_text)
logging.info(res)
x, y, f = fringiness(res_to_matrix(res))
logging.info(f)
try:
f_input_score = f[0]
except:
f_input_score = -1.
return jsonify(
{'input': current_article_text, "fringe_score": f_input_score}), 201
"""
@app.route('/fringe-plots/v1.0', methods=['POST'])
def frindge():
if not request.json or not 'input' in request.json:
abort(400)
current_article_text = request.json['input']
logging.info(current_article_text)
res = fastrun(current_article_text)
logging.info(res)
try:
x, y, f = fringiness(res_to_matrix(res)[0])
except Exception as e:
traceback.print_exc()
return jsonify({"fringe_score": str(e)}), 200
logging.info(f)
plot = embedding_plot_bokeh(x, y, f, res)
histogram = histogram_bokeh(f)
plot_html = components(plot, wrap_script=False, wrap_plot_info=False)
histogram_html = components(histogram, wrap_script=False, wrap_plot_info=False)
try:
f_input_score = f[0]
except:
f_input_score = -1.
return jsonify(
{'input': current_article_text, "fringe_score": f_input_score, "scatter_html": plot_html,
"histogram_html":histogram_html}), 200
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)